1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
39 * ***** END LICENSE BLOCK ***** */
42 * PR assertion checker.
59 * Checks the assumption that JS_FUNC_TO_DATA_PTR and JS_DATA_TO_FUNC_PTR
60 * macros uses to implement casts between function and data pointers.
62 JS_STATIC_ASSERT(sizeof(void *) == sizeof(void (*)()));
64 JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln)
66 fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln);
70 * We used to call DebugBreak() on Windows, but amazingly, it causes
71 * the MSVS 2010 debugger not to be able to recover a call stack.
75 #elif defined(__APPLE__)
77 * On Mac OS X, Breakpad ignores signals. Only real Mach exceptions are
80 *((int *) NULL) = 0; /* To continue from here in GDB: "return" then "continue". */
81 raise(SIGABRT); /* In case above statement gets nixed by the optimizer. */
83 raise(SIGABRT); /* To continue from here in GDB: "signal 0". */
95 * Histogram bins count occurrences of values <= the bin label, as follows:
97 * linear: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or more
98 * 2**x: 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 or more
99 * 10**x: 0, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
101 * We wish to count occurrences of 0 and 1 values separately, always.
104 BinToVal(uintN logscale, uintN bin)
106 JS_ASSERT(bin <= 10);
107 if (bin <= 1 || logscale == 0)
112 JS_ASSERT(logscale == 10);
113 return (uint32) pow(10.0, (double) bin);
117 ValToBin(uintN logscale, uint32 val)
123 bin = (logscale == 10)
124 ? (uintN) ceil(log10((double) val))
126 ? (uintN) JS_CeilingLog2(val)
128 return JS_MIN(bin, 10);
132 JS_BasicStatsAccum(JSBasicStats *bs, uint32 val)
134 uintN oldscale, newscale, bin;
141 bs->sqsum += (double)val * val;
143 oldscale = bs->logscale;
144 if (oldscale != 10) {
145 mean = bs->sum / bs->num;
146 if (bs->max > 16 && mean > 8) {
147 newscale = (bs->max > 1e6 && mean > 1000) ? 10 : 2;
148 if (newscale != oldscale) {
149 uint32 newhist[11], newbin;
151 PodArrayZero(newhist);
152 for (bin = 0; bin <= 10; bin++) {
153 newbin = ValToBin(newscale, BinToVal(oldscale, bin));
154 newhist[newbin] += bs->hist[bin];
156 memcpy(bs->hist, newhist, sizeof bs->hist);
157 bs->logscale = newscale;
162 bin = ValToBin(bs->logscale, val);
167 JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma)
171 if (num == 0 || sum == 0) {
176 var = num * sqsum - sum * sum;
177 if (var < 0 || num == 1)
180 var /= (double)num * (num - 1);
182 /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
183 *sigma = (var != 0) ? sqrt(var) : 0;
188 JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp)
192 mean = JS_MeanAndStdDevBS(bs, &sigma);
193 fprintf(fp, "\nmean %s %g, std. deviation %g, max %lu\n",
194 title, mean, sigma, (unsigned long) bs->max);
195 JS_DumpHistogram(bs, fp);
199 JS_DumpHistogram(JSBasicStats *bs, FILE *fp)
205 for (bin = 0, max = 0, sum = 0; bin <= 10; bin++) {
212 for (bin = 0; bin <= 10; bin++) {
213 uintN val = BinToVal(bs->logscale, bin);
214 uintN end = (bin == 10) ? 0 : BinToVal(bs->logscale, bin + 1);
217 fprintf(fp, " [%6u]", val);
219 fprintf(fp, "[%6u, %6u]", val, end - 1);
221 fprintf(fp, "[%6u, +inf]", val);
222 fprintf(fp, ": %8u ", cnt);
224 if (max > 1e6 && mean > 1e3)
225 cnt = (uint32) ceil(log10((double) cnt));
226 else if (max > 16 && mean > 8)
227 cnt = JS_CeilingLog2(cnt);
228 for (uintN i = 0; i < cnt; i++)
235 #endif /* JS_BASIC_STATS */
237 #if defined(DEBUG_notme) && defined(XP_UNIX)
245 JSCallsite js_calltree_root = {0, NULL, NULL, 0, NULL, NULL, NULL, NULL};
250 void **bpup, **bpdown, *pc;
251 JSCallsite *parent, *site, **csp;
257 /* Reverse the stack frame list to avoid recursion. */
260 bpdown = (void**) bp[0];
261 bp[0] = (void*) bpup;
262 if ((void**) bpdown[0] < bpdown)
268 /* Reverse the stack again, finding and building a path in the tree. */
269 parent = &js_calltree_root;
271 bpup = (void**) bp[0];
272 bp[0] = (void*) bpdown;
276 while ((site = *csp) != NULL) {
277 if (site->pc == (uint32)pc) {
278 /* Put the most recently used site at the front of siblings. */
279 *csp = site->siblings;
280 site->siblings = parent->kids;
283 /* Site already built -- go up the stack. */
286 csp = &site->siblings;
289 /* Check for recursion: see if pc is on our ancestor line. */
290 for (site = parent; site; site = site->parent) {
291 if (site->pc == (uint32)pc)
296 * Not in tree at all: let's find our symbolic callsite info.
297 * XXX static syms are masked by nearest lower global
299 info.dli_fname = info.dli_sname = NULL;
300 ok = dladdr(pc, &info);
302 fprintf(stderr, "dladdr failed!\n");
306 /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */
307 symbol = info.dli_sname;
308 offset = (char*)pc - (char*)info.dli_fbase;
311 : JS_smprintf("%s+%X",
312 info.dli_fname ? info.dli_fname : "main",
317 /* Create a new callsite record. */
318 site = (JSCallsite *) js_malloc(sizeof(JSCallsite));
322 /* Insert the new site into the tree. */
323 site->pc = (uint32)pc;
325 site->library = info.dli_fname;
326 site->offset = offset;
327 site->parent = parent;
328 site->siblings = parent->kids;
341 JS_FRIEND_API(JSCallsite *)
342 JS_Backtrace(int skip)
346 /* Stack walking code adapted from Kipp's "leaky". */
348 __asm__( "movl %%ebp, %0" : "=g"(bp));
349 #elif defined(__x86_64__)
350 __asm__( "movq %%rbp, %0" : "=g"(bp));
353 * It would be nice if this worked uniformly, but at least on i386 and
354 * x86_64, it stopped working with gcc 4.1, because it points to the
355 * end of the saved registers instead of the start.
357 bp = (void**) __builtin_frame_address(0);
359 while (--skip >= 0) {
360 bpdown = (void**) *bp++;
370 JS_DumpBacktrace(JSCallsite *trace)
373 fprintf(stdout, "%s [%s +0x%X]\n", trace->name, trace->library,
375 trace = trace->parent;
379 #endif /* defined(DEBUG_notme) && defined(XP_UNIX) */