| 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- | |
| 2 | * | |
| 3 | * ***** BEGIN LICENSE BLOCK ***** | |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 5 | * | |
| 6 | * The contents of this file are subject to the Mozilla Public License Version | |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with | |
| 8 | * the License. You may obtain a copy of the License at | |
| 9 | * http://www.mozilla.org/MPL/ | |
| 10 | * | |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, | |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| 13 | * for the specific language governing rights and limitations under the | |
| 14 | * License. | |
| 15 | * | |
| 16 | * The Original Code is Mozilla Communicator client code, released | |
| 17 | * March 31, 1998. | |
| 18 | * | |
| 19 | * The Initial Developer of the Original Code is | |
| 20 | * Netscape Communications Corporation. | |
| 21 | * Portions created by the Initial Developer are Copyright (C) 1998 | |
| 22 | * the Initial Developer. All Rights Reserved. | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * IBM Corp. | |
| 26 | * | |
| 27 | * Alternatively, the contents of this file may be used under the terms of | |
| 28 | * either of the GNU General Public License Version 2 or later (the "GPL"), | |
| 29 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
| 30 | * in which case the provisions of the GPL or the LGPL are applicable instead | |
| 31 | * of those above. If you wish to allow use of your version of this file only | |
| 32 | * under the terms of either the GPL or the LGPL, and not to allow others to | |
| 33 | * use your version of this file under the terms of the MPL, indicate your | |
| 34 | * decision by deleting the provisions above and replace them with the notice | |
| 35 | * and other provisions required by the GPL or the LGPL. If you do not delete | |
| 36 | * the provisions above, a recipient may use your version of this file under | |
| 37 | * the terms of any one of the MPL, the GPL or the LGPL. | |
| 38 | * | |
| 39 | * ***** END LICENSE BLOCK ***** */ | |
| 40 | ||
| 41 | /* | |
| 42 | * PR assertion checker. | |
| 43 | */ | |
| 44 | #include "jsstddef.h" | |
| 45 | #include <stdio.h> | |
| 46 | #include <stdlib.h> | |
| 47 | #include "jstypes.h" | |
| 48 | #include "jsutil.h" | |
| 49 | ||
| 50 | #ifdef WIN32 | |
| 51 | # include <windows.h> | |
| 52 | #endif | |
| 53 | ||
| 54 | JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln) | |
| 55 | 0 | { |
| 56 | 0 | fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln); |
| 57 | #if defined(WIN32) | |
| 58 | DebugBreak(); | |
| 59 | exit(3); | |
| 60 | #endif | |
| 61 | #if defined(XP_OS2) | |
| 62 | asm("int $3"); | |
| 63 | #endif | |
| 64 | 0 | abort(); |
| 65 | } | |
| 66 | ||
| 67 | #if defined DEBUG_notme && defined XP_UNIX | |
| 68 | ||
| 69 | #define __USE_GNU 1 | |
| 70 | #include <dlfcn.h> | |
| 71 | #include <setjmp.h> | |
| 72 | #include <string.h> | |
| 73 | #include "jshash.h" | |
| 74 | #include "jsprf.h" | |
| 75 | ||
| 76 | JSCallsite js_calltree_root = {0, NULL, NULL, 0, NULL, NULL, NULL, NULL}; | |
| 77 | ||
| 78 | static JSCallsite * | |
| 79 | CallTree(uint32 *bp) | |
| 80 | { | |
| 81 | uint32 *bpup, *bpdown, pc; | |
| 82 | JSCallsite *parent, *site, **csp; | |
| 83 | Dl_info info; | |
| 84 | int ok, offset; | |
| 85 | const char *symbol; | |
| 86 | char *method; | |
| 87 | ||
| 88 | /* Reverse the stack frame list to avoid recursion. */ | |
| 89 | bpup = NULL; | |
| 90 | for (;;) { | |
| 91 | bpdown = (uint32*) bp[0]; | |
| 92 | bp[0] = (uint32) bpup; | |
| 93 | if ((uint32*) bpdown[0] < bpdown) | |
| 94 | break; | |
| 95 | bpup = bp; | |
| 96 | bp = bpdown; | |
| 97 | } | |
| 98 | ||
| 99 | /* Reverse the stack again, finding and building a path in the tree. */ | |
| 100 | parent = &js_calltree_root; | |
| 101 | do { | |
| 102 | bpup = (uint32*) bp[0]; | |
| 103 | bp[0] = (uint32) bpdown; | |
| 104 | pc = bp[1]; | |
| 105 | ||
| 106 | csp = &parent->kids; | |
| 107 | while ((site = *csp) != NULL) { | |
| 108 | if (site->pc == pc) { | |
| 109 | /* Put the most recently used site at the front of siblings. */ | |
| 110 | *csp = site->siblings; | |
| 111 | site->siblings = parent->kids; | |
| 112 | parent->kids = site; | |
| 113 | ||
| 114 | /* Site already built -- go up the stack. */ | |
| 115 | goto upward; | |
| 116 | } | |
| 117 | csp = &site->siblings; | |
| 118 | } | |
| 119 | ||
| 120 | /* Check for recursion: see if pc is on our ancestor line. */ | |
| 121 | for (site = parent; site; site = site->parent) { | |
| 122 | if (site->pc == pc) | |
| 123 | goto upward; | |
| 124 | } | |
| 125 | ||
| 126 | /* | |
| 127 | * Not in tree at all: let's find our symbolic callsite info. | |
| 128 | * XXX static syms are masked by nearest lower global | |
| 129 | */ | |
| 130 | info.dli_fname = info.dli_sname = NULL; | |
| 131 | ok = dladdr((void*) pc, &info); | |
| 132 | if (ok < 0) { | |
| 133 | fprintf(stderr, "dladdr failed!\n"); | |
| 134 | return NULL; | |
| 135 | } | |
| 136 | ||
| 137 | /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */ | |
| 138 | symbol = info.dli_sname; | |
| 139 | offset = (char*)pc - (char*)info.dli_fbase; | |
| 140 | method = symbol | |
| 141 | ? strdup(symbol) | |
| 142 | : JS_smprintf("%s+%X", | |
| 143 | info.dli_fname ? info.dli_fname : "main", | |
| 144 | offset); | |
| 145 | if (!method) | |
| 146 | return NULL; | |
| 147 | ||
| 148 | /* Create a new callsite record. */ | |
| 149 | site = (JSCallsite *) malloc(sizeof(JSCallsite)); | |
| 150 | if (!site) | |
| 151 | return NULL; | |
| 152 | ||
| 153 | /* Insert the new site into the tree. */ | |
| 154 | site->pc = pc; | |
| 155 | site->name = method; | |
| 156 | site->library = info.dli_fname; | |
| 157 | site->offset = offset; | |
| 158 | site->parent = parent; | |
| 159 | site->siblings = parent->kids; | |
| 160 | parent->kids = site; | |
| 161 | site->kids = NULL; | |
| 162 | ||
| 163 | upward: | |
| 164 | parent = site; | |
| 165 | bpdown = bp; | |
| 166 | bp = bpup; | |
| 167 | } while (bp); | |
| 168 | ||
| 169 | return site; | |
| 170 | } | |
| 171 | ||
| 172 | JSCallsite * | |
| 173 | JS_Backtrace(int skip) | |
| 174 | { | |
| 175 | jmp_buf jb; | |
| 176 | uint32 *bp, *bpdown; | |
| 177 | ||
| 178 | setjmp(jb); | |
| 179 | ||
| 180 | /* Stack walking code adapted from Kipp's "leaky". */ | |
| 181 | bp = (uint32*) jb[0].__jmpbuf[JB_BP]; | |
| 182 | while (--skip >= 0) { | |
| 183 | bpdown = (uint32*) *bp++; | |
| 184 | if (bpdown < bp) | |
| 185 | break; | |
| 186 | bp = bpdown; | |
| 187 | } | |
| 188 | ||
| 189 | return CallTree(bp); | |
| 190 | } | |
| 191 |