github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/debug/gosym/pclntab.go (about) 1 // Copyright 2009 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. 4 5 /* 6 * Line tables 7 */ 8 9 package gosym 10 11 import ( 12 "encoding/binary" 13 "sync" 14 ) 15 16 // A LineTable is a data structure mapping program counters to line numbers. 17 // 18 // In Go 1.1 and earlier, each function (represented by a Func) had its own LineTable, 19 // and the line number corresponded to a numbering of all source lines in the 20 // program, across all files. That absolute line number would then have to be 21 // converted separately to a file name and line number within the file. 22 // 23 // In Go 1.2, the format of the data changed so that there is a single LineTable 24 // for the entire program, shared by all Funcs, and there are no absolute line 25 // numbers, just line numbers within specific files. 26 // 27 // For the most part, LineTable's methods should be treated as an internal 28 // detail of the package; callers should use the methods on Table instead. 29 type LineTable struct { 30 Data []byte 31 PC uint64 32 Line int 33 34 // Go 1.2 state 35 mu sync.Mutex 36 go12 int // is this in Go 1.2 format? -1 no, 0 unknown, 1 yes 37 binary binary.ByteOrder 38 quantum uint32 39 ptrsize uint32 40 functab []byte 41 nfunctab uint32 42 filetab []byte 43 nfiletab uint32 44 fileMap map[string]uint32 45 } 46 47 // NOTE(rsc): This is wrong for GOARCH=arm, which uses a quantum of 4, 48 // but we have no idea whether we're using arm or not. This only 49 // matters in the old (pre-Go 1.2) symbol table format, so it's not worth 50 // fixing. 51 const oldQuantum = 1 52 53 func (t *LineTable) parse(targetPC uint64, targetLine int) (b []byte, pc uint64, line int) { 54 // The PC/line table can be thought of as a sequence of 55 // <pc update>* <line update> 56 // batches. Each update batch results in a (pc, line) pair, 57 // where line applies to every PC from pc up to but not 58 // including the pc of the next pair. 59 // 60 // Here we process each update individually, which simplifies 61 // the code, but makes the corner cases more confusing. 62 b, pc, line = t.Data, t.PC, t.Line 63 for pc <= targetPC && line != targetLine && len(b) > 0 { 64 code := b[0] 65 b = b[1:] 66 switch { 67 case code == 0: 68 if len(b) < 4 { 69 b = b[0:0] 70 break 71 } 72 val := binary.BigEndian.Uint32(b) 73 b = b[4:] 74 line += int(val) 75 case code <= 64: 76 line += int(code) 77 case code <= 128: 78 line -= int(code - 64) 79 default: 80 pc += oldQuantum * uint64(code-128) 81 continue 82 } 83 pc += oldQuantum 84 } 85 return b, pc, line 86 } 87 88 func (t *LineTable) slice(pc uint64) *LineTable { 89 data, pc, line := t.parse(pc, -1) 90 return &LineTable{Data: data, PC: pc, Line: line} 91 } 92 93 // PCToLine returns the line number for the given program counter. 94 // Callers should use Table's PCToLine method instead. 95 func (t *LineTable) PCToLine(pc uint64) int { 96 if t.isGo12() { 97 return t.go12PCToLine(pc) 98 } 99 _, _, line := t.parse(pc, -1) 100 return line 101 } 102 103 // LineToPC returns the program counter for the given line number, 104 // considering only program counters before maxpc. 105 // Callers should use Table's LineToPC method instead. 106 func (t *LineTable) LineToPC(line int, maxpc uint64) uint64 { 107 if t.isGo12() { 108 return 0 109 } 110 _, pc, line1 := t.parse(maxpc, line) 111 if line1 != line { 112 return 0 113 } 114 // Subtract quantum from PC to account for post-line increment 115 return pc - oldQuantum 116 } 117 118 // NewLineTable returns a new PC/line table 119 // corresponding to the encoded data. 120 // Text must be the start address of the 121 // corresponding text segment. 122 func NewLineTable(data []byte, text uint64) *LineTable { 123 return &LineTable{Data: data, PC: text, Line: 0} 124 } 125 126 // Go 1.2 symbol table format. 127 // See golang.org/s/go12symtab. 128 // 129 // A general note about the methods here: rather than try to avoid 130 // index out of bounds errors, we trust Go to detect them, and then 131 // we recover from the panics and treat them as indicative of a malformed 132 // or incomplete table. 133 // 134 // The methods called by symtab.go, which begin with "go12" prefixes, 135 // are expected to have that recovery logic. 136 137 // isGo12 reports whether this is a Go 1.2 (or later) symbol table. 138 func (t *LineTable) isGo12() bool { 139 t.go12Init() 140 return t.go12 == 1 141 } 142 143 const go12magic = 0xfffffffb 144 145 // uintptr returns the pointer-sized value encoded at b. 146 // The pointer size is dictated by the table being read. 147 func (t *LineTable) uintptr(b []byte) uint64 { 148 if t.ptrsize == 4 { 149 return uint64(t.binary.Uint32(b)) 150 } 151 return t.binary.Uint64(b) 152 } 153 154 // go12init initializes the Go 1.2 metadata if t is a Go 1.2 symbol table. 155 func (t *LineTable) go12Init() { 156 t.mu.Lock() 157 defer t.mu.Unlock() 158 if t.go12 != 0 { 159 return 160 } 161 162 defer func() { 163 // If we panic parsing, assume it's not a Go 1.2 symbol table. 164 recover() 165 }() 166 167 // Check header: 4-byte magic, two zeros, pc quantum, pointer size. 168 t.go12 = -1 // not Go 1.2 until proven otherwise 169 if len(t.Data) < 16 || t.Data[4] != 0 || t.Data[5] != 0 || 170 (t.Data[6] != 1 && t.Data[6] != 4) || // pc quantum 171 (t.Data[7] != 4 && t.Data[7] != 8) { // pointer size 172 return 173 } 174 175 switch uint32(go12magic) { 176 case binary.LittleEndian.Uint32(t.Data): 177 t.binary = binary.LittleEndian 178 case binary.BigEndian.Uint32(t.Data): 179 t.binary = binary.BigEndian 180 default: 181 return 182 } 183 184 t.quantum = uint32(t.Data[6]) 185 t.ptrsize = uint32(t.Data[7]) 186 187 t.nfunctab = uint32(t.uintptr(t.Data[8:])) 188 t.functab = t.Data[8+t.ptrsize:] 189 functabsize := t.nfunctab*2*t.ptrsize + t.ptrsize 190 fileoff := t.binary.Uint32(t.functab[functabsize:]) 191 t.functab = t.functab[:functabsize] 192 t.filetab = t.Data[fileoff:] 193 t.nfiletab = t.binary.Uint32(t.filetab) 194 t.filetab = t.filetab[:t.nfiletab*4] 195 196 t.go12 = 1 // so far so good 197 } 198 199 // findFunc returns the func corresponding to the given program counter. 200 func (t *LineTable) findFunc(pc uint64) []byte { 201 if pc < t.uintptr(t.functab) || pc >= t.uintptr(t.functab[len(t.functab)-int(t.ptrsize):]) { 202 return nil 203 } 204 205 // The function table is a list of 2*nfunctab+1 uintptrs, 206 // alternating program counters and offsets to func structures. 207 f := t.functab 208 nf := t.nfunctab 209 for nf > 0 { 210 m := nf / 2 211 fm := f[2*t.ptrsize*m:] 212 if t.uintptr(fm) <= pc && pc < t.uintptr(fm[2*t.ptrsize:]) { 213 return t.Data[t.uintptr(fm[t.ptrsize:]):] 214 } else if pc < t.uintptr(fm) { 215 nf = m 216 } else { 217 f = f[(m+1)*2*t.ptrsize:] 218 nf -= m + 1 219 } 220 } 221 return nil 222 } 223 224 // readvarint reads, removes, and returns a varint from *pp. 225 func (t *LineTable) readvarint(pp *[]byte) uint32 { 226 var v, shift uint32 227 p := *pp 228 for shift = 0; ; shift += 7 { 229 b := p[0] 230 p = p[1:] 231 v |= (uint32(b) & 0x7F) << shift 232 if b&0x80 == 0 { 233 break 234 } 235 } 236 *pp = p 237 return v 238 } 239 240 // string returns a Go string found at off. 241 func (t *LineTable) string(off uint32) string { 242 for i := off; ; i++ { 243 if t.Data[i] == 0 { 244 return string(t.Data[off:i]) 245 } 246 } 247 } 248 249 // step advances to the next pc, value pair in the encoded table. 250 func (t *LineTable) step(p *[]byte, pc *uint64, val *int32, first bool) bool { 251 uvdelta := t.readvarint(p) 252 if uvdelta == 0 && !first { 253 return false 254 } 255 if uvdelta&1 != 0 { 256 uvdelta = ^(uvdelta >> 1) 257 } else { 258 uvdelta >>= 1 259 } 260 vdelta := int32(uvdelta) 261 pcdelta := t.readvarint(p) * t.quantum 262 *pc += uint64(pcdelta) 263 *val += vdelta 264 return true 265 } 266 267 // pcvalue reports the value associated with the target pc. 268 // off is the offset to the beginning of the pc-value table, 269 // and entry is the start PC for the corresponding function. 270 func (t *LineTable) pcvalue(off uint32, entry, targetpc uint64) int32 { 271 if off == 0 { 272 return -1 273 } 274 p := t.Data[off:] 275 276 val := int32(-1) 277 pc := entry 278 for t.step(&p, &pc, &val, pc == entry) { 279 if targetpc < pc { 280 return val 281 } 282 } 283 return -1 284 } 285 286 // findFileLine scans one function in the binary looking for a 287 // program counter in the given file on the given line. 288 // It does so by running the pc-value tables mapping program counter 289 // to file number. Since most functions come from a single file, these 290 // are usually short and quick to scan. If a file match is found, then the 291 // code goes to the expense of looking for a simultaneous line number match. 292 func (t *LineTable) findFileLine(entry uint64, filetab, linetab uint32, filenum, line int32) uint64 { 293 if filetab == 0 || linetab == 0 { 294 return 0 295 } 296 297 fp := t.Data[filetab:] 298 fl := t.Data[linetab:] 299 fileVal := int32(-1) 300 filePC := entry 301 lineVal := int32(-1) 302 linePC := entry 303 fileStartPC := filePC 304 for t.step(&fp, &filePC, &fileVal, filePC == entry) { 305 if fileVal == filenum && fileStartPC < filePC { 306 // fileVal is in effect starting at fileStartPC up to 307 // but not including filePC, and it's the file we want. 308 // Run the PC table looking for a matching line number 309 // or until we reach filePC. 310 lineStartPC := linePC 311 for linePC < filePC && t.step(&fl, &linePC, &lineVal, linePC == entry) { 312 // lineVal is in effect until linePC, and lineStartPC < filePC. 313 if lineVal == line { 314 if fileStartPC <= lineStartPC { 315 return lineStartPC 316 } 317 if fileStartPC < linePC { 318 return fileStartPC 319 } 320 } 321 lineStartPC = linePC 322 } 323 } 324 fileStartPC = filePC 325 } 326 return 0 327 } 328 329 // go12PCToLine maps program counter to line number for the Go 1.2 pcln table. 330 func (t *LineTable) go12PCToLine(pc uint64) (line int) { 331 defer func() { 332 if recover() != nil { 333 line = -1 334 } 335 }() 336 337 f := t.findFunc(pc) 338 if f == nil { 339 return -1 340 } 341 entry := t.uintptr(f) 342 linetab := t.binary.Uint32(f[t.ptrsize+5*4:]) 343 return int(t.pcvalue(linetab, entry, pc)) 344 } 345 346 // go12PCToFile maps program counter to file name for the Go 1.2 pcln table. 347 func (t *LineTable) go12PCToFile(pc uint64) (file string) { 348 defer func() { 349 if recover() != nil { 350 file = "" 351 } 352 }() 353 354 f := t.findFunc(pc) 355 if f == nil { 356 return "" 357 } 358 entry := t.uintptr(f) 359 filetab := t.binary.Uint32(f[t.ptrsize+4*4:]) 360 fno := t.pcvalue(filetab, entry, pc) 361 if fno <= 0 { 362 return "" 363 } 364 return t.string(t.binary.Uint32(t.filetab[4*fno:])) 365 } 366 367 // go12LineToPC maps a (file, line) pair to a program counter for the Go 1.2 pcln table. 368 func (t *LineTable) go12LineToPC(file string, line int) (pc uint64) { 369 defer func() { 370 if recover() != nil { 371 pc = 0 372 } 373 }() 374 375 t.initFileMap() 376 filenum := t.fileMap[file] 377 if filenum == 0 { 378 return 0 379 } 380 381 // Scan all functions. 382 // If this turns out to be a bottleneck, we could build a map[int32][]int32 383 // mapping file number to a list of functions with code from that file. 384 for i := uint32(0); i < t.nfunctab; i++ { 385 f := t.Data[t.uintptr(t.functab[2*t.ptrsize*i+t.ptrsize:]):] 386 entry := t.uintptr(f) 387 filetab := t.binary.Uint32(f[t.ptrsize+4*4:]) 388 linetab := t.binary.Uint32(f[t.ptrsize+5*4:]) 389 pc := t.findFileLine(entry, filetab, linetab, int32(filenum), int32(line)) 390 if pc != 0 { 391 return pc 392 } 393 } 394 return 0 395 } 396 397 // initFileMap initializes the map from file name to file number. 398 func (t *LineTable) initFileMap() { 399 t.mu.Lock() 400 defer t.mu.Unlock() 401 402 if t.fileMap != nil { 403 return 404 } 405 m := make(map[string]uint32) 406 407 for i := uint32(1); i < t.nfiletab; i++ { 408 s := t.string(t.binary.Uint32(t.filetab[4*i:])) 409 m[s] = i 410 } 411 t.fileMap = m 412 } 413 414 // go12MapFiles adds to m a key for every file in the Go 1.2 LineTable. 415 // Every key maps to obj. That's not a very interesting map, but it provides 416 // a way for callers to obtain the list of files in the program. 417 func (t *LineTable) go12MapFiles(m map[string]*Obj, obj *Obj) { 418 defer func() { 419 recover() 420 }() 421 422 t.initFileMap() 423 for file := range t.fileMap { 424 m[file] = obj 425 } 426 }