⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postmort.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* postmort - post mortem dump		Author: C. W. Rose *//* Postmort: perform post-mortem on PC Minix 1.7 core files. * * $Id$ */ /* The 1.5 core file structure is a struct mem_map, the segment memory map,  * followed by a struct proc, the process table, followed by a dump of the  * text, data, and stack segments.  *   * This is the 8086/Intel version; 386 and 68K will differ.  It defaults to  * using the name 'core' for the core file, and 'a.out' for the symbol file.  * If there is no 'a.out', it will try and read the symbol table from  * 'symbol.out', then give up.  A non-existant symbol table is not a fatal  * error unless the -s option was used.  *   * The PC 1.5 kernel dump routines are odd - they dump the memory maps twice,  * the second time as part of the kernel process table, and the kernel  * process table size must be a multiple of 4.  Should a core file have a  * header with a magic number in future?  *   * The kernel include file paths need to be edited for each machine. */#include <sys/types.h>#include <minix/config.h>#include <minix/const.h>#include <minix/type.h>#include <limits.h>#include <signal.h>#include <stdlib.h>#undef EXTERN			/* <minix/const.h> defined this */#define EXTERN			/* so we get proc & mproc */#include "../../kernel/const.h"#include "../../kernel/type.h"#include "../../kernel/proc.h"#undef printf			/* kernel's const.h defined this */#include "../../mm/mproc.h"#include <a.out.h>#include <ctype.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#undef NULL#include <string.h>#include <unistd.h>#define FALSE		0#undef TRUE#define TRUE		~FALSE#define OK		1#define FAILED		-1#define CORE		"core"#define AOUT		"a.out"#define SYMB		"symbol.out"#define LINE_LEN	16#define MAXSYM		200#define SYMLEN		8/* Global variables */int opt_c = FALSE;		/* name of core file */int opt_d = FALSE;		/* dump raw data and stack segments */int opt_p = FALSE;		/* dump the kernel process table */int opt_s = FALSE;		/* name of symbol file */int opt_t = FALSE;		/* trace back the stack */int opt_x = FALSE;		/* debugging flag */char progname[20];		/* program name */char *segment_name[] = {	/* array of segment names */  "Text",  "Data",  "Stack"};int dbglvl = 0;			/* debugging level */int maxsym;			/* maximum symbol number */unsigned int baseptr;		/* reference copy of stack base pointer */unsigned int stackptr;		/* reference copy of stack pointer */long int lengths[NR_SEGS];	/* segment lengths */long int bases[NR_SEGS];	/* segment base addresses */struct sym {			/* symbol table addresses and labels */  unsigned int addr;  char label[SYMLEN + 1];} symtab[MAXSYM];/* Used by getopt(3) package */extern int optind, opterr, optopt;extern char *optarg;_PROTOTYPE(int binary, (int uc, char *sp));_PROTOTYPE(void dump_all_segs, (int fd));_PROTOTYPE(void dump_maps, (struct mem_map * mp));_PROTOTYPE(void dump_one_seg, (int fd, int segindex));_PROTOTYPE(void dump_proc_table, (struct proc * pt));_PROTOTYPE(void dump_registers, (struct proc * pt));_PROTOTYPE(void dump_sym_tab, (struct sym *st));_PROTOTYPE(void dump_stack, (struct stackframe_s * sp));_PROTOTYPE(int main, (int argc, char *argv[]));_PROTOTYPE(int parse_line, (char *ps));_PROTOTYPE(int read_symbol, (int fd));_PROTOTYPE(void stack_trace, (int fd));_PROTOTYPE(void usage, (void));/* B i n a r y * * Produce a binary representation of an 8-bit number. */int binary(ucc, sp)int ucc;char *sp;{  int j;  unsigned char k, uc;  uc = (unsigned char) ucc;  for (k = 0x80, j = 0; j < 8; j++) {	if ((uc & k) == 0)		*sp++ = '0';	else		*sp++ = '1';	if (j == 3) *sp++ = '$';	k >>= 1;  }  *sp = '\0';  return(0);}/* D u m p _ a l l _ s e g s * * Dump all the segments except for text */void dump_all_segs(fd)int fd;{  int j;  long int start;  start = (long) (NR_SEGS * sizeof(struct mem_map)) + sizeof(struct proc);  for (j = 1; j < NR_SEGS; j++) {	start += lengths[j - 1];	(void) lseek(fd, start, 0);	printf("\n");	dump_one_seg(fd, j);  }}/* D u m p _ m a p s * * Dump the memory maps */void dump_maps(mp)struct mem_map *mp;{  int j;  long int vir, phy, len;  printf("\t  Virtual\t  Physical\tLength\n");  printf("\t  address\t  address\n");  for (j = 0; j < NR_SEGS; j++) {	vir = (long) mp[j].mem_vir << CLICK_SHIFT;	phy = (long) mp[j].mem_phys << CLICK_SHIFT;	len = (long) mp[j].mem_len << CLICK_SHIFT;	printf("%s:\t0x%08.8lx\t0x%08.8lx\t%8ld (0x%08.8lx)\n",	       segment_name[j], vir, phy, len, len);	lengths[j] = len;	bases[j] = vir;  }}/* D u m p _ o n e _ s e g * * Dump a single segment */void dump_one_seg(fd, segindex)int fd, segindex;{  unsigned char dlen[LINE_LEN];  int i, amt, amt_read;  long int len, offset;  printf("%s segment\n\n", segment_name[segindex]);  len = lengths[segindex];  amt = LINE_LEN;  for (offset = 0; offset < len; offset += amt) {	if ((len - offset) < LINE_LEN) amt = (int) (len - offset);	if (dbglvl > 0)		printf("Length %ld, offset %ld, amt %d\n", len, offset, amt);	if ((amt_read = read(fd, (char *) dlen, (unsigned int) amt)) == -1) {		printf("Unexpected end of file\n");		exit(1);	}	printf("%08.8lx: ", bases[segindex] + offset);	for (i = 0; i < amt_read; i++) {		if (i == LINE_LEN / 2) printf("- ");		printf("%02.2x ", dlen[i]);	}	printf("  ");	for (i = 0; i < amt_read; i++) {		if (isprint(dlen[i]))			(void) putchar((char) dlen[i]);		else			(void) putchar('.');	}	(void) putchar('\n');	if (dbglvl > 0 && amt_read != amt)		printf("wanted = %d, got = %d, offset = %ld\n",		       amt, amt_read, offset);  }}/* D u m p _ p r o c _ t a b l e * * Dump the entire kernel proc table */void dump_proc_table(pt)struct proc *pt;{  printf("Kernel process table entries:\n\n");#if 0  printf("Process' registers:			0x%04.4x\n", pt->p_reg);	/* struct stackframe_s */  printf("Selector in gdt:			0x%04.4x\n", pt->p_ldt_sel);	/* reg_t */  printf("Descriptors for code and data:	0x%04.4x\n", pt->p_ldt[2]);	/* struct segdesc_s */#endif  printf("Number of this process:			0x%04.4x\n", pt->p_nr);	/* int */  printf("Nonzero if blocked by busy task:	0x%04.4x\n", pt->p_int_blocked);	/* int */  printf("Nonzero if held by busy syscall:	0x%04.4x\n", pt->p_int_held);	/* int */#if 0  printf("Next in chain of held-up processes:	0x%04.4x\n", pt->p_nextheld);	/* struct proc * */#endif  printf("P_SLOT_FREE SENDING, RECEIVING, etc.:	0x%04.4x\n", pt->p_flags);	/* int */#if 0  printf("Memory map:				0x%04.4x\n", pt->p_map[NR_SEGS]);	/* struct mem_map */#endif  printf("Process id passed in from MM:		0x%04.4x\n", pt->p_pid);	/* int */  printf("User time in ticks:			%ld\n", pt->user_time);	/* time_t */  printf("Sys time in ticks:			%ld\n", pt->sys_time);	/* time_t */  printf("Cumulative user time of children:	%ld\n", pt->child_utime);	/* time_t */  printf("Cumulative sys time of children:	%ld\n", pt->child_stime);	/* time_t */  printf("Time of next alarm in ticks or 0:	%ld\n", pt->p_alarm);	/* time_t */#if 0  printf("Ticks used in current quantum:	%d\n", pt->quantum_time);	/* int */  printf("Ticks used in last quantum:		%d\n", pt->quantum_last);	/* int */  printf("Current priority of the process:	%d\n", pt->curr_prio);	/* int */  printf("Base priority of the process:		%d\n", pt->base_prio);	/* int */  printf("Scale for profiling, 0 = none:	%u\n", pt->p_pscale);	/* unsigned */  printf("Profiling pc lower boundary:		%d\n", pt->p_plow);	/* vir_bytes */  printf("Profiling pc upper boundary:		%d\n", pt->p_phigh);	/* vir_bytes */  printf("Profiling buffer:			%d\n", pt->p_pbuf);	/* vir_bytes */  printf("Profiling buffer size:		%d\n", pt->p_psiz);	/* vir_bytes */#endif#if 0  printf("First proc wishing to send:		0x%04.4x\n", pt->p_callerq);	/* struct proc * */  printf("Link to next proc wishing to send:	0x%04.4x\n", pt->p_sendlink);	/* struct proc * */  printf("Pointer to message buffer:		0x%04.4x\n", pt->p_messbuf);	/* message * */#endif  printf("Expecting message from:			0x%04.4x\n", pt->p_getfrom);	/* int */#if 0  printf("Pointer to next ready process:	0x%04.4x\n", pt->p_nextready);	/* struct proc * */#endif  printf("Bit map for pending signals 1-16:	0x%04.4x\n", pt->p_pending);	/* int */  printf("Count of pending/unfinished signals:	0x%04.4x\n", pt->p_pendcount);	/* unsigned */}/* D u m p _ r e g i s t e r s * * Dump the registers from the proc table */void dump_registers(pt)struct proc *pt;{  char buff[32];  unsigned char uc;  /* Print the registers */  dump_stack(&pt->p_reg);  /* Build up a binary representation of the signal flags */  uc = (pt->p_pending >> 8) & 0xff;  (void) binary((int) uc, buff);  buff[9] = '$';  uc = pt->p_pending & 0xff;  (void) binary((int) uc, buff + 10);  printf("Pending signals = %s\n", buff);}/* D u m p _ s y m _ t a b * * Dump the symbol table */void dump_sym_tab(st)struct sym *st;{  int j;  printf("Symbol table entries (text):\n\n");  for (j = 0; j < maxsym; j++) 	printf("0x%08.8x T %s\n", symtab[j].addr, symtab[j].label);}/* D u m p _ s t a c k * * Dump the stack frame */void dump_stack(sp)struct stackframe_s *sp;{  char buff[32];  unsigned char uc;  /* Build up the binary PSW representation */  uc = (sp->psw >> 8) & 0xff;  (void) binary((int) uc, buff);  uc = sp->psw & 0xff;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -