xref: /petsc/src/sys/error/adebug.c (revision 9a34e50ff82deba4ab327939c3f78f4610eebb1d)
1 /*
2       Code to handle PETSc starting up in debuggers,etc.
3 */
4 
5 #include <petscsys.h> /*I   "petscsys.h"   I*/
6 #include <signal.h>
7 #if defined(PETSC_HAVE_UNISTD_H)
8   #include <unistd.h>
9 #endif
10 
11 /*
12       These are the debugger and display used if the debugger is started up
13 */
14 static char      PetscDebugger[PETSC_MAX_PATH_LEN];
15 static char      DebugTerminal[PETSC_MAX_PATH_LEN];
16 static PetscBool UseDebugTerminal    = PETSC_TRUE;
17 PetscBool        petscwaitonerrorflg = PETSC_FALSE;
18 PetscBool        petscindebugger     = PETSC_FALSE;
19 
20 /*@C
21    PetscSetDebugTerminal - Sets the terminal to use for debugging.
22 
23    Not Collective
24 
25    Input Parameter:
26 .  terminal - name of terminal and any flags required to execute a program.
27               For example xterm, "urxvt -e", "gnome-terminal -x".
28               On Apple MacOS you can use Terminal (note the capital T)
29 
30    Options Database Key:
31    -debug_terminal terminal - use this terminal instead of the default
32 
33    Level: developer
34 
35    Notes:
36    You can start the debugger for all processes in the same GNU screen session.
37 
38      mpiexec -n 4 ./myapp -start_in_debugger -debug_terminal "screen -X -S debug screen"
39 
40    will open 4 windows in the session named "debug".
41 
42    The default on Apple is Terminal, on other systems the default is xterm
43 
44    Fortran Note:
45    This routine is not supported in Fortran.
46 
47 .seealso: `PetscSetDebugger()`
48 @*/
49 PetscErrorCode PetscSetDebugTerminal(const char terminal[])
50 {
51   PetscBool xterm;
52 
53   PetscFunctionBegin;
54   PetscCall(PetscStrncpy(DebugTerminal, terminal, sizeof(DebugTerminal)));
55   PetscCall(PetscStrcmp(terminal, "xterm", &xterm));
56   if (xterm) PetscCall(PetscStrlcat(DebugTerminal, " -e", sizeof(DebugTerminal)));
57   PetscFunctionReturn(0);
58 }
59 
60 /*@C
61    PetscSetDebugger - Sets options associated with the debugger.
62 
63    Not Collective
64 
65    Input Parameters:
66 +  debugger - name of debugger, which should be in your path,
67               usually "lldb", "dbx", "gdb", "cuda-gdb", "idb", "xxgdb", "kdgb" or "ddd". Also, HP-UX
68               supports "xdb", and IBM rs6000 supports "xldb".
69 
70 -  usedebugterminal - flag to indicate debugger window, set to either PETSC_TRUE (to indicate
71             debugger should be started in a new terminal window) or PETSC_FALSE (to start debugger
72             in initial window (the option PETSC_FALSE makes no sense when using more
73             than one MPI process.)
74 
75    Level: developer
76 
77    Fortran Note:
78    This routine is not supported in Fortran.
79 
80 .seealso: `PetscAttachDebugger()`, `PetscAttachDebuggerErrorHandler()`, `PetscSetDebugTerminal()`
81 @*/
82 PetscErrorCode PetscSetDebugger(const char debugger[], PetscBool usedebugterminal)
83 {
84   PetscFunctionBegin;
85   if (debugger) PetscCall(PetscStrncpy(PetscDebugger, debugger, sizeof(PetscDebugger)));
86   if (UseDebugTerminal) UseDebugTerminal = usedebugterminal;
87   PetscFunctionReturn(0);
88 }
89 
90 /*@C
91     PetscSetDefaultDebugger - Causes PETSc to use its default debugger and output terminal
92 
93    Not collective
94 
95     Level: developer
96 
97 .seealso: `PetscSetDebugger()`, `PetscSetDebuggerFromString()`
98 @*/
99 PetscErrorCode PetscSetDefaultDebugger(void)
100 {
101   PetscFunctionBegin;
102 #if defined(PETSC_USE_DEBUGGER)
103   PetscCall(PetscSetDebugger(PETSC_USE_DEBUGGER, PETSC_TRUE));
104 #endif
105 #if defined(__APPLE__)
106   PetscCall(PetscSetDebugTerminal("Terminal"));
107 #else
108   PetscCall(PetscSetDebugTerminal("xterm"));
109 #endif
110   PetscFunctionReturn(0);
111 }
112 
113 static PetscErrorCode PetscCheckDebugger_Private(const char defaultDbg[], const char string[], const char *debugger[])
114 {
115   PetscBool exists;
116   char     *f;
117 
118   PetscFunctionBegin;
119   PetscCall(PetscStrstr(string, defaultDbg, &f));
120   if (f) {
121     PetscCall(PetscTestFile(string, 'x', &exists));
122     if (exists) *debugger = string;
123     else *debugger = defaultDbg;
124   }
125   PetscFunctionReturn(0);
126 }
127 
128 /*@C
129     PetscSetDebuggerFromString - Set the complete path for the
130        debugger for PETSc to use.
131 
132    Not collective
133 
134    Level: developer
135 
136 .seealso: `PetscSetDebugger()`, `PetscSetDefaultDebugger()`
137 @*/
138 PetscErrorCode PetscSetDebuggerFromString(const char *string)
139 {
140   const char *debugger    = NULL;
141   PetscBool   useterminal = PETSC_TRUE;
142   char       *f;
143 
144   PetscFunctionBegin;
145   PetscCall(PetscStrstr(string, "noxterm", &f));
146   if (f) useterminal = PETSC_FALSE;
147   PetscCall(PetscStrstr(string, "ddd", &f));
148   if (f) useterminal = PETSC_FALSE;
149   PetscCall(PetscStrstr(string, "noterminal", &f));
150   if (f) useterminal = PETSC_FALSE;
151   PetscCall(PetscCheckDebugger_Private("xdb", string, &debugger));
152   PetscCall(PetscCheckDebugger_Private("dbx", string, &debugger));
153   PetscCall(PetscCheckDebugger_Private("xldb", string, &debugger));
154   PetscCall(PetscCheckDebugger_Private("gdb", string, &debugger));
155   PetscCall(PetscCheckDebugger_Private("cuda-gdb", string, &debugger));
156   PetscCall(PetscCheckDebugger_Private("idb", string, &debugger));
157   PetscCall(PetscCheckDebugger_Private("xxgdb", string, &debugger));
158   PetscCall(PetscCheckDebugger_Private("ddd", string, &debugger));
159   PetscCall(PetscCheckDebugger_Private("kdbg", string, &debugger));
160   PetscCall(PetscCheckDebugger_Private("ups", string, &debugger));
161   PetscCall(PetscCheckDebugger_Private("workshop", string, &debugger));
162   PetscCall(PetscCheckDebugger_Private("pgdbg", string, &debugger));
163   PetscCall(PetscCheckDebugger_Private("pathdb", string, &debugger));
164   PetscCall(PetscCheckDebugger_Private("lldb", string, &debugger));
165   PetscCall(PetscSetDebugger(debugger, useterminal));
166   PetscFunctionReturn(0);
167 }
168 
169 /*@
170    PetscWaitOnError - If an error is detected and the process would normally exit the main program with `MPI_Abort()` sleep instead
171                       of exiting.
172 
173    Not Collective
174 
175    Level: advanced
176 
177    Note:
178       When -start_in_debugger -debugger_ranks x,y,z is used this prevents the processes NOT listed in x,y,z from calling MPI_Abort and
179       killing the user's debugging sessions.
180 
181 .seealso: `PetscSetDebugger()`, `PetscAttachDebugger()`
182 @*/
183 PetscErrorCode PetscWaitOnError(void)
184 {
185   petscwaitonerrorflg = PETSC_TRUE;
186   return 0;
187 }
188 
189 /*@
190    PetscAttachDebugger - Attaches the debugger to the running process.
191 
192    Not Collective
193 
194    Options Database Keys:
195 -  -start_in_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] [-debugger_ranks m,n] -debug_terminal xterm or Terminal (for Apple)
196 .  -on_error_attach_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] - Activates debugger attachment
197 
198    Level: advanced
199 
200    Developer Note:
201     Since this can be called by the error handler should it be calling `SETERRQ()` and `PetscCall()`?
202 
203 .seealso: `PetscSetDebugger()`, `PetscSetDefaultDebugger()`, `PetscSetDebugTerminal()`, `PetscAttachDebuggerErrorHandler()`, `PetscStopForDebugger()`
204 @*/
205 PetscErrorCode PetscAttachDebugger(void)
206 {
207 #if !defined(PETSC_CANNOT_START_DEBUGGER) && defined(PETSC_HAVE_FORK)
208   int       child     = 0;
209   PetscReal sleeptime = 0;
210   char      program[PETSC_MAX_PATH_LEN], display[256], hostname[64];
211 #endif
212 
213 #if defined(PETSC_CANNOT_START_DEBUGGER) || !defined(PETSC_HAVE_FORK)
214   (*PetscErrorPrintf)("System cannot start debugger\n");
215   (*PetscErrorPrintf)("On Cray run program in Totalview debugger\n");
216   (*PetscErrorPrintf)("On Windows use Developer Studio(MSDEV)\n");
217   PETSCABORT(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS);
218 #else
219   if (PetscUnlikely(PetscGetDisplay(display, sizeof(display)))) {
220     (*PetscErrorPrintf)("Cannot determine display\n");
221     return PETSC_ERR_SYS;
222   }
223   if (PetscUnlikely(PetscGetProgramName(program, sizeof(program)))) {
224     (*PetscErrorPrintf)("Cannot determine program name needed to attach debugger\n");
225     return PETSC_ERR_SYS;
226   }
227   if (PetscUnlikely(!program[0])) {
228     (*PetscErrorPrintf)("Cannot determine program name needed to attach debugger\n");
229     return PETSC_ERR_SYS;
230   }
231   child = (int)fork();
232   if (PetscUnlikely(child < 0)) {
233     (*PetscErrorPrintf)("Error in fork() prior to attaching debugger\n");
234     return PETSC_ERR_SYS;
235   }
236   petscindebugger = PETSC_TRUE;
237 
238   /*
239       Swap role the parent and child. This is (I think) so that control c typed
240     in the debugger goes to the correct process.
241   */
242   #if !defined(PETSC_DO_NOT_SWAP_CHILD_FOR_DEBUGGER)
243   child = child ? 0 : (int)getppid();
244   #endif
245 
246   if (child) { /* I am the parent, will run the debugger */
247     const char *args[10];
248     char        pid[10];
249     PetscInt    j, jj;
250     PetscBool   isdbx, isidb, isxldb, isxxgdb, isups, isxdb, isworkshop, isddd, iskdbg, islldb;
251 
252     PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
253     /*
254          We need to send a continue signal to the "child" process on the
255        alpha, otherwise it just stays off forever
256     */
257   #if defined(PETSC_NEED_KILL_FOR_DEBUGGER)
258     kill(child, SIGCONT);
259   #endif
260     PetscCall(PetscSNPrintf(pid, PETSC_STATIC_ARRAY_LENGTH(pid), "%d", child));
261 
262     PetscCall(PetscStrcmp(PetscDebugger, "xxgdb", &isxxgdb));
263     PetscCall(PetscStrcmp(PetscDebugger, "ddd", &isddd));
264     PetscCall(PetscStrcmp(PetscDebugger, "kdbg", &iskdbg));
265     PetscCall(PetscStrcmp(PetscDebugger, "ups", &isups));
266     PetscCall(PetscStrcmp(PetscDebugger, "xldb", &isxldb));
267     PetscCall(PetscStrcmp(PetscDebugger, "xdb", &isxdb));
268     PetscCall(PetscStrcmp(PetscDebugger, "dbx", &isdbx));
269     PetscCall(PetscStrcmp(PetscDebugger, "idb", &isidb));
270     PetscCall(PetscStrcmp(PetscDebugger, "workshop", &isworkshop));
271     PetscCall(PetscStrcmp(PetscDebugger, "lldb", &islldb));
272 
273     if (isxxgdb || isups || isddd) {
274       args[1] = program;
275       args[2] = pid;
276       args[3] = "-display";
277       args[0] = PetscDebugger;
278       args[4] = display;
279       args[5] = NULL;
280       printf("PETSC: Attaching %s to %s %s on %s\n", args[0], args[1], pid, hostname);
281       if (execvp(args[0], (char **)args) < 0) {
282         perror("Unable to start debugger");
283         exit(0);
284       }
285     } else if (iskdbg) {
286       args[1] = "-p";
287       args[2] = pid;
288       args[3] = program;
289       args[4] = "-display";
290       args[0] = PetscDebugger;
291       args[5] = display;
292       args[6] = NULL;
293       printf("PETSC: Attaching %s to %s %s on %s\n", args[0], args[3], pid, hostname);
294       if (execvp(args[0], (char **)args) < 0) {
295         perror("Unable to start debugger");
296         exit(0);
297       }
298     } else if (isxldb) {
299       args[1] = "-a";
300       args[2] = pid;
301       args[3] = program;
302       args[4] = "-display";
303       args[0] = PetscDebugger;
304       args[5] = display;
305       args[6] = NULL;
306       printf("PETSC: Attaching %s to %s %s on %s\n", args[0], args[1], pid, hostname);
307       if (execvp(args[0], (char **)args) < 0) {
308         perror("Unable to start debugger");
309         exit(0);
310       }
311     } else if (isworkshop) {
312       args[1] = "-s";
313       args[2] = pid;
314       args[3] = "-D";
315       args[4] = "-";
316       args[0] = PetscDebugger;
317       args[5] = pid;
318       args[6] = "-display";
319       args[7] = display;
320       args[8] = NULL;
321       printf("PETSC: Attaching %s to %s on %s\n", args[0], pid, hostname);
322       if (execvp(args[0], (char **)args) < 0) {
323         perror("Unable to start debugger");
324         exit(0);
325       }
326     } else {
327       j = 0;
328       if (UseDebugTerminal) {
329         PetscBool cmp;
330         char     *tmp, *tmp1;
331         PetscCall(PetscStrncmp(DebugTerminal, "Terminal", 8, &cmp));
332         if (cmp) {
333           char command[1024];
334           if (islldb) PetscCall(PetscSNPrintf(command, sizeof(command), "osascript -e 'tell app \"Terminal\" to do script \"lldb  -p %s \"'\n", pid));
335           else {
336             char fullprogram[PETSC_MAX_PATH_LEN];
337             PetscCall(PetscGetFullPath(program, fullprogram, sizeof(fullprogram)));
338             PetscCall(PetscSNPrintf(command, sizeof(command), "osascript -e 'tell app \"Terminal\" to do script \"%s  %s %s \"'\n", PetscDebugger, fullprogram, pid));
339           }
340   #if defined(PETSC_HAVE_POPEN)
341           PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, command, "r", NULL));
342   #else
343           printf("-debug_terminal Terminal is not available on this system since PETSC_HAVE_POPEN is not defined in this configuration\n");
344   #endif
345           exit(0);
346         }
347 
348         PetscCall(PetscStrncmp(DebugTerminal, "screen", 6, &cmp));
349         if (!cmp) PetscCall(PetscStrncmp(DebugTerminal, "gnome-terminal", 6, &cmp));
350         if (cmp) display[0] = 0; /* when using screen, we never pass -display */
351         args[j++] = tmp = DebugTerminal;
352         if (display[0]) {
353           args[j++] = "-display";
354           args[j++] = display;
355         }
356         while (*tmp) {
357           PetscCall(PetscStrchr(tmp, ' ', &tmp1));
358           if (!tmp1) break;
359           *tmp1     = 0;
360           tmp       = tmp1 + 1;
361           args[j++] = tmp;
362         }
363       }
364       args[j++] = PetscDebugger;
365       jj        = j;
366       /* this is for default gdb */
367       args[j++] = program;
368       args[j++] = pid;
369       args[j++] = NULL;
370 
371       if (isidb) {
372         j         = jj;
373         args[j++] = "-pid";
374         args[j++] = pid;
375         args[j++] = "-gdb";
376         args[j++] = program;
377         args[j++] = NULL;
378       }
379       if (islldb) {
380         j         = jj;
381         args[j++] = "-p";
382         args[j++] = pid;
383         args[j++] = NULL;
384       }
385       if (isdbx) {
386         j         = jj;
387   #if defined(PETSC_USE_P_FOR_DEBUGGER)
388         args[j++] = "-p";
389         args[j++] = pid;
390         args[j++] = program;
391   #elif defined(PETSC_USE_LARGEP_FOR_DEBUGGER)
392         args[j++] = "-l";
393         args[j++] = "ALL";
394         args[j++] = "-P";
395         args[j++] = pid;
396         args[j++] = program;
397   #elif defined(PETSC_USE_A_FOR_DEBUGGER)
398         args[j++] = "-a";
399         args[j++] = pid;
400   #elif defined(PETSC_USE_PID_FOR_DEBUGGER)
401         args[j++] = "-pid";
402         args[j++] = pid;
403         args[j++] = program;
404   #else
405         args[j++] = program;
406         args[j++] = pid;
407   #endif
408         args[j++] = NULL;
409       }
410       if (UseDebugTerminal) {
411         if (display[0]) printf("PETSC: Attaching %s to %s of pid %s on display %s on machine %s\n", PetscDebugger, program, pid, display, hostname);
412         else printf("PETSC: Attaching %s to %s on pid %s on %s\n", PetscDebugger, program, pid, hostname);
413 
414         if (execvp(args[0], (char **)args) < 0) {
415           perror("Unable to start debugger in xterm");
416           exit(0);
417         }
418       } else {
419         printf("PETSC: Attaching %s to %s of pid %s on %s\n", PetscDebugger, program, pid, hostname);
420         if (execvp(args[0], (char **)args) < 0) {
421           perror("Unable to start debugger");
422           exit(0);
423         }
424       }
425     }
426   } else {          /* I am the child, continue with user code */
427     sleeptime = 10; /* default to sleep waiting for debugger */
428     PetscCall(PetscOptionsGetReal(NULL, NULL, "-debugger_pause", &sleeptime, NULL));
429     if (sleeptime < 0) sleeptime = -sleeptime;
430   #if defined(PETSC_NEED_DEBUGGER_NO_SLEEP)
431     /*
432         HP cannot attach process to sleeping debugger, hence count instead
433     */
434     {
435       PetscReal x = 1.0;
436       int       i = 10000000;
437       while (i--) x++; /* cannot attach to sleeper */
438     }
439   #elif defined(PETSC_HAVE_SLEEP_RETURNS_EARLY)
440     /*
441         IBM sleep may return at anytime, hence must see if there is more time to sleep
442     */
443     {
444       int left = sleeptime;
445       while (left > 0) left = PetscSleep(left) - 1;
446     }
447   #else
448     PetscSleep(sleeptime);
449   #endif
450   }
451 #endif
452   return 0;
453 }
454 
455 /*@C
456    PetscAttachDebuggerErrorHandler - Error handler that attaches
457    a debugger to a running process when an error is detected.
458    This routine is useful for examining variables, etc.
459 
460    Not Collective
461 
462    Input Parameters:
463 +  comm - communicator over which error occurred
464 .  line - the line number of the error (indicated by __LINE__)
465 .  file - the file in which the error was detected (indicated by __FILE__)
466 .  message - an error text string, usually just printed to the screen
467 .  number - the generic error number
468 .  p - `PETSC_ERROR_INITIAL` if error just detected, otherwise `PETSC_ERROR_REPEAT`
469 -  ctx - error handler context
470 
471    Options Database Keys:
472 +  -on_error_attach_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] - Activates debugger attachment
473 -  -start_in_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] [-debugger_ranks m,n]
474 
475    Level: developer
476 
477    Notes:
478    By default the GNU debugger, gdb, is used.  Alternatives are cuda-gdb, lldb, dbx and
479    xxgdb,xldb (on IBM rs6000), xdb (on HP-UX).
480 
481    Most users need not directly employ this routine and the other error
482    handlers, but can instead use the simplified interface SETERR, which has
483    the calling sequence
484 $     SETERRQ(PETSC_COMM_SELF,number,p,message)
485 
486    Notes for experienced users:
487    Use `PetscPushErrorHandler()` to set the desired error handler.  The
488    currently available PETSc error handlers are
489 $    PetscTraceBackErrorHandler()
490 $    PetscAttachDebuggerErrorHandler()
491 $    PetscAbortErrorHandler()
492    or you may write your own.
493 
494    Developer Note:
495      This routine calls abort instead of returning because if it returned then `MPI_Abort()` would get called which can generate an exception
496      causing the debugger to be attached again in a cycle.
497 
498 .seealso: `PetscSetDebuggerFromString()`, `PetscSetDebugger()`, `PetscSetDefaultDebugger()`, `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`,
499           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscEmacsClientErrorHandler()`, `PetscReturnErrorHandler()`, `PetscSetDebugTermainal()`
500 @*/
501 PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode num, PetscErrorType p, const char *mess, void *ctx)
502 {
503   if (!mess) mess = " ";
504 
505   if (fun) (*PetscErrorPrintf)("%s() at %s:%d %s\n", fun, file, line, mess);
506   else (*PetscErrorPrintf)("%s:%d %s\n", file, line, mess);
507 
508   PetscAttachDebugger();
509   abort(); /* call abort because don't want to kill other MPI ranks that may successfully attach to debugger */
510   PetscFunctionReturn(0);
511 }
512 
513 /*@C
514    PetscStopForDebugger - Prints a message to the screen indicating how to
515          attach to the process with the debugger and then waits for the
516          debugger to attach.
517 
518    Not Collective
519 
520    Options Database Key:
521 .   -stop_for_debugger - will stop for you to attach the debugger when PetscInitialize() is called
522 
523    Level: developer
524 
525    Note:
526     This is likely never needed since `PetscAttachDebugger()` is easier to use and seems to always work.
527 
528    Developer Note:
529     Since this can be called by the error handler, should it be calling `SETERRQ()` and `PetscCall()`?
530 
531 .seealso: `PetscSetDebugger()`, `PetscAttachDebugger()`
532 @*/
533 PetscErrorCode PetscStopForDebugger(void)
534 {
535   PetscErrorCode ierr;
536   PetscInt       sleeptime = 0;
537 #if !defined(PETSC_CANNOT_START_DEBUGGER)
538   int         ppid;
539   PetscMPIInt rank;
540   char        program[PETSC_MAX_PATH_LEN], hostname[256];
541   PetscBool   isdbx, isxldb, isxxgdb, isddd, iskdbg, isups, isxdb, islldb;
542 #endif
543 
544   PetscFunctionBegin;
545 #if defined(PETSC_CANNOT_START_DEBUGGER)
546   (*PetscErrorPrintf)("System cannot start debugger; just continuing program\n");
547 #else
548   ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
549   if (ierr) rank = 0; /* ignore error since this may be already in error handler */
550   ierr = PetscGetHostName(hostname, sizeof(hostname));
551   if (ierr) {
552     (*PetscErrorPrintf)("Cannot determine hostname; just continuing program\n");
553     PetscFunctionReturn(0);
554   }
555 
556   ierr = PetscGetProgramName(program, sizeof(program));
557   if (ierr) {
558     (*PetscErrorPrintf)("Cannot determine program name; just continuing program\n");
559     PetscFunctionReturn(0);
560   }
561   if (!program[0]) {
562     (*PetscErrorPrintf)("Cannot determine program name; just continuing program\n");
563     PetscFunctionReturn(0);
564   }
565 
566   ppid = getpid();
567 
568   PetscCall(PetscStrcmp(PetscDebugger, "xxgdb", &isxxgdb));
569   PetscCall(PetscStrcmp(PetscDebugger, "ddd", &isddd));
570   PetscCall(PetscStrcmp(PetscDebugger, "kdbg", &iskdbg));
571   PetscCall(PetscStrcmp(PetscDebugger, "ups", &isups));
572   PetscCall(PetscStrcmp(PetscDebugger, "xldb", &isxldb));
573   PetscCall(PetscStrcmp(PetscDebugger, "xdb", &isxdb));
574   PetscCall(PetscStrcmp(PetscDebugger, "dbx", &isdbx));
575   PetscCall(PetscStrcmp(PetscDebugger, "lldb", &islldb));
576 
577   if (isxxgdb || isups || isddd || iskdbg) printf("[%d]%s>>%s %s %d\n", rank, hostname, PetscDebugger, program, ppid);
578   else if (isxldb) printf("[%d]%s>>%s -a %d %s\n", rank, hostname, PetscDebugger, ppid, program);
579   else if (islldb) printf("[%d]%s>>%s -p %d\n", rank, hostname, PetscDebugger, ppid);
580   else if (isdbx) {
581   #if defined(PETSC_USE_P_FOR_DEBUGGER)
582     printf("[%d]%s>>%s -p %d %s\n", rank, hostname, PetscDebugger, ppid, program);
583   #elif defined(PETSC_USE_LARGEP_FOR_DEBUGGER)
584     printf("[%d]%s>>%s -l ALL -P %d %s\n", rank, hostname, PetscDebugger, ppid, program);
585   #elif defined(PETSC_USE_A_FOR_DEBUGGER)
586     printf("[%d]%s>>%s -a %d\n", rank, hostname, PetscDebugger, ppid);
587   #elif defined(PETSC_USE_PID_FOR_DEBUGGER)
588     printf("[%d]%s>>%s -pid %d %s\n", rank, hostname, PetscDebugger, ppid, program);
589   #else
590     printf("[%d]%s>>%s %s %d\n", rank, hostname, PetscDebugger, program, ppid);
591   #endif
592   }
593 #endif /* PETSC_CANNOT_START_DEBUGGER */
594 
595   fflush(stdout); /* ignore error because may already be in error handler */
596 
597   sleeptime = 25;                                                      /* default to sleep waiting for debugger */
598   PetscOptionsGetInt(NULL, NULL, "-debugger_pause", &sleeptime, NULL); /* ignore error because may already be in error handler */
599   if (sleeptime < 0) sleeptime = -sleeptime;
600 #if defined(PETSC_NEED_DEBUGGER_NO_SLEEP)
601   /*
602       HP cannot attach process to sleeping debugger, hence count instead
603   */
604   {
605     PetscReal x = 1.0;
606     int       i = 10000000;
607     while (i--) x++; /* cannot attach to sleeper */
608   }
609 #elif defined(PETSC_HAVE_SLEEP_RETURNS_EARLY)
610   /*
611       IBM sleep may return at anytime, hence must see if there is more time to sleep
612   */
613   {
614     int left = sleeptime;
615     while (left > 0) left = sleep(left) - 1;
616   }
617 #else
618   PetscSleep(sleeptime);
619 #endif
620   PetscFunctionReturn(0);
621 }
622