* fyi - code to implement procedure backtrack
@ 1998-04-02 5:00 Dirk Grunwald
0 siblings, 0 replies; only message in thread
From: Dirk Grunwald @ 1998-04-02 5:00 UTC (permalink / raw)
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 <unknown>, at line -1 of <unknown>
Stack unwound all the way
Now, use a signal...
In procedure handler(int), at line 101 of proctrace.C
In procedure <unknown>, at line -1 of <unknown>
In procedure main, at line 134 of proctrace.C
In procedure __start, at line -1 of
In procedure <unknown>, at line -1 of <unknown>
Stack unwound all the way
--------
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <excpt.h>
#include <stream.h>
#include <obj.h>
#include <sym.h>
#include <demangle.h>
/*
* 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 = "<unknown>";
char proc_name_buf[1024];
char *filename = "<unknown>";
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1998-04-02 5:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-04-02 5:00 fyi - code to implement procedure backtrack Dirk Grunwald
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox