2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
14 #include "builtin32.h"
18 #include "selectors.h"
19 #include "stackframe.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(snoop);
24 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
28 extern void WINAPI SNOOP_Entry();
29 extern void WINAPI SNOOP_Return();
31 #ifdef NEED_UNDERSCORE_PREFIX
39 typedef struct tagSNOOP_FUN {
41 BYTE lcall; /* 0xe8 call snoopentry (relative) */
42 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
45 DWORD snoopentry; /* SNOOP_Entry relative */
52 typedef struct tagSNOOP_DLL {
57 struct tagSNOOP_DLL *next;
59 typedef struct tagSNOOP_RETURNENTRY {
61 BYTE lcall; /* 0xe8 call snoopret relative*/
62 /* NOTE: If you move snoopret OR origreturn fix the relative offset
65 DWORD snoopret; /* SNOOP_Ret relative */
71 DWORD *args; /* saved args across a stdcall */
74 typedef struct tagSNOOP_RETURNENTRIES {
75 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
76 struct tagSNOOP_RETURNENTRIES *next;
77 } SNOOP_RETURNENTRIES;
81 static SNOOP_DLL *firstdll = NULL;
82 static SNOOP_RETURNENTRIES *firstrets = NULL;
84 /***********************************************************************
85 * SNOOP_ShowDebugmsgSnoop
87 * Simple function to decide if a particular debugging message is
90 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
92 if(debug_snoop_excludelist || debug_snoop_includelist) {
95 int len, len2, itemlen, show;
97 if(debug_snoop_excludelist) {
99 listitem = debug_snoop_excludelist;
102 listitem = debug_snoop_includelist;
106 sprintf(buf, "%s.%d", dll, ord);
108 for(; *listitem; listitem++) {
109 itemlen = strlen(*listitem);
110 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
111 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
112 !strcmp(*listitem, fname)) {
123 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
124 SNOOP_DLL **dll = &(firstdll);
127 if (!TRACE_ON(snoop)) return;
129 if ((*dll)->hmod == hmod)
130 return; /* already registered */
131 dll = &((*dll)->next);
133 *dll = (SNOOP_DLL*)HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
136 (*dll)->nrofordinals = nrofordinals;
137 (*dll)->name = HEAP_strdupA(SystemHeap,0,name);
138 if ((s=strrchr((*dll)->name,'.')))
140 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
141 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
143 HeapFree(SystemHeap,0,*dll);
144 FIXME("out of memory\n");
150 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
151 SNOOP_DLL *dll = firstdll;
154 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
156 if (!TRACE_ON(snoop)) return origfun;
157 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
159 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
160 /* 0x42 is special ELF loader tag */
161 if ((pe_seg[j].VirtualAddress==0x42) ||
162 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
163 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
164 pe_seg[j].SizeOfRawData
168 /* If we looked through all sections (and didn't find one)
169 * or if the sectionname contains "data", we return the
170 * original function since it is most likely a datareference.
172 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
173 (strstr(pe_seg[j].Name,"data")) ||
174 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
179 if (hmod == dll->hmod)
183 if (!dll) /* probably internal */
185 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
187 assert(ordinal<dll->nrofordinals);
188 fun = dll->funs+ordinal;
189 if (!fun->name) fun->name = HEAP_strdupA(SystemHeap,0,name);
191 /* NOTE: origreturn struct member MUST come directly after snoopentry */
192 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
193 fun->origfun = origfun;
195 return (FARPROC)&(fun->lcall);
199 SNOOP_PrintArg(DWORD x) {
200 static char buf[200];
202 MEMORY_BASIC_INFORMATION mbi;
205 !VirtualQuery((LPVOID)x,&mbi,sizeof(mbi)) ||
208 sprintf(buf,"%08lx",x);
211 if (!IsBadStringPtrA((LPSTR)x,80)) {
216 if (s[i]<0x20) {nostring=1;break;}
217 if (s[i]>=0x80) {nostring=1;break;}
222 wsnprintfA(buf,sizeof(buf),"%08lx %s",x,debugstr_an((LPSTR)x,sizeof(buf)-10));
227 if (!IsBadStringPtrW((LPWSTR)x,80)) {
232 if (s[i]<0x20) {nostring=1;break;}
233 if (s[i]>0x100) {nostring=1;break;}
238 wsnprintfA(buf,sizeof(buf),"%08lx %s",x,debugstr_wn((LPWSTR)x,sizeof(buf)-10));
243 sprintf(buf,"%08lx",x);
247 #define CALLER1REF (*(DWORD*)ESP_reg(context))
249 void WINAPI SNOOP_DoEntry( CONTEXT86 *context );
250 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Entry, SNOOP_DoEntry );
251 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
253 DWORD ordinal=0,entry = EIP_reg(context)-5;
254 SNOOP_DLL *dll = firstdll;
255 SNOOP_FUN *fun = NULL;
256 SNOOP_RETURNENTRIES **rets = &firstrets;
257 SNOOP_RETURNENTRY *ret;
261 if ( ((char*)entry>=(char*)dll->funs) &&
262 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
264 fun = (SNOOP_FUN*)entry;
265 ordinal = fun-dll->funs;
271 FIXME("entrypoint 0x%08lx not found\n",entry);
274 /* guess cdecl ... */
275 if (fun->nrofargs<0) {
276 /* Typical cdecl return frame is:
278 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
279 * (after that 81 C2 xx xx xx xx)
281 LPBYTE reteip = (LPBYTE)CALLER1REF;
284 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
285 fun->nrofargs=reteip[2]/4;
291 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
292 if (!(*rets)->entry[i].origreturn)
294 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
296 rets = &((*rets)->next);
299 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
300 memset(*rets,0,4096);
301 i = 0; /* entry 0 is free */
303 ret = &((*rets)->entry[i]);
305 /* NOTE: origreturn struct member MUST come directly after snoopret */
306 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
307 ret->origreturn = (FARPROC)CALLER1REF;
308 CALLER1REF = (DWORD)&ret->lcall;
311 ret->ordinal = ordinal;
312 ret->origESP = ESP_reg(context);
314 EIP_reg(context)= (DWORD)fun->origfun;
316 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
317 if (fun->nrofargs>0) {
318 max = fun->nrofargs; if (max>16) max=16;
320 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+4+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
321 if (max!=fun->nrofargs)
323 } else if (fun->nrofargs<0) {
324 DPRINTF("<unknown, check return>");
325 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(DWORD));
326 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+4),sizeof(DWORD)*16);
328 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
331 void WINAPI SNOOP_DoReturn( CONTEXT86 *context );
332 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Return, SNOOP_DoReturn );
333 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
335 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(EIP_reg(context)-5);
337 /* We haven't found out the nrofargs yet. If we called a cdecl
338 * function it is too late anyway and we can just set '0' (which
339 * will be the difference between orig and current ESP
340 * If stdcall -> everything ok.
342 if (ret->dll->funs[ret->ordinal].nrofargs<0)
343 ret->dll->funs[ret->ordinal].nrofargs=(ESP_reg(context)-ret->origESP-4)/4;
344 EIP_reg(context) = (DWORD)ret->origreturn;
348 DPRINTF("Ret %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
349 max = ret->dll->funs[ret->ordinal].nrofargs;
353 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
354 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
355 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
357 HeapFree(SystemHeap,0,ret->args);
360 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
361 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
362 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
364 ret->origreturn = NULL; /* mark as empty */
366 #else /* !__i386__ */
367 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
368 FIXME("snooping works only on i386 for now.\n");
372 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
375 #endif /* !__i386__ */