如何使用在同一CPU上运行的调试器来读取CPU寄存器

如何使用在同一CPU上运行的调试器来读取CPU寄存器

本文介绍了如何使用在同一CPU上运行的调试器来读取CPU寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习关于assembly的知识时,我使用了GDB:
$ b

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

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

当我使用相同的CPU来打印寄存器时,为什么我可以看到程序使用的寄存器?不应该使用GDB(或操作系统)覆盖寄存器,只显示覆盖的寄存器?
我能想到的唯一答案是,我的CPU是双核的,其中一个核心正在使用,另一个是为程序保留的。

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.

推荐答案

操作系统维护每个执行线程的寄存器状态。在检查gdb中的寄存器时,调试器实际上是要求操作系统从保存的状态读取寄存器值。你的程序没有在那个时间点运行,它是调试器。

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. 调试器启动并获取cpu

  2. 调试器询问调试器要求操作系统放置断点

  3. 调试器要求操作系统开始执行你的程序。操作系统保存gdb寄存器状态并将控制权转交给您的程序。
  4. 您的程序命中断点。操作系统接受控制,保存程序的寄存器状态,重新加载gdb寄存器,并将cpu送回gdb。
  5. 调试器要求操作系统从保存状态读取程序的寄存器。

请注意,这种机制是多任务操作系统的正常职责的一部分,它不是特定于调试。当OS调度程序决定执行一个不同的程序时,它会保存当前状态并加载另一个程序。这被称为上下文切换,它可能每秒发生多次,给人一种错觉,即使你只有一个CPU核心,程序也能同时执行。

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.

这篇关于如何使用在同一CPU上运行的调试器来读取CPU寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:51