home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / aix / 11737 < prev    next >
Encoding:
Text File  |  1992-11-19  |  3.0 KB  |  99 lines

  1. Xref: sparky comp.unix.aix:11737 bit.listserv.aix-l:1746 bit.listserv.power-l:324
  2. Newsgroups: bull.sys.aix,comp.unix.aix,bit.listserv.aix-l,bit.listserv.power-l
  3. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!yktnews!lucien
  4. From: lucien@watson.ibm.com (Lucien Van Elsen)
  5. Subject: Re: How to get more info about SIGSEGV
  6. Sender: news@watson.ibm.com (NNTP News Poster)
  7. Message-ID: <LUCIEN.92Nov19142240@fionavar.watson.ibm.com>
  8. In-Reply-To: mwarren@rws1.ma30.bull.com's message of Tue, 17 Nov 1992 16:30:09 GMT
  9. Date: Thu, 19 Nov 1992 19:22:40 GMT
  10. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  11. References: <1992Nov17.163009.11601@mips2.ma30.bull.com>
  12. Nntp-Posting-Host: fionavar.watson.ibm.com
  13. Organization: IBM T.J Watson Research Center
  14. Lines: 83
  15.  
  16. mwarren@rws1.ma30.bull.com (Mark Warren) writes:
  17.  
  18. >We have a program that can (in normal operation) perform a read or
  19. >write access to a non-existent (un-mapped) virtual address, or a write
  20. >to a read-only address.  We can tell when it occurs by catching
  21. >SIGSEGV, but it is very to tell within the program whether the fault
  22. >occurred due to a write to a read-only address, or due to a write (or
  23. >read) to an un-mapped address.  Also, we have a hard time determining
  24. >the offending address.
  25. >
  26. >Is there any way we can interrogate any kernel service to determine
  27. >this information from within the SIGSEGV handler?
  28.  
  29. You don't need to deal with the kernel directly to determine this- the
  30. information you need is passed to the signal handler in the sigcontext 
  31. struct.  You can look at the scp->sc_jmpbuf.jmp_context.excp_type to
  32. determine what exception caused the SIGSEGV, and at the jmp_context.iar to
  33. determine which instruction caused it.  An example and program are below:
  34.  
  35. fionavar /u/lucien/test/sigcontext)./sigc prot
  36. protection fault
  37. Error at address 100005d4
  38.  
  39. fionavar /u/lucien/test/sigcontext)./sigc addr
  40. invalid address
  41. Error at address 1000060c
  42.  
  43.  
  44. ----- sigc.c -----
  45.  
  46. #include <sys/errno.h>
  47. #include <sys/types.h>
  48. #include <sys/except.h>
  49. #include <sys/signal.h>
  50.  
  51. void
  52. sig_handler(signal, code, scp)
  53.      int signal, code;
  54.      struct sigcontext *scp;
  55. {
  56.  
  57.   switch (scp->sc_jmpbuf.jmp_context.excp_type) {
  58.   case EXCEPT_PROT:
  59.     printf("protection fault\n");
  60.     break;
  61.   case EXCEPT_INV_ADDR:
  62.   case EFAULT:
  63.     printf("invalid address\n");
  64.     break;
  65.   default:
  66.     printf("Unknown exception %d:\n",scp->sc_jmpbuf.jmp_context.excp_type);
  67.     break;
  68.   }
  69.   printf("Error at address %x\n",scp->sc_jmpbuf.jmp_context.iar);
  70.   exit(0);
  71. }
  72.  
  73. main(argc,argv)
  74.      int argc;
  75.      char **argv;
  76. {
  77.   struct sigaction action;
  78.   char *p;
  79.  
  80.   action.sa_flags = 0;
  81.   sigemptyset(&action.sa_mask);
  82.   action.sa_handler = sig_handler;
  83.  
  84.   sigaction(SIGSEGV, &action, NULL);
  85.  
  86.   if(!strcmp("prot",argv[1])) {
  87.     p = NULL;
  88.     *p = '\0';
  89.   }
  90.   if(!strcmp("addr",argv[1])) {
  91.     p = (char *) 0x80000000;
  92.     *p = '\0';
  93.   }
  94. }
  95. --
  96. -----------------------------------------------------------------------
  97. Lucien Van Elsen                                          IBM  Research
  98. lucien@watson.ibm.com                                     Project Agora
  99.