建议和反馈

请填写你的反馈内容

问答 > Go/Python > 问答详情
已解决

go gdb问题 520LK

QQ图片20181020105331.png


我是这样编译的  go build -gcflags "all=-N -l" -o test1 test1.go


QQ图片20181020105337.jpg

QQ图片20181020105421.png

No symbol "s" in current context ?? 怎么解决 ?

2018-10-20
0
请先登陆或注册

小熊winnie2019-09-04
  • 编译(gcc)与调试(gdb)

  • centos7 安装go和gdb

yum install -y go gdb
  • main.go

package main

import "unsafe"func main() {
	s := "hello world!"
	println(unsafe.Sizeof(s), len(s))}
  • 开始调试

[root@localhost go]# ls
main.go[root@localhost go]# go build -gcflags "-N -l" -o main ./main.go 
[root@localhost go]# gdb main
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-redhat-linux-gnu".For bug reporting instructions, please see:...Reading symbols from /usr/local/www/go/main...done.(gdb) l1	package main2	3	import "unsafe"4	5	func main() {6		s := "hello world!"7		println(unsafe.Sizeof(s), len(s))8	}(gdb) b main.go:6Breakpoint 1 at 0x450b4d: file /usr/local/www/go/main.go, line 6.(gdb) run
Starting program: /usr/local/www/go/main 

Breakpoint 1, main.main () at /usr/local/www/go/main.go:66		s := "hello world!"(gdb) p s
$1 = 0x45b180 "\001", '\000' , "\305\006\377\023\a\001\001\201\200\255J\000\000\000\000\000T\212G\000\000\000\000\000\200\t\000\000\000k\000\000\000\000\000\000\000\000\000\000\020\000\000\000\000\000\000\000\b\000\000\000\000\000\000\000\b\000\000\000\000\000\000\000\270H\337\335\002\b\b2\260\255J\000\000\000\000\000T\212G\000\000\000\000\000\023\037\000\000\000\000\000\000\200\261E\000\000\000\000\000\003\000\000\000\000\000\000\000\b\000\000\000\000\000\000\000\b\000\000\000\000\000\000\000\221U\313q\002\b\b2\260\255J\000\000\000\000\000T\212G\000\000\000\000\000\067\032\000\000\000\000\000\000@\265E\000\000\000\000\000\003\000\000\000\000\000\000\000\020", '\000' ...(gdb) ptype s
type = struct string {
    uint8 *str;
    int len;}(gdb)


投一票
评论(0)
赞赏(0)
jail2019-02-15

要先设个断点,停到main函数里,才能看到s


(gdb) b main.main

Breakpoint 1 at 0x4547b0: file /home/yajwu/gows/test.go, line 5.

(gdb) c

The program is not being run.

(gdb) run

Starting program: /home/yajwu/gows/test 


Breakpoint 1, main.main () at /home/yajwu/gows/test.go:5

5 func main() {

(gdb) ptype s

type = struct string {

    uint8 *str;

    int len;

}

(gdb) 


投一票
评论(0)
赞赏(0)
Hconk2018-10-24
用你的代码试了下,gdb调试没什么问题,以下是调试过程。
(上面乱了还没发删除,只能重新发了)
➜  test.com go build -gcflags "-N -l" -o test ./test.go
➜  test.com gdb test                                   
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) l
1	package main
2	import "unsafe"
3	
4	func main() {
5		s := "hello world!"
6		println(unsafe.Sizeof(s),len(s))
7	
8	}
(gdb) b test.go:6
Breakpoint 1 at 0x401028: file /usr/local/go/gopath/src/test.com/test.go, line 6.
(gdb) run
Starting program: /usr/local/go/gopath/src/test.com/test 
[New LWP 24125]
[New LWP 24126]
[New LWP 24127]

Thread 1 "test" hit Breakpoint 1, main.main () at /usr/local/go/gopath/src/test.com/test.go:6
6		println(unsafe.Sizeof(s),len(s))
(gdb) l
1	package main
2	import "unsafe"
3	
4	func main() {
5		s := "hello world!"
6		println(unsafe.Sizeof(s),len(s))
7	
8	}
(gdb) p s
$1 = 0x475150 "hello world!"
(gdb) ptype s
type = struct string {
    uint8 *str;
    int len;
}
(gdb)


投一票
评论(4)
赞赏(0)
    4条回答
  • 追风少年

    你go的版本是多少? (gdb) p s 的时候我老是出现 No symbol "s" in current context.

  • ubuntu@VM-0-2-ubuntu:~/6.824$ go version go version go1.6.2 linux/amd64 ubuntu@VM-0-2-ubuntu:~/6.824$ gdb -v GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

  • 要在main函数里,s才存在,要让程序停在main里

  • 要在main函数里,s才存在,要让程序停在main里

Hconk2018-10-24
用你的代码试了下,gdb调试没什么问题,以下是调试过程。

➜ test.com go build -gcflags "-N -l" -o test ./test.go ➜ test.com gdb test GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from test...done. (gdb) l 1 package main 2 import "unsafe" 3 4 func main() { 5 s := "hello world!" 6 println(unsafe.Sizeof(s),len(s)) 7 8 } (gdb) b test.go:6 Breakpoint 1 at 0x401028: file /usr/local/go/gopath/src/test.com/test.go, line 6. (gdb) run Starting program: /usr/local/go/gopath/src/test.com/test [New LWP 24125] [New LWP 24126] [New LWP 24127] Thread 1 "test" hit Breakpoint 1, main.main () at /usr/local/go/gopath/src/test.com/test.go:6 6 println(unsafe.Sizeof(s),len(s)) (gdb) l 1 package main 2 import "unsafe" 3 4 func main() { 5 s := "hello world!" 6 println(unsafe.Sizeof(s),len(s)) 7 8 } (gdb) p s $1 = 0x475150 "hello world!" (gdb) ptype s type = struct string { uint8 *str; int len; } (gdb)

投一票
评论(0)
赞赏(0)

求助中

解决了这个问题,预计可以帮助到

  • 1
  • 5
  • 0
  • 3
  • 9
邀请

感谢您的善举,每一次解答会成为新人的灯塔,回答被采纳后获得20算力和相应的LK币奖励