runtime: Copy runtime_printf from other Go library.
[gcc.git] / libgo / runtime / go-traceback.c
1 /* go-traceback.c -- stack backtrace 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 "unwind.h"
10
11 #include "runtime.h"
12 #include "go-string.h"
13
14 static _Unwind_Reason_Code
15 traceback (struct _Unwind_Context *context, void *varg)
16 {
17 int *parg = (int *) varg;
18 uintptr pc;
19 int ip_before_insn = 0;
20 struct __go_string fn;
21 struct __go_string file;
22 int line;
23
24 #ifdef HAVE_GETIPINFO
25 pc = _Unwind_GetIPInfo (context, &ip_before_insn);
26 #else
27 pc = _Unwind_GetIP (context);
28 #endif
29
30 if (*parg > 100)
31 return _URC_END_OF_STACK;
32 ++*parg;
33
34 /* FIXME: If PC is in the __morestack routine, we should ignore
35 it. */
36
37 /* Back up to the call instruction. */
38 if (!ip_before_insn)
39 --pc;
40
41 if (!__go_file_line (pc, &fn, &file, &line))
42 return _URC_END_OF_STACK;
43
44 if (runtime_showframe (fn.__data))
45 {
46 runtime_printf ("%s\n", fn.__data);
47 runtime_printf ("\t%s:%d\n", file.__data, line);
48 }
49
50 return _URC_NO_REASON;
51 }
52
53 /* Print a stack trace for the current goroutine. */
54
55 void
56 runtime_traceback ()
57 {
58 int c;
59
60 c = 0;
61 _Unwind_Backtrace (traceback, &c);
62 }