我想访问虚拟页面到某个进程的物理映射。操作系统是Solaris,可以从https://stackoverflow.com/users/760807/metallicpriest
询问确切的版本我希望获得如下列表:
virt_addrs phys_addrs
0x000000-0x001000 0x537000-0x538000
0x001000-0x002000 0x832000-0x833000
...
cpu是x86或x86_64。页面大小为4K;交换已关闭。我对FS(可执行映像)支持并且进程未使用的页面不感兴趣。
答案 0 :(得分:2)
您可以使用pmap和内核调试器(mdb -k)来实现这一目标。
Pmap将首先显示进程使用的(虚拟)内存区域,然后在mdb下获取进程结构(pid2proc)并显示其p_as字段(进程地址空间)。将该值作为参数传递时,vtop命令可以显示进程虚拟到物理地址映射。
例如:
$ pmap -s 609
609: /usr/lib/utmpd
Address Bytes Pgsz Mode Mapped File
08046000 8K 4K rw--- [ stack ]
08050000 12K 4K r-x-- /usr/lib/utmpd
08063000 4K 4K rw--- /usr/lib/utmpd
...
# mdb -k
Loading modules: [ unix genunix specfs dtrace mac cpu.generic cpu_ms.AuthenticAMD.15 uppc pcplusmp scsi_vhci zfs ip hook neti arp usba sd sockfs stmf stmf_sbd s1394 fctl lofs random nfs sppp crypto cpc fcip ptm ufs logindmux ipc ]
> 0t609::pid2proc | ::print proc_t p_as
p_as = 0xffffff018cf38b00
> 08046000::vtop -a 0xffffff018cf38b00
virtual 8046000 mapped to physical a5c16000
> 8047000::vtop -a 0xffffff018cf38b00
virtual 8047000 mapped to physical a1267000
...
答案 1 :(得分:1)
有点晚了,但在Solaris上根本不难做到。只需使用libkvm
。
这适用于Solaris 11:
/* needed to get latest /proc structures */
#define _STRUCTURED_PROC 1
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <kvm.h>
#include <sys/proc.h>
#include <sys/procfs.h>
void printVirtToPhysMappings( kvm_t *kvm, const char *pidStr );
int main( int argc, char **argv )
{
kvm_t *kvm = kvm_open( NULL, NULL, NULL, O_RDONLY, argv[ 0 ] );
for ( int ii = 1; ii < argc; ii++ )
{
printVirtToPhysMappings( kvm, argv[ ii ] );
}
kvm_close( kvm );
return( 0 );
}
void printVirtToPhysMappings( kvm_t *kvm, const char *pidStr )
{
char mapFile[ PATH_MAX ];
struct stat sb;
sprintf( mapFile, "/proc/%s/xmap", pidStr );
pid_t pid = strtol( pidStr, NULL, 0 );
struct proc *procPtr = kvm_getproc( kvm, pid );
int mapFD = open( mapFile, O_RDONLY );
fstat( mapFD, &sb );
size_t numMaps = sb.st_size / sizeof( prxmap_t );
prxmap_t mapEntries[ numMaps ];
pread( mapFD, mapEntries, sb.st_size, 0UL );
for ( size_t ii = 0; ii < numMaps; ii++ )
{
/* use the actual page size - page sizes can vary */
size_t pageSize = mapEntries[ ii ].pr_hatpagesize;
/* if page size is 0, page isn't mapped - set default
page size so we emit the output anyway */
if ( 0 == pageSize ) pageSize = 4096;
size_t numPages = mapEntries[ ii ].pr_size / pageSize;
for ( size_t jj = 0; jj < numPages; jj++ )
{
uintptr_t virtAddr = mapEntries[ ii ].pr_vaddr + jj * pageSize;
/* kvm_physaddr() is an undocumented feature of libkvm */
void *physAddr = ( void * ) kvm_physaddr( kvm, procPtr->p_as, virtAddr );
printf( "virtAddr: %p, page size: %ld, physAddr: %p\n", virtAddr, pageSize, physAddr );
}
}
close( mapFD );
}
你需要成为root用户才能运行它,而且根本没有错误检查。给它一个坏的PID,它可能是SEGV。