home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!rutgers!news.columbia.edu!cucbs.chem.columbia.edu!puglia
- From: puglia@cucbs.chem.columbia.edu (Paul Puglia)
- Newsgroups: comp.unix.aix
- Subject: Program name from a core file
- Message-ID: <1992Sep12.173414.20931@cucbs.chem.columbia.edu>
- Date: 12 Sep 92 17:34:14 GMT
- Organization: Center for Biomolecular Simulation
- Lines: 93
-
-
- I as a goof I took on the problem of writing a program to extract
- the filename out of a core file. The program below seems to
- work fine with core generated when you issue a QUIT signal from
- the terminal. It doesn't work with arbitrary core files. I have
- alot of core from mwm and xdt lying around the system, but this
- program doesn't seem to be able to snag their name.
-
- While developing the program, I did discover a method that seems
- to always work. It relies on the fact that the program's full
- pathname is located after the the characters _= . I can't figure
- out if it is the shell that stores this or it is a result of the
- the core dump. This sequence is close to the SHELL environment so
- I would guess that it is store as part of the shell. It is
- really easy to extract the programs name out of the the core using
- this the sequence:
-
- strings core | grep _= | awk -F= '{print $2}'
-
- works just dandy. (cut or the a clever IFS would work just as
- well in place of the awk).
-
- If anyone can explain why my program doesn't work, and what the _=
- sequence is and who generates it I would appreciate it greatly .
-
- Thanks in advance,
-
- Paul Puglia
- Dept of Civil Engineering
- Columbia University
-
- puglia@cucevx.civil.columbia.edu
-
-
- /* Filename: corename.c
- Programmer: Paul J. Puglia
- created: 12-SEP-1992
-
- SYNOPSIS: corename [filename]
-
- DESCRIPTION: corename extracts the filename of the program that
- generated a core file in the current directory or from the path
- name that is supplied on the command line.
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/user.h>
- #include <sys/param.h>
- #include <sys/ldr.h>
- #include <sys/core.h>
-
- main(int argc,char **argv)
- {
- /* We need a file descriptor and some space for the
- core dump header */
-
- FILE *corefile; /* file descriptor for corefile */
-
- char programmename[MAXPATHLEN]; /*buffer to hold program name */
-
- /* Open up the core file. If the command is given with no
- arguement then try and open up a file named core in
- the current directory */
-
- if( argc < 2 )
- {
- if( (corefile = fopen("core","r")) == NULL)
- {
- fprintf(stderr, "can't open core file\n");
- exit(1);
- }
- }
- else
- {
- if( (corefile = fopen(argv[1],"r")) == NULL)
- {
- fprintf(stderr, "can't open %s\n",argv[1]);
- exit(1);
- }
- }
-
- /* We can get the name of the file that generated this core
- dump by positioning the file pointer sizeof(core_dump)+
- sizeof(ld_info)-sizeof(char *) from the start of the file
- then read until we hit a NULL. */
-
- fseek(corefile,CHDRSIZE+sizeof(struct ld_info)-sizeof(char *),SEEK_SET);
- fgets(programmename,MAXPATHLEN,corefile);
- printf("%s\n",programmename);
-
- }
-