xref: /petsc/src/sys/error/errtrace.c (revision 0ab362b593432ed36b0927f034d82f20831b3e37)
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 
120   PetscFunctionBegin;
121 
122   if (p == 1) {
123     (*PetscErrorPrintf)("--------------------- Error Message ------------------------------------\n");
124     if (n == PETSC_ERR_MEM) {
125       (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
126       (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
127       (*PetscErrorPrintf)("destroying unneeded objects.\n");
128       PetscMallocGetCurrentUsage(&mem);
129       PetscMemoryGetCurrentUsage(&rss);
130       PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
131       PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg2);
132       if (flg2) {
133         PetscMallocDumpLog(stdout);
134       } else {
135         (*PetscErrorPrintf)("Memory allocated %D Memory used by process %D\n",(PetscInt)mem,(PetscInt)rss);
136         if (flg1) {
137           PetscMallocDump(stdout);
138         } else {
139           (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_log for info.\n");
140         }
141       }
142     } else {
143         const char *text;
144         PetscErrorMessage(n,&text,PETSC_NULL);
145         if (text) (*PetscErrorPrintf)("%s!\n",text);
146     }
147     if (mess) {
148       (*PetscErrorPrintf)("%s!\n",mess);
149     }
150     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
151     (*PetscErrorPrintf)("%s\n",version);
152     (*PetscErrorPrintf)("See docs/changes/index.html for recent updates.\n");
153     (*PetscErrorPrintf)("See docs/faq.html for hints about trouble shooting.\n");
154     (*PetscErrorPrintf)("See docs/index.html for manual pages.\n");
155     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
156     if (PetscErrorPrintfInitializeCalled) {
157       (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
158     }
159     (*PetscErrorPrintf)("Libraries linked from %s\n",PETSC_LIB_DIR);
160     (*PetscErrorPrintf)("Configure run at %s\n",petscconfigureruntime);
161     (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
162     (*PetscErrorPrintf)("------------------------------------------------------------------------\n");
163   }
164 
165 
166   /* first line in stack trace? */
167   (*PetscErrorPrintf)("%s() line %d in %s%s\n",fun,line,dir,file);
168 
169 
170   PetscFunctionReturn(n);
171 }
172 
173