sigar/src/os/win32/peb.c

106 lines
2.4 KiB
C
Raw Normal View History

2004-06-22 06:37:04 +08:00
/*
* functions for getting info from the Process Environment Block
*/
#define UNICODE
#define _UNICODE
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_os.h"
2004-08-02 04:31:42 +08:00
#define PAGE_START 0x00020000
#define CWD_OFFSET PAGE_START + 0x0290
#define PATH_OFFSET PAGE_START + 0x0498
#define START_ADDRESS PAGE_START + 0x0498
2004-06-22 06:37:04 +08:00
static int sigar_peb_get(sigar_t *sigar, HANDLE proc, DWORD *base)
{
MEMORY_BASIC_INFORMATION mbi;
DWORD bytes;
if (!sigar->peb) {
sigar->peb = malloc(sigar->pagesize);
}
2004-08-02 04:31:42 +08:00
if (!VirtualQueryEx(proc, (char*)START_ADDRESS, &mbi, sizeof(mbi))) {
2004-06-22 06:37:04 +08:00
return GetLastError();
}
if (!ReadProcessMemory(proc, mbi.BaseAddress, sigar->peb,
sigar->pagesize, &bytes))
{
return GetLastError();
}
*base = (DWORD)mbi.BaseAddress;
return SIGAR_OK;
}
2004-08-02 04:31:42 +08:00
#define SKIP_NULL(scratch) \
if (*scratch == '\0') scratch += sizeof(WCHAR)
#define PEB_START(scratch, base, offset) \
scratch = sigar->peb + ((DWORD)offset - base)
2004-06-22 06:37:04 +08:00
2004-07-29 05:47:14 +08:00
//point scratch to next string (assumes PEB_FIRST)
#define PEB_NEXT(scratch) \
2004-08-02 04:31:42 +08:00
scratch = scratch + (wcslen((LPWSTR)scratch) + 1) * sizeof(WCHAR); \
SKIP_NULL(scratch)
2004-06-22 06:37:04 +08:00
2004-08-02 04:31:42 +08:00
int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe)
2004-06-22 06:37:04 +08:00
{
int status;
LPBYTE scratch;
DWORD base;
WCHAR buf[MAX_PATH];
if ((status = sigar_peb_get(sigar, proc, &base)) != SIGAR_OK) {
return status;
}
2004-08-02 04:31:42 +08:00
PEB_START(scratch, base, CWD_OFFSET);
2004-06-22 06:37:04 +08:00
2004-08-02 04:31:42 +08:00
wcsncpy(buf, (LPWSTR)scratch, MAX_PATH);
buf[MAX_PATH-1] = L'\0';
2004-06-22 06:37:04 +08:00
2004-08-02 04:31:42 +08:00
SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd));
PEB_START(scratch, base, PATH_OFFSET);
PEB_NEXT(scratch); //skip PATH
2004-06-22 06:37:04 +08:00
wcsncpy(buf, (LPWSTR)scratch, MAX_PATH);
buf[MAX_PATH-1] = L'\0';
2004-08-02 04:31:42 +08:00
SIGAR_W2A(buf, procexe->name, sizeof(procexe->name));
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
2004-07-29 05:47:14 +08:00
int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline)
{
int status;
LPBYTE scratch;
DWORD base;
2004-08-03 10:45:26 +08:00
WCHAR buf[SIGAR_CMDLINE_MAX];
2004-07-29 05:47:14 +08:00
if ((status = sigar_peb_get(sigar, proc, &base)) != SIGAR_OK) {
return status;
}
2004-08-02 04:31:42 +08:00
PEB_START(scratch, base, PATH_OFFSET);
2004-07-29 05:47:14 +08:00
2004-08-02 04:31:42 +08:00
PEB_NEXT(scratch); //skip PATH
2004-07-29 05:47:14 +08:00
2004-08-02 04:31:42 +08:00
PEB_NEXT(scratch); //skip exe name
2004-07-29 05:47:14 +08:00
2004-08-03 10:45:26 +08:00
wcsncpy(buf, (LPWSTR)scratch, SIGAR_CMDLINE_MAX);
buf[SIGAR_CMDLINE_MAX-1] = L'\0';
2004-07-29 05:47:14 +08:00
2004-08-03 10:45:26 +08:00
SIGAR_W2A(buf, cmdline, SIGAR_CMDLINE_MAX);
2004-07-29 05:47:14 +08:00
return SIGAR_OK;
}