gdb零散學習心得。

引用:正則表達式

 

記錄學習心得。將來將統一整理。shell

1、基礎知識函數

1:細節整理。學習

  1,啓動GDB的前置要求。ui

    1)使用gcc/g++時,添加-g的指令。例如:g++ -g test.cpp -o testspa

    2)如沒有添加編譯信息,能夠運行程序,後使用源代碼關聯。(不肯定此方式的具體操做方法線程

  2,GDB中能夠使用shell調試

    1)例:shell make <make-args> // rebuild source code.code

    2)能夠忽略shell前置:make <make-args> // rebuild source code.server

  3,自動補全和查找功能。

    1)b 'buffer( M-? 能夠用來搜索buffer函數的位置。

    2)能夠使用TAB鍵進行自動補全。

  4,調試已運行程序。

    1)查找到進程ID,使用ID來調試

    2)使用gdb <program>來連接源代碼,而後attach來連接到進程。

  5,暫停和繼續。

    1)斷點(breakpoint),觀察點(watchpoint),捕捉點(catchpoint),信號(single),線程中止(thread stop)。

    2)繼續(continue)

  6,if判斷:break/watch支持if判斷。catch不支持。

  7,GDB中能夠查看三種變量值:

    1)全局變量。

    2)靜態全局變量。

    3)局部變量。

基本命令:

1 啓動GDB
  gdb <program>
  gdb <program> core    // add a core file.。
  gdb <program> <PID>   // if it is a server process. gdb will attach it automitically.
2 運行參數。
  set args  // 指定運行時參數。
  show args  // 顯示運行參數。
3 運行環境
  path <dir>  // 設置程序運行路徑
  show path
  set environment varname [=value]  // 設置環境變量,set env USER=zheng
  show environment [varname]
4 工做目錄
  cd <dir>  // 和shell指令一致。
  pwd
5 設置程序的輸入輸出。
  info terminal
  tty <file>/<dev>
6 斷點方式:break
  ~ <function>  // 查看函數
  ~ <+/- offset>  // 從當前行偏移多少行。
  ~ <filename:linenum>  // 源文件 多少行。
  ~ <filename:function>  // 源文件:函數
  ~ <*address>  // 內存地址處中止。
  info ~[n]  // 查看第N個斷點
7 觀察點:watch
  watch <expr>  // 表達式發生變化時中止。
  rwatch <expr>  // 表達式被讀時中止。
  awatch <expr>  // 表達式被讀/寫時中止。
  info watchpoints  // 列出當前所設置的全部觀察點。
8 捕捉點:catch
  catch <event>
  // 1 參數event:throw拋出,catch捕捉,exec調用,fork,vfork,un/load 共享庫。
9 清除中止點:clear。
  ~
  ~ <function>
  ~ <linenum>
  ~ <filename:linenum>
  ~ <filename:function>
  delete [breakpoint][range]  // range: 例[2-5][1-3]
  disable/enable    // 使能和失能 中止點。
10 自動化調試的一個例子(包括if)
  break foo if x>0
  commands
  printf "x is %d\n", x
  continue
  end
11 信號處理
  handle <single><keywords>
  no/stop
  no/print
  no/pass
  no/ignore
  info singles
  info handle
12 線程斷點
  break <linespec> thread <threadno>  // break的其餘方式也能夠使用。
13 查看棧。
  backtrace(bt)
  bt [n]  // 打印n層信息
  bt [-n] // 打印底下n層信息
  frame(f) [n]  // 查看第N層
  up/down [n]  // 向 上/下 移動 N層。默認爲1
  info frame  //  打印更詳細的信息。
14 顯示源代碼
  list <first>,<last>/<>  // 以參數肯定顯示位置。break能夠使用的參數,list均可以使用
  set listsize <count>  // 設置顯示行數。
  show listsize
15 搜索(正則表達式)
  forward-search <regex>
  search <regex>
  reverse-search <regex>
16 指定源文件路徑。
  directory(dir) <dirname>
  shwo dir
17 自動顯示功能
  display <expr>
  display/<fmt> <expr>/<addr>
  undisplay ...
  delete display ...
18 GDB環境變量
  set $foo = *obj_ptr
  show convenience
  例:
  set $i = 0;
  print array[$i++]->content
19 修改程序中的變量:
  print x=4  // X時C/C++中變量
20 調轉,強制調用函數和強制返回。
  jump <linespec>/<address>
  call <expr>
  return <expr>/<>

 

 3、

相關文章
相關標籤/搜索