Merge branch 'upstream'
[nativeclient.git] / tools / libnacl / stacktrace.c
blobab93fd35619b66006f1f104cf84a024d4d3c5e17
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Used to provide stack traceback support in NaCl modules in the debugger.
35 * When the application is compiled with -finstrument functions, stack tracing
36 * is turned on. This module maintains a stack of function entry addresses.
37 * The state of the stack trace can be printed at any point by calling
38 * __nacl_dump_stack_trace.
41 #include <stdio.h>
43 #define STACK_MAX_DEPTH 1024
44 static void* stack[STACK_MAX_DEPTH];
46 static int top_loc = 0;
48 static void push(void* function_address) {
49 if (top_loc < STACK_MAX_DEPTH) {
50 stack[top_loc] = function_address;
53 * Although we have a static limit on the number of function addresses that
54 * may be pushed, we keep track of the depth even beyond that. Deeply
55 * recursive chains will look confused, and may make top_loc wrap around
56 * (to a negative number) in unusual cases, but we can improve this later.
58 ++top_loc;
61 static void pop() {
63 * It's possible the calls and returns won't match. Don't decrement past the
64 * bottom of the stack.
66 if (top_loc > 0) {
67 --top_loc;
71 void __cyg_profile_func_enter(void* this_fn, void* call_site) {
72 push(this_fn);
75 void __cyg_profile_func_exit(void* this_fn, void* call_site) {
76 pop();
79 void __nacl_dump_stack_trace() {
80 int i;
81 if (top_loc > STACK_MAX_DEPTH) {
82 printf("Overflow: %d calls were not recorded.\n",
83 top_loc - STACK_MAX_DEPTH);
85 for (i = top_loc - 1; i >= 0; i--) {
86 printf("%d: %p\n", top_loc - 1 - i, stack[i]);