5

gdb带源码调试libc

 2 years ago
source link: https://xuanxuanblingbling.github.io/ctf/tools/2020/03/20/gdb/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

gdb带源码调试libc

发表于 2020-03-20

| 分类于 CTF/tools

在使用gcc编译时,如果采用带-g选项编译,即可在二进制文件中附加调试信息以便gdb进行源码级别的调试。如果二进制中存在调试信息并且源码文件存在,则可以使用gdb的list命令来查看源码。通过在ubuntu中安装带调试信息的libc,并下载libc源码,即可配置gdb跟入libc的库函数后进行源码级别的调试

推荐阅读:

源码调试示例

这里我们先自己尝试调试任意的一个带源码的程序:

# include <stdio.h>
int main(){
  puts("hello");
  puts("world");
  return 0;
}

-g选项进行编译:

➜  gcc -g test.c -o test

gdb启动:

➜  gdb -q ./test 
GEF for linux ready, type 'gef' to start, 'gef config' to configure
77 commands loaded for GDB 7.11.1 using Python engine 3.5
[*] 3 commands could not be loaded, run 'gef missing' to know why.
Reading symbols from ./test...done.
gef➤  b main
Breakpoint 1 at 0x40052a: file test.c, line 4.
gef➤  r

便可以在gef中看到源码一栏:

───────────────────────────────────────────────────────────────────────────────────────────────────────────── source:test.c+4 ────
      1	 # include <stdio.h>
      2	 int main(){
      3	   puts("hello");
 →    4	   puts("world");
      5	   return 0;
      6	 }
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
gef➤  

不过如果我们gdb在当前目录找不到test.c文件,则无法进行源码调试。如果源码在其他目录可以在gdb中用directory命令指定,或者在启动gdb时用-d参数指定,一样可以看到源码。比如我们将test.c移动到其父目录后:

➜  mv test.c  ../
➜  gdb -q ./test -d ../

源码调试libc

所以显然我们要准备两个东西:

  • 带调试的libc.so
  • 对应版本的glibc源码

所以首先安装带调试版本的libc:

➜  sudo apt install libc6-dbg  
➜  sudo apt install libc6-dbg:i386

然后下载当前版本的libc源码:

  1. 首先修改/etc/apt/sources.list,将deb-src配置开启
  2. 更新sudo apt update
  3. 使用apt source下载源码apt source libc6-dev

然后在调试时用directory把目录指向对应子文件夹就可以了,比如我要调试malloc:

➜  gdb -q ./applestore -d ../../glibc_source/glibc-2.23/malloc

然后在当GOT表已经初始化完成之后进入到malloc函数之后便可以跟到malloc源码中进行调试:

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── source:malloc.c+2902 ────
   2897	 
   2898	 /*------------------------ Public wrappers. --------------------------------*/
   2899	 
   2900	 void *
   2901	 __libc_malloc (size_t bytes)
 → 2902	 {
   2903	   mstate ar_ptr;
   2904	   void *victim;
   2905	 
   2906	   void *(*hook) (size_t, const void *)
   2907	     = atomic_forced_read (__malloc_hook);
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────

Recommend

  • 62
    • blog.51cto.com 7 years ago
    • Cache

    调试利器GDB(下)-在路上

    本文讲了GDB的数据断点使用和栈帧信息的查看以及其他使用技巧

  • 73
    • Github github.com 6 years ago
    • Cache

    GDB 单步调试汇编

    之前在看汇编的时候一直是肉眼看GCC -S的结果,缺点是很不直观,无法实时的看到寄存器的值,所以研究了下如何用GDB调试汇编。当然,写这篇文章更重要的一个目的是半年没有写博客了,博客要长草了。^_^ 我调试汇编的需求有几点:

  • 47
    • www.tuicool.com 5 years ago
    • Cache

    GDB 调试指南

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev) ,专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫。

  • 12
    • blog.studygolang.com 4 years ago
    • Cache

    GDB调试Go程序

    GDB调试Go程序 GDB调试Go程序 说明:作为一门静态语言,似乎支持调试是必须的,而且,Go初学者喜欢问的问题也是:大家都用什么IDE?怎么调试? 其实,Go是为多核和并发而生,真正的项目,你用单步调试,原本没问题的,可能...

  • 10
    • blog.csdn.net 4 years ago
    • Cache

    GDB 调试程序 详解 使用实例

    GDB 调试程序 详解 使用实例 用GDB调试程序

  • 7
    • jiajunhuang.com 4 years ago
    • Cache

    Go使用gdb调试

    Go使用gdb调试 其实我一般调试都是直接打log的,不过gdb调试还是很有用处,尤其是当碰到一些底层错误的需要单步跟踪的时候,比如,想研究一下 Go的runtime是如何实现的的时候。 首先在编译Go程序的时候,要让Go带上编译信息:

  • 16
    • zhuanlan.zhihu.com 4 years ago
    • Cache

    使用VS 2017进行Linux C/C++远程GDB调试

    使用VS 2017进行Linux C/C++远程GDB调试同济大学 计算机科学与技术硕士在读既然你已经知道了makefile是咋回事了,又嫌麻烦每次都要用ftp传代码+开个终端测试那就不妨...

  • 11
    • taowusheng.cn 4 years ago
    • Cache

    gdb和qemu调试Linux内核

    gdb和qemu调试Linux内核 之前学习了利用KGDB双机调试内核,这种...

  • 10

    GDB 调试 libvirt 源码之 domblkinfo 命令源码跟踪记 September 2, 2021 • Read: 43 •

  • 8

    GDB 调试 QEMU 源码跟踪 QMP 协议执行 September 3, 2021 • Read: 63 •

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK