Debugging OS on real hardware

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
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Debugging OS on real hardware

Post by osdever »

My OS crashes on real hardware for now, but it worked fine a week ago. So, the question is: How to debug my operating system on real hardware and fix my error?
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Debugging OS on real hardware

Post by Nable »

Hard but powerful way:
1. Debug it using Bochs.
2. Implement GDB stub and debug via serial port.

Easy way: don't forget about printf-debugging. Insert printing calls around suspicious parts of code, then look where your execution has stopped.
shmx
Member
Member
Posts: 68
Joined: Sat Jan 16, 2016 10:43 am

Re: Debugging OS on real hardware

Post by shmx »

catnikita255 wrote:How to debug my operating system on real hardware and fix my error?
You can print debug information on screen or use RS232 or another port (it is harder). You also can use infinite loop (in the case of triggering a hardware reset).
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Debugging OS on real hardware

Post by BrightLight »

You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Debugging OS on real hardware

Post by osdever »

Nable wrote:Hard but powerful way:
1. Debug it using Bochs.
2. Implement GDB stub and debug via serial port.

Easy way: don't forget about printf-debugging. Insert printing calls around suspicious parts of code, then look where your execution has stopped.
My OS works fine on Bochs. I can't debug by printfs, because it crashes before it can write comething on the screen. And about serial port - I don't have serial port on my notebook. I can do it with my development machine, but I don't have any serial cables.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Debugging OS on real hardware

Post by osdever »

I am stupid. Delete this thread, please, it was one line added by me I don't know for what - accessing memory on -1.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Debugging OS on real hardware

Post by onlyonemac »

If you use a version-control system such as git, you can view the commit history and see what's been changed in the past week. Yet another reason why I've started using git as part of my regular workflow.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Debugging OS on real hardware

Post by Schol-R-LEA »

onlyonemac wrote:If you use a version-control system such as git, you can view the commit history and see what's been changed in the past week. Yet another reason why I've started using git as part of my regular workflow.
I cannot agree more. While it doesn't have to be git and Github (a lot of people dislike git, it seems, though I've never had much trouble with it), some sort of off-site version control is a must even as a hobbyist, especially in a major project like OS dev. There are enough different [CD]VCSes, and free, low-cost, or ad-supported [CD]VCS hosts, that there are very good few reasons why anyone wouldn't use one.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Debugging OS on real hardware

Post by onlyonemac »

Schol-R-LEA wrote:
onlyonemac wrote:If you use a version-control system such as git, you can view the commit history and see what's been changed in the past week. Yet another reason why I've started using git as part of my regular workflow.
I cannot agree more. While it doesn't have to be git and Github (a lot of people dislike git, it seems, though I've never had much trouble with it), some sort of off-site version control is a must even as a hobbyist, especially in a major project like OS dev. There are enough different [CD]VCSes, and free, low-cost, or ad-supported [CD]VCS hosts, that there are very good few reasons why anyone wouldn't use one.
Note also the value of a VCS even if you don't link it to an online host. Personally I use github for some things, but a lot of the time I use git "offline" (i.e. just a local repository, with no source repository to push to) to manage my own code on my hard drive.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
rdos
Member
Member
Posts: 3303
Joined: Wed Oct 01, 2008 1:55 pm

Re: Debugging OS on real hardware

Post by rdos »

Testing on real hardware should be done ASAP because emulators are not real hardware. Debugging is best done by catching serious bugs like protection fault, stack fault and double faults with handlers, and then printing register state on-screen. These handlers can be tested on an emulator. Printf-debugging is worthless.
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: Debugging OS on real hardware

Post by Combuster »

rdos wrote:Debugging is best done by (...) printing register state on-screen.
rdos wrote:Printf-debugging is worthless.
Yup, rdos is back in full glory! :mrgreen:
"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 ]
Hellbender
Member
Member
Posts: 63
Joined: Fri May 01, 2015 2:23 am
Libera.chat IRC: Hellbender

Re: Debugging OS on real hardware

Post by Hellbender »

onlyonemac wrote:If you use a version-control system such as git, you can view the commit history and see what's been changed in the past week. Yet another reason why I've started using git as part of my regular workflow.
And don't forget about bisect. Just let git tell which commit exactly caused the bug!
Hellbender OS at github.
JoeEagar
Posts: 6
Joined: Tue Mar 15, 2016 8:46 am
Libera.chat IRC: joeedh

Re: Debugging OS on real hardware

Post by JoeEagar »

rdos wrote:Testing on real hardware should be done ASAP because emulators are not real hardware. Debugging is best done by catching serious bugs like protection fault, stack fault and double faults with handlers, and then printing register state on-screen. These handlers can be tested on an emulator. Printf-debugging is worthless.
Heh, especially when said printfs alter state, either preventing bugs or triggering them. I started out printing stuff to the screen, but later switched to Bochs's debug IO port. Even that can alter behavior significantly (blah).

Btw, in addition to printing out registers in exception handlers, it can also be helpful to print out the first dozen or so bytes from the top of the stack.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Debugging OS on real hardware

Post by gerryg400 »

I must be misunderstanding printf debugging. What's wrong with it ?
If a trainstation is where trains stop, what is a workstation ?
rdos
Member
Member
Posts: 3303
Joined: Wed Oct 01, 2008 1:55 pm

Re: Debugging OS on real hardware

Post by rdos »

gerryg400 wrote:I must be misunderstanding printf debugging. What's wrong with it ?
Everything. They alter state and introduce new bugs + cannot tell you what a debugger typically can tell you. Once the basic exception handlers are in place, the next step is to integrate a remote debugger or local debugger thread so you can single step the kernel, preferably in source-code form. Before that is done, planting "int 3" in code, which triggers the exception handlers, is a good way to get the current register state.

Of course, if you are in an emulator rather than on real hardware, then the emulator might have good options for doing the above an easier way, but you still need it when you come to real hardware that isn't booting.
Post Reply