OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 1:12 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Implementation of debugger using INT 3
PostPosted: Wed Nov 27, 2019 8:37 am 
Offline
Member
Member
User avatar

Joined: Wed Feb 06, 2019 10:41 am
Posts: 84
Dear.
I would like to have a minimal debugger implemented as the software-generated breakpoint for my OS.
What I try to implement is a function to generate a breakpoint and look at some variables in my C code.
for instance:
Code:
void do_something(void)
{
    int n = 0;
    n += 10;
    // here comes the function call to generate the breakpoint to look at the value of the variable n
    [...]
}

What I have read is that I should know exactly in which memory address, I have to put the INT 3.
The question is that which strategies should I follow for this reason?
Is it good to have the address of variable n (i.e. int* m = &n;) and set the INT 3 at that particular address?
In the end I need it for variables changing in a large loop. Perhaps simply using printf would not be a good way.

Best regards.
Iman.

_________________
Iman Abdollahzadeh
Github
Codeberg


Top
 Profile  
 
 Post subject: Re: Implementation of debugger using INT 3
PostPosted: Wed Nov 27, 2019 9:00 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
You need to put the int 3 instruction at the point in your code where you want it to break. You can't use the address of a variable for this.

If you are running in a virtual machine (highly recommended whilst developing your OS) use a good debugger, such as gdb, rather than trying to reinvent the wheel.


Top
 Profile  
 
 Post subject: Re: Implementation of debugger using INT 3
PostPosted: Wed Nov 27, 2019 10:49 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1604
If you want to write a debugger, you are going to have to parse the debug information present in the file. Debug information is really complicated. Believe me, the specifics of how breakpoints work are going to pale in comparison to that. For instance, you want to look at "n". Where is "n"? The debug information will tell you whether it is in a register or in memory, but sometimes the value will be only part of a combined expression saved somewhere. For instance:
Code:
void *memset(void *pv, int x, size_t c) {
  char *p = pv;
  for (size_t i = 0; i < c; i++)
    p[i] = x;
  return pv;
}

It is highly likely that "p" will not be saved independent of "pv", and "i" will never be saved, but instead only "p + i". Which is a transformation the compiler can perform if it notices that it can decrement "c" for the loop counter. And now imagine the format of the structures that tell you that.

Breakpoints usually work this way: First the debugger determines the code address of the breakpoint target. Then it decides whether to use a hardware breakpoint or a software one. Hardware means it tells the OS to set the debug registers. Software means, the debugger saves the code at the target, then overwrites it with the breakpoint instruction. Then the target process is continued. When the BP hits, the target process is stopped and the debugger can examine the situation. If you want to continue from there, the BP is usually deactivated and the program is continued for a single step. Then, the breakpoint is reactivated.

Also, one detail: x86's breakpoint instruction is "int3" (CC), not "int 3" (CD 03).

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 119 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group