Why GDB can't work properly when debugging real mode codes?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by Solar »

I cannot talk about driver development, but on the application level...
rdos wrote:
Solar wrote:Another part of the skill involved. Your log statements should not have side effects, and your code should be stable enough not to react to stuff like compiler-introduced reorderings (a.k.a. "if I enable optimization my code breaks").
If the code debugged depends on timing, any insertion of code of whatever triviality might change the behavior. Printf is just a major distrurber because it takes time to execute.
...code that "depends on timing" like that is broken, period. 8)
It is a little hard to do unit testing for hardware-device-drivers meant for an OS.
That's what I said: Choose the appropriate tool for the task at hand. Sometimes it's GDB, sometimes printf(), and most of the time (but not always) you should've written a unit test in the first place. 8)
Every good solution is obvious once you've found it.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by AJ »

Solar wrote:...code that "depends on timing" like that is broken, period. 8)
What if I'm trying to write some complex control code for my atomic clock? :P
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why GDB can't work properly when debugging real mode cod

Post by rdos »

AJ wrote:
Solar wrote:...code that "depends on timing" like that is broken, period. 8)
What if I'm trying to write some complex control code for my atomic clock? :P
Yes, or what about if the AHCI HBA posts ready-requests to the OS before the data is physically transfered? Or if the device-driver uses the incorrect algorithm to know when data is available? Most hardware-device communication that is layed-out in physical memory using DMA busmasters is like this, and this is common in modern NICs / USBs and disc implementations. A simple printf could be the difference between working / not-working.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by Solar »

Solar wrote:I cannot talk about driver development, but on the application level...
Every good solution is obvious once you've found it.
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why GDB can't work properly when debugging real mode cod

Post by rdos »

Solar wrote:
Solar wrote:I cannot talk about driver development, but on the application level...
:mrgreen:

Interestingly, I've validated our terminal applications on RDOS at least 5 years prior to the first installation that actually runs RDOS itself. I did this because I didn't like the idea of "printf"-debugging, as this was the only means of debugging on V25-based hardware. And porting the terminal to Windows didn't work very well as Windows lacked critical functionality (exact waits and decent thread-scheduling policies). 8)
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Re: Why GDB can't work properly when debugging real mode cod

Post by harvey »

I'm eager for a kernel debugger, because in the booting time, especially in real mode, I haven't finished a log function like printk.

Your replies really open my mind.
Last edited by harvey on Fri Dec 09, 2011 4:25 am, edited 1 time in total.
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
SDS
Member
Member
Posts: 64
Joined: Fri Oct 23, 2009 8:45 am
Location: Cambridge, UK

Re: Why GDB can't work properly when debugging real mode cod

Post by SDS »

rdos wrote:If the code debugged depends on timing, any insertion of code of whatever triviality might change the behavior. Printf is just a major distrurber because it takes time to execute.
To be honest, the worst type of these bugs is where different threads are interacting with each other (race conditions etc.). Both printf debugging, and single stepping threads will move/hide these bugs. As may just running the code again.

That said, both printf and single-step debugging are essential in different circumstances.

Most of the code I write is computational chemistry code, and often the place where a bug shows up (where you notice a numerical discrepancy) is very different to what caused a slight numerical slip, which may be very deeply hidden far from any data output. I find stepwise debugging here next to useless, as generally all of the sections of the code seem to be doing something reasonable - and even isolating which bit to step through is not easy in 70000 odd lines of code. Careful use of printf statements to check consistency between elements of input and output of different sections is far easier to isolate the problem to a section - and then the bug is often obvious to inspection, not needing single-stepping line by line. Lines of code do tend to do what they say they will, after all...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by Combuster »

rdos wrote:A simple printf could be the difference between working / not-working.
Solar wrote:code that "depends on timing" like that is broken, period.
Point in case: If your driver can't even handle the latency of a print function, it can't handle the latency of several interrupts firing simultaneously either.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why GDB can't work properly when debugging real mode cod

Post by rdos »

Combuster wrote:
rdos wrote:A simple printf could be the difference between working / not-working.
Solar wrote:code that "depends on timing" like that is broken, period.
Point in case: If your driver can't even handle the latency of a print function, it can't handle the latency of several interrupts firing simultaneously either.
That was not the point. By inserting the printf-code, the code would work, and when it is removed it would fail.

OTOH, I could envision a NIC-driver that would do the opposite under heavy traffic. With too much logging, buffers would overflow, and the NIC-driver would fail with loggings but not without.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Why GDB can't work properly when debugging real mode cod

Post by gerryg400 »

RDOS, of course it's okay (sometimes) to use a kernel debugger to step your code. In fact you need lots of different debugging techniques. I think you mentioned that sometimes you use segmentation to debug software. The point of my original reply was that it's not necessary to use GDB to debug. And I stand by that. Especially with something simple like boot code that generally runs a linear sequence of code with no timing issues.
If a trainstation is where trains stop, what is a workstation ?
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why GDB can't work properly when debugging real mode cod

Post by rdos »

For boot code, I'd use an emulator. Back in the days when I did this, I had my own processor-emulator. Today we have emulators that are useful, so there is no need in writing one yourself.
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Re: Why GDB can't work properly when debugging real mode cod

Post by harvey »

Good news!!! My codes have finished the real mode work and enabled the paging. And I find since the OS transfers from the real mode to protected mode with paging enabled, the GDB has worked properly as I expected. Especially, "print xxx" and "backtrace" commands print correct results when debugging C codes.
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by Combuster »

berkus wrote:What is the reason for calling them codes? Is it nowadays popular on the internets?
Typical for a speaker that hasn't seen and used the word "code" often enough to realize its uncountable (and therefore has a 90% chance that the person in question lacks the experience to post here. This thread being no exception)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
SDS
Member
Member
Posts: 64
Joined: Fri Oct 23, 2009 8:45 am
Location: Cambridge, UK

Re: Why GDB can't work properly when debugging real mode cod

Post by SDS »

This also appears to be in common usage for computational science (at least in the department here). For example;

I have a code which can calculate ...

... research group has several codes to calculate x and y.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Why GDB can't work properly when debugging real mode cod

Post by Combuster »

Show me teh codez, how professional.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply