Export a stack walking API (usable from C or C++) from XPCOM. b=374689 r=bsmedberg a=bzbarsky

git-svn-id: svn://10.0.0.236/trunk@231858 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
dbaron%dbaron.org
2007-08-10 21:32:49 +00:00
parent 47aa30fa5c
commit 82477775ec
9 changed files with 221 additions and 158 deletions

View File

@@ -37,11 +37,11 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsStackFrameUnix.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "nscore.h"
#include <stdio.h>
// On glibc 2.1, the Dl_info api defined in <dlfcn.h> is only exposed
// if __USE_GNU is defined. I suppose its some kind of standards
@@ -86,9 +86,12 @@ void DemangleSymbol(const char * aSymbol,
#if defined(linux) && defined(__GNUC__) && (defined(__i386) || defined(PPC) || defined(__x86_64__)) // i386 or PPC Linux stackwalking code
void DumpStackToFile(FILE* aStream)
EXPORT_XPCOM_API(nsresult)
NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
void *aClosure)
{
// Stack walking code courtesy Kipp's "leaky".
char buf[512];
// Get the frame pointer
void **bp;
@@ -103,14 +106,15 @@ void DumpStackToFile(FILE* aStream)
bp = (void**) __builtin_frame_address(0);
#endif
int skip = 2;
int skip = aSkipFrames;
for ( ; (void**)*bp > bp; bp = (void**)*bp) {
void *pc = *(bp+1);
if (--skip <= 0) {
if (--skip < 0) {
Dl_info info;
int ok = dladdr(pc, &info);
if (!ok) {
fprintf(aStream, "UNKNOWN %p\n", pc);
snprintf(buf, sizeof(buf), "UNKNOWN %p\n", pc);
(*aCallback)(buf, aClosure);
continue;
}
@@ -119,8 +123,9 @@ void DumpStackToFile(FILE* aStream)
const char * symbol = info.dli_sname;
int len;
if (!symbol || !(len = strlen(symbol))) {
fprintf(aStream, "UNKNOWN [%s +0x%08X]\n",
info.dli_fname, foff);
snprintf(buf, sizeof(buf), "UNKNOWN [%s +0x%08X]\n",
info.dli_fname, foff);
(*aCallback)(buf, aClosure);
continue;
}
@@ -134,10 +139,12 @@ void DumpStackToFile(FILE* aStream)
}
PRUint32 off = (char*)pc - (char*)info.dli_saddr;
fprintf(aStream, "%s+0x%08X [%s +0x%08X]\n",
symbol, off, info.dli_fname, foff);
snprintf(buf, sizeof(buf), "%s+0x%08X [%s +0x%08X]\n",
symbol, off, info.dli_fname, foff);
(*aCallback)(buf, aClosure);
}
}
return NS_OK;
}
#elif defined(__sun) && (defined(__sparc) || defined(sparc) || defined(__i386) || defined(i386))
@@ -152,16 +159,15 @@ void DumpStackToFile(FILE* aStream)
#include <sys/regset.h>
#include <sys/stack.h>
static int load_address ( void * pc, void * arg, FILE * aStream );
static int write_address_file ( void * pc );
static int load_address ( void * pc, void * arg );
static struct bucket * newbucket ( void * pc );
static struct frame * cs_getmyframeptr ( void );
static void cs_walk_stack ( void * (*read_func)(char * address),
struct frame * fp,
int (*operate_func)(void *, void *),
void * usrarg, FILE * aStream );
void * usrarg );
static void cs_operate ( void (*operate_func)(void *, void *),
void * usrarg, FILE * aStream );
void * usrarg );
#ifndef STACK_BIAS
#define STACK_BIAS 0
@@ -190,9 +196,10 @@ struct bucket {
struct bucket * next;
};
struct mybuf {
char * buffer;
int chars_left;
struct my_user_args {
NS_WalkStackCallback callback;
PRUint32 skipFrames;
void *closure;
};
@@ -225,11 +232,12 @@ myinit()
static int
write_address_file(void * pc, FILE* aStream)
load_address(void * pc, void * arg )
{
static struct bucket table[2048];
static mutex_t lock;
struct bucket * ptr;
struct my_user_args * args = (struct my_user_args *) arg;
unsigned int val = NS_PTR_TO_INT32(pc);
@@ -244,7 +252,6 @@ write_address_file(void * pc, FILE* aStream)
if (ptr->next) {
mutex_unlock(&lock);
return (ptr->next->index);
} else {
char buffer[4096], dembuff[4096];
Dl_info info;
@@ -269,37 +276,12 @@ write_address_file(void * pc, FILE* aStream)
if (strlen(dembuff)) {
func = dembuff;
}
fprintf(aStream, "%u %s:%s+0x%x\n",
ptr->next->index,
lib,
func,
(char *)pc - (char*)info.dli_saddr);
return (ptr->next->index);
snprintf(buffer, sizeof(buffer), "%u %s:%s+0x%x\n",
ptr->next->index, lib, func,
(char *)pc - (char*)info.dli_saddr);
(*args.callback)(buffer, args.closure);
}
}
static int
load_address(void * pc, void * arg, FILE * aStream)
{
struct mybuf * buf = (struct mybuf *) arg;
char name[80];
int len;
sprintf(name, " %u", write_address_file(pc, aStream));
len = strlen(name);
if (len >= buf->chars_left)
return (1);
strcat(buf->buffer, name);
buf->chars_left -= len;
return (0);
return 0;
}
@@ -336,12 +318,12 @@ csgetframeptr()
static void
cswalkstack(struct frame *fp, int (*operate_func)(void *, void *, FILE *),
void *usrarg, FILE * aStream)
void *usrarg)
{
while (fp != 0 && fp->fr_savpc != 0) {
if (operate_func((void *)fp->fr_savpc, usrarg, aStream) != 0)
if (operate_func((void *)fp->fr_savpc, usrarg) != 0)
break;
/*
* watch out - libthread stacks look funny at the top
@@ -355,21 +337,24 @@ cswalkstack(struct frame *fp, int (*operate_func)(void *, void *, FILE *),
static void
cs_operate(int (*operate_func)(void *, void *, FILE *), void * usrarg, FILE *aStream)
cs_operate(int (*operate_func)(void *, void *, FILE *), void * usrarg)
{
cswalkstack(csgetframeptr(), operate_func, usrarg, aStream);
cswalkstack(csgetframeptr(), operate_func, usrarg);
}
void DumpStackToFile(FILE* aStream)
EXPORT_XPCOM_API(nsresult)
NS_StackWalk(NS_WalkStackCallback aCallback, PRUint32 aSkipFrames,
void *aClosure)
{
char buffer[LOGSIZE];
struct mybuf mybuf;
struct my_user_args args;
if (!initialized)
myinit();
mybuf.chars_left = LOGSIZE - strlen(buffer)-1;
mybuf.buffer = buffer;
cs_operate(load_address, &mybuf, aStream);
args.callback = aCallback;
args.skipFrames = aSkipFrames; /* XXX Not handled! */
args.closure = aClosure;
cs_operate(load_address, &args);
return NS_OK;
}
#endif