gdb go optimized out

575 views
Skip to first unread message

mount...@gmail.com

unread,
Mar 14, 2019, 12:03:50 AM3/14/19
to golang-nuts

Very strange, when I compile with go build -o main -gcflags "-N -l" main.go, it appears optimized out when debugging with gdb.
Who knows why?

jake...@gmail.com

unread,
Mar 14, 2019, 12:49:51 PM3/14/19
to golang-nuts
First off. Please don't use a giant font in this group. It adds no valuable information, and makes it seem like you are yelling at the reader.

I use delve not gdb, so I can not speak directly to that. I use -gcflags="-N -l", and so far it has always produced a reasonably debuggable (with delve at least) executable.

Your question is probably too vague to answer though. What, specifically, are you experiencing as being "optimized out"? If you provided code for a small sample program, and told us what you actually see, and what you expect to see, you would have a better chance of a useful response.

mount...@gmail.com

unread,
Mar 15, 2019, 12:56:38 AM3/15/19
to golang-nuts
First of all, thank you very much for your answer. I use the giant font because I copied it directly from Google Translate.
When I debug grow func of mheap.go file, I print the ask variable, it output No symbol "ask" in current context, print npage it output optimized out.

func (h *mheap) grow(npage uintptr) bool {
ask := npage << _PageShift
v, size := h.sysAlloc(ask)
if v == nil {
print("runtime: out of memory: cannot allocate ", ask, "-byte block (", memstats.heap_sys, " in use)\n")
return false
}

// Create a fake "in use" span and free it, so that the
// right coalescing happens.
s := (*mspan)(h.spanalloc.alloc())
s.init(uintptr(v), size/pageSize)
h.setSpans(s.base(), s.npages, s)
atomic.Store(&s.sweepgen, h.sweepgen)
s.state = _MSpanInUse
h.pagesInUse += uint64(s.npages)
h.freeSpanLocked(s, false, true, 0)
return true
}


在 2019年3月15日星期五 UTC+8上午12:49:51,Jake Montgomery写道:

mount...@gmail.com

unread,
Mar 15, 2019, 3:23:53 AM3/15/19
to golang-nuts


在 2019年3月15日星期五 UTC+8下午12:56:38,mount...@gmail.com写道:
dlv

mount...@gmail.com

unread,
Mar 15, 2019, 3:26:28 AM3/15/19
to golang-nuts


在 2019年3月15日星期五 UTC+8上午12:49:51,Jake Montgomery写道:
First off. Please don't use a giant font in this group. It adds no valuable information, and makes it seem like you are yelling at the reader.
dlv.png

jake...@gmail.com

unread,
Mar 15, 2019, 12:12:42 PM3/15/19
to golang-nuts
First off, we are always happy to have new users in the group. But one more bit of etiquette - Please don't post screenshots of text. Instead copy paste the text into the message. I like to use fixed width font, or put it in a code block. See the recent discussion at https://groups.google.com/d/topic/golang-nuts/KR_JXhBWCk8/discussion for the many reasons why.

Looking at the screenshot, I see that you are trying to debug into a runtime library  function. That is why it is optimized. IIRC, the way you have built will only build main.go without optimization, but it will not effect the standard libraries. I think you want something like  go build -o main -gcflags "all=-N -l" main.go . The "all=" is the key here.

Good luck

mount...@gmail.com

unread,
Mar 18, 2019, 4:21:00 AM3/18/19
to golang-nuts

I set the parameters in your way and execute dlv or not.Thanks.
Reply all
Reply to author
Forward
0 new messages