Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Export a stack walking API (usable from C or C++) from XPCOM. b=37468…
Browse files Browse the repository at this point in the history
…9 r=bsmedberg a=bzbarsky
  • Loading branch information
dbaron committed Aug 10, 2007
1 parent 6bf9895 commit a0a7b49
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 144 deletions.
15 changes: 1 addition & 14 deletions toolkit/xre/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,6 @@ CMMSRCS = MacApplicationDelegate.mm
endif

ifneq (,$(filter-out OS2 WINNT,$(OS_ARCH)))
STACKWALK_SRC_LCSRCS = \
nsStackFrameUnix.cpp \
nsStackFrameUnix.h \
$(NULL)

STACKWALK_CPPSRCS := $(addprefix $(topsrcdir)/xpcom/base/, $(STACKWALK_SRC_LCSRCS))
ifndef MOZ_ENABLE_LIBXUL
CPPSRCS += nsStackFrameUnix.cpp
endif
SHAREDCPPSRCS += nsSigHandlers.cpp
endif

Expand All @@ -181,10 +172,6 @@ ifeq ($(OS_ARCH),WINNT)
GARBAGE += $(addprefix $(srcdir)/,$(SHAREDCPPSRCS))
endif

ifneq (,$(filter-out OS2 WINNT,$(OS_ARCH)))
GARBAGE += $(STACKWALK_SRC_LCSRCS)
endif

SHARED_LIBRARY_LIBS += ../profile/src/$(LIB_PREFIX)profile_s.$(LIB_SUFFIX)

ifdef MOZ_ENABLE_XREMOTE
Expand Down Expand Up @@ -254,7 +241,7 @@ ifdef WRAP_SYSTEM_INCLUDES
DEFINES += -DWRAP_SYSTEM_INCLUDES
endif

export:: $(addprefix $(topsrcdir)/xpfe/bootstrap/, $(SHAREDCPPSRCS)) $(STACKWALK_CPPSRCS)
export:: $(addprefix $(topsrcdir)/xpfe/bootstrap/, $(SHAREDCPPSRCS))
$(INSTALL) $^ .

platform.ini: FORCE
Expand Down
7 changes: 2 additions & 5 deletions xpcom/base/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CPPSRCS = \
nsUUIDGenerator.cpp \
nsSystemInfo.cpp \
nsCycleCollector.cpp \
nsStackWalk.cpp \
$(NULL)

ifdef GC_LEAK_DETECTOR
Expand All @@ -85,6 +86,7 @@ EXPORTS = \
nsIAllocator.h \
nsIID.h \
nsISupportsObsolete.h \
nsStackWalk.h \
nsTraceRefcntImpl.h \
nsWeakPtr.h \
nsInterfaceRequestorAgg.h \
Expand All @@ -95,11 +97,6 @@ ifdef MOZ_DEBUG
CSRCS += pure_api.c
EXPORTS += pure.h
endif
CPPSRCS += nsStackFrameWin.cpp
endif

ifneq ($(OS_ARCH),WINNT)
CPPSRCS += nsStackFrameUnix.cpp
endif

SDK_XPIDLSRCS = \
Expand Down
101 changes: 43 additions & 58 deletions xpcom/base/nsStackFrameUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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;
};


Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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;
}


Expand Down Expand Up @@ -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
Expand All @@ -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
Loading

0 comments on commit a0a7b49

Please sign in to comment.