xref: /petsc/src/sys/error/errtrace.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
10039db0dSBarry Smith #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */
2c6db04a5SJed Brown #include <petscsys.h>                    /*I "petscsys.h" I*/
3f67a399dSBarry Smith #include <petsc/private/petscimpl.h>
4c6db04a5SJed Brown #include <petscconfiginfo.h>
5114011d0SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
6114011d0SBarry Smith   #include <unistd.h>
7114011d0SBarry Smith #endif
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay /*@C
10811af0c4SBarry Smith    PetscIgnoreErrorHandler - Deprecated, use `PetscReturnErrorHandler()`. Ignores the error, allows program to continue as if error did not occure
11e5c89e4eSSatish Balay 
12e5c89e4eSSatish Balay    Not Collective
13e5c89e4eSSatish Balay 
14e5c89e4eSSatish Balay    Input Parameters:
15e32f2f54SBarry Smith +  comm - communicator over which error occurred
16e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
17e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
18e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
19e5c89e4eSSatish Balay .  n - the generic error number
20e5c89e4eSSatish Balay .  p - specific error number
21e5c89e4eSSatish Balay -  ctx - error handler context
22e5c89e4eSSatish Balay 
23e5c89e4eSSatish Balay    Level: developer
24e5c89e4eSSatish Balay 
25811af0c4SBarry Smith    Note:
26811af0c4SBarry Smith    Users do not directly call this routine
27e5c89e4eSSatish Balay 
28db781477SPatrick Sanan .seealso: `PetscReturnErrorHandler()`
29e5c89e4eSSatish Balay  @*/
30*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx)
31*d71ae5a4SJacob Faibussowitsch {
3211cc89d2SBarry Smith   return n;
33e5c89e4eSSatish Balay }
34e5c89e4eSSatish Balay 
35107894f0SSatish Balay /* ---------------------------------------------------------------------------------------*/
36107894f0SSatish Balay 
375abee1b0SJed Brown static char      arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
38ace3abfcSBarry Smith static PetscBool PetscErrorPrintfInitializeCalled = PETSC_FALSE;
393f6e4ae9SSatish Balay static char      version[256];
40107894f0SSatish Balay 
41107894f0SSatish Balay /*
42107894f0SSatish Balay    Initializes arch, hostname, username, date so that system calls do NOT need
43107894f0SSatish Balay    to be made during the error handler.
44107894f0SSatish Balay */
45*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfInitialize(void)
46*d71ae5a4SJacob Faibussowitsch {
47ace3abfcSBarry Smith   PetscBool use_stdout = PETSC_FALSE, use_none = PETSC_FALSE;
48107894f0SSatish Balay 
49107894f0SSatish Balay   PetscFunctionBegin;
509566063dSJacob Faibussowitsch   PetscCall(PetscGetArchType(arch, sizeof(arch)));
519566063dSJacob Faibussowitsch   PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
529566063dSJacob Faibussowitsch   PetscCall(PetscGetUserName(username, sizeof(username)));
539566063dSJacob Faibussowitsch   PetscCall(PetscGetProgramName(pname, sizeof(pname)));
549566063dSJacob Faibussowitsch   PetscCall(PetscGetDate(date, sizeof(date)));
559566063dSJacob Faibussowitsch   PetscCall(PetscGetVersion(version, sizeof(version)));
56e8fb0fc0SBarry Smith 
579566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_stdout", &use_stdout, NULL));
58a297a907SKarl Rupp   if (use_stdout) PETSC_STDERR = PETSC_STDOUT;
599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-error_output_none", &use_none, NULL));
60a297a907SKarl Rupp   if (use_none) PetscErrorPrintf = PetscErrorPrintfNone;
61107894f0SSatish Balay   PetscErrorPrintfInitializeCalled = PETSC_TRUE;
62107894f0SSatish Balay   PetscFunctionReturn(0);
63107894f0SSatish Balay }
64107894f0SSatish Balay 
65*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfNone(const char format[], ...)
66*d71ae5a4SJacob Faibussowitsch {
67e8fb0fc0SBarry Smith   return 0;
68e8fb0fc0SBarry Smith }
69e8fb0fc0SBarry Smith 
70*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscErrorPrintfDefault(const char format[], ...)
71*d71ae5a4SJacob Faibussowitsch {
72e8fb0fc0SBarry Smith   va_list          Argp;
73ace3abfcSBarry Smith   static PetscBool PetscErrorPrintfCalled = PETSC_FALSE;
74e8fb0fc0SBarry Smith 
75e8fb0fc0SBarry Smith   /*
76e8fb0fc0SBarry Smith       This function does not call PetscFunctionBegin and PetscFunctionReturn() because
77e8fb0fc0SBarry Smith     it may be called by PetscStackView().
78e8fb0fc0SBarry Smith 
79e8fb0fc0SBarry Smith       This function does not do error checking because it is called by the error handlers.
80e8fb0fc0SBarry Smith   */
81e8fb0fc0SBarry Smith 
82e8fb0fc0SBarry Smith   if (!PetscErrorPrintfCalled) {
83e8fb0fc0SBarry Smith     PetscErrorPrintfCalled = PETSC_TRUE;
84e8fb0fc0SBarry Smith 
85e8fb0fc0SBarry Smith     /*
86e8fb0fc0SBarry Smith         On the SGI machines and Cray T3E, if errors are generated  "simultaneously" by
87e8fb0fc0SBarry Smith       different processors, the messages are printed all jumbled up; to try to
88e8fb0fc0SBarry Smith       prevent this we have each processor wait based on their rank
89e8fb0fc0SBarry Smith     */
90e8fb0fc0SBarry Smith #if defined(PETSC_CAN_SLEEP_AFTER_ERROR)
91e8fb0fc0SBarry Smith     {
92e8fb0fc0SBarry Smith       PetscMPIInt rank;
93a297a907SKarl Rupp       if (PetscGlobalRank > 8) rank = 8;
94a297a907SKarl Rupp       else rank = PetscGlobalRank;
95a6d0e24fSJed Brown       PetscSleep((PetscReal)rank);
96e8fb0fc0SBarry Smith     }
97e8fb0fc0SBarry Smith #endif
98e8fb0fc0SBarry Smith   }
99e8fb0fc0SBarry Smith 
1001179db26SBarry Smith   PetscFPrintf(PETSC_COMM_SELF, PETSC_STDERR, "[%d]PETSC ERROR: ", PetscGlobalRank);
101e8fb0fc0SBarry Smith   va_start(Argp, format);
1021179db26SBarry Smith   (*PetscVFPrintf)(PETSC_STDERR, format, Argp);
103e8fb0fc0SBarry Smith   va_end(Argp);
104e8fb0fc0SBarry Smith   return 0;
105e8fb0fc0SBarry Smith }
106e8fb0fc0SBarry Smith 
107c2eed0edSBarry Smith /*
108c2eed0edSBarry Smith    On some systems when the stderr is nested through several levels of shell script
109c2eed0edSBarry Smith    before being passed to a file the isatty() falsely returns true resulting in
110c2eed0edSBarry Smith    the screen highlight variables being passed through the test harness. Therefore
111c2eed0edSBarry Smith    simply do not highlight when the PETSC_STDERR is PETSC_STDOUT.
112c2eed0edSBarry Smith */
113*d71ae5a4SJacob Faibussowitsch static void PetscErrorPrintfHilight(void)
114*d71ae5a4SJacob Faibussowitsch {
11598ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
116c2eed0edSBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
117114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[1;31m");
118114011d0SBarry Smith   }
119114011d0SBarry Smith #endif
120114011d0SBarry Smith }
121114011d0SBarry Smith 
122*d71ae5a4SJacob Faibussowitsch static void PetscErrorPrintfNormal(void)
123*d71ae5a4SJacob Faibussowitsch {
12498ed35c3SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) && defined(PETSC_USE_ISATTY)
125c2eed0edSBarry Smith   if (PetscErrorPrintf == PetscErrorPrintfDefault && PETSC_STDERR != PETSC_STDOUT) {
126114011d0SBarry Smith     if (isatty(fileno(PETSC_STDERR))) fprintf(PETSC_STDERR, "\033[0;39m\033[0;49m");
127114011d0SBarry Smith   }
128114011d0SBarry Smith #endif
129114011d0SBarry Smith }
130114011d0SBarry Smith 
13195c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void);
132114011d0SBarry Smith 
133e5c89e4eSSatish Balay /*@C
134e5c89e4eSSatish Balay 
135e5c89e4eSSatish Balay    PetscTraceBackErrorHandler - Default error handler routine that generates
136e5c89e4eSSatish Balay    a traceback on error detection.
137e5c89e4eSSatish Balay 
138e5c89e4eSSatish Balay    Not Collective
139e5c89e4eSSatish Balay 
140e5c89e4eSSatish Balay    Input Parameters:
141e32f2f54SBarry Smith +  comm - communicator over which error occurred
142e32f2f54SBarry Smith .  line - the line number of the error (indicated by __LINE__)
143e5c89e4eSSatish Balay .  file - the file in which the error was detected (indicated by __FILE__)
144e5c89e4eSSatish Balay .  mess - an error text string, usually just printed to the screen
145e5c89e4eSSatish Balay .  n - the generic error number
146811af0c4SBarry Smith .  p - `PETSC_ERROR_INITIAL` if this is the first call the error handler, otherwise `PETSC_ERROR_REPEAT`
147e5c89e4eSSatish Balay -  ctx - error handler context
148e5c89e4eSSatish Balay 
149811af0c4SBarry Smith   Options Database Keys:
15045b666d6SBarry Smith +  -error_output_stdout - output the error messages to stdout instead of the default stderr
15145b666d6SBarry Smith -  -error_output_none - do not output the error messages
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay    Notes:
154811af0c4SBarry Smith    Users do not directly call this routine
155e5c89e4eSSatish Balay 
156811af0c4SBarry Smith    Use `PetscPushErrorHandler()` to set the desired error handler.
157e5c89e4eSSatish Balay 
15845b666d6SBarry Smith    Level: developer
159e5c89e4eSSatish Balay 
160db781477SPatrick Sanan .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
161db781477SPatrick Sanan           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscReturnErrorHandler()`, `PetscEmacsClientErrorHandler()`
162e5c89e4eSSatish Balay  @*/
163*d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, void *ctx)
164*d71ae5a4SJacob Faibussowitsch {
165e5c89e4eSSatish Balay   PetscLogDouble mem, rss;
166574034a9SJed Brown   PetscBool      flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flg3 = PETSC_FALSE;
167997adca8SBarry Smith   PetscMPIInt    rank = 0;
168e5c89e4eSSatish Balay 
169a297a907SKarl Rupp   if (comm != PETSC_COMM_SELF) MPI_Comm_rank(comm, &rank);
170a297a907SKarl Rupp 
171660278c0SBarry Smith   if (rank == 0 && (!PetscCIEnabledPortableErrorOutput || PetscGlobalRank == 0)) {
172fbfcfee5SBarry Smith     PetscBool  ismain;
173114011d0SBarry Smith     static int cnt = 1;
174114011d0SBarry Smith 
17557a177adSBarry Smith     if (cnt == 1) {
176114011d0SBarry Smith       PetscErrorPrintfHilight();
177114011d0SBarry Smith       (*PetscErrorPrintf)("--------------------- Error Message --------------------------------------------------------------\n");
1785f3a2c8dSBarry Smith       PetscErrorPrintfNormal();
179e5c89e4eSSatish Balay       if (n == PETSC_ERR_MEM) {
180e5c89e4eSSatish Balay         (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
181e5c89e4eSSatish Balay         (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
182e5c89e4eSSatish Balay         (*PetscErrorPrintf)("destroying unneeded objects.\n");
183e5c89e4eSSatish Balay         PetscMallocGetCurrentUsage(&mem);
184e5c89e4eSSatish Balay         PetscMemoryGetCurrentUsage(&rss);
185c5929fdfSBarry Smith         PetscOptionsGetBool(NULL, NULL, "-malloc_dump", &flg1, NULL);
18692f119d6SBarry Smith         PetscOptionsGetBool(NULL, NULL, "-malloc_view", &flg2, NULL);
18792f119d6SBarry Smith         PetscOptionsHasName(NULL, NULL, "-malloc_view_threshold", &flg3);
18892f119d6SBarry Smith         if (flg2 || flg3) PetscMallocView(stdout);
189a297a907SKarl Rupp         else {
190b85f3346SJed Brown           (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n", mem, rss);
191a297a907SKarl Rupp           if (flg1) PetscMallocDump(stdout);
19292f119d6SBarry Smith           else (*PetscErrorPrintf)("Try running with -malloc_dump or -malloc_view for info.\n");
193e5c89e4eSSatish Balay         }
194e5c89e4eSSatish Balay       } else {
195e5c89e4eSSatish Balay         const char *text;
1960298fd71SBarry Smith         PetscErrorMessage(n, &text, NULL);
197114011d0SBarry Smith         if (text) (*PetscErrorPrintf)("%s\n", text);
198e5c89e4eSSatish Balay       }
199114011d0SBarry Smith       if (mess) (*PetscErrorPrintf)("%s\n", mess);
200f4bc716fSBarry Smith       PetscOptionsLeftError();
201a17b96a8SKyle Gerard Felker       (*PetscErrorPrintf)("See https://petsc.org/release/faq/ for trouble shooting.\n");
202660278c0SBarry Smith       if (!PetscCIEnabledPortableErrorOutput) {
203107894f0SSatish Balay         (*PetscErrorPrintf)("%s\n", version);
204a297a907SKarl Rupp         if (PetscErrorPrintfInitializeCalled) (*PetscErrorPrintf)("%s on a %s named %s by %s %s\n", pname, arch, hostname, username, date);
205107894f0SSatish Balay         (*PetscErrorPrintf)("Configure options %s\n", petscconfigureoptions);
206107894f0SSatish Balay       }
207660278c0SBarry Smith     }
208997adca8SBarry Smith     /* print line of stack trace */
209660278c0SBarry Smith     if (fun) (*PetscErrorPrintf)("#%d %s() at %s:%d\n", cnt++, fun, PetscCIFilename(file), PetscCILinenumber(line));
210660278c0SBarry Smith     else if (file) (*PetscErrorPrintf)("#%d %s:%d\n", cnt++, PetscCIFilename(file), PetscCILinenumber(line));
21149c86fc7SBarry Smith     if (fun) {
212114011d0SBarry Smith       PetscStrncmp(fun, "main", 4, &ismain);
213fbfcfee5SBarry Smith       if (ismain) {
214ad540459SPierre Jolivet         if ((n <= PETSC_ERR_MIN_VALUE) || (n >= PETSC_ERR_MAX_VALUE)) (*PetscErrorPrintf)("Reached the main program with an out-of-range error code %d. This should never happen\n", n);
215e11779c2SBarry Smith         PetscOptionsViewError();
216114011d0SBarry Smith         PetscErrorPrintfHilight();
217f15a11d2SBarry Smith         (*PetscErrorPrintf)("----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------\n");
218114011d0SBarry Smith         PetscErrorPrintfNormal();
219114011d0SBarry Smith       }
22049c86fc7SBarry Smith     }
221997adca8SBarry Smith   } else {
222a5b23f4aSJose E. Roman     /* do not print error messages since process 0 will print them, sleep before aborting so will not accidentally kill process 0*/
223997adca8SBarry Smith     PetscSleep(10.0);
224c16385d5SBarry Smith     exit(0);
225997adca8SBarry Smith   }
226362febeeSStefano Zampini   return n;
227e5c89e4eSSatish Balay }
228