标题: 检查当前CPU是否支持Intel Processor Trace (Intel PT)
创建: 2024-04-12 14:48 更新: 2024-04-18 15:33 链接: https://scz.617.cn/unix/202404121448.txt
据云海说,Intel PT没有虚拟化,VMware Guest中没法用这个。参看:
Vmware workstation support intel processor tracing feature - [2016-05-31] https://communities.vmware.com/t5/VMware-Workstation-Pro/Vmware-workstation-support-intel-processor-tracing-feature/td-p/2742688
Does VMware Workstation Pro 15 support Intel Processor Tracing (Intel PT) - [2018-10-20] https://communities.vmware.com/t5/VMware-Workstation-Pro/Does-VMware-Workstation-Pro-15-support-Intel-Processor-Tracing/td-p/511703
检查vmware.log,包含:
Host VT-x Capabilities
Processor trace in VMX yes
Guest VT-x Capabilities
Processor trace in VMX no
即是说Host支持PT,Guest不支持。有人提过修改.vmx,增加:
pt.enable = "TRUE" pt.available = "TRUE"
实测无效。
虽然Guest不支持PT,但后续仍用Guest演示相关命令,未做特别说明时,均默认在 Guest中。
$ perf record -e intel_pt//ub date
event syntax error: 'intel_pt//ub' ___ Cannot find PMU `intel_pt'. Missing kernel support? Run 'perf list' for a list of valid events
若内核不旧,这种报错一般是因为CPU不支持PT所致。"perf list"未见"intel_pt"。
$ ls -la /sys/bus/event_source/devices/intel_pt/ ls: cannot access '/sys/bus/event_source/devices/intel_pt/': No such file or directory
$ ls -la /sys/devices/intel_pt/ ls: cannot access '/sys/devices/intel_pt/': No such file or directory
同样表示当前系统不支持PT。但这些办法都是表象,绝对可靠的办法是用CPUID指令, 参看:
《Intel 64 and IA-32 Architectures Software Developer Manuals》 https://cdrdv2-public.intel.com/812392/325462-sdm-vol-1-2abcd-3abcd-4.pdf
Table 3-8. Information Returned by CPUID Instruction 817/5082 EAX=0x7 827/5082 EAX=0x14
33.2.8.2 IA32_RTIT_CTL MSR (0x570) 4187/5082
33.3.1 Detection of Intel Processor Trace and Capability Enumeration Table 33-11. CPUID Leaf 14H Enumeration of Intel Processor Trace Capabilities 4196/5082
Processor support for Intel Processor Trace is indicated by CPUID.(EAX=07H,ECX=0H):EBX[bit 25] = 1. CPUID function 14H is dedicated to enumerate the resource and capability of processors that report CPUID.(EAX=07H,ECX=0H):EBX[bit 25] = 1.
如下代码用内联汇编执行cpuid指令并检查返回值,以此判断当前CPU是否支持PT:
/ * gcc -Wall -pipe -O3 -s -o check_pt check_pt.c /
include
define BIT(x) (1ULL << (x))
int main ( int argc, char * argv[] ) { int a = 0x7, b, c = 0, d;
asm
(
"cpuid"
: "=a" (a), "=b" (b), "=c" (c), "=d" (d)
: "0" (a), "2" (c)
);
printf( "ebx=%#x\n", b );
if ( !( b & BIT(25) ) )
{
printf( "Intel PT not supported\n" );
}
else
{
printf( "Intel PT supported\n" );
}
return 0;
} / end of main /
$ ./check_pt ebx=0x9c27ab Intel PT not supported
此法可辅助检测是否位于VMware Guest中: 若支持Intel PT,肯定不在VMware Guest 中;若不支持Intel PT,有可能在VMware Guest中,有可能只是CPU太老。
Ubuntu、Debian有cpuid命令,可在bash中直接检查cpuid结果:
$ apt install cpuid
$ cpuid -1 -r -l 7 CPU: 0x00000007 0x00: eax=0x00000000 ebx=0x009c27ab ecx=0x00000000 edx=0xbc000400
-1表示只检查第1个核,-r表示显示寄存器值,"-l 7"相当于指定eax=7
检查ebx的bit25(从0开始计数)是否为1,为1表示支持Intel PT。具体到本例,检 查ebx最高字节bit1,为0表示不支持Intel PT。
cpuid命令已对返回的寄存器值做了解码,无需手工检查bit:
$ cpuid -1 | grep "Intel processor trace" Intel processor trace = false
可不用cpuid命令,有其他原生方案:
$ grep intel_pt /proc/cpuinfo > /dev/null;echo $? 1
$ lscpu | grep intel_pt > /dev/null;echo $? 1
为0表示grep命中,支持PT;为1表示grep无命中,不支持PT。
假设CPU支持PT,可通过IA32_RTIT_CTL MSR (0x570) bit0启用、禁用PT,有必要检 查之:
$ rdmsr -x0 -a 0x570
$ rdmsr -x0 -p 0 0x570 0000000000000000
-a表示所有CPU,"-p n"指定第n个CPU(从0计),bit0为0,表示禁用PT。
总之,有许多大同小异的办法检查当前CPU是否支持PT、是否启用PT。即使CPU支持, 也可能出现内核太旧而不支持PT,这是另一回事了,优先检查CPU是否支持。前述手 段也适用于WSL1,可藉此检查Host CPU。
Intel PT是一种强大的诊断、调试手段,本想用之,奈何家境贫寒,主力Linux均在 VMware Guest中,遗憾。