home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / aix / 9610 < prev    next >
Encoding:
Text File  |  1992-09-12  |  3.1 KB  |  103 lines

  1. Path: sparky!uunet!stanford.edu!rutgers!news.columbia.edu!cucbs.chem.columbia.edu!puglia
  2. From: puglia@cucbs.chem.columbia.edu (Paul Puglia)
  3. Newsgroups: comp.unix.aix
  4. Subject: Program name from a core file
  5. Message-ID: <1992Sep12.173414.20931@cucbs.chem.columbia.edu>
  6. Date: 12 Sep 92 17:34:14 GMT
  7. Organization: Center for Biomolecular Simulation
  8. Lines: 93
  9.  
  10.  
  11. I as a goof I took on the problem of writing a program to extract
  12. the filename out of a core file.  The program below seems to 
  13. work fine with core generated when you issue a QUIT signal from 
  14. the terminal. It doesn't work with arbitrary core files. I have
  15. alot of core from mwm and xdt lying around the system, but this 
  16. program doesn't seem to be able to snag their name.  
  17.  
  18. While developing the program, I did discover a method that seems 
  19. to always work.  It relies on the fact that the program's full
  20. pathname is located after the the characters _= . I can't figure
  21. out if it is the shell that stores this or it is a result of the
  22. the core dump.  This sequence is close to the SHELL environment so
  23. I would guess that it is store as part of the shell.  It is
  24. really easy to extract the programs name out of the the core using
  25. this the sequence:
  26.  
  27. strings core | grep _= | awk -F= '{print $2}' 
  28.  
  29. works just dandy. (cut or the a clever IFS would work just as 
  30. well in place of the awk).   
  31.  
  32. If anyone can explain why my program doesn't work, and what the _=
  33. sequence is and who generates it I would appreciate it greatly .
  34.  
  35. Thanks in advance,
  36.  
  37. Paul Puglia 
  38. Dept of Civil Engineering 
  39. Columbia University
  40.  
  41. puglia@cucevx.civil.columbia.edu
  42.  
  43.  
  44. /* Filename: corename.c
  45.    Programmer: Paul J. Puglia
  46.    created: 12-SEP-1992
  47.  
  48.    SYNOPSIS:  corename [filename]
  49.     
  50.    DESCRIPTION:  corename extracts the filename of the program that
  51.    generated a core file in the current directory or from the path 
  52.    name that is supplied on the command line.
  53.  
  54. */
  55.  
  56.   #include <stdio.h>
  57.   #include <stdlib.h>
  58.   #include <sys/user.h>
  59.   #include <sys/param.h>
  60.   #include <sys/ldr.h>
  61.   #include <sys/core.h>
  62.  
  63.   main(int argc,char **argv)
  64.   {
  65.     /* We need a file descriptor and some space for the
  66.      core dump header */
  67.  
  68.     FILE *corefile;  /* file descriptor for corefile */
  69.  
  70.     char programmename[MAXPATHLEN];  /*buffer to hold program name */
  71.  
  72.     /* Open up the core file. If the command is given with no
  73.        arguement then try and open up a file named core in
  74.        the current directory */
  75.  
  76.     if( argc < 2 )
  77.       {
  78.     if( (corefile = fopen("core","r")) == NULL)
  79.       {
  80.         fprintf(stderr, "can't open core file\n");
  81.         exit(1);
  82.       }
  83.       }
  84.     else
  85.       {
  86.     if( (corefile = fopen(argv[1],"r")) == NULL)
  87.       {
  88.         fprintf(stderr, "can't open %s\n",argv[1]);
  89.         exit(1);
  90.       }
  91.       }
  92.  
  93.     /* We can get the name of the file that generated this core
  94.        dump by positioning the file pointer  sizeof(core_dump)+
  95.        sizeof(ld_info)-sizeof(char *) from the start of the file
  96.        then read until we hit a NULL. */ 
  97.     
  98.     fseek(corefile,CHDRSIZE+sizeof(struct ld_info)-sizeof(char *),SEEK_SET);
  99.     fgets(programmename,MAXPATHLEN,corefile);
  100.     printf("%s\n",programmename);
  101.     
  102.   }
  103.