Actual source code: adebug.c

  1: /*
  2:       Code to handle PETSc starting up in debuggers,etc.
  3: */

  5: #include <petscsys.h>
  6: #include <signal.h>
  7: #if defined(PETSC_HAVE_UNISTD_H)
  8: #include <unistd.h>
  9: #endif

 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;

 20: /*@C
 21:    PetscSetDebugTerminal - Sets the terminal to use for debugging.

 23:    Not Collective

 25:    Input Parameters:
 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)

 30:    Options Database Keys:
 31:    -debug_terminal terminal - use this terminal instead of the default

 33:    Level: developer

 35:    Notes:
 36:    You can start the debugger for all processes in the same GNU screen session.

 38:      mpiexec -n 4 ./myapp -start_in_debugger -debug_terminal "screen -X -S debug screen"

 40:    will open 4 windows in the session named "debug".

 42:    The default on Apple is Terminal, on other systems the default is xterm

 44:    Fortran Note:
 45:    This routine is not supported in Fortran.

 47: .seealso: PetscSetDebugger()
 48: @*/
 49: PetscErrorCode PetscSetDebugTerminal(const char terminal[])
 50: {
 51:   PetscBool xterm;

 53:   PetscStrncpy(DebugTerminal,terminal,sizeof(DebugTerminal));
 54:   PetscStrcmp(terminal,"xterm",&xterm);
 55:   if (xterm) PetscStrlcat(DebugTerminal," -e",sizeof(DebugTerminal));
 56:   return 0;
 57: }

 59: /*@C
 60:    PetscSetDebugger - Sets options associated with the debugger.

 62:    Not Collective

 64:    Input Parameters:
 65: +  debugger - name of debugger, which should be in your path,
 66:               usually "lldb", "dbx", "gdb", "cuda-gdb", "idb", "xxgdb", "kdgb" or "ddd". Also, HP-UX
 67:               supports "xdb", and IBM rs6000 supports "xldb".

 69: -  usedebugterminal - flag to indicate debugger window, set to either PETSC_TRUE (to indicate
 70:             debugger should be started in a new terminal window) or PETSC_FALSE (to start debugger
 71:             in initial window (the option PETSC_FALSE makes no sense when using more
 72:             than one MPI process.)

 74:    Level: developer

 76:    Fortran Note:
 77:    This routine is not supported in Fortran.

 79: .seealso: PetscAttachDebugger(), PetscAttachDebuggerErrorHandler(), PetscSetDebugTerminal()
 80: @*/
 81: PetscErrorCode PetscSetDebugger(const char debugger[], PetscBool usedebugterminal)
 82: {
 83:   if (debugger) PetscStrncpy(PetscDebugger,debugger,sizeof(PetscDebugger));
 84:   if (UseDebugTerminal) UseDebugTerminal = usedebugterminal;
 85:   return 0;
 86: }

 88: /*@C
 89:     PetscSetDefaultDebugger - Causes PETSc to use its default debugger and output terminal

 91:    Not collective

 93:     Level: developer

 95: .seealso: PetscSetDebugger(), PetscSetDebuggerFromString()
 96: @*/
 97: PetscErrorCode PetscSetDefaultDebugger(void)
 98: {
 99: #if defined(PETSC_USE_DEBUGGER)
100:   PetscSetDebugger(PETSC_USE_DEBUGGER,PETSC_TRUE);
101: #endif
102: #if defined(__APPLE__)
103:   PetscSetDebugTerminal("Terminal");
104: #else
105:   PetscSetDebugTerminal("xterm");
106: #endif
107:   return 0;
108: }

111: {
112:   PetscBool  exists;
113:   char      *f;

115:   PetscStrstr(string, defaultDbg, &f);
116:   if (f) {
117:     PetscTestFile(string, 'x', &exists);
118:     if (exists) *debugger = string;
119:     else        *debugger = defaultDbg;
120:   }
121:   return 0;
122: }

124: /*@C
125:     PetscSetDebuggerFromString - Set the complete path for the
126:        debugger for PETSc to use.

128:    Not collective

130:    Level: developer

132: .seealso: PetscSetDebugger(), PetscSetDefaultDebugger()
133: @*/
134: PetscErrorCode  PetscSetDebuggerFromString(const char *string)
135: {
136:   const char *debugger    = NULL;
137:   PetscBool   useterminal = PETSC_TRUE;
138:   char       *f;

140:   PetscStrstr(string, "noxterm", &f);
141:   if (f) useterminal = PETSC_FALSE;
142:   PetscStrstr(string, "ddd", &f);
143:   if (f) useterminal = PETSC_FALSE;
144:   PetscStrstr(string, "noterminal", &f);
145:   if (f) useterminal = PETSC_FALSE;
160:   PetscSetDebugger(debugger, useterminal);
161:   return 0;
162: }

164: /*@
165:    PetscWaitOnError - If an error is detected and the process would normally exit the main program with MPI_Abort() sleep instead
166:                       of exiting.

168:    Not Collective

170:    Level: advanced

172:    Notes:
173:       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
174:       killing the user's debugging sessions.

176: .seealso: PetscSetDebugger(), PetscAttachDebugger()
177: @*/
178: PetscErrorCode  PetscWaitOnError()
179: {
180:   petscwaitonerrorflg  = PETSC_TRUE;
181:   return 0;
182: }

184: /*@
185:    PetscAttachDebugger - Attaches the debugger to the running process.

187:    Not Collective

189:    Options Database Keys:
190: -  -start_in_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] [-debugger_ranks m,n] -debug_terminal xterm or Terminal (for Apple)
191: .  -on_error_attach_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] - Activates debugger attachment

193:    Level: advanced

195:    Developer Notes:
196:     Since this can be called by the error handler should it be calling SETERRQ() and PetscCall()?

198: .seealso: PetscSetDebugger(), PetscSetDefaultDebugger(), PetscSetDebugTerminal(), PetscAttachDebuggerErrorHandler(), PetscStopForDebugger()
199: @*/
200: PetscErrorCode PetscAttachDebugger(void)
201: {
202: #if !defined(PETSC_CANNOT_START_DEBUGGER) && defined(PETSC_HAVE_FORK)
203:   int            child    =0;
204:   PetscReal      sleeptime=0;
206:   char           program[PETSC_MAX_PATH_LEN],display[256],hostname[64];
207: #endif

209: #if defined(PETSC_CANNOT_START_DEBUGGER) || !defined(PETSC_HAVE_FORK)
210:   (*PetscErrorPrintf)("System cannot start debugger\n");
211:   (*PetscErrorPrintf)("On Cray run program in Totalview debugger\n");
212:   (*PetscErrorPrintf)("On Windows use Developer Studio(MSDEV)\n");
213:   PETSCABORT(PETSC_COMM_WORLD,PETSC_ERR_SUP_SYS);
214: #else
215:   PetscGetDisplay(display,sizeof(display));
216:   if (PetscUnlikely(ierr)) {
217:     (*PetscErrorPrintf)("Cannot determine display\n");
218:     return PETSC_ERR_SYS;
219:   }
220:   PetscGetProgramName(program,sizeof(program));
221:   if (PetscUnlikely(ierr)) {
222:     (*PetscErrorPrintf)("Cannot determine program name\n");
223:     return PETSC_ERR_SYS;
224:   }
225:   if (PetscUnlikely(!program[0])) {
226:     (*PetscErrorPrintf)("Cannot determine program name\n");
227:     return PETSC_ERR_SYS;
228:   }
229:   child = (int)fork();
230:   if (PetscUnlikely(child < 0)) {
231:     (*PetscErrorPrintf)("Error in fork() prior to attaching debugger\n");
232:     return PETSC_ERR_SYS;
233:   }
234:   petscindebugger = PETSC_TRUE;

236:   /*
237:       Swap role the parent and child. This is (I think) so that control c typed
238:     in the debugger goes to the correct process.
239:   */
240: #if !defined(PETSC_DO_NOT_SWAP_CHILD_FOR_DEBUGGER)
241:   if (child) child = 0;
242:   else       child = (int)getppid();
243: #endif

245:   if (child) { /* I am the parent, will run the debugger */
246:     const char *args[10];
247:     char       pid[10];
248:     PetscInt   j,jj;
249:     PetscBool  isdbx,isidb,isxldb,isxxgdb,isups,isxdb,isworkshop,isddd,iskdbg,islldb;

251:     PetscGetHostName(hostname,sizeof(hostname));
252:     /*
253:          We need to send a continue signal to the "child" process on the
254:        alpha, otherwise it just stays off forever
255:     */
256: #if defined(PETSC_NEED_KILL_FOR_DEBUGGER)
257:     kill(child,SIGCONT);
258: #endif
259:     sprintf(pid,"%d",child);

261:     PetscStrcmp(PetscDebugger,"xxgdb",&isxxgdb);
262:     PetscStrcmp(PetscDebugger,"ddd",&isddd);
263:     PetscStrcmp(PetscDebugger,"kdbg",&iskdbg);
264:     PetscStrcmp(PetscDebugger,"ups",&isups);
265:     PetscStrcmp(PetscDebugger,"xldb",&isxldb);
266:     PetscStrcmp(PetscDebugger,"xdb",&isxdb);
267:     PetscStrcmp(PetscDebugger,"dbx",&isdbx);
268:     PetscStrcmp(PetscDebugger,"idb",&isidb);
269:     PetscStrcmp(PetscDebugger,"workshop",&isworkshop);
270:     PetscStrcmp(PetscDebugger,"lldb",&islldb);

272:     if (isxxgdb || isups || isddd) {
273:       args[1] = program; args[2] = pid; args[3] = "-display";
274:       args[0] = PetscDebugger; args[4] = display; args[5] = NULL;
275:       printf("PETSC: Attaching %s to %s %s on %s\n",args[0],args[1],pid,hostname);
276:       if (execvp(args[0],(char**)args)  < 0) {
277:         perror("Unable to start debugger");
278:         exit(0);
279:       }
280:     } else if (iskdbg) {
281:       args[1] = "-p"; args[2] = pid; args[3] = program;  args[4] = "-display";
282:       args[0] = PetscDebugger; args[5] = display; args[6] = NULL;
283:       printf("PETSC: Attaching %s to %s %s on %s\n",args[0],args[3],pid,hostname);
284:       if (execvp(args[0],(char**)args)  < 0) {
285:         perror("Unable to start debugger");
286:         exit(0);
287:       }
288:     } else if (isxldb) {
289:       args[1] = "-a"; args[2] = pid; args[3] = program;  args[4] = "-display";
290:       args[0] = PetscDebugger; args[5] = display; args[6] = NULL;
291:       printf("PETSC: Attaching %s to %s %s on %s\n",args[0],args[1],pid,hostname);
292:       if (execvp(args[0],(char**)args)  < 0) {
293:         perror("Unable to start debugger");
294:         exit(0);
295:       }
296:     } else if (isworkshop) {
297:       args[1] = "-s"; args[2] = pid; args[3] = "-D"; args[4] = "-";
298:       args[0] = PetscDebugger; args[5] = pid; args[6] = "-display"; args[7] = display; args[8] = NULL;
299:       printf("PETSC: Attaching %s to %s on %s\n",args[0],pid,hostname);
300:       if (execvp(args[0],(char**)args)  < 0) {
301:         perror("Unable to start debugger");
302:         exit(0);
303:       }
304:     } else {
305:       j = 0;
306:       if (UseDebugTerminal) {
307:         PetscBool cmp;
308:         char      *tmp,*tmp1;
309:         PetscStrncmp(DebugTerminal,"Terminal",8,&cmp);
310:         if (cmp) {
311:           char command[1024];
312:           PetscSNPrintf(command,sizeof(command),"osascript -e 'tell app \"Terminal\" to do script \"lldb  -p %s  %s \"'\n",pid,program);
313:           PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",NULL);
314:           exit(0);
315:         }

317:         PetscStrncmp(DebugTerminal,"screen",6,&cmp);
318:         if (!cmp) PetscStrncmp(DebugTerminal,"gnome-terminal",6,&cmp);
319:         if (cmp) display[0] = 0; /* when using screen, we never pass -display */
320:         args[j++] = tmp = DebugTerminal;
321:         if (display[0]) {
322:           args[j++] = "-display"; args[j++] = display;
323:         }
324:         while (*tmp) {
325:           PetscStrchr(tmp,' ',&tmp1);
326:           if (!tmp1) break;
327:           *tmp1     = 0;
328:           tmp       = tmp1+1;
329:           args[j++] = tmp;
330:         }
331:       }
332:       args[j++] = PetscDebugger;
333:       jj = j;
334:       /* this is for default gdb */
335:       args[j++] = program;
336:       args[j++] = pid;
337:       args[j++] = NULL;

339:       if (isidb) {
340:         j = jj;
341:         args[j++] = "-pid";
342:         args[j++] = pid;
343:         args[j++] = "-gdb";
344:         args[j++] = program;
345:         args[j++] = NULL;
346:       }
347:       if (islldb) {
348:         j = jj;
349:         args[j++] = "-p";
350:         args[j++] = pid;
351:         args[j++] = NULL;
352:       }
353:       if (isdbx) {
354:         j = jj;
355: #if defined(PETSC_USE_P_FOR_DEBUGGER)
356:         args[j++] = "-p";
357:         args[j++] = pid;
358:         args[j++] = program;
359: #elif defined(PETSC_USE_LARGEP_FOR_DEBUGGER)
360:         args[j++] = "-l";
361:         args[j++] = "ALL";
362:         args[j++] = "-P";
363:         args[j++] = pid;
364:         args[j++] = program;
365: #elif defined(PETSC_USE_A_FOR_DEBUGGER)
366:         args[j++] = "-a";
367:         args[j++] = pid;
368: #elif defined(PETSC_USE_PID_FOR_DEBUGGER)
369:         args[j++] = "-pid";
370:         args[j++] = pid;
371:         args[j++] = program;
372: #else
373:         args[j++] = program;
374:         args[j++] = pid;
375: #endif
376:         args[j++] = NULL;
377:       }
378:       if (UseDebugTerminal) {
379:         if (display[0]) printf("PETSC: Attaching %s to %s of pid %s on display %s on machine %s\n",PetscDebugger,program,pid,display,hostname);
380:         else            printf("PETSC: Attaching %s to %s on pid %s on %s\n",PetscDebugger,program,pid,hostname);

382:         if (execvp(args[0],(char**)args)  < 0) {
383:           perror("Unable to start debugger in xterm");
384:           exit(0);
385:         }
386:       } else {
387:         printf("PETSC: Attaching %s to %s of pid %s on %s\n",PetscDebugger,program,pid,hostname);
388:         if (execvp(args[0],(char**)args)  < 0) {
389:           perror("Unable to start debugger");
390:           exit(0);
391:         }
392:       }
393:     }
394:   } else {   /* I am the child, continue with user code */
395:     sleeptime = 10; /* default to sleep waiting for debugger */
396:     PetscOptionsGetReal(NULL,NULL,"-debugger_pause",&sleeptime,NULL);
397:     if (sleeptime < 0) sleeptime = -sleeptime;
398: #if defined(PETSC_NEED_DEBUGGER_NO_SLEEP)
399:     /*
400:         HP cannot attach process to sleeping debugger, hence count instead
401:     */
402:     {
403:       PetscReal x = 1.0;
404:       int       i =10000000;
405:       while (i--) x++;  /* cannot attach to sleeper */
406:     }
407: #elif defined(PETSC_HAVE_SLEEP_RETURNS_EARLY)
408:     /*
409:         IBM sleep may return at anytime, hence must see if there is more time to sleep
410:     */
411:     {
412:       int left = sleeptime;
413:       while (left > 0) left = PetscSleep(left) - 1;
414:     }
415: #else
416:     PetscSleep(sleeptime);
417: #endif
418:   }
419: #endif
420:   return 0;
421: }

423: /*@C
424:    PetscAttachDebuggerErrorHandler - Error handler that attaches
425:    a debugger to a running process when an error is detected.
426:    This routine is useful for examining variables, etc.

428:    Not Collective

430:    Input Parameters:
431: +  comm - communicator over which error occurred
432: .  line - the line number of the error (indicated by __LINE__)
433: .  file - the file in which the error was detected (indicated by __FILE__)
434: .  message - an error text string, usually just printed to the screen
435: .  number - the generic error number
436: .  p - PETSC_ERROR_INITIAL if error just detected, otherwise PETSC_ERROR_REPEAT
437: -  ctx - error handler context

439:    Options Database Keys:
440: +  -on_error_attach_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] - Activates debugger attachment
441: -  -start_in_debugger [noxterm,dbx,xxgdb,xdb,xldb,gdb] [-display name] [-debugger_ranks m,n]

443:    Level: developer

445:    Notes:
446:    By default the GNU debugger, gdb, is used.  Alternatives are cuda-gdb, lldb, dbx and
447:    xxgdb,xldb (on IBM rs6000), xdb (on HP-UX).

449:    Most users need not directly employ this routine and the other error
450:    handlers, but can instead use the simplified interface SETERR, which has
451:    the calling sequence
452: $     SETERRQ(PETSC_COMM_SELF,number,p,message)

454:    Notes for experienced users:
455:    Use PetscPushErrorHandler() to set the desired error handler.  The
456:    currently available PETSc error handlers are
457: $    PetscTraceBackErrorHandler()
458: $    PetscAttachDebuggerErrorHandler()
459: $    PetscAbortErrorHandler()
460:    or you may write your own.

462: .seealso:  PetscSetDebuggerFromString(), PetscSetDebugger(), PetscSetDefaultDebugger(), PetscError(), PetscPushErrorHandler(), PetscPopErrorHandler(), PetscTraceBackErrorHandler(),
463:            PetscAbortErrorHandler(), PetscMPIAbortErrorHandler(), PetscEmacsClientErrorHandler(), PetscReturnErrorHandler(), PetscSetDebugTermainal()
464: @*/
465: PetscErrorCode  PetscAttachDebuggerErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode num,PetscErrorType p,const char *mess,void *ctx)
466: {

469:   if (!fun) fun = "User provided function";
470:   if (!mess) mess = " ";

472:   (*PetscErrorPrintf)("%s() at %s:%d %s\n",fun,file,line,mess);

474:   PetscAttachDebugger();
475:   if (ierr) abort(); /* call abort because don't want to kill other MPI processes that may successfully attach to debugger */
476:   return 0;
477: }

479: /*@C
480:    PetscStopForDebugger - Prints a message to the screen indicating how to
481:          attach to the process with the debugger and then waits for the
482:          debugger to attach.

484:    Not Collective

486:    Options Database:
487: .   -stop_for_debugger - will stop for you to attach the debugger when PetscInitialize() is called

489:    Level: developer

491:    Notes:
492:     This is likely never needed since PetscAttachDebugger() is easier to use and seems to always work.

494:    Developer Notes:
495:     Since this can be called by the error handler, should it be calling SETERRQ() and PetscCall()?

497: .seealso: PetscSetDebugger(), PetscAttachDebugger()
498: @*/
499: PetscErrorCode  PetscStopForDebugger(void)
500: {
502:   PetscInt       sleeptime=0;
503: #if !defined(PETSC_CANNOT_START_DEBUGGER)
504:   int            ppid;
505:   PetscMPIInt    rank;
506:   char           program[PETSC_MAX_PATH_LEN],hostname[256];
507:   PetscBool      isdbx,isxldb,isxxgdb,isddd,iskdbg,isups,isxdb,islldb;
508: #endif

510: #if defined(PETSC_CANNOT_START_DEBUGGER)
511:   (*PetscErrorPrintf)("System cannot start debugger; just continuing program\n");
512: #else
513:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
514:   if (ierr) rank = 0; /* ignore error since this may be already in error handler */
515:   PetscGetHostName(hostname,sizeof(hostname));
516:   if (ierr) {
517:     (*PetscErrorPrintf)("Cannot determine hostname; just continuing program\n");
518:     return 0;
519:   }

521:   PetscGetProgramName(program,sizeof(program));
522:   if (ierr) {
523:     (*PetscErrorPrintf)("Cannot determine program name; just continuing program\n");
524:     return 0;
525:   }
526:   if (!program[0]) {
527:     (*PetscErrorPrintf)("Cannot determine program name; just continuing program\n");
528:     return 0;
529:   }

531:   ppid = getpid();

533:   PetscStrcmp(PetscDebugger,"xxgdb",&isxxgdb);
534:   PetscStrcmp(PetscDebugger,"ddd",&isddd);
535:   PetscStrcmp(PetscDebugger,"kdbg",&iskdbg);
536:   PetscStrcmp(PetscDebugger,"ups",&isups);
537:   PetscStrcmp(PetscDebugger,"xldb",&isxldb);
538:   PetscStrcmp(PetscDebugger,"xdb",&isxdb);
539:   PetscStrcmp(PetscDebugger,"dbx",&isdbx);
540:   PetscStrcmp(PetscDebugger,"lldb",&islldb);

542:   if (isxxgdb || isups || isddd || iskdbg) printf("[%d]%s>>%s %s %d\n",rank,hostname,PetscDebugger,program,ppid);
543:   else if (isxldb) printf("[%d]%s>>%s -a %d %s\n",rank,hostname,PetscDebugger,ppid,program);
544:   else if (islldb) printf("[%d]%s>>%s -p %d\n",rank,hostname,PetscDebugger,ppid);
545:   else if (isdbx) {
546: #if defined(PETSC_USE_P_FOR_DEBUGGER)
547:      printf("[%d]%s>>%s -p %d %s\n",rank,hostname,PetscDebugger,ppid,program);
548: #elif defined(PETSC_USE_LARGEP_FOR_DEBUGGER)
549:      printf("[%d]%s>>%s -l ALL -P %d %s\n",rank,hostname,PetscDebugger,ppid,program);
550: #elif defined(PETSC_USE_A_FOR_DEBUGGER)
551:      printf("[%d]%s>>%s -a %d\n",rank,hostname,PetscDebugger,ppid);
552: #elif defined(PETSC_USE_PID_FOR_DEBUGGER)
553:      printf("[%d]%s>>%s -pid %d %s\n",rank,hostname,PetscDebugger,ppid,program);
554: #else
555:      printf("[%d]%s>>%s %s %d\n",rank,hostname,PetscDebugger,program,ppid);
556: #endif
557:   }
558: #endif /* PETSC_CANNOT_START_DEBUGGER */

560:   fflush(stdout); /* ignore error because may already be in error handler */

562:   sleeptime = 25; /* default to sleep waiting for debugger */
563:   PetscOptionsGetInt(NULL,NULL,"-debugger_pause",&sleeptime,NULL); /* ignore error because may already be in error handler */
564:   if (sleeptime < 0) sleeptime = -sleeptime;
565: #if defined(PETSC_NEED_DEBUGGER_NO_SLEEP)
566:   /*
567:       HP cannot attach process to sleeping debugger, hence count instead
568:   */
569:   {
570:     PetscReal x = 1.0;
571:     int       i =10000000;
572:     while (i--) x++;  /* cannot attach to sleeper */
573:   }
574: #elif defined(PETSC_HAVE_SLEEP_RETURNS_EARLY)
575:   /*
576:       IBM sleep may return at anytime, hence must see if there is more time to sleep
577:   */
578:   {
579:     int left = sleeptime;
580:     while (left > 0) left = sleep(left) - 1;
581:   }
582: #else
583:   PetscSleep(sleeptime);
584: #endif
585:   return 0;
586: }