2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
16 #include "stackframe.h"
17 #include "debugtools.h"
18 #include "wine/exception.h"
20 DEFAULT_DEBUG_CHANNEL(snoop);
22 static WINE_EXCEPTION_FILTER(page_fault)
24 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
25 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
26 return EXCEPTION_EXECUTE_HANDLER;
27 return EXCEPTION_CONTINUE_SEARCH;
30 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
34 extern void WINAPI SNOOP_Entry();
35 extern void WINAPI SNOOP_Return();
37 #ifdef NEED_UNDERSCORE_PREFIX
45 typedef struct tagSNOOP_FUN {
47 BYTE lcall; /* 0xe8 call snoopentry (relative) */
48 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
51 DWORD snoopentry; /* SNOOP_Entry relative */
58 typedef struct tagSNOOP_DLL {
64 struct tagSNOOP_DLL *next;
66 typedef struct tagSNOOP_RETURNENTRY {
68 BYTE lcall; /* 0xe8 call snoopret relative*/
69 /* NOTE: If you move snoopret OR origreturn fix the relative offset
72 DWORD snoopret; /* SNOOP_Ret relative */
78 DWORD *args; /* saved args across a stdcall */
81 typedef struct tagSNOOP_RETURNENTRIES {
82 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
83 struct tagSNOOP_RETURNENTRIES *next;
84 } SNOOP_RETURNENTRIES;
88 static SNOOP_DLL *firstdll = NULL;
89 static SNOOP_RETURNENTRIES *firstrets = NULL;
91 /***********************************************************************
92 * SNOOP_ShowDebugmsgSnoop
94 * Simple function to decide if a particular debugging message is
97 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
99 if(debug_snoop_excludelist || debug_snoop_includelist) {
102 int len, len2, itemlen, show;
104 if(debug_snoop_excludelist) {
106 listitem = debug_snoop_excludelist;
109 listitem = debug_snoop_includelist;
113 sprintf(buf, "%s.%d", dll, ord);
115 for(; *listitem; listitem++) {
116 itemlen = strlen(*listitem);
117 if((itemlen == len && !strncasecmp(*listitem, buf, len)) ||
118 (itemlen == len2 && !strncasecmp(*listitem, buf, len2)) ||
119 !strcasecmp(*listitem, fname)) {
130 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD ordbase,DWORD nrofordinals) {
131 SNOOP_DLL **dll = &(firstdll);
134 if (!TRACE_ON(snoop)) return;
136 if ((*dll)->hmod == hmod)
137 return; /* already registered */
138 dll = &((*dll)->next);
140 *dll = (SNOOP_DLL*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
143 (*dll)->ordbase = ordbase;
144 (*dll)->nrofordinals = nrofordinals;
145 (*dll)->name = HEAP_strdupA(GetProcessHeap(),0,name);
146 if ((s=strrchr((*dll)->name,'.')))
148 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
149 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
151 HeapFree(GetProcessHeap(),0,*dll);
152 FIXME("out of memory\n");
158 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
159 SNOOP_DLL *dll = firstdll;
162 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
164 if (!TRACE_ON(snoop)) return origfun;
165 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
167 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
168 /* 0x42 is special ELF loader tag */
169 if ((pe_seg[j].VirtualAddress==0x42) ||
170 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
171 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
172 pe_seg[j].SizeOfRawData
176 /* If we looked through all sections (and didn't find one)
177 * or if the sectionname contains "data", we return the
178 * original function since it is most likely a datareference.
180 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
181 (strstr(pe_seg[j].Name,"data")) ||
182 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
187 if (hmod == dll->hmod)
191 if (!dll) /* probably internal */
193 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
195 assert(ordinal < dll->nrofordinals);
196 fun = dll->funs+ordinal;
197 if (!fun->name) fun->name = HEAP_strdupA(GetProcessHeap(),0,name);
199 /* NOTE: origreturn struct member MUST come directly after snoopentry */
200 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
201 fun->origfun = origfun;
203 return (FARPROC)&(fun->lcall);
207 SNOOP_PrintArg(DWORD x) {
208 static char buf[200];
210 char * volatile ret=0;
212 if ( !HIWORD(x) ) { /* trivial reject to avoid faults */
213 sprintf(buf,"%08lx",x);
221 if (s[i]<0x20) {nostring=1;break;}
222 if (s[i]>=0x80) {nostring=1;break;}
227 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_an((LPSTR)x,sizeof(buf)-10));
232 __EXCEPT(page_fault){}
241 if (s[i]<0x20) {nostring=1;break;}
242 if (s[i]>0x100) {nostring=1;break;}
247 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_wn((LPWSTR)x,sizeof(buf)-10));
252 __EXCEPT(page_fault){}
256 sprintf(buf,"%08lx",x);
260 #define CALLER1REF (*(DWORD*)context->Esp)
262 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
264 DWORD ordinal=0,entry = context->Eip - 5;
265 SNOOP_DLL *dll = firstdll;
266 SNOOP_FUN *fun = NULL;
267 SNOOP_RETURNENTRIES **rets = &firstrets;
268 SNOOP_RETURNENTRY *ret;
272 if ( ((char*)entry>=(char*)dll->funs) &&
273 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
275 fun = (SNOOP_FUN*)entry;
276 ordinal = fun-dll->funs;
282 FIXME("entrypoint 0x%08lx not found\n",entry);
285 /* guess cdecl ... */
286 if (fun->nrofargs<0) {
287 /* Typical cdecl return frame is:
289 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
290 * (after that 81 C2 xx xx xx xx)
292 LPBYTE reteip = (LPBYTE)CALLER1REF;
295 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
296 fun->nrofargs=reteip[2]/4;
302 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
303 if (!(*rets)->entry[i].origreturn)
305 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
307 rets = &((*rets)->next);
310 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
311 memset(*rets,0,4096);
312 i = 0; /* entry 0 is free */
314 ret = &((*rets)->entry[i]);
316 /* NOTE: origreturn struct member MUST come directly after snoopret */
317 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
318 ret->origreturn = (FARPROC)CALLER1REF;
319 CALLER1REF = (DWORD)&ret->lcall;
322 ret->ordinal = ordinal;
323 ret->origESP = context->Esp;
325 context->Eip = (DWORD)fun->origfun;
327 DPRINTF("%08lx:CALL %s.%ld: %s(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal,fun->name);
328 if (fun->nrofargs>0) {
329 max = fun->nrofargs; if (max>16) max=16;
331 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
332 if (max!=fun->nrofargs)
334 } else if (fun->nrofargs<0) {
335 DPRINTF("<unknown, check return>");
336 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD));
337 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
339 DPRINTF(") ret=%08lx\n",(DWORD)ret->origreturn);
343 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
345 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
347 /* We haven't found out the nrofargs yet. If we called a cdecl
348 * function it is too late anyway and we can just set '0' (which
349 * will be the difference between orig and current ESP
350 * If stdcall -> everything ok.
352 if (ret->dll->funs[ret->ordinal].nrofargs<0)
353 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
354 context->Eip = (DWORD)ret->origreturn;
358 DPRINTF("%08lx:RET %s.%ld: %s(",
359 GetCurrentThreadId(),
360 ret->dll->name,ret->dll->ordbase+ret->ordinal,ret->dll->funs[ret->ordinal].name);
361 max = ret->dll->funs[ret->ordinal].nrofargs;
365 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
366 DPRINTF(") retval = %08lx ret=%08lx\n",
367 context->Eax,(DWORD)ret->origreturn );
368 HeapFree(GetProcessHeap(),0,ret->args);
371 DPRINTF("%08lx:RET %s.%ld: %s() retval = %08lx ret=%08lx tid=%08lx\n",
372 GetCurrentThreadId(),
373 ret->dll->name,ret->dll->ordbase+ret->ordinal,ret->dll->funs[ret->ordinal].name,
374 context->Eax, (DWORD)ret->origreturn, GetCurrentThreadId());
375 ret->origreturn = NULL; /* mark as empty */
378 /* assembly wrappers that save the context */
379 __ASM_GLOBAL_FUNC( SNOOP_Entry,
380 "call " __ASM_NAME("CALL32_Regs") "\n\t"
381 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
382 __ASM_GLOBAL_FUNC( SNOOP_Return,
383 "call " __ASM_NAME("CALL32_Regs") "\n\t"
384 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
386 #else /* !__i386__ */
387 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
388 if (!TRACE_ON(snoop)) return;
389 FIXME("snooping works only on i386 for now.\n");
392 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
395 #endif /* !__i386__ */