xref: /petsc/src/sys/error/errtrace.c (revision 3f6e4ae9eecaae8a44170722877b4a2c1d9ba4c4)
1 #define PETSC_DLL
2 
3 #include "petscsys.h"        /*I "petscsys.h" I*/
4 #include "petscconfiginfo.h"
5 
6 #undef __FUNCT__
7 #define __FUNCT__ "PetscIgnoreErrorHandler"
8 /*@C
9    PetscIgnoreErrorHandler - Ignores the error, allows program to continue as if error did not occure
10 
11    Not Collective
12 
13    Input Parameters:
14 +  line - the line number of the error (indicated by __LINE__)
15 .  func - the function where error is detected (indicated by __FUNCT__)
16 .  file - the file in which the error was detected (indicated by __FILE__)
17 .  dir - the directory of the file (indicated by __SDIR__)
18 .  mess - an error text string, usually just printed to the screen
19 .  n - the generic error number
20 .  p - specific error number
21 -  ctx - error handler context
22 
23    Level: developer
24 
25    Notes:
26    Most users need not directly employ this routine and the other error
27    handlers, but can instead use the simplified interface SETERRQ, which has
28    the calling sequence
29 $     SETERRQ(number,p,mess)
30 
31    Notes for experienced users:
32    Use PetscPushErrorHandler() to set the desired error handler.  The
33    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
34    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscStopErrorHandler()
35 
36    Concepts: error handler^traceback
37    Concepts: traceback^generating
38 
39 .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
40           PetscAbortErrorHandler(), PetscTraceBackErrorHandler()
41  @*/
42 PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx)
43 {
44   PetscFunctionBegin;
45   PetscFunctionReturn(n);
46 }
47 
48 /* ---------------------------------------------------------------------------------------*/
49 
50 static char  arch[10],hostname[64],username[16],pname[PETSC_MAX_PATH_LEN],date[64];
51 static PetscTruth PetscErrorPrintfInitializeCalled = PETSC_FALSE;
52 static char version[256];
53 
54 #undef __FUNCT__
55 #define __FUNCT__ "PetscErrorPrintfInitialize"
56 /*
57    Initializes arch, hostname, username,date so that system calls do NOT need
58    to be made during the error handler.
59 */
60 PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize()
61 {
62   PetscErrorCode ierr;
63 
64   PetscFunctionBegin;
65   ierr = PetscGetArchType(arch,10);CHKERRQ(ierr);
66   ierr = PetscGetHostName(hostname,64);CHKERRQ(ierr);
67   ierr = PetscGetUserName(username,16);CHKERRQ(ierr);
68   ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
69   ierr = PetscGetDate(date,64);CHKERRQ(ierr);
70   ierr = PetscGetVersion(&version,256);CHKERRQ(ierr);
71   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
72   PetscFunctionReturn(0);
73 }
74 
75 
76 
77 #undef __FUNCT__
78 #define __FUNCT__ "PetscTraceBackErrorHandler"
79 /*@C
80 
81    PetscTraceBackErrorHandler - Default error handler routine that generates
82    a traceback on error detection.
83 
84    Not Collective
85 
86    Input Parameters:
87 +  line - the line number of the error (indicated by __LINE__)
88 .  func - the function where error is detected (indicated by __FUNCT__)
89 .  file - the file in which the error was detected (indicated by __FILE__)
90 .  dir - the directory of the file (indicated by __SDIR__)
91 .  mess - an error text string, usually just printed to the screen
92 .  n - the generic error number
93 .  p - specific error number
94 -  ctx - error handler context
95 
96    Level: developer
97 
98    Notes:
99    Most users need not directly employ this routine and the other error
100    handlers, but can instead use the simplified interface SETERRQ, which has
101    the calling sequence
102 $     SETERRQ(number,p,mess)
103 
104    Notes for experienced users:
105    Use PetscPushErrorHandler() to set the desired error handler.  The
106    currently available PETSc error handlers include PetscTraceBackErrorHandler(),
107    PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler(), and PetscStopErrorHandler()
108 
109    Concepts: error handler^traceback
110    Concepts: traceback^generating
111 
112 .seealso:  PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(),
113           PetscAbortErrorHandler()
114  @*/
115 PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int line,const char *fun,const char* file,const char *dir,PetscErrorCode n,int p,const char *mess,void *ctx)
116 {
117   PetscLogDouble    mem,rss;
118   PetscTruth        flg1,flg2;
119   PetscErrorCode    ierr;
120 
121   PetscFunctionBegin;
122 
123   if (p == 1) {
124     (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n");
125     if (n == PETSC_ERR_MEM) {
126       (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
127       (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
128       (*PetscErrorPrintf)("destroying unneeded objects.\n");
129       PetscMallocGetCurrentUsage(&mem);
130       PetscMemoryGetCurrentUsage(&rss);
131       PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
132       PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg2);
133       if (flg2) {
134         PetscMallocDumpLog(stdout);
135       } else {
136         (*PetscErrorPrintf)("Memory allocated %D Memory used by process %D\n",(PetscInt)mem,(PetscInt)rss);
137         if (flg1) {
138           PetscMallocDump(stdout);
139         } else {
140           (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
141         }
142       }
143     } else {
144         const char *text;
145         PetscErrorMessage(n,&text,PETSC_NULL);
146         if (text) (*PetscErrorPrintf)("%s!\n",text);
147     }
148     if (mess) {
149       (*PetscErrorPrintf)("%s!\n",mess);
150     }
151     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
152     (*PetscErrorPrintf)("%s\n",version);
153     (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n");
154     (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n");
155     (*PetscErrorPrintf)("See docs/index.html for manual pages.\n");
156     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
157     if (PetscErrorPrintfInitializeCalled) {
158       (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
159     }
160     (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR);
161     (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime);
162     (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
163     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
164   }
165 
166 
167   /* first line in stack trace? */
168   (*PetscErrorPrintf)("%s() line %d in %s%s\n",fun,line,dir,file);
169 
170 
171   PetscFunctionReturn(n);
172 }
173 
174