2016-08-05 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgo / go / runtime / symtab.go
blob7b76f11f3a687faae5a1c21e2814267de8687bbb
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package runtime
7 // Frames may be used to get function/file/line information for a
8 // slice of PC values returned by Callers.
9 type Frames struct {
10 callers []uintptr
12 // The last PC we saw.
13 last uintptr
15 // The number of times we've seen last.
16 lastCount int
19 // Frame is the information returned by Frames for each call frame.
20 type Frame struct {
21 // Program counter for this frame; multiple frames may have
22 // the same PC value.
23 PC uintptr
25 // Func for this frame; may be nil for non-Go code or fully
26 // inlined functions.
27 Func *Func
29 // Function name, file name, and line number for this call frame.
30 // May be the empty string or zero if not known.
31 // If Func is not nil then Function == Func.Name().
32 Function string
33 File string
34 Line int
36 // Entry point for the function; may be zero if not known.
37 // If Func is not nil then Entry == Func.Entry().
38 Entry uintptr
41 // CallersFrames takes a slice of PC values returned by Callers and
42 // prepares to return function/file/line information.
43 // Do not change the slice until you are done with the Frames.
44 func CallersFrames(callers []uintptr) *Frames {
45 return &Frames{callers: callers}
48 // Next returns frame information for the next caller.
49 // If more is false, there are no more callers (the Frame value is valid).
50 func (ci *Frames) Next() (frame Frame, more bool) {
51 if len(ci.callers) == 0 {
52 return Frame{}, false
55 pc := ci.callers[0]
56 ci.callers = ci.callers[1:]
58 i := 0
59 if pc == ci.last {
60 ci.lastCount++
61 i = ci.lastCount
62 } else {
63 ci.last = pc
64 ci.lastCount = 0
66 more = len(ci.callers) > 0
68 f, file, line := funcframe(pc, i)
69 if f == nil {
70 return Frame{}, more
73 entry := f.Entry()
74 xpc := pc
75 if xpc > entry {
76 xpc--
79 function := f.Name()
81 frame = Frame{
82 PC: xpc,
83 Func: f,
84 Function: function,
85 File: file,
86 Line: line,
87 Entry: entry,
90 return frame, more
93 // NOTE: Func does not expose the actual unexported fields, because we return *Func
94 // values to users, and we want to keep them from being able to overwrite the data
95 // with (say) *f = Func{}.
96 // All code operating on a *Func must call raw to get the *_func instead.
98 // A Func represents a Go function in the running binary.
99 type Func struct {
100 opaque struct{} // unexported field to disallow conversions
103 // FuncForPC returns a *Func describing the function that contains the
104 // given program counter address, or else nil.
105 func FuncForPC(pc uintptr) *Func
107 // Name returns the name of the function.
108 func (f *Func) Name() string {
109 return funcname_go(f)
112 // Entry returns the entry address of the function.
113 func (f *Func) Entry() uintptr {
114 return funcentry_go(f)
117 // FileLine returns the file name and line number of the
118 // source code corresponding to the program counter pc.
119 // The result will not be accurate if pc is not a program
120 // counter within f.
121 func (f *Func) FileLine(pc uintptr) (file string, line int) {
122 return funcline_go(f, pc)
125 // implemented in symtab.c
126 func funcline_go(*Func, uintptr) (string, int)
127 func funcname_go(*Func) string
128 func funcentry_go(*Func) uintptr
129 func funcframe(uintptr, int) (*Func, string, int)