From 20173464d2169892bde1a91bbfd1c2a811a79ace Mon Sep 17 00:00:00 2001 From: rsc Date: Tue, 18 Jan 2005 20:26:26 +0000 Subject: add core cmd --- include/mach.h | 1 + src/libmach/cmdline.c | 1 + src/libmach/crackelf.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/libmach/elf.h | 1 + 4 files changed, 65 insertions(+) diff --git a/include/mach.h b/include/mach.h index 79d8ac08..fdd68dfc 100644 --- a/include/mach.h +++ b/include/mach.h @@ -259,6 +259,7 @@ struct Fhdr void *dwarf; /* handle to dwarf image */ void *macho; /* handle to mach-o image */ struct Stab stabs; + char *cmd; /* command-line that produced core */ /* private */ Symbol *sym; /* cached list of symbols */ diff --git a/src/libmach/cmdline.c b/src/libmach/cmdline.c index d06f139e..4da38d9e 100644 --- a/src/libmach/cmdline.c +++ b/src/libmach/cmdline.c @@ -58,6 +58,7 @@ attachargs(int argc, char **argv, int omode) } fprint(2, "%s: %s %s %s\n", argv[i], hdr->aname, hdr->mname, hdr->fname); if(hdr->ftype == FCORE){ + fprint(2, "core cmd: %s\n", hdr->cmd); if(corpid){ fprint(2, "already have corpid %d; ignoring core %s\n", corpid, argv[i]); uncrackhdr(hdr); diff --git a/src/libmach/crackelf.c b/src/libmach/crackelf.c index f0f84230..6ed3ee51 100644 --- a/src/libmach/crackelf.c +++ b/src/libmach/crackelf.c @@ -6,6 +6,7 @@ static int mapelf(Fhdr *fp, ulong base, Map *map, Regs**); static int mapcoreregs(Fhdr *fp, Map *map, Regs**); +static char *getcorecmd(Fhdr *fp, Map *map); static struct { @@ -137,6 +138,7 @@ crackelf(int fd, Fhdr *fp) || ctab[i].mtype != fp->mtype) continue; elf->coreregs = ctab[i].coreregs; + elf->corecmd = ctab[i].corecmd; break; } return 0; @@ -257,6 +259,8 @@ mapelf(Fhdr *fp, ulong base, Map *map, Regs **regs) if(fp->ftype == FCORE){ if(mapcoreregs(fp, map, regs) < 0) fprint(2, "warning: reading core regs: %r"); + if((fp->cmd = getcorecmd(fp, map)) == nil) + fprint(2, "warning: reading core command: %r"); } return 0; @@ -347,3 +351,61 @@ mapcoreregs(Fhdr *fp, Map *map, Regs **rp) return -1; } +static char* +getcorecmd(Fhdr *fp, Map *map) +{ + int i; + uchar *a, *sa, *ea; + char *cmd; + uint n; + ElfNote note; + ElfProg *p; + Elf *elf; + + elf = fp->elf; + if(elf->corecmd == 0){ + werrstr("cannot parse %s %s cores", fp->mname, fp->aname); + return nil; + } + + for(i=0; inprog; i++){ + p = &elf->prog[i]; + if(p->type != ElfProgNote) + continue; + n = p->filesz; + a = malloc(n); + if(a == nil) + return nil; + if(seek(fp->fd, p->offset, 0) < 0 || readn(fp->fd, a, n) != n){ + free(a); + continue; + } + sa = a; + ea = a+n; + while(a < ea){ + note.offset = (a-sa) + p->offset; + if(unpacknote(elf, a, ea, ¬e, &a) < 0) + break; + switch(note.type){ + case ElfNotePrPsinfo: + if((n = elf->corecmd(elf, ¬e, &cmd)) < 0){ + free(sa); + return nil; + } + free(sa); + return cmd; + case ElfNotePrStatus: + case ElfNotePrFpreg: + case ElfNotePrTaskstruct: + case ElfNotePrAuxv: + case ElfNotePrXfpreg: + break; + } + // fprint(2, "0x%lux note %s/%lud %p\n", note.offset, note.name, note.type, note.desc); + } + free(sa); + } + fprint(2, "could not find registers in core file\n"); + return nil; +} + diff --git a/src/libmach/elf.h b/src/libmach/elf.h index 02fb5851..6b65cfc8 100644 --- a/src/libmach/elf.h +++ b/src/libmach/elf.h @@ -221,6 +221,7 @@ struct Elf ulong dynamic; /* offset to elf dynamic crap */ int (*coreregs)(Elf*, ElfNote*, uchar**); + int (*corecmd)(Elf*, ElfNote*, char**); }; Elf* elfopen(char*); -- cgit v1.2.3