Giter Site home page Giter Site logo

mborgerson / gdbstub Goto Github PK

View Code? Open in Web Editor NEW
212.0 10.0 23.0 83 KB

Simple, single-file, dependency-free GDB stub that can be easily dropped in to your project.

License: MIT License

Makefile 4.23% C 89.89% Assembly 4.69% Shell 1.19%
gdb debug development tools embedded hacktoberfest

gdbstub's Introduction

gdbstub

This is a simple, single-file1 GDB stub library that can be easily dropped in to your project to allow you to debug a target platform using GDB (or another application which supports remote GDB targets). It has no library dependencies (such as libc) and requires just standard tools to build.

Protocol

Communication between the stub and the debugger takes place via the GDB Remote Serial Protocol.

Usage

In some .c file in your project:

/* Define to enable bare metal x86 support, or define your own architecture */
#define GDBSTUB_ARCH_X86

/* Enable function definitions */
#define GDBSTUB_IMPLEMENTATION

#include "gdbstub.h"

void _start(void) {
	dbg_sys_init(); /* Setup, wait for debugger on serial port  */
}

See gdbstub.c for example usage.

Architecture Support

  • GDBSTUB_ARCH_MOCK: A mock architecture for testing
  • GDBSTUB_ARCH_X86: Bare-metal x86 (32-bit). You'll also need interrupt handlers (so not .

Porting

This was originally developed for embedded x86 systems, but it's fairly modular. With a little effort, it can be easily ported to other platforms.

I recommend copying the mock architecture implementation and adjusting it to fit your platform's needs accordingly.

PR's for other platforms are welcome!

Building

By default, running make produces a gdbstub program. This is simply a stub for a mock architecture (just a handful of registers and some memory) running inside a normal program that communicates over stdio.

A stub intended for bare metal x86 machines can be built with make ARCH=x86. This produces an ELF binary gdbstub.elf that will hook the current IDT (to support debug interrupts) and break.

Additionally, a simple flat binary gdbstub.bin is created from the ELF binary. The intent for this flat binary is to be easily loaded into memory and jumped to.

x86 Demo

In gdbstub.c there is a simple function that's used for demonstration and testing. To use it, build the stub with:

$ make ARCH=x86 INCLUDE_DEMO=1

Then, to test the GDB stub out, launch an instance of the full-system emulator QEMU as follows:

qemu-system-i386 -serial tcp:127.0.0.1:1234,server -display none -kernel gdbstub.elf

This will launch QEMU, create a virtual machine with a virtual serial port that can be connected to through local TCP port 1234, then load and run the stub executable inside the virtual machine.

You can then launch your local GDB client to get your GDB client to connect to the virtual serial port and begin debugging the demo application:

$ gdb
(gdb) symbol-file gdbstub.elf
(gdb) target remote 127.0.0.1:1234
(gdb) b simple_loop
(gdb) layout split
(gdb) c

For example, step a couple of times and print out the value of x:

(gdb) s 3
(gdb) p/x x
$1 = 0xdeadbeef

License

This software is published under the terms of the MIT License. See LICENSE.txt for full license.

Matt Borgerson, 2016-2022

Footnotes

  1. The main protocol bits and most architecture support is in a single file: gdbstub.h. Some platform files might also be required depending on your use case.

gdbstub's People

Contributors

mborgerson 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

gdbstub's Issues

fail to connect

I'm writing my own little OS to run on a real machine and I'm looking to debug it via the serial port. But after I call gdb_sys_init() function in kernel file and compile gdbstub.h and gdb_stub_x86_int.nasm with my OS kernel, I was unable to connect to the target machine when I ran gdb on another debugger and entered the command target remote /dev/ttyS0.

I can confirm that the serial ports of the two devices can communicate normally. I see that the gdb_x86_hook_idt() function called in the gdb_sys_init() uses local variables, which seems to have no practical effect, so I tried to call the gdb_x86_init_idt() function in gdb_sys_init(), but it still doesn't work.

The makefile statements associated with gdbstub are as follows:

ASMKFLAGS_gdbstub = -I gdbstub/ -f elf

OBJS = os/kernel/kernel.o os/kernel/start.o os/kernel/main.o os/kernel/clock.o
        ...
        os/gdbstub/gdbstub_x86_int.o

os/gdbstub/gdbstub_x86_int.o: os/gdbstub/gdbstub_x86_int.nasm
      $(ASM) $(ASMKFLAGS_gdbstub) -o $@ $<

Debug mode

Can you make instructions to run in debug mode?

License update

Hi!

Can you update the license to GPLv3 which is more modern and compatible?

Thank you!

Add unit tests

  • I'd like to see all of the RSP parsing stuff get unit tests
  • Mock architecture was added that should make this pretty easy

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.