* const-uniq-1.c: Expand regex to match AIX XCOFF labels.
[official-gcc.git] / libgo / runtime / go-callers.c
blob1dd3e71c8efda563c7b434f5705cb69a4e0d9909
1 /* go-callers.c -- get callers for Go.
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. */
7 #include "config.h"
9 #include "backtrace.h"
11 #include "runtime.h"
12 #include "array.h"
14 /* Argument passed to callback function. */
16 struct callers_data
18 uintptr *pcbuf;
19 int index;
20 int max;
23 /* Callback function for backtrace_simple. Just collect the PC
24 values. Return zero to continue, non-zero to stop. */
26 static int
27 callback (void *data, uintptr_t pc)
29 struct callers_data *arg = (struct callers_data *) data;
31 arg->pcbuf[arg->index] = pc;
32 ++arg->index;
33 return arg->index >= arg->max;
36 /* Error callback. */
38 static void
39 error_callback (void *data __attribute__ ((unused)),
40 const char *msg, int errnum)
42 if (errnum != 0)
43 runtime_printf ("%s errno %d\n", msg, errnum);
44 runtime_throw (msg);
47 /* Gather caller PC's. */
49 int32
50 runtime_callers (int32 skip, uintptr *pcbuf, int32 m)
52 struct callers_data data;
54 data.pcbuf = pcbuf;
55 data.index = 0;
56 data.max = m;
57 backtrace_simple (__go_get_backtrace_state (), skip + 1, callback,
58 error_callback, &data);
59 return data.index;
62 int Callers (int, struct __go_open_array)
63 __asm__ ("runtime.Callers");
65 int
66 Callers (int skip, struct __go_open_array pc)
68 /* In the Go 1 release runtime.Callers has an off-by-one error,
69 which we can not correct because it would break backward
70 compatibility. Adjust SKIP here to be compatible. */
71 return runtime_callers (skip - 1, (uintptr *) pc.__values, pc.__count);