runtime: runtime.Caller should succeed even without debug info.
[gcc.git] / libgo / runtime / go-callers.c
1 /* go-callers.c -- get callers for Go.
2
3 Copyright 2012 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 #include "config.h"
8
9 #include "backtrace.h"
10
11 #include "runtime.h"
12
13 /* Argument passed to callback function. */
14
15 struct callers_data
16 {
17 uintptr *pcbuf;
18 int index;
19 int max;
20 };
21
22 /* Callback function for backtrace_simple. Just collect the PC
23 values. Return zero to continue, non-zero to stop. */
24
25 static int
26 callback (void *data, uintptr_t pc)
27 {
28 struct callers_data *arg = (struct callers_data *) data;
29
30 arg->pcbuf[arg->index] = pc;
31 ++arg->index;
32 return arg->index >= arg->max;
33 }
34
35 /* Error callback. */
36
37 static void
38 error_callback (void *data __attribute__ ((unused)),
39 const char *msg, int errnum)
40 {
41 if (errnum != 0)
42 runtime_printf ("%s errno %d\n", msg, errnum);
43 runtime_throw (msg);
44 }
45
46 /* Gather caller PC's. */
47
48 int32
49 runtime_callers (int32 skip, uintptr *pcbuf, int32 m)
50 {
51 struct callers_data data;
52
53 data.pcbuf = pcbuf;
54 data.index = 0;
55 data.max = m;
56 backtrace_simple (__go_get_backtrace_state (), skip + 1, callback,
57 error_callback, &data);
58 return data.index;
59 }
60
61 int Callers (int, struct __go_open_array)
62 __asm__ ("runtime.Callers");
63
64 int
65 Callers (int skip, struct __go_open_array pc)
66 {
67 /* In the Go 1 release runtime.Callers has an off-by-one error,
68 which we can not correct because it would break backward
69 compatibility. Adjust SKIP here to be compatible. */
70 return runtime_callers (skip - 1, (uintptr *) pc.__values, pc.__count);
71 }