OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:15 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [solved] GDB single-step during CPU exception
PostPosted: Fri Jul 09, 2021 1:31 am 
Offline

Joined: Mon Dec 03, 2018 4:10 pm
Posts: 12
Hey all, I am writing a specialized OS for $COMPANY, and I have written remote GDB support for this project. My problem is that GDB seems to have a hard time understanding the way back into the guest stacks. We can assume amd64 for now, and I am using IST. The CPU exceptions are in CPL=3, so no privilege change is happening.

Code:
%macro CPU_EXCEPT 1
ALIGN 0x10
   push rbp
   mov rbp, QWORD [rsp+32]
   push rdx
   add rsp, 16  ;; skip rdx, rbp
   mov dx, 0xFF00 + %1
   out dx, ax
   sub rsp, 16
   pop rdx
   pop rbp
   iretq
%endmacro


As you can probably guess, I am trying to pretend the exception handler is a function call by setting rbp to the stack pointer before the exception happens. I am building my guest program with -O0 -g, which will create stack frames even in leaf functions. The OUT instruction is what I'm using to trap out to handle the exception, and is not relevant to the GDB remote debugging. The stack I'm seeing in GDB isn't completely broken, but it only goes one frame back for the most part, and sometimes it skips the function that is causing an exception. In my test cases I am just using the UD2 instruction to generate exceptions and inspecting with GDB to see what's going on.

Code:
Breakpoint 2, test_ud2 () at musl.c:21
(gdb) bt
#0  test_ud2 () at musl.c:21
#1  0x0000000000401321 in main (argc=2, argv=0x1ffcc8) at musl.c:45


Seen above is backtrace before executing UD2. There is no problems with remote GDB before the CPU exception.

What do you think I am doing wrong here? Assuming no stack smashing, is it possible to get decent backtraces in programs?


Last edited by gonzo on Fri Jul 09, 2021 3:37 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: GDB single-step during CPU exception
PostPosted: Fri Jul 09, 2021 3:36 am 
Offline

Joined: Mon Dec 03, 2018 4:10 pm
Posts: 12
For now I solved it another way, although I'm not happy with it. Remote GDB will now report the Return-RIP and Return-RSP when in an exception handler, which makes GDB happy again. It's annoying because now I need another way to report that an exception happened, possiby by sending the correct trap signal to GDB when executing the instruction, but at least the backtrace is useful again.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: 8infy, SemrushBot [Bot] and 54 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