From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,ef87ccc19881d1dd,start X-Google-Attributes: gidfee84,public From: Dirk Grunwald Subject: fyi - code to implement procedure backtrack Date: 1998/04/02 Message-ID: #1/1 X-Deja-AN: 340372526 Organization: University of Colorado Newsgroups: comp.unix.osf.osf1 I recently whacked up this code for a consulting gig, and the interfaces are a little obscure so it might serve as useful documentation for others. This code uses the symbol table and exception interface to produce a procedural back-trace, including line numbers and functions. This particular variant also uses the C++ name demangler to translate the procedure names. You need to link with the "-lmld" library for this to work. [foobar-293] ./proctrace Just calling handler in the normal way... In procedure handler(int), at line 101 of proctrace.C In procedure main, at line 131 of proctrace.C In procedure __start, at line -1 of In procedure , at line -1 of Stack unwound all the way Now, use a signal... In procedure handler(int), at line 101 of proctrace.C In procedure , at line -1 of In procedure main, at line 134 of proctrace.C In procedure __start, at line -1 of In procedure , at line -1 of Stack unwound all the way -------- #include #include #include #include #include #include #include #include /* * A list of all the objects (the pure ones) in this application. */ struct obj_list * ObjList; void handler(int x); void print_context(sigcontext& c, ostream& o) { unsigned long pc = c.sc_pc; // // Get the object corresponding to this PC // struct obj *obj = address_to_obj(ObjList, pc); char *procedure_name = ""; char proc_name_buf[1024]; char *filename = ""; int line_number = -1; if (obj) { // // Get the procedure in this object corresponding to this PC. // unsigned long mldproc = address_to_procedure(obj, pc); if (mldproc != OBJ_FAIL) { // // Get the procedure name // unsigned long mldsym = procedure_symbol(obj, mldproc); if ( mldsym != OBJ_FAIL) { procedure_name = symbol_name(obj, mldsym); // // And try to demangle it.. // int result = MLD_demangle_string(procedure_name, proc_name_buf, sizeof(proc_name_buf), MLD_SHOW_DEMANGLED_NAME | MLD_SHOW_INFO); if ( result == MLD_SOMETHING_RECOGNIZED) { procedure_name = proc_name_buf; } } long mldfile = procedure_to_file(obj, mldproc); if ( mldfile != OBJ_FAIL ) { filename = file_name(obj, mldfile); } long mldline = address_to_line(obj, pc); if ( mldline != OBJ_FAIL ) { line_number = mldline; } } } // // See /usr/include/machine/context.h for structure // // o << "Context" << endl; // o << "PC = 0x" << hex << c.sc_pc << dec << endl; o << "In procedure " << procedure_name << ", at line "; o << line_number << " of " << filename << endl; // o << endl << endl; #if 0 o << "Integer register contents" << endl; for (int i = 0; i < 32; i++) { o << "Reg[" << i << "] = 0x" << hex << c.sc_regs[i] << dec << endl; } #endif } void handler(int x) { // // Estbalish the current context. If this were a signal // handler, I assume I would be given the sigcontext and would // walk back from that. // sigcontext starting_context; exc_capture_context(&starting_context); print_context(starting_context, cout); for(;;) { exc_virtual_unwind( NULL, &starting_context); print_context(starting_context, cout); if ( starting_context.sc_pc == NULL ) { cout << "Stack unwound all the way" << endl; break; } } if (x != 'BEEF' ) { exit(0); } } int main(int argc, char **argv) { add_obj(&ObjList, argv[0]); // cout << "Just calling handler in the normal way..." << endl; handler('BEEF'); cout << "Now, use a signal..." << endl; signal(SIGSEGV, handler); char *foo = NULL; *foo = 'a'; return 0; } -- Dirk Grunwald Assoc. Prof, Univ. of Colorado at Boulder