Actual source code: errtrace.c
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */
2: #include <petscsys.h>
3: #include <petsc/private/petscimpl.h>
4: #include <petscconfiginfo.h>
5: #if defined(PETSC_HAVE_UNISTD_H)
6: #include <unistd.h>
7: #endif
9: /*@C
10: PetscIgnoreErrorHandler - Deprecated, use PetscReturnErrorHandler(). Ignores the error, allows program to continue as if error did not occure
12: Not Collective
14: Input Parameters:
15: + comm - communicator over which error occurred
16: . line - the line number of the error (indicated by __LINE__)
17: . file - the file in which the error was detected (indicated by __FILE__)
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
23: Level: developer
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(comm,number,p,mess)
31: .seealso: PetscReturnErrorHandler()
32: @*/
33: PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
34: {
36: PetscFunctionReturn(n);
37: }
39: /* ---------------------------------------------------------------------------------------*/
41: static char arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
42: static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
43: static char version[256];
45: /*
46: Initializes arch, hostname, username, date so that system calls do NOT need
47: to be made during the error handler.
48: */
49: PetscErrorCode PetscErrorPrintfInitialize(void)
50: {
52: PetscBool use_stdout = PETSC_FALSE,use_none = PETSC_FALSE;
55: PetscGetArchType(arch,sizeof(arch));
56: PetscGetHostName(hostname,sizeof(hostname));
57: PetscGetUserName(username,sizeof(username));
58: PetscGetProgramName(pname,sizeof(pname));
59: PetscGetDate(date,sizeof(date));
60: PetscGetVersion(version,sizeof(version));
62: PetscOptionsGetBool(NULL,NULL,"-error_output_stdout",&use_stdout,NULL);
63: if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
64: PetscOptionsGetBool(NULL,NULL,"-error_output_none",&use_none,NULL);
65: if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
66: PetscErrorPrintfInitializeCalled = PETSC_TRUE;
67: return(0);
68: }
70: PetscErrorCode PetscErrorPrintfNone(const char format[],...)
71: {
72: return 0;
73: }
75: PetscErrorCode PetscErrorPrintfDefault(const char format[],...)
76: {
77: va_list Argp;
78: static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
80: /*
82: it may be called by PetscStackView().
84: This function does not do error checking because it is called by the error handlers.
85: */
87: if (!PetscErrorPrintfCalled) {
88: PetscErrorPrintfCalled = PETSC_TRUE;
90: /*
91: On the SGI machines and Cray T3E, if errors are generated "simultaneously" by
92: different processors, the messages are printed all jumbled up; to try to
93: prevent this we have each processor wait based on their rank
94: */
95: #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
96: {
97: PetscMPIInt rank;
98: if (PetscGlobalRank > 8) rank = 8;
99: else rank = PetscGlobalRank;
100: PetscSleep((PetscReal)rank);
101: }
102: #endif
103: }
105: PetscFPrintf(PETSC_COMM_SELF,PETSC_STDERR,"[%d]PETSC ERROR: ",PetscGlobalRank);
106: va_start(Argp,format);
107: (*PetscVFPrintf)(PETSC_STDERR,format,Argp);
108: va_end(Argp);
109: return 0;
110: }
112: /*
113: On some systems when the stderr is nested through several levels of shell script
114: before being passed to a file the isatty() falsely returns true resulting in
115: the screen highlight variables being passed through the test harness. Therefore
116: simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
117: */
118: static void PetscErrorPrintfHilight(void)
119: {
120: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
121: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
122: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[1;31m");
123: }
124: #endif
125: }
127: static void PetscErrorPrintfNormal(void)
128: {
129: #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
130: if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
131: if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR,"\033[0;39m\033[0;49m");
132: }
133: #endif
134: }
136: PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);
138: /*@C
140: PetscTraceBackErrorHandler - Default error handler routine that generates
141: a traceback on error detection.
143: Not Collective
145: Input Parameters:
146: + comm - communicator over which error occurred
147: . line - the line number of the error (indicated by __LINE__)
148: . file - the file in which the error was detected (indicated by __FILE__)
149: . mess - an error text string, usually just printed to the screen
150: . n - the generic error number
151: . p - PETSC_ERROR_INITIAL if this is the first call the error handler, otherwise PETSC_ERROR_REPEAT
152: - ctx - error handler context
154: Options Database:
155: + -error_output_stdout - output the error messages to stdout instead of the default stderr
156: - -error_output_none - do not output the error messages
158: Notes:
159: Most users need not directly employ this routine and the other error
160: handlers, but can instead use the simplified interface SETERRQ, which has
161: the calling sequence
162: $ SETERRQ(comm,number,n,mess)
164: Notes for experienced users:
165: Use PetscPushErrorHandler() to set the desired error handler.
167: Level: developer
169: .seealso: PetscError(), PetscPushErrorHandler(), PetscPopErrorHandler(), PetscAttachDebuggerErrorHandler(),
170: PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), PetscReturnErrorHandler(), PetscEmacsClientErrorHandler()
171: @*/
172: PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx)
173: {
174: PetscLogDouble mem,rss;
175: PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
176: PetscMPIInt rank = 0;
178: if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm,&rank);
180: if (rank == 0) {
181: PetscBool ismain;
182: static int cnt = 1;
184: if (cnt == 1) {
185: PetscErrorPrintfHilight();
186: (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
187: PetscErrorPrintfNormal();
188: if (n == PETSC_ERR_MEM) {
189: (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
190: (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
191: (*PetscErrorPrintf)("destroying unneeded objects.\n");
192: PetscMallocGetCurrentUsage(&mem);
193: PetscMemoryGetCurrentUsage(&rss);
194: PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
195: PetscOptionsGetBool(NULL,NULL,"-malloc_view",&flg2,NULL);
196: PetscOptionsHasName(NULL,NULL,"-malloc_view_threshold",&flg3);
197: if (flg2 || flg3) PetscMallocView(stdout);
198: else {
199: (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n",mem,rss);
200: if (flg1) PetscMallocDump(stdout);
201: else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_view for info.\n");
202: }
203: } else {
204: const char *text;
205: PetscErrorMessage(n,&text,NULL);
206: if (text) (*PetscErrorPrintf)("%s\n",text);
207: }
208: if (mess) (*PetscErrorPrintf)("%s\n",mess);
209: (*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
210: (*PetscErrorPrintf)("%s\n",version);
211: if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n",pname,arch,hostname,username,date);
212: (*PetscErrorPrintf)("Configure options %s\n",petscconfigureoptions);
213: }
214: /* print line of stack trace */
215: (*PetscErrorPrintf)("#%d %s() at %s:%d\n",cnt++,fun,file,line);
216: PetscStrncmp(fun,"main",4,&ismain);
217: if (ismain) {
218: if ((n <= PETSC_ERR_MIN_VALUE) || (n >= PETSC_ERR_MAX_VALUE)) {
219: (*PetscErrorPrintf)("Reached the main program with an out-of-range error code %d. This should never happen\n",n);
220: }
221: PetscOptionsViewError();
222: PetscErrorPrintfHilight();
223: (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
224: PetscErrorPrintfNormal();
225: }
226: } else {
227: /* do not print error messages since process 0 will print them, sleep before aborting so will not accidentally kill process 0*/
228: PetscSleep(10.0);
229: exit(0);
230: }
231: return n;
232: }