Blob


1 #ifndef _MACH_H_
2 #define _MACH_H_ 1
3 #if defined(__cplusplus)
4 extern "C" {
5 #endif
7 AUTOLIB(mach)
9 /*
10 * Architecture-dependent application data.
11 *
12 * The code assumes that ulong is big enough to hold
13 * an address on any system of interest as well as any
14 * register. Debugging 64-bit code on 32-bit machines
15 * will be interesting.
16 *
17 * Supported architectures:
18 *
19 * MIPS R3000
20 * Motorola 68020
21 * Intel 386
22 * SPARC
23 * PowerPC (limited)
24 * ARM (limited)
25 * Intel 960 (limited)
26 * AT&T 3210 DSP (limited)
27 * MIPS2 (R4000)
28 */
30 typedef struct Fhdr Fhdr;
31 typedef struct Loc Loc;
32 typedef struct Mach Mach;
33 typedef struct Map Map;
34 typedef struct Regdesc Regdesc;
35 typedef struct Regs Regs;
36 typedef struct Seg Seg;
37 typedef struct Symbol Symbol;
38 typedef struct Symtype Symtype;
40 typedef int (*Tracer)(Map*, Regs*, ulong, ulong, Symbol*, int);
42 extern Mach *mach;
43 extern Mach *machcpu;
45 /*
46 * Byte-order data layout manipulation.
47 * swap.c ieee.c
48 */
49 u16int beswap2(u16int u);
50 u32int beswap4(u32int u);
51 u64int beswap8(u64int u);
52 int beieeeftoa32(char*, uint, void*);
53 int beieeeftoa64(char*, uint, void*);
54 int beieeeftoa80(char*, uint, void*);
56 u16int leswap2(u16int u);
57 u32int leswap4(u32int u);
58 u64int leswap8(u64int u);
59 int leieeeftoa32(char *a, uint n, void *v);
60 int leieeeftoa64(char *a, uint n, void *v);
61 int leieeeftoa80(char *a, uint n, void *v);
63 u16int beload2(uchar*);
64 u32int beload4(uchar*);
65 u64int beload8(uchar*);
67 u16int leload2(uchar*);
68 u32int leload4(uchar*);
69 u64int leload8(uchar*);
71 int ieeeftoa32(char *a, uint n, u32int u);
72 int ieeeftoa64(char *a, uint n, u32int h, u32int u);
74 /*
75 * Machine-independent access to an executable image.
76 * map.c
77 */
78 struct Seg
79 {
80 char *name;
81 char *file;
82 uchar *p;
83 int fd;
84 int pid;
85 ulong base;
86 ulong size;
87 ulong offset;
88 int (*rw)(Map*, Seg*, ulong, void*, uint, int);
89 };
91 struct Map
92 {
93 int nseg;
94 Seg *seg;
95 };
97 struct Regs
98 {
99 int (*rw)(Regs*, char*, ulong*, int);
100 };
102 typedef struct UregRegs UregRegs;
103 struct UregRegs
105 Regs r;
106 uchar *ureg;
107 };
108 int _uregrw(Regs*, char*, ulong*, int);
110 typedef struct PidRegs PidRegs;
111 struct PidRegs
113 Regs r;
114 int pid;
115 };
117 Map* allocmap(void);
118 int addseg(Map *map, Seg seg);
119 int findseg(Map *map, char *name, char *file);
120 int addrtoseg(Map *map, ulong addr, Seg *seg);
121 int addrtosegafter(Map *map, ulong addr, Seg *seg);
122 void removeseg(Map *map, int i);
123 void freemap(Map*);
125 int get1(Map *map, ulong addr, uchar *a, uint n);
126 int get2(Map *map, ulong addr, u16int *u);
127 int get4(Map *map, ulong addr, u32int *u);
128 int get8(Map *map, ulong addr, u64int *u);
130 int put1(Map *map, ulong addr, uchar *a, uint n);
131 int put2(Map *map, ulong addr, u16int u);
132 int put4(Map *map, ulong addr, u32int u);
133 int put8(Map *map, ulong addr, u64int u);
135 int rget(Regs*, char*, ulong*);
136 int rput(Regs*, char*, ulong);
138 /*
139 * A location is either a memory address or a register.
140 * It is useful to be able to specify constant values that
141 * originate from outside the register set and memory,
142 * hence LCONST. If the register values are known, then
143 * we can dispense with LOFFSET, but it's useful to be able
144 * to look up local symbols (via findlsym) with locations
145 * like 8(BP).
147 * loc.c
148 */
150 enum
152 /* location type */
153 LNONE,
154 LREG, /* register */
155 LADDR, /* absolute address */
156 LCONST, /* constant (an anonymous readonly location) */
157 LOFFSET, /* dereference offset + register ptr */
158 };
160 struct Loc
162 uint type; /* LNONE, ... */
163 char *reg; /* LREG */
164 ulong addr; /* LADDR, CONST */
165 long offset; /* LOFFSET */
166 };
168 int lget1(Map *map, Regs *regs, Loc loc, uchar *a, uint n);
169 int lget2(Map *map, Regs *regs, Loc loc, u16int *v);
170 int lget4(Map *map, Regs *regs, Loc loc, u32int *v);
171 int lget8(Map *map, Regs *regs, Loc loc, u64int *v);
173 int lput1(Map *map, Regs *regs, Loc loc, uchar *a, uint n);
174 int lput2(Map *map, Regs *regs, Loc loc, u16int v);
175 int lput4(Map *map, Regs *regs, Loc loc, u32int v);
176 int lput8(Map *map, Regs *regs, Loc loc, u64int v);
178 Loc locnone(void);
179 Loc locaddr(ulong addr);
180 Loc locconst(ulong con);
181 Loc locreg(char*);
182 Loc locindir(char*, long);
184 /*
185 * Executable file parsing.
187 * An Fhdr represents an open file image.
188 * The contents are a grab bag of constants used for the
189 * various file types. Not all elements are used by all
190 * file types.
192 * crackadotplan9.c crackadotunix.c
193 * crackelf.c crackdwarf.c
194 */
195 enum
197 /* file types */
198 FNONE,
199 FEXEC, /* executable image */
200 FLIB, /* library */
201 FOBJ, /* object file */
202 FRELOC, /* relocatable executable */
203 FSHLIB, /* shared library */
204 FSHOBJ, /* shared object */
205 FCORE, /* core dump */
206 FBOOT, /* bootable image */
207 FKERNEL, /* kernel image */
208 NFTYPE,
210 /* abi types */
211 ANONE = 0,
212 APLAN9,
213 ALINUX,
214 AFREEBSD,
215 AMACH,
216 NATYPE
217 };
219 /* I wish this could be kept in stabs.h */
220 struct Stab
222 uchar *stabbase;
223 uint stabsize;
224 char *strbase;
225 uint strsize;
226 u16int (*e2)(uchar*);
227 u32int (*e4)(uchar*);
228 };
230 struct Fhdr
232 int fd; /* file descriptor */
233 char *filename; /* file name */
234 Mach *mach; /* machine */
235 char *mname; /* 386, power, ... */
236 uint mtype; /* machine type M386, ... */
237 char *fname; /* core, executable, boot image, ... */
238 uint ftype; /* file type FCORE, ... */
239 char *aname; /* abi name */
240 uint atype; /* abi type ALINUX, ... */
242 ulong magic; /* magic number */
243 ulong txtaddr; /* text address */
244 ulong entry; /* entry point */
245 ulong txtsz; /* text size */
246 ulong txtoff; /* text offset in file */
247 ulong dataddr; /* data address */
248 ulong datsz; /* data size */
249 ulong datoff; /* data offset in file */
250 ulong bsssz; /* bss size */
251 ulong symsz; /* symbol table size */
252 ulong symoff; /* symbol table offset in file */
253 ulong sppcsz; /* size of sp-pc table */
254 ulong sppcoff; /* offset of sp-pc table in file */
255 ulong lnpcsz; /* size of line number-pc table */
256 ulong lnpcoff; /* size of line number-pc table */
257 char *txtfil; /* text name, for core files */
258 void *elf; /* handle to elf image */
259 void *dwarf; /* handle to dwarf image */
260 void *macho; /* handle to mach-o image */
261 struct Stab stabs;
262 char *cmd; /* command-line that produced core */
264 /* private */
265 Symbol *sym; /* cached list of symbols */
266 Symbol **byname;
267 uint nsym;
268 Symbol *esym; /* elf symbols */
269 Symbol **ebyname;
270 uint nesym;
271 ulong base; /* base address for relocatables */
272 Fhdr *next; /* link to next fhdr (internal) */
274 /* file mapping */
275 int (*map)(Fhdr*, ulong, Map*, Regs**);
277 /* debugging symbol access; see below */
278 int (*syminit)(Fhdr*);
279 void (*symclose)(Fhdr*);
281 int (*pc2file)(Fhdr*, ulong, char*, uint, ulong*);
282 int (*file2pc)(Fhdr*, char*, ulong, ulong*);
283 int (*line2pc)(Fhdr*, ulong, ulong, ulong*);
285 int (*lookuplsym)(Fhdr*, Symbol*, char*, Symbol*);
286 int (*indexlsym)(Fhdr*, Symbol*, uint, Symbol*);
287 int (*findlsym)(Fhdr*, Symbol*, Loc, Symbol*);
289 int (*unwind)(Fhdr*, Map*, Regs*, ulong*, Symbol*);
290 };
292 Fhdr* crackhdr(char *file, int mode);
293 void uncrackhdr(Fhdr *hdr);
294 int crackelf(int fd, Fhdr *hdr);
295 int crackmacho(int fd, Fhdr *hdr);
297 int symopen(Fhdr*);
298 int symdwarf(Fhdr*);
299 int symelf(Fhdr*);
300 int symstabs(Fhdr*);
301 int symmacho(Fhdr*);
302 void symclose(Fhdr*);
304 int mapfile(Fhdr *fp, ulong base, Map *map, Regs **regs);
305 void unmapfile(Fhdr *fp, Map *map);
307 /*
308 * Process manipulation.
309 */
310 int mapproc(int pid, Map *map, Regs **regs);
311 void unmapproc(Map *map);
312 int detachproc(int pid);
313 int ctlproc(int pid, char *msg);
314 int procnotes(int pid, char ***notes);
315 char* proctextfile(int pid);
317 /*
318 * Command-line debugger help
319 */
320 extern Fhdr *symhdr;
321 extern Fhdr *corhdr;
322 extern char *symfil;
323 extern char *corfil;
324 extern int corpid;
325 extern Regs *correg;
326 extern Map *symmap;
327 extern Map *cormap;
329 int attachproc(int pid);
330 int attachcore(Fhdr *hdr);
331 int attachargs(int argc, char **argv, int omode);
333 /*
334 * Machine descriptions.
336 * mach.c
337 * mach386.c dis386.c
338 * machsparc.c dissparc.c
339 * ...
340 */
342 /*
343 * Register sets. The Regs are opaque, accessed by using
344 * the reglist (and really the accessor functions).
345 */
346 enum
348 /* must be big enough for all machine register sets */
349 REGSIZE = 256,
351 RINT = 0<<0,
352 RFLT = 1<<0,
353 RRDONLY = 1<<1,
354 };
356 struct Regdesc
358 char *name; /* register name */
359 uint offset; /* offset in b */
360 uint flags; /* RINT/RFLT/RRDONLY */
361 uint format; /* print format: 'x', 'X', 'f', 'z', 'Z' */
362 };
364 enum
366 /* machine types */
367 MNONE,
368 MMIPS, /* MIPS R3000 */
369 MSPARC, /* SUN SPARC */
370 M68000, /* Motorola 68000 */
371 M386, /* Intel 32-bit x86*/
372 M960, /* Intel 960 */
373 M3210, /* AT&T 3210 DSP */
374 MMIPS2, /* MIPS R4000 */
375 M29000, /* AMD 29000 */
376 MARM, /* ARM */
377 MPOWER, /* PowerPC */
378 MALPHA, /* DEC/Compaq Alpha */
379 NMTYPE
380 };
382 struct Mach
384 char *name; /* "386", ... */
385 uint type; /* M386, ... */
386 Regdesc *reglist; /* register set */
387 uint regsize; /* size of register set in bytes */
388 uint fpregsize; /* size of fp register set in bytes */
389 char *pc; /* name of program counter */
390 char *sp; /* name of stack pointer */
391 char *fp; /* name of frame pointer */
392 char *link; /* name of link register */
393 char *sbreg; /* name of static base */
394 ulong sb; /* value of static base */
395 uint pgsize; /* page size */
396 ulong kbase; /* kernel base address for Plan 9 */
397 ulong ktmask; /* ktzero = kbase & ~ktmask */
398 uint pcquant; /* pc quantum */
399 uint szaddr; /* size of pointer in bytes */
400 uint szreg; /* size of integer register */
401 uint szfloat; /* size of float */
402 uint szdouble; /* size of double */
403 char** windreg; /* unwinding registers */
404 uint nwindreg;
406 uchar bpinst[4]; /* break point instruction */
407 uint bpsize; /* size of bp instruction */
409 int (*foll)(Map*, Regs*, ulong, ulong*); /* follow set */
410 char* (*exc)(Map*, Regs*); /* last exception */
411 int (*unwind)(Map*, Regs*, ulong*, Symbol*);
413 /* cvt to local byte order */
414 u16int (*swap2)(u16int);
415 u32int (*swap4)(u32int);
416 u64int (*swap8)(u64int);
417 int (*ftoa32)(char*, uint, void*);
418 int (*ftoa64)(char*, uint, void*);
419 int (*ftoa80)(char*, uint, void*);
421 /* disassembly */
422 int (*das)(Map*, ulong, char, char*, int); /* symbolic */
423 int (*kendas)(Map*, ulong, char, char*, int); /* symbolic */
424 int (*codas)(Map*, ulong, char, char*, int);
425 int (*hexinst)(Map*, ulong, char*, int); /* hex */
426 int (*instsize)(Map*, ulong); /* instruction size */
427 };
429 Mach *machbyname(char*);
430 Mach *machbytype(uint);
432 extern Mach mach386;
433 extern Mach machsparc;
434 extern Mach machmips;
435 extern Mach machpower;
437 /*
438 * Debugging symbols and type information.
439 * (Not all objects include type information.)
441 * sym.c
442 */
444 enum
446 /* symbol table classes */
447 CNONE,
448 CAUTO, /* stack variable */
449 CPARAM, /* function parameter */
450 CTEXT, /* text segment */
451 CDATA, /* data segment */
452 CANY,
453 };
455 struct Symbol
457 char *name; /* name of symbol */
458 /* Symtype *typedesc; /* type info, if any */
459 Loc loc; /* location of symbol */
460 Loc hiloc; /* location of end of symbol */
461 char class; /* CAUTO, ... */
462 char type; /* type letter from a.out.h */
463 Fhdr *fhdr; /* where did this come from? */
464 uint index; /* in by-address list */
466 /* private use by various symbol implementations */
467 union {
468 struct {
469 uint unit;
470 uint uoff;
471 } dwarf;
472 struct {
473 uint i;
474 uint locals;
475 char *dir;
476 char *file;
477 char frameptr;
478 uint framesize;
479 } stabs;
480 } u;
481 };
483 /* look through all currently cracked Fhdrs calling their fns */
484 int pc2file(ulong pc, char *file, uint nfile, ulong *line);
485 int file2pc(char *file, ulong line, ulong *addr);
486 int line2pc(ulong basepc, ulong line, ulong *pc);
487 int fnbound(ulong pc, ulong *bounds);
488 int fileline(ulong pc, char *a, uint n);
489 int pc2line(ulong pc, ulong *line);
491 int lookupsym(char *fn, char *var, Symbol *s);
492 int indexsym(uint ndx, Symbol *s);
493 int findsym(Loc loc, uint class, Symbol *s);
494 int findexsym(Fhdr*, uint, Symbol*);
496 int lookuplsym(Symbol *s1, char *name, Symbol *s2);
497 int indexlsym(Symbol *s1, uint ndx, Symbol *s2);
498 int findlsym(Symbol *s1, Loc loc, Symbol *s);
499 int symoff(char *a, uint n, ulong addr, uint class);
500 int unwindframe(Map *map, Regs *regs, ulong *next, Symbol*);
502 void _addhdr(Fhdr*);
503 void _delhdr(Fhdr*);
504 extern Fhdr* fhdrlist;
505 Fhdr* findhdr(char*);
507 Symbol* flookupsym(Fhdr*, char*);
508 Symbol* ffindsym(Fhdr*, Loc, uint);
509 Symbol* _addsym(Fhdr*, Symbol*);
511 /*
512 * Stack frame walking.
514 * frame.c
515 */
516 int stacktrace(Map*, Regs*, Tracer);
517 int windindex(char*);
518 Loc* windreglocs(void);
520 /*
521 * Debugger help.
522 */
523 int localaddr(Map *map, Regs *regs, char *fn, char *var, ulong *val);
524 int fpformat(Map *map, Regdesc *reg, char *a, uint n, uint code);
525 char* _hexify(char*, ulong, int);
526 int locfmt(Fmt*);
527 int loccmp(Loc*, Loc*);
528 int locsimplify(Map *map, Regs *regs, Loc loc, Loc *newloc);
529 Regdesc* regdesc(char*);
531 struct ps_prochandle
533 int pid;
534 };
536 extern int machdebug;
537 #if defined(__cplusplus)
539 #endif
540 #endif