Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug/gosym: report different line number than cmd/addr2line #56869

Open
zdyj3170101136 opened this issue Nov 21, 2022 · 2 comments
Open

debug/gosym: report different line number than cmd/addr2line #56869

zdyj3170101136 opened this issue Nov 21, 2022 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@zdyj3170101136
Copy link

What version of Go are you using (go version)?

$ go version
go1.18.8

What operating system and processor architecture are you using (go env)?

go env
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.8"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4192850700=/tmp/go-build -gno-record-gcc-switches"

What did you do?

get pprof:

curl pprof 127.0.0.1:8887/debug/pprof/profile?seconds=1

open it with pprof raw:

  793: 0x410ab7 M=1 runtime.nextFreeFast /usr/local/go1.18/go/src/runtime/malloc.go:850 s=0
             runtime.mallocgc /usr/local/go1.18/go/src/runtime/malloc.go:1083 s=0
Mappings
1: 0x400000/0x99c2000/0x0 /home/data/server/otel-collector/data/otelcol-contrib 0bfdbe13c5603e284aed96d5ad62f47eea0cb692 [FN]

use debug/gosym to get funcname and line:

func parse(file string, f *elf.File) (*elf.File, *gosym.Table) {
	pclndat, err := f.Section(".gopclntab").Data()
	if err != nil {
		f.Close()
		log.Fatalf("reading %s gopclntab: %v", file, err)
	}

	pcln := gosym.NewLineTable(pclndat, f.Section(".text").Addr)
	tab, err := gosym.NewTable(nil, pcln)
	if err != nil {
		f.Close()
		log.Fatalf("parsing %s gosymtab: %v", file, err)
	}

	return f, tab
}

What did you expect to see?

with same address, the debug/gosym should have same line number compared with go tool addr2line.

use go tool addr2line to get funcname and line:

[root@plat-sh-infra-prod-jaeger-collector003 ~]#  /go/bin/go tool addr2line /home/data/server/otel-collector/data/otelcol-contrib
410ab7
runtime.mallocgc
/usr/local/go1.18/go/src/runtime/malloc.go:850

What did you see instead?

the debug/gosym have different line number compared with go tool addr2line.

debug/gosym:

/usr/local/go1.18/go/src/runtime/malloc.go 1117

it's line number is 1117, not 850.

@bcmills bcmills added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 21, 2022
@cherrymui
Copy link
Member

The line numbers from the addr2line and pprof commands both look reasonable. Could you share your complete code about how you get the line number with the debug/gosym package? (The code snippet above only includes loading the table, not getting the line number.) Thanks,

@cherrymui cherrymui changed the title cmd/addr2line: addr2line have different line number compared with debug/gosym debug/gosym: report different line number than cmd/addr2line Nov 21, 2022
@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 21, 2022
@zdyj3170101136
Copy link
Author

zdyj3170101136 commented Nov 22, 2022

来自 addr2line 和 pprof 命令的行号看起来都很合理。您能否分享有关如何获取debug/gosym包行号的完整代码?(上面的代码片段只包括加载表格,而不是获取行号。)谢谢,

code:

package main

import (
	"debug/elf"
	"debug/gosym"
	"log"
	"os"
	"strconv"
)

func main() {
	if len(os.Args) >= 3 {
		_, tab := crack(os.Args[1])
		pc, _ := strconv.ParseUint(os.Args[2], 16, 64)
		log.Println(tab.PCToLine(pc))
	}
}

func crack(file string) (*elf.File, *gosym.Table) {
	// Open self
	f, err := elf.Open(file)
	if err != nil {
		log.Fatal(err)
	}
	return parse(file, f)
}

func parse(file string, f *elf.File) (*elf.File, *gosym.Table) {
	pclndat, err := f.Section(".gopclntab").Data()
	if err != nil {
		f.Close()
		log.Fatalf("reading %s gopclntab: %v", file, err)
	}

	pcln := gosym.NewLineTable(pclndat, f.Section(".text").Addr)
	tab, err := gosym.NewTable(nil, pcln)
	if err != nil {
		f.Close()
		log.Fatalf("parsing %s gosymtab: %v", file, err)
	}

	return f, tab
}

command:

[root@plat-sh-infra-prod-jaeger-collector003 ~]# /go/bin/go build main.go && ./main /home/data/server/otel-collector/data/otelcol-contrib 410ab7
2022/11/22 10:12:41 /usr/local/go1.18/go/src/runtime/malloc.go 1117 &{4261280 0xc0047d0730 4263648 [] [] 0 0xc0000c4000 <nil>}

@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Dec 8, 2022
@seankhliao seankhliao added this to the Unplanned milestone Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: No status
Development

No branches or pull requests

4 participants