Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 608 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How is it possible to read the CPU registers using a debugger running on the same CPU?

#1
As I was learning about assembly, I used GDB the following way:

<!-- language: none -->

gdb ./a.out (a is a compiled C script that only prints hello world)
break main
run
info registers

Why can I see the registers used by my program when I am myself using the same CPU to print the registers? Shouldn't the use of GDB (or operating system) overwrite the registers and only show me the overwritten registers?
The only answer I can think of is the fact that my CPU is dual-core and that one of the cores is being used and the other is kept for the program.
Reply

#2
The operating system maintains the state of the registers for each execution thread. When you are examining registers in gdb, the debugger is actually asking the OS to read the register value from the saved state. Your program is not running at that point in time, it's the debugger which is.

Let's assume there are no other processes on your system. Here is a simplified view of what happens:

1. Debugger launches and gets the cpu
2. Debugger asks the OS to load your program
3. Debugger asks the OS to place the breakpoint
4. Debugger asks the OS to start executing your program. The OS saves gdb register state and transfers control to your program.
5. Your program hits the breakpoint. The OS takes control, saves your program's register state, reloads gdb registers and gives cpu back to gdb.
6. Debugger asks the OS to read the program's registers from the saved state.

Note that this mechanism is part of the normal duties of a multitasking operating system, it's not specific to debugging. When the OS scheduler decides a different program should be executing, it saves the current state and loads another. This is called a context switch and it may happen many times per second, giving the illusion that programs execute simultaneously even if you only have a single cpu core.
Reply

#3
Back in the old days of single tasking OSses, the only things that could get in the way of the execution of your program were interrupts. Now, interrupt handlers have the same problem you're talking about, your program is calculating something, user presses a key - interrupt - the interrupt service routine _has_ to do some work but _must not_ modify a single register in the process. That's the main reason, the stack was invented in the first place. A usual 80x86 DOS interrupt service routine would look like this:

push ax
push cx
push dx
push bx
push si
push di
push bp
// no need to push sp
[do actual work, caller registers avaiable on stack if needed]
pop bp
pop di
pop si
pop bx
pop dx
pop cx
pop ax
iret

This was even so common, that a new instruction pair `pusha` and `popa` (for push/pop all) was created to ease this task.

In today's CPUs with address space isolation between the operation systems and applications, the CPUs provide some task states system and allow the operation system to switch tasks (interrupts may still work similar to outlined above, but can also be handled via task switching). All modern OSses use this kine of task state systems, where the CPU saves all the registers of a process while it is not being actively executed. Like [Jester](

[To see links please register here]

) already explained, `gdb` just asks the OS for this values on the process to be debugged and then print them.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through