天天看點

gdb學習gdb調試的三種類型:指令編譯可執行檔案的要求核心轉儲遇到過的問題

gdb調試的三種類型:

  • 調試一個可執行檔案
  • 調試一個coredump檔案
  • 調試一個正在運作的程序
    • gdb -nx /proc/pid/exe pid
    • 參考查找c++程序cpu占滿的原因

指令

  • gdb program core

  • show language, set language c++

  • bt, bt full

  • f 可以跳轉到某個棧中位置
  • i locals 顯示目前調用棧的所有變量
  • i register 顯示目前調用棧的寄存器值,主要是檢視位址
  • thread apply all bt

  • info threads 可以檢視所有線程的資訊
  • thread 可以具體跳轉到某個線程
  • disassemble 顯示指令

編譯可執行檔案的要求

  • gcc -DDEBUG -g -o debug debug.c

核心轉儲

啟用

  • ulimint -a, ulimit -c, ulimit -c unlimited

路徑與檔案名

  • 核心參數kernel.core_pattern
    • echo "core" > /proc/sys/kernel/core_pattern

    • 在 可執行檔案所在目錄 生成檔案名為core的核心轉儲檔案???
    • 在哪個目錄啟動可執行檔案,core檔案就生成在這個目錄???
    • echo"/corefile/core-%e-%p-%t" >core_pattern

      • 将core檔案統一生成到/corefile目錄下
      • 産生的檔案名為core-指令名-pid-時間戳
    • 以下是參數清單:
      • %p - insert pid into filename 添加pid
      • %u - insert current uid into filename 添加目前uid
      • %g - insert current gid into filename 添加目前gid
      • %s - insert signal that caused the coredump into the filename 添加導緻産生core的信号
      • %t - insert UNIX time that the coredump occurred into filename 添加core檔案生成時的unix時間
      • %h - insert hostname where the coredump happened into filename 添加主機名
      • %e - insert coredumping executable name into filename 添加指令名
  • 核心參數kernel.core_uses_pid
    • 表示是否在生成的核心轉儲檔案後添加 程序号字尾
    • echo "1" >/proc/sys/kernel/core_uses_pid

  • 修改/etc/sysctl.conf檔案
kernel.core_pattern=/var/core/core.%p
kernel.core_uses_pid=0
           

遇到過的問題

signal SIGBUS 7, Bus error

  • 懷疑是由于mmap之後,源檔案被改變(内容變少),造成通路越界

signal SIGABRT 6, Aborted

  • 懷疑是double free
  • 使用boost::shared_ptr出現了問題
  • 也懷疑是記憶體不足,配置設定失敗
  • 析構的時候有競态問題?

參考資料

gdb調試之基礎篇

gdb調試之實用技巧篇

Linux GDB core進階調試

善用GDB 調試一些函數棧被毀壞的問題

段錯誤調試神器 - Core Dump詳解

由mmap引發的SIGBUS

排錯經曆:全局變量被多次析構

轉載于:https://www.cnblogs.com/wangzhiyi/p/9545564.html