Coder Social home page Coder Social logo

minidbg's Introduction

This project has been expanded into a book! It covers many more topics in much greater detail. You can now pre-order Building a Debugger.

minidbg

A mini x86 linux debugger for teaching purposes

See my Writing a linux debugger blog post series for a tutorial on how to write something like this.

minidbg's People

Contributors

tartanllama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minidbg's Issues

A tiny enhancement

Awesome project!

But I think if add \n for this line of code in every branch:

std::cerr << "Program name not specified\n";

It will be better!

Thanks!

Development environment issues

I like your course very much. I hope you can provide your development environment configuration.
1、gcc version?
2、g++ version?
3、Is the operating system environment ubuntu or centos?

Error during libelfin submodule initialization

alexander@alexander-pc:~/code/test/debugger/minidbg$ git submodule init
Submodule 'ext/libelfin' (https://github.com/aclements/libelfin.git) registered for path 'ext/libelfin'
Submodule 'ext/linenoise' (https://github.com/antirez/linenoise.git) registered for path 'ext/linenoise'
alexander@alexander-pc:~/code/test/debugger/minidbg$ git submodule update
Cloning into 'ext/libelfin'...
remote: Counting objects: 818, done.
remote: Total 818 (delta 0), reused 0 (delta 0), pack-reused 818
Receiving objects: 100% (818/818), 190.57 KiB | 235.00 KiB/s, done.
Resolving deltas: 100% (487/487), done.
Checking connectivity... done.
fatal: reference is not a tree: cc25b47c11f6798f79f09ec2345b18d663f9d548
Cloning into 'ext/linenoise'...
remote: Counting objects: 396, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 396 (delta 0), reused 1 (delta 0), pack-reused 393
Receiving objects: 100% (396/396), 165.21 KiB | 248.00 KiB/s, done.
Resolving deltas: 100% (225/225), done.
Checking connectivity... done.
Submodule path 'ext/linenoise': checked out 'c894b9e59f02203dbe4e2be657572cf88c4230c3'
Unable to checkout 'cc25b47c11f6798f79f09ec2345b18d663f9d548' in submodule path 'ext/libelfin'

I was able to fix it by changing the submodule url to your fork.

alexander@alexander-pc:~/code/test/debugger/minidbg$ git diff
diff --git a/.gitmodules b/.gitmodules
index 13aada1..4017271 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,6 +1,6 @@
 [submodule "ext/libelfin"]
        path = ext/libelfin
-       url = https://github.com/aclements/libelfin.git
+       url = https://github.com/TartanLlama/libelfin.git
 [submodule "ext/linenoise"]
        path = ext/linenoise
        url = https://github.com/antirez/linenoise.git

Some mistakes about the data type.

std::intptr_t is a better choice than uint64_t when applies to an address. Once I use uint64_t data type to describe an address, step_in, step_out, stepi, step_over... funcs will not work normally.

So, this problem has to be fixed.

also, std::sto-functions may be used properly, I chose stoull when i need to trans string 0xADDR to std::intptr_t instead stoi, or something wrong may occur. like:
m_load_address = std::stoull(addr, 0, 16);
that could be better.

I also noticed that this project seems dead, so I will not pr, just reminding other people who are doing this small project.

dwarf::format_error

When I test miniDebugger, I encountered the following problem:
Start debugger process 132818 terminate called after throwing an instance of 'dwarf::format_error'
what(): unknown compilation unit version 5 Aborted (core dumped)

then I use gdb to debug miniDebugger, I got the following info, how can I fix this bug
(gdb) run hello
Starting program: /home/ccy/Desktop/miniDebugger/build/miniDebugger hello
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Detaching after fork from child process 132695]
Start debugger process 132695
terminate called after throwing an instance of 'dwarf::format_error'
what(): unknown compilation unit version 5

Program received signal SIGABRT, Aborted.
__pthread_kill_implementation (no_tid=0, signo=6, threadid=140737347836800) at ./nptl/pthread_kill.c:44
44 ./nptl/pthread_kill.c: No such file or directory.

Build failure

I git cloned this project and did a 'cmake CMakeLists.txt'. It failed because I did not have linenoise.c under ext/linenoise. I then placed linenoise.c under ext/linenoise and repeated the above command and it succeeded. It generated a Makefile. But when I hit 'make', I get the following error. Can you please tell me what I should do? I git cloned the libelfin project separately, built it and then placed the .so files under ext/libelfin. Please advise.

[ 22%] Built target hello
make[3]: *** No targets specified and no makefile found. Stop.
CMakeFiles/libelfin.dir/build.make:57: recipe for target 'CMakeFiles/libelfin' failed
make[2]: *** [CMakeFiles/libelfin] Error 2
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/libelfin.dir/all' failed
make[1]: *** [CMakeFiles/libelfin.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

linenoise.c file missing

CMake Error at CMakeLists.txt:7 (add_executable):
Cannot find source file:

ext/linenoise/linenoise.c

In main branch, an error in minidbg.cpp

Obviously, when we want to get entry from pc, we must pass an off_set pc, so this needs to be fixed like:

 else if(is_prefix(command, "stepi")) {
        single_step_instruction_with_breakpoint_check();
        auto line_entry = get_line_entry_from_pc(get_pc());
        print_source(line_entry->file->path, line_entry->line);
    }
    else {
        std::cerr << "Unknown command\n";
    }
 else if(is_prefix(command, "stepi")) {
        single_step_instruction_with_breakpoint_check();
        auto line_entry = get_line_entry_from_pc(get_offset_pc());
        print_source(line_entry->file->path, line_entry->line);
    }
    else {
        std::cerr << "Unknown command\n";
    }

After compiling, stepi works normall.

address read from dwarf is not absolute

Thank you for your good tutorial!

But I can't make a breakpoint on a simple function following the code.
It seems dwarf++ read a relative PC offset, which begins from 0x0000000, thus I cannot write INT 3 on it.

How can I solve it?

an error in main branch

void Debugger::print_backtrace() {
  auto output_frame = [frame_number = 0](auto &&func) mutable {
    std::cout << "frame #" << frame_number++ << ": 0x" << dwarf::at_low_pc(func)
              << ' ' << dwarf::at_name(func) << std::endl;
  };

  auto current_func = get_function_from_pc(get_offset_pc());
  output_frame(current_func);

  std::intptr_t frame_pointer = get_register_value(m_pid, registers::reg::rbp);
  std::intptr_t return_address = read_memory(frame_pointer + 8);

  while (dwarf::at_name(current_func) != "main") {
    current_func = get_function_from_pc(offset_load_address(return_address));
    output_frame(current_func);
    frame_pointer = read_memory(frame_pointer);
    return_address = read_memory(frame_pointer + 8);
  }
}

function should be fixed like this, when we want to get function by pc, the pc value must a an off-set one.Also, data type to describe an addr must be std::intptr_t in case of occurring bizarre problems.

build failed with LLVM 10

As mentioned in aclements/libelfin#44, a small typo in libelfin will cause build failure with LLVM 10

diff --git a/elf/data.hh b/elf/data.hh
index ed5c7a1..4a60944 100644
--- a/elf/data.hh
+++ b/elf/data.hh
@@ -553,7 +553,7 @@ struct Sym<Elf64, Order>
                 return (stb)(info >> 4);
         }

-        void set_binding(stb v) const
+        void set_binding(stb v)
         {
                 info = (info & 0xF) | ((unsigned char)v << 4);
         }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.