Set breakpoint on static variable initialization using LLDB or GDB

0

Say I have the following Go file named test.go.

package main

import(
    "fmt"
)

var test_var int = 3

func main() {
    fmt.Printf("test_var is %v\n", test_var)
}

And compile it using

$ go build -o test test.go

How can I set a break-point on the initialization of the test_var variable after loading the binary into either LLDB or GDB?

I'm not really asking what command to run, but what symbol should I be looking up to set a breakpoint on? What symbol handles the initialization of the package.

I've already tried to lookup the main.init function based on what I read here but it doesn't seem to exist.

$ go build -o test test.go
$ lldb test
(lldb) target create "test"
Current executable set to 'test' (x86_64).
(lldb) image lookup -r -s 'main\.init$'
(lldb)

Additionally, If i just look up all init functions and try to set a break-point on one of them, it never gets triggered.

(lldb) image lookup -r -s '\.init$' test
21 symbols match the regular expression '\.init$' in /vcb/test/darwin/test:
        Address: test[0x00000000010623e0] (test.__TEXT.__text + 398304)
        Summary: test`errors.init        Address: test[0x000000000109e790] (test.__TEXT.__text + 645008)
        Summary: test`fmt.init        Address: test[0x0000000001084110] (test.__TEXT.__text + 536848)
        Summary: test`internal/oserror.init        Address: test[0x000000000108f030] (test.__TEXT.__text + 581680)
        Summary: test`internal/poll.(*pollDesc).init        Address: test[0x0000000001090030] (test.__TEXT.__text + 585776)
        Summary: test`internal/poll.init        Address: test[0x0000000001083e50] (test.__TEXT.__text + 536144)
        Summary: test`io.init        Address: test[0x0000000001062470] (test.__TEXT.__text + 398448)
        Summary: test`math.init        Address: test[0x00000000010933c0] (test.__TEXT.__text + 598976)
        Summary: test`os.init        Address: test[0x000000000107e270] (test.__TEXT.__text + 512624)
        Summary: test`reflect.init        Address: test[0x0000000001029870] (test.__TEXT.__text + 166000)
        Summary: test`runtime.(*addrRanges).init        Address: test[0x0000000001020840] (test.__TEXT.__text + 129088)
        Summary: test`runtime.(*gcWork).init        Address: test[0x00000000010087c0] (test.__TEXT.__text + 30656)
        Summary: test`runtime.(*itab).init        Address: test[0x00000000010217c0] (test.__TEXT.__text + 133056)
        Summary: test`runtime.(*mheap).init        Address: test[0x000000000103a1a0] (test.__TEXT.__text + 233888)
        Summary: test`runtime.(*p).init        Address: test[0x00000000010247b0] (test.__TEXT.__text + 145328)
        Summary: test`runtime.(*pageAlloc).init        Address: test[0x0000000001058f10] (test.__TEXT.__text + 360208)
        Summary: test`runtime.init        Address: test[0x0000000001068d40] (test.__TEXT.__text + 425280)
        Summary: test`strconv.init        Address: test[0x000000000106bba0] (test.__TEXT.__text + 437152)
        Summary: test`sync.init        Address: test[0x0000000001086540] (test.__TEXT.__text + 546112)
        Summary: test`syscall.init        Address: test[0x000000000108e110] (test.__TEXT.__text + 577808)
        Summary: test`time.init        Address: test[0x000000000106bd80] (test.__TEXT.__text + 437632)
        Summary: test`unicode.init
(lldb) break set -a 0x0000000001084110
Breakpoint 1: address = 0x0000000001084110
(lldb) run
Process 1917 launched: '/vcb/test/darwin/test' (x86_64)
test_var is 3
Process 1917 exited with status = 0 (0x00000000)

In the above example, i tried to break on the fmt packages init function with no success.

go
variables
initialization
asked on Stack Overflow Aug 9, 2020 by Nathan F. • edited Aug 9, 2020 by Nathan F.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0