2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
13 #include "builtin32.h"
17 #include "selectors.h"
18 #include "stackframe.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(snoop)
24 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
26 extern void SNOOP_Entry();
27 extern void 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);
212 if (!IsBadStringPtrA((LPSTR)x,80)) {
217 if (s[i]<0x20) {nostring=1;break;}
218 if (s[i]>=0x80) {nostring=1;break;}
223 sprintf(buf,"%08lx \"",x);
224 strncat(buf,(LPSTR)x,198-strlen(buf)-2);
231 if (!IsBadStringPtrW((LPWSTR)x,80)) {
236 if (s[i]<0x20) {nostring=1;break;}
237 if (s[i]>0x100) {nostring=1;break;}
242 sprintf(buf,"%08lx L",x);
243 strcat(buf,debugstr_wn((LPWSTR)x,198-strlen(buf)-2));
248 sprintf(buf,"%08lx",x);
252 #define CALLER1REF (*(DWORD*)ESP_reg(context))
253 void WINAPI REGS_FUNC(SNOOP_Entry)( CONTEXT *context )
255 DWORD ordinal=0,entry = EIP_reg(context)-5;
256 SNOOP_DLL *dll = firstdll;
257 SNOOP_FUN *fun = NULL;
258 SNOOP_RETURNENTRIES **rets = &firstrets;
259 SNOOP_RETURNENTRY *ret;
263 if ( ((char*)entry>=(char*)dll->funs) &&
264 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
266 fun = (SNOOP_FUN*)entry;
267 ordinal = fun-dll->funs;
273 FIXME("entrypoint 0x%08lx not found\n",entry);
276 /* guess cdecl ... */
277 if (fun->nrofargs<0) {
278 /* Typical cdecl return frame is:
280 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
281 * (after that 81 C2 xx xx xx xx)
283 LPBYTE reteip = (LPBYTE)CALLER1REF;
286 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
287 fun->nrofargs=reteip[2]/4;
293 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
294 if (!(*rets)->entry[i].origreturn)
296 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
298 rets = &((*rets)->next);
301 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
302 memset(*rets,0,4096);
303 i = 0; /* entry 0 is free */
305 ret = &((*rets)->entry[i]);
307 /* NOTE: origreturn struct member MUST come directly after snoopret */
308 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
309 ret->origreturn = (FARPROC)CALLER1REF;
310 CALLER1REF = (DWORD)&ret->lcall;
313 ret->ordinal = ordinal;
314 ret->origESP = ESP_reg(context);
316 EIP_reg(context)= (DWORD)fun->origfun;
318 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
319 if (fun->nrofargs>0) {
320 max = fun->nrofargs; if (max>16) max=16;
322 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+4+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
323 if (max!=fun->nrofargs)
325 } else if (fun->nrofargs<0) {
326 DPRINTF("<unknown, check return>");
327 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(DWORD));
328 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+4),sizeof(DWORD)*16);
330 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
333 void WINAPI REGS_FUNC(SNOOP_Return)( CONTEXT *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) {
376 void WINAPI REGS_FUNC(SNOOP_Entry)( CONTEXT *context )
380 void WINAPI REGS_FUNC(SNOOP_Return)( CONTEXT *context )
384 #endif /* !__i386__ */