[WIP] 实验:用gdb观察gdb

Can I use gdb to observe gdb?

I always forget how gdb was made. for me, I went though the website on stackoverflow of Zhihu serveral times only for this question, and I always forget everything about ptrace. And I think it's annoying me, since I am just hanging around on the same field. So one day a crazy and funny idea comes to me:

Can I use gdb to observe gdb?

That was a great idea! I am going to do that!

how gdb was made?

If you know ptrace well, you can skip to next section!

ptrace is a great syscall, it can provide many infomation that we want for a process in Linux, and gdb was built upon it. Here I make a simple program

#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>

int main() {

    pid_t child_pid = fork();

    if (child_pid == 0) {
        printf("Child process starting 'aaa'...\n");
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);  
        execl("./pTrace", "pTrace", NULL);
        perror("execl failed");
        exit(EXIT_FAILURE);
    } else {
        int status;
        waitpid(child_pid, &status, 0);
        while (1) {
            if (WIFEXITED(status)) {
                printf("Child process terminated normally.\n");
                break;
            }
            long rax = ptrace(PTRACE_PEEKUSER, child_pid, 8 * RAX, NULL);
            if (rax == -1) {
                perror("ptrace failed");
                break;
            }
            printf("Child process RAX register value: %lx\n", rax);

            ptrace(PTRACE_CONT, child_pid, NULL, NULL);
            waitpid(child_pid, &status, 0);  
        }
        ptrace(PTRACE_DETACH, child_pid, NULL, NULL);
        waitpid(child_pid, &status, 0);
    }
    return 0;
}

⚠️upload failed, check dev console

⚠️upload failed, check dev console
⚠️upload failed, check dev console

下载:
lib

wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.1.0.tar.bz2
wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.2.1.tar.bz2
wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.2.1.tar.gz

gdb
Index of /gnu/gdb

wget https://ftp.gnu.org/gnu/gdb/gdb-15.2.tar.xz

编译:

gmp

位置:

/home/haibin/experiment/GMP/install

1.   
    ./configure --prefix=$(pwd)
    /home/haibin/experiment/GMP/

2. make

3. make install

mpfr

./configure --prefix=$(pwd) --with-gmp-include=/home/haibin/experiment/gmp-6.2.1/include --with-gmp-lib=/home/haibin/experiment/gmp-6.2.1/lib

mpc

tar -xvzf mpc-1.2.1.tar.gz
cd mpc-1.2.1/

./configure --prefix=$(pwd) \
    --with-gmp-include=/home/haibin/experiment/gmp-6.2.1/include \
    --with-gmp-lib=/home/haibin/experiment/gmp-6.2.1/lib \
    --with-mpfr-include=/home/haibin/experiment/mpfr-4.1.0/include \
    --with-mpfr-lib=/home/haibin/experiment/mpfr-4.1.0/lib \

make
make install
export LD_LIBRARY_PATH=$(pwd)/gmp-6.2.1/lib:$(pwd)/mpfr-4.1.0/lib:$(pwd)/mpc-1.2.1/lib:$LD_LIBRARY_PATH

编译好这些lib后,我们可以编译自己的gdb

./configure --with-gmp=/home/haibin/experiment/gmp-6.2.1/ --with-mpfr=/home/haibin/experiment/mpfr-4.1.0

随后,

./
gdb ./gdb
2316    in rtld.c
(gdb) n 500
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Single stepping until exit from function _dl_start_user,
which has no line number information.
Cannot find bounds of current function
(gdb) n 500
Cannot find bounds of current function
(gdb) n 500
Cannot find bounds of current function
(gdb) n 500
Cannot find bounds of current function
(gdb) s
Cannot find bounds of current function
(gdb) continue
Continuing.
[Detaching after vfork from child process 1011434]
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2
Copyright (C) 2020 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) 

⚠️upload failed, check dev console
⚠️upload failed, check dev console
⚠️upload failed, check dev console