Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

x86 - 使用GDB读取MSR(Using GDB to read MSRs)

Is there some way to read the x86-64 model-specific registers, specifically IA32_FS_BASE and IA32_GS_BASE, while debugging a program using GDB?

(在使用GDB调试程序时,有什么方法可以读取特定于x86-64模型的寄存器,尤其是IA32_FS_BASE和IA32_GS_BASE?)

Less preferable would be a solution using a dynamic instrumentation package like Intel's Pintool, but it would be appreciated all the same.

(使用像Intel的Pintool这样的动态工具包的解决方案是不太可取的,但是同样可以理解。)

  ask by shigoel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The x86 MSRs can be read with the RDMSR instruction, which is privileged (Ring 0) .

(可以使用特权(Ring 0)RDMSR指令读取x86 MSR 。)

In Linux there are system calls that a user thread can invoke to read FS_BASE and GS_BASE.

(在Linux中,用户线程可以调用一些系统调用来读取FS_BASE和GS_BASE。)

There are no library wrappers for them, so you have to write code to invoke them yourself.

(它们没有库包装器,因此您必须编写代码以自己调用它们。)

Here's one way to do it in C++, you add these global function definitions to your program:

(这是在C ++中执行此操作的一种方法,可以将这些全局函数定义添加到程序中:)

#include <cstdint>
#include <asm/prctl.h>
#include <sys/syscall.h>
namespace x86 {
    uint64_t fs_base() {
        uint64_t fs_base;
        syscall(SYS_arch_prctl,ARCH_GET_FS,&fs_base);
        return fs_base;
    }
    uint64_t gs_base() {
        uint64_t gs_base;
        syscall(SYS_arch_prctl,ARCH_GET_GS,&gs_base);
        return gs_base;
    }
}

Now you can call these functions from gdb and print their return value in hex, like this:

(现在,您可以从gdb调用这些函数,并以十六进制打印其返回值,如下所示:)

(gdb) p/x x86::fs_base()
$1 = 0x7ffff5e01780
(gdb) p/x x86::gs_base()
$2 = 0x0
(gdb)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...