2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
17 #include "builtin32.h"
21 #include "selectors.h"
22 #include "stackframe.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(snoop);
27 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
31 extern void WINAPI SNOOP_Entry();
32 extern void WINAPI SNOOP_Return();
34 #ifdef NEED_UNDERSCORE_PREFIX
42 typedef struct tagSNOOP_FUN {
44 BYTE lcall; /* 0xe8 call snoopentry (relative) */
45 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
48 DWORD snoopentry; /* SNOOP_Entry relative */
55 typedef struct tagSNOOP_DLL {
60 struct tagSNOOP_DLL *next;
62 typedef struct tagSNOOP_RETURNENTRY {
64 BYTE lcall; /* 0xe8 call snoopret relative*/
65 /* NOTE: If you move snoopret OR origreturn fix the relative offset
68 DWORD snoopret; /* SNOOP_Ret relative */
74 DWORD *args; /* saved args across a stdcall */
77 typedef struct tagSNOOP_RETURNENTRIES {
78 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
79 struct tagSNOOP_RETURNENTRIES *next;
80 } SNOOP_RETURNENTRIES;
84 static SNOOP_DLL *firstdll = NULL;
85 static SNOOP_RETURNENTRIES *firstrets = NULL;
87 /***********************************************************************
88 * SNOOP_ShowDebugmsgSnoop
90 * Simple function to decide if a particular debugging message is
93 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
95 if(debug_snoop_excludelist || debug_snoop_includelist) {
98 int len, len2, itemlen, show;
100 if(debug_snoop_excludelist) {
102 listitem = debug_snoop_excludelist;
105 listitem = debug_snoop_includelist;
109 sprintf(buf, "%s.%d", dll, ord);
111 for(; *listitem; listitem++) {
112 itemlen = strlen(*listitem);
113 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
114 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
115 !strcmp(*listitem, fname)) {
126 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
127 SNOOP_DLL **dll = &(firstdll);
130 if (!TRACE_ON(snoop)) return;
132 if ((*dll)->hmod == hmod)
133 return; /* already registered */
134 dll = &((*dll)->next);
136 *dll = (SNOOP_DLL*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
139 (*dll)->nrofordinals = nrofordinals;
140 (*dll)->name = HEAP_strdupA(GetProcessHeap(),0,name);
141 if ((s=strrchr((*dll)->name,'.')))
143 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
144 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
146 HeapFree(GetProcessHeap(),0,*dll);
147 FIXME("out of memory\n");
153 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
154 SNOOP_DLL *dll = firstdll;
157 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
159 if (!TRACE_ON(snoop)) return origfun;
160 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
162 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
163 /* 0x42 is special ELF loader tag */
164 if ((pe_seg[j].VirtualAddress==0x42) ||
165 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
166 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
167 pe_seg[j].SizeOfRawData
171 /* If we looked through all sections (and didn't find one)
172 * or if the sectionname contains "data", we return the
173 * original function since it is most likely a datareference.
175 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
176 (strstr(pe_seg[j].Name,"data")) ||
177 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
182 if (hmod == dll->hmod)
186 if (!dll) /* probably internal */
188 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
190 assert(ordinal<dll->nrofordinals);
191 fun = dll->funs+ordinal;
192 if (!fun->name) fun->name = HEAP_strdupA(GetProcessHeap(),0,name);
194 /* NOTE: origreturn struct member MUST come directly after snoopentry */
195 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
196 fun->origfun = origfun;
198 return (FARPROC)&(fun->lcall);
202 SNOOP_PrintArg(DWORD x) {
203 static char buf[200];
205 MEMORY_BASIC_INFORMATION mbi;
208 !VirtualQuery((LPVOID)x,&mbi,sizeof(mbi)) ||
211 sprintf(buf,"%08lx",x);
214 if (!IsBadStringPtrA((LPSTR)x,80)) {
219 if (s[i]<0x20) {nostring=1;break;}
220 if (s[i]>=0x80) {nostring=1;break;}
225 wsnprintfA(buf,sizeof(buf),"%08lx %s",x,debugstr_an((LPSTR)x,sizeof(buf)-10));
230 if (!IsBadStringPtrW((LPWSTR)x,80)) {
235 if (s[i]<0x20) {nostring=1;break;}
236 if (s[i]>0x100) {nostring=1;break;}
241 wsnprintfA(buf,sizeof(buf),"%08lx %s",x,debugstr_wn((LPWSTR)x,sizeof(buf)-10));
246 sprintf(buf,"%08lx",x);
250 #define CALLER1REF (*(DWORD*)ESP_reg(context))
252 void WINAPI SNOOP_DoEntry( CONTEXT86 *context );
253 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Entry, SNOOP_DoEntry );
254 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
256 DWORD ordinal=0,entry = EIP_reg(context)-5;
257 SNOOP_DLL *dll = firstdll;
258 SNOOP_FUN *fun = NULL;
259 SNOOP_RETURNENTRIES **rets = &firstrets;
260 SNOOP_RETURNENTRY *ret;
264 if ( ((char*)entry>=(char*)dll->funs) &&
265 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
267 fun = (SNOOP_FUN*)entry;
268 ordinal = fun-dll->funs;
274 FIXME("entrypoint 0x%08lx not found\n",entry);
277 /* guess cdecl ... */
278 if (fun->nrofargs<0) {
279 /* Typical cdecl return frame is:
281 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
282 * (after that 81 C2 xx xx xx xx)
284 LPBYTE reteip = (LPBYTE)CALLER1REF;
287 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
288 fun->nrofargs=reteip[2]/4;
294 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
295 if (!(*rets)->entry[i].origreturn)
297 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
299 rets = &((*rets)->next);
302 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
303 memset(*rets,0,4096);
304 i = 0; /* entry 0 is free */
306 ret = &((*rets)->entry[i]);
308 /* NOTE: origreturn struct member MUST come directly after snoopret */
309 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
310 ret->origreturn = (FARPROC)CALLER1REF;
311 CALLER1REF = (DWORD)&ret->lcall;
314 ret->ordinal = ordinal;
315 ret->origESP = ESP_reg(context);
317 EIP_reg(context)= (DWORD)fun->origfun;
319 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
320 if (fun->nrofargs>0) {
321 max = fun->nrofargs; if (max>16) max=16;
323 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+4+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
324 if (max!=fun->nrofargs)
326 } else if (fun->nrofargs<0) {
327 DPRINTF("<unknown, check return>");
328 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD));
329 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+4),sizeof(DWORD)*16);
331 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
334 void WINAPI SNOOP_DoReturn( CONTEXT86 *context );
335 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Return, SNOOP_DoReturn );
336 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
338 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(EIP_reg(context)-5);
340 /* We haven't found out the nrofargs yet. If we called a cdecl
341 * function it is too late anyway and we can just set '0' (which
342 * will be the difference between orig and current ESP
343 * If stdcall -> everything ok.
345 if (ret->dll->funs[ret->ordinal].nrofargs<0)
346 ret->dll->funs[ret->ordinal].nrofargs=(ESP_reg(context)-ret->origESP-4)/4;
347 EIP_reg(context) = (DWORD)ret->origreturn;
351 DPRINTF("Ret %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
352 max = ret->dll->funs[ret->ordinal].nrofargs;
356 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
357 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
358 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
360 HeapFree(GetProcessHeap(),0,ret->args);
363 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
364 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
365 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
367 ret->origreturn = NULL; /* mark as empty */
369 #else /* !__i386__ */
370 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
371 FIXME("snooping works only on i386 for now.\n");
375 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
378 #endif /* !__i386__ */