| 369 | } |
| 370 | |
| 371 | func (this *MapsHelper) GetOffset(pid uint32, addr uint64) (info string) { |
| 372 | maps_lock.Lock() |
| 373 | defer maps_lock.Unlock() |
| 374 | pid_maps, ok := this.pid_maps[pid] |
| 375 | if !ok { |
| 376 | // 一般不会进入这个分支 |
| 377 | err := this.ParseMaps(pid, false) |
| 378 | if err != nil { |
| 379 | return fmt.Sprintf("UNNKOWN + 0x%x", addr) |
| 380 | } |
| 381 | pid_maps, ok = this.pid_maps[pid] |
| 382 | if !ok { |
| 383 | return fmt.Sprintf("UNNKOWN + 0x%x", addr) |
| 384 | } |
| 385 | } |
| 386 | // 这里的计算是以库的每个段都是前后连续为前提的,暂时就这样 |
| 387 | // 但是实际上确实存在一些奇怪的操作 |
| 388 | // 1. 不连续的段 |
| 389 | // 2. 两个或者多个同名/同路径的库存在于maps中 |
| 390 | // 3. 其他... |
| 391 | // 全部遍历是一种低效的写法,但是暂时没有更好的想法,就这样 |
| 392 | // 一定要优化那么应该在每次 pid_maps 变更的时候就进行排序 并按照基址大小插入 |
| 393 | var off_list []string = []string{} |
| 394 | for _, lib_infos := range *pid_maps { |
| 395 | for _, lib_info := range lib_infos { |
| 396 | if addr >= lib_info.BaseAddr && addr < lib_info.EndAddr { |
| 397 | offset := lib_info.Off + (addr - lib_info.BaseAddr) |
| 398 | off_info := fmt.Sprintf("%s + 0x%x", lib_info.LibName, offset) |
| 399 | if !slices.Contains(off_list, off_info) { |
| 400 | off_list = append(off_list, off_info) |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | if len(off_list) == 0 { |
| 406 | return fmt.Sprintf("NOTFOUND + 0x%x", addr) |
| 407 | } |
| 408 | return strings.Join(off_list[:], ",") |
| 409 | } |
| 410 | |
| 411 | var maps_helper = NewMapsHelper() |
| 412 | var maps_lock sync.Mutex |