2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
13 #include "builtin32.h"
16 #include "selectors.h"
17 #include "stackframe.h"
23 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
25 #ifdef NEED_UNDERSCORE_PREFIX
31 /* Well, not exactly extern since they are in the same file (in the lines
32 * below). But the C Compiler doesn't see them there, so we have to help a bit.
34 extern void SNOOP_Return();
35 extern void SNOOP_Entry();
36 __asm__(".align 4\n\t"
37 ".globl "PREFIX"SNOOP_Entry\n\t"
38 ".type "PREFIX"SNOOP_Entry,@function\n\t"
39 PREFIX"SNOOP_Entry:\n\t"
40 "pushl $"PREFIX"__regs_SNOOP_Entry\n\t"
41 "pushl $"PREFIX"CALL32_Regs\n\t"
44 ".globl "PREFIX"SNOOP_Return\n\t"
45 ".type "PREFIX"SNOOP_Return,@function\n\t"
46 PREFIX"SNOOP_Return:\n\t"
47 "pushl $"PREFIX"__regs_SNOOP_Return\n\t"
48 "pushl $"PREFIX"CALL32_Regs\n\t"
54 typedef struct tagSNOOP_FUN {
56 BYTE lcall; /* 0xe8 call snoopentry (relative) */
57 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
60 DWORD snoopentry; /* SNOOP_Entry relative */
67 typedef struct tagSNOOP_DLL {
72 struct tagSNOOP_DLL *next;
74 typedef struct tagSNOOP_RETURNENTRY {
76 BYTE lcall; /* 0xe8 call snoopret relative*/
77 /* NOTE: If you move snoopret OR origreturn fix the relative offset
80 DWORD snoopret; /* SNOOP_Ret relative */
86 DWORD *args; /* saved args across a stdcall */
89 typedef struct tagSNOOP_RETURNENTRIES {
90 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
91 struct tagSNOOP_RETURNENTRIES *next;
92 } SNOOP_RETURNENTRIES;
96 static SNOOP_DLL *firstdll = NULL;
97 static SNOOP_RETURNENTRIES *firstrets = NULL;
99 /***********************************************************************
100 * SNOOP_ShowDebugmsgSnoop
102 * Simple function to decide if a particular debugging message is
105 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
107 if(debug_snoop_excludelist || debug_snoop_includelist) {
110 int len, len2, itemlen, show;
112 if(debug_snoop_excludelist) {
114 listitem = debug_snoop_excludelist;
117 listitem = debug_snoop_includelist;
121 sprintf(buf, "%s.%d", dll, ord);
123 for(; *listitem; listitem++) {
124 itemlen = strlen(*listitem);
125 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
126 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
127 !strcmp(*listitem, fname)) {
138 SNOOP_RegisterDLL(HMODULE32 hmod,LPCSTR name,DWORD nrofordinals) {
139 SNOOP_DLL **dll = &(firstdll);
142 if (!TRACE_ON(snoop)) return;
144 if ((*dll)->hmod == hmod)
145 return; /* already registered */
146 dll = &((*dll)->next);
148 *dll = (SNOOP_DLL*)HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
151 (*dll)->nrofordinals = nrofordinals;
152 (*dll)->name = HEAP_strdupA(SystemHeap,0,name);
153 if ((s=strrchr((*dll)->name,'.')))
155 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
156 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
158 HeapFree(SystemHeap,0,*dll);
159 FIXME(snoop,"out of memory\n");
165 SNOOP_GetProcAddress32(HMODULE32 hmod,LPCSTR name,DWORD ordinal,FARPROC32 origfun) {
166 SNOOP_DLL *dll = firstdll;
169 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
171 if (!TRACE_ON(snoop)) return origfun;
172 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
174 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
175 /* 0x42 is special ELF loader tag */
176 if ((pe_seg[j].VirtualAddress==0x42) ||
177 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
178 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
179 pe_seg[j].SizeOfRawData
183 /* If we looked through all sections (and didn't find one)
184 * or if the sectionname contains "data", we return the
185 * original function since it is most likely a datareference.
187 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
188 (strstr(pe_seg[j].Name,"data")) ||
189 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
194 if (hmod == dll->hmod)
198 if (!dll) /* probably internal */
200 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
202 assert(ordinal<dll->nrofordinals);
203 fun = dll->funs+ordinal;
204 if (!fun->name) fun->name = HEAP_strdupA(SystemHeap,0,name);
206 /* NOTE: origreturn struct member MUST come directly after snoopentry */
207 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
208 fun->origfun = origfun;
210 return (FARPROC32)&(fun->lcall);
214 SNOOP_PrintArg(DWORD x) {
215 static char buf[200];
217 MEMORY_BASIC_INFORMATION mbi;
220 !VirtualQuery((LPVOID)x,&mbi,sizeof(mbi)) ||
223 sprintf(buf,"%08lx",x);
227 if (!IsBadStringPtr32A((LPSTR)x,80)) {
232 if (s[i]<0x20) {nostring=1;break;}
233 if (s[i]>=0x80) {nostring=1;break;}
238 sprintf(buf,"%08lx \"",x);
239 strncat(buf,(LPSTR)x,198-strlen(buf)-2);
246 if (!IsBadStringPtr32W((LPWSTR)x,80)) {
251 if (s[i]<0x20) {nostring=1;break;}
252 if (s[i]>0x100) {nostring=1;break;}
257 sprintf(buf,"%08lx L",x);
258 strcat(buf,debugstr_wn((LPWSTR)x,198-strlen(buf)-2));
263 sprintf(buf,"%08lx",x);
267 #define CALLER1REF (*(DWORD*)(ESP_reg(context)+4))
268 REGS_ENTRYPOINT(SNOOP_Entry) {
269 DWORD ordinal=0,entry = EIP_reg(context)-5;
270 SNOOP_DLL *dll = firstdll;
271 SNOOP_FUN *fun = NULL;
272 SNOOP_RETURNENTRIES **rets = &firstrets;
273 SNOOP_RETURNENTRY *ret;
277 if ( ((char*)entry>=(char*)dll->funs) &&
278 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
280 fun = (SNOOP_FUN*)entry;
281 ordinal = fun-dll->funs;
287 FIXME(snoop,"entrypoint 0x%08lx not found\n",entry);
290 /* guess cdecl ... */
291 if (fun->nrofargs<0) {
292 /* Typical cdecl return frame is:
294 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
295 * (after that 81 C2 xx xx xx xx)
297 LPBYTE reteip = (LPBYTE)CALLER1REF;
300 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
301 fun->nrofargs=reteip[2]/4;
307 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
308 if (!(*rets)->entry[i].origreturn)
310 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
312 rets = &((*rets)->next);
315 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
316 memset(*rets,0,4096);
317 i = 0; /* entry 0 is free */
319 ret = &((*rets)->entry[i]);
321 /* NOTE: origreturn struct member MUST come directly after snoopret */
322 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
323 ret->origreturn = (FARPROC32)CALLER1REF;
324 CALLER1REF = (DWORD)&ret->lcall;
327 ret->ordinal = ordinal;
328 ret->origESP = ESP_reg(context);
330 EIP_reg(context)= (DWORD)fun->origfun;
332 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
333 if (fun->nrofargs>0) {
334 max = fun->nrofargs; if (max>16) max=16;
336 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+8+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
337 if (max!=fun->nrofargs)
339 } else if (fun->nrofargs<0) {
340 DPRINTF("<unknown, check return>");
341 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(DWORD));
342 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+8),sizeof(DWORD)*16);
344 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
347 REGS_ENTRYPOINT(SNOOP_Return) {
348 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(EIP_reg(context)-5);
350 /* We haven't found out the nrofargs yet. If we called a cdecl
351 * function it is too late anyway and we can just set '0' (which
352 * will be the difference between orig and current ESP
353 * If stdcall -> everything ok.
355 if (ret->dll->funs[ret->ordinal].nrofargs<0)
356 ret->dll->funs[ret->ordinal].nrofargs=(ESP_reg(context)-ret->origESP-4)/4;
357 EIP_reg(context) = (DWORD)ret->origreturn;
361 DPRINTF("Ret %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
362 max = ret->dll->funs[ret->ordinal].nrofargs;
366 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
367 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
368 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
370 HeapFree(SystemHeap,0,ret->args);
373 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
374 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
375 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
377 ret->origreturn = NULL; /* mark as empty */
379 #else /* !__i386__ */
380 void SNOOP_RegisterDLL(HMODULE32 hmod,LPCSTR name,DWORD nrofordinals) {
381 FIXME(snoop,"snooping works only on i386 for now.\n");
385 FARPROC32 SNOOP_GetProcAddress32(HMODULE32 hmod,LPCSTR name,DWORD ordinal,FARPROC32 origfun) {
388 #endif /* !__i386__ */