gdb tips


1, output the stack info of all threads into a local file. 

set logging file ./local.txt

set logging on

thread apply all bt 

set logging off 


2, output the all stack info into a file automatically 

this bash script demonstrates how to get it 

#! /bin/bash

# $1 is pid of target process 

batchcmd=gdb.cmd
log=gdb.$1.log

rm -rf $batchcmd
echo "set pagination off" >> $batchcmd
echo "set logging file "$log >> $batchcmd
echo "set logging on" >> $batchcmd
echo "thread apply all bt full" >> $batchcmd
echo "set loggint off" >> $batchcmd
echo "quit" >> $batchcmd

gdb -batch-silent -x $batchcmd -p $1

the key is  gdb batch mode 

the references

gdb choosing modes:
http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_3.html
http://sourceware.org/gdb/current/onlinedocs/gdb/Mode-Options.html

gdb command file :
http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_16.html#SEC143

'''

gdb commands : http://www.programlife.net/gdb-manual.html 


3, how to print the containers 

use gdb-stl-views :

gdb-stl-views is a set of GDB macros that can display the contents of many STL containers: list, vector, map, multimap, set, multiset, dequeue, stack, queue, priority_queue, bitset, string, and widestring.

you can down load it and copy to user home directory as .gdbinit, which gdb will load it at ever start 

here is a sample :

(gdb) p vecTest
$1 = {
   >> = {
    _M_impl = {
      > = {
        > = {}, },
      members of std::_Vector_base >::_Vector_impl:
      _M_start = 0x804b040,
      _M_finish = 0x804b054,
      _M_end_of_storage = 0x804b060
    }
  }, }
(gdb) pvector vecTest
elem[0]: $2 = 0
elem[1]: $3 = 1
elem[2]: $4 = 2
elem[3]: $5 = 3
elem[4]: $6 = 4
Vector size = 5
Vector capacity = 8
Element type = int *
(gdb)


4, how to print data  structure at a memory address
there is a sample: 

the test code as follow:


debug the core file with gdb

(gdb) p dl
$1 = {
  <std::_Vector_base<data,std::allocator<data> >> = {
    _M_impl = {
      <std::allocator<data>> = {
        <__gnu_cxx::new_allocator<data>> = {<No data fields>}, <No data fields>},
      members of std::_Vector_base<data,std::allocator<data> >::_Vector_impl:
      _M_start = 0x603050,
      _M_finish = 0x603068,
      _M_end_of_storage = 0x603070
    }
  }, <No data fields>}

print the vector

(gdb) pvector dl
elem[0]: $2 = {
  x = 0,
  y = 0
}
elem[1]: $3 = {
  x = 1,
  y = 10
}
elem[2]: $4 = {
  x = 2,
  y = 20
}
Vector size = 3
Vector capacity = 4
Element type = data *

there are 2 ways to see the first element in vector  with memory address

(gdb) p '{data}'0x603050
No symbol "{data}" in current context.
(gdb) p {'data'}0x603050
$5 = {
  x = 0,
  y = 0
}
(gdb) p *(data*)0x603050
$6 = {
  x = 0,
  y = 0
}
so, p '{....}'0x**** or p *(*....)0x**** can get it


to be continued


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值