gdb生产环境实际案例调试

获取进程编号


ps aux | grep -v grep | grep myapp_as_brpc
www-data 18900 10.1  1.2 9162996 407160 ?      Sl   14:42   1:29 ./bin/myapp_as_brpc --flagfile=./conf/dish_as.conf >/dev/null 2>&1 &

启动gdb进行调试


sudo gdb attach 18900
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 <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
attach: No such file or directory.
Attaching to process 18900
Reading symbols from /data/myapp_as_brpc/bin/myapp_as_brpc...done.
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.1
...
Reading symbols from /lib64/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 19019]
[New LWP 19018]
[New LWP 19017]
...
[New LWP 19007]
[New LWP 18903]
[New LWP 18902]
[New LWP 18901]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /lib64/libidn.so.11...Reading symbols from /lib64/libidn.so.11...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/libidn.so.11
...
0x00007fe999f2480d in nanosleep () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install cyrus-sasl-lib-2.1.26-23.el7.x86_64 glibc-2.17-292.el7.x86_64 keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.15.1-18.el7.x86_64 libcom_err-1.42.9-11.el7.x86_64 libcurl-7.43.0-3.0.cf.rhel7.x86_64 libgcc-4.8.5-28.el7.x86_64 libidn-1.28-4.el7.x86_64 libselinux-2.5-12.el7.x86_64 libssh2-1.4.3-10.el7_2.1.x86_64 libstdc++-4.8.5-28.el7.x86_64 nspr-4.17.0-1.el7.x86_64 nss-3.34.0-4.el7.x86_64 nss-softokn-freebl-3.34.0-2.el7.x86_64 nss-util-3.34.0-2.el7.x86_64 openldap-2.4.44-13.el7.x86_64 openssl-libs-1.0.2k-12.el7.x86_64 pcre-8.32-17.el7.x86_64 zlib-1.2.7-17.el7.x86_64
(gdb)

注:会发现这个时候程序已经卡住了,等待你输入指令

设置断点


(gdb) b dish_as_service_impl.cpp:38
Breakpoint 1 at 0xa532ec: file /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp, line 38.
(gdb) b spatial_bs_client.cpp:166
Breakpoint 2 at 0xa509e6: spatial_bs_client.cpp:166. (2 locations)

程序放行(等待进入到设置断点的地方会自动停下来)


(gdb) c
Continuing.
[New Thread 0x7fe8c77fe700 (LWP 27871)]
[Switching to Thread 0x7fe92d7fa700 (LWP 19002)]

Breakpoint 1, wmsearch::DishAsServiceImpl::dish_search (this=<optimized out>, controller=<optimized out>, request=0x7fe90c9897c0, response=0x7fe90c01d630, done=0x7fe90c0269f0)
    at /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp:38
38	/tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp: No such file or directory.
(gdb)

注:这个是生产环境,输入指令 c 之后,马上就卡住了,从提示来看已经到达设置的第一个断点了

单步调试&查看变量值


(gdb) n
46	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
47	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
46	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
29	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
46	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
29	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
46	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) n
[New Thread 0x7fe8bf7fe700 (LWP 30245)]
[Thread 0x7fe8c77fe700 (LWP 27871) exited]
[New Thread 0x7fe8c77fe700 (LWP 30293)]
[Thread 0x7fe8bf7fe700 (LWP 30245) exited]
[Thread 0x7fe8c77fe700 (LWP 30293) exited]
47	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/service/dish_as_service_impl.cpp
(gdb) print cntl->remote_side()
value has been optimized out
(gdb) print dishAsLog.log_id
$1 = 56
(gdb)

注:上面print了2次,一次成功打印出了值,一次没有,提示“value has been optimized out” ,这个是编译优化造成的,没法看了,除非重新编译一下源码,并禁用编译优化,参见 https://blog.csdn.net/baidu_38172402/article/details/81152192

继续运行程序直接运行到下一个断点

注:这里,如果没有断点就一直运行。


(gdb) c
Continuing.

Breakpoint 2, wmsearch::SpatialBsClient::spatial_bs_search (this=0x225e260, req=std::shared_ptr (count 2, weak 0) 0x7fe8d8220600, resp=std::shared_ptr (count 1, weak 0) 0x7fe8d8a20660,
    service_name="spatial-bs", log=log@entry=0x7fe8c49d78a0) at /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp:166
166	/tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp: No such file or directory.
(gdb) n

Breakpoint 2, wmsearch::SpatialBsClient::spatial_bs_search (this=0x225e260, req=std::shared_ptr (count 2, weak 0) 0x7fe8d8220600, resp=std::shared_ptr (count 1, weak 0) 0x7fe8d8a20660,
    service_name="spatial-bs", log=log@entry=0x7fe8c49d78a0) at /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp:166
166	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp
(gdb) n
[New Thread 0x7fe8bf7fe700 (LWP 949)]
[Thread 0x7fe8bf7fe700 (LWP 949) exited]
171	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp
(gdb) n
[New Thread 0x7fe8bf7fe700 (LWP 962)]
[Thread 0x7fe8bf7fe700 (LWP 962) exited]
172	in /tmp/cpp/xxxxxx/c_search/dish-as-brpc/src/clients/spatial_bs_client.cpp
(gdb) print shop_as_req->x
Attempt to take address of value not located in memory.
(gdb) print request.mutable_external_header()
[New Thread 0x7fe8c77fe700 (LWP 2426)]
[New Thread 0x7fe8bf7fe700 (LWP 2425)]
$2 = (eureka::proto::ExternalHeader *) 0x7fe8d8022a20
(gdb) print request.mutable_external_header().x_eleme_rc_request
[Thread 0x7fe8c77fe700 (LWP 2426) exited]
[Thread 0x7fe8bf7fe700 (LWP 2425) exited]
[New Thread 0x7fe8be9f7700 (LWP 2479)]
$3 = {const std::string &(const eureka::proto::ExternalHeader * const)} 0xcb7bb4 <eureka::proto::ExternalHeader::x_eleme_rc_request() const>
(gdb)
[New Thread 0x7fe8bf7fe700 (LWP 2614)]
[Thread 0x7fe8be9f7700 (LWP 2479) exited]
$4 = {const std::string &(const eureka::proto::ExternalHeader * const)} 0xcb7bb4 <eureka::proto::ExternalHeader::x_eleme_rc_request() const>
(gdb) print request.range_num
$5 = {google::protobuf::uint32 (const eureka::proto::SpatialBsRequest * const)} 0xcb85a2 <eureka::proto::SpatialBsRequest::range_num() const>

清除断点&终止一个正在调试的程序


(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) quit

注:仅仅执行delete,程序还是会卡住的

查看所有断点

i b

查看string类型的值

p (char*)str1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章