2

I have been asked to write a program in C which should debug another program in C language and then store the value of each variable of every line,loop or function in a log file.

I have been searching over the internet and I found articles on debugging using gdb.

Can I somehow use GDB in my program for this purpose and then store the values of each variable line by line.

I've got basic knowledge of C/C++ so please reply in simple terms.

Thanks

3
  • 6
    Take a look at ptrace. Commented Jun 25, 2013 at 14:01
  • No reason to close this, it's a great opportunity to discuss how debuggers work. Commented Jun 25, 2013 at 15:12
  • If the objective is to have and use a debugger, then sure, why not. If the objective is to learn how to write a debugger, then using an existing debugger kinda defeats the purpose. Commented Jun 25, 2013 at 15:17

1 Answer 1

4

Debuggers depend on some special capability of the hardware, which must be exposed by the operating system (if any).

The basic idea is that the hardware is configured to transfer control to a debugger stub either after every instruction of the target program, or after certain types of instructions such system calls, or those meeting a hardware breakpoint condition. Typically this would look like an interrupt, supervisor exception, or the like - very platform-specific detail.

As mentioned in comments, on Linux, you use the ptrace functionality of the kernel to interact with the debugger support provided by the hardware and the kernel, abstracting away a lot of the hardware-unique detail and managing the permission issues. Typically you must either be the same user id as the process being debugged, or be the superuser (root). Linux's ptrace also gives you an indirect ability to do to things like access the memory (literally, address space) of the target application, something critical to debugger functionality which you cannot ordinarily do from another user-mode program on a multitasking operating system.

Other operating systems will have different methods. Some embedded targets use debug pods which connect your development machine to the embedded board by a few wires. In other cases, debug capability built into the hardware is managed by a small program running on the target processor, which then talks back over a serial or network port to the full debugger program residing on the development machine.

A program such as GDB can do more than just the basics of setting debug stop conditions, dumping registers, and dumping program instructions. Much of its code deals with annotating what it displays based on debug metadata optionally left behind by compilers, walking back through stack frames, and giving the user powerful tools to configure all of this - and of course it does most of this in a target-independent way, with the target-unique code mostly confined to a few interchangeable directories.

You can indeed "drive" GDB from another program - many, many GUI type debuggers do exactly that, existing as graphical front ends for GDB. However, if you were assigned to write a debugger, doing it that way may or may not by consistent with your assignment.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.