2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
40 WINE_DECLARE_DEBUG_CHANNEL(seh);
42 static WINE_EXCEPTION_FILTER(page_fault)
44 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
45 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
46 return EXCEPTION_EXECUTE_HANDLER;
47 return EXCEPTION_CONTINUE_SEARCH;
50 extern const char **debug_snoop_excludelist;
51 extern const char **debug_snoop_includelist;
55 extern void WINAPI SNOOP_Entry();
56 extern void WINAPI SNOOP_Return();
60 typedef struct tagSNOOP_FUN {
62 BYTE lcall; /* 0xe8 call snoopentry (relative) */
63 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
66 DWORD snoopentry; /* SNOOP_Entry relative */
73 typedef struct tagSNOOP_DLL {
78 struct tagSNOOP_DLL *next;
82 typedef struct tagSNOOP_RETURNENTRY {
84 BYTE lcall; /* 0xe8 call snoopret relative*/
85 /* NOTE: If you move snoopret OR origreturn fix the relative offset
88 DWORD snoopret; /* SNOOP_Ret relative */
94 DWORD *args; /* saved args across a stdcall */
97 typedef struct tagSNOOP_RETURNENTRIES {
98 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
99 struct tagSNOOP_RETURNENTRIES *next;
100 } SNOOP_RETURNENTRIES;
104 static SNOOP_DLL *firstdll = NULL;
105 static SNOOP_RETURNENTRIES *firstrets = NULL;
107 /***********************************************************************
108 * SNOOP_ShowDebugmsgSnoop
110 * Simple function to decide if a particular debugging message is
113 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
115 if(debug_snoop_excludelist || debug_snoop_includelist) {
116 const char **listitem;
118 int len, len2, itemlen, show;
120 if(debug_snoop_excludelist) {
122 listitem = debug_snoop_excludelist;
125 listitem = debug_snoop_includelist;
129 sprintf(buf, "%s.%d", dll, ord);
131 for(; *listitem; listitem++) {
132 itemlen = strlen(*listitem);
133 if((itemlen == len && !strncasecmp(*listitem, buf, len)) ||
134 (itemlen == len2 && !strncasecmp(*listitem, buf, len2)) ||
135 !strcasecmp(*listitem, fname)) {
146 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD ordbase,DWORD nrofordinals) {
147 SNOOP_DLL **dll = &(firstdll);
152 TRACE("hmod=%p, name=%s, ordbase=%ld, nrofordinals=%ld\n",
153 hmod, name, ordbase, nrofordinals);
155 if (!TRACE_ON(snoop)) return;
157 if ((*dll)->hmod == hmod)
159 /* another dll, loaded at the same address */
161 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
162 NtFreeVirtualMemory(GetCurrentProcess(), &addr, &size, MEM_RELEASE);
165 dll = &((*dll)->next);
167 *dll = RtlReAllocateHeap(ntdll_get_process_heap(),
168 HEAP_ZERO_MEMORY, *dll,
169 sizeof(SNOOP_DLL) + strlen(name));
171 (*dll)->ordbase = ordbase;
172 (*dll)->nrofordinals = nrofordinals;
173 strcpy( (*dll)->name, name );
174 if ((s=strrchr((*dll)->name,'.')))
176 size = nrofordinals * sizeof(SNOOP_FUN);
177 NtAllocateVirtualMemory(GetCurrentProcess(), &addr, NULL, &size,
178 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
180 RtlFreeHeap(ntdll_get_process_heap(),0,*dll);
181 FIXME("out of memory\n");
185 memset((*dll)->funs,0,size);
188 FARPROC SNOOP_GetProcAddress( HMODULE hmod, IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
189 FARPROC origfun, DWORD ordinal )
195 SNOOP_DLL *dll = firstdll;
197 IMAGE_SECTION_HEADER *sec;
199 if (!TRACE_ON(snoop)) return origfun;
200 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
203 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
205 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
206 return origfun; /* most likely a data reference */
209 if (hmod == dll->hmod)
213 if (!dll) /* probably internal */
216 /* try to find a name for it */
218 names = (DWORD *)((char *)hmod + exports->AddressOfNames);
219 ordinals = (WORD *)((char *)hmod + exports->AddressOfNameOrdinals);
220 if (names) for (i = 0; i < exports->NumberOfNames; i++)
222 if (ordinals[i] == ordinal)
224 ename = (char *)hmod + names[i];
228 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
230 assert(ordinal < dll->nrofordinals);
231 fun = dll->funs+ordinal;
236 /* NOTE: origreturn struct member MUST come directly after snoopentry */
237 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
238 fun->origfun = origfun;
241 return (FARPROC)&(fun->lcall);
244 static void SNOOP_PrintArg(DWORD x)
249 if (!HIWORD(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
256 if (s[i]<0x20) {nostring=1;break;}
257 if (s[i]>=0x80) {nostring=1;break;}
260 if (!nostring && i > 5)
261 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
262 else /* try unicode */
268 if (s[i]<0x20) {nostring=1;break;}
269 if (s[i]>0x100) {nostring=1;break;}
272 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
281 #define CALLER1REF (*(DWORD*)context->Esp)
283 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
285 DWORD ordinal=0,entry = context->Eip - 5;
286 SNOOP_DLL *dll = firstdll;
287 SNOOP_FUN *fun = NULL;
288 SNOOP_RETURNENTRIES **rets = &firstrets;
289 SNOOP_RETURNENTRY *ret;
293 if ( ((char*)entry>=(char*)dll->funs) &&
294 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
296 fun = (SNOOP_FUN*)entry;
297 ordinal = fun-dll->funs;
303 FIXME("entrypoint 0x%08lx not found\n",entry);
306 /* guess cdecl ... */
307 if (fun->nrofargs<0) {
308 /* Typical cdecl return frame is:
310 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
311 * (after that 81 C2 xx xx xx xx)
313 LPBYTE reteip = (LPBYTE)CALLER1REF;
316 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
317 fun->nrofargs=reteip[2]/4;
323 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
324 if (!(*rets)->entry[i].origreturn)
326 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
328 rets = &((*rets)->next);
334 NtAllocateVirtualMemory(GetCurrentProcess(), &addr, NULL, &size,
335 MEM_COMMIT | MEM_RESERVE,
336 PAGE_EXECUTE_READWRITE);
339 memset(*rets,0,4096);
340 i = 0; /* entry 0 is free */
342 ret = &((*rets)->entry[i]);
344 /* NOTE: origreturn struct member MUST come directly after snoopret */
345 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
346 ret->origreturn = (FARPROC)CALLER1REF;
347 CALLER1REF = (DWORD)&ret->lcall;
350 ret->ordinal = ordinal;
351 ret->origESP = context->Esp;
353 context->Eip = (DWORD)fun->origfun;
355 if (fun->name) DPRINTF("%04lx:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
356 else DPRINTF("%04lx:CALL %s.%ld(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
357 if (fun->nrofargs>0) {
358 max = fun->nrofargs; if (max>16) max=16;
361 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
362 if (i<fun->nrofargs-1) DPRINTF(",");
364 if (max!=fun->nrofargs)
366 } else if (fun->nrofargs<0) {
367 DPRINTF("<unknown, check return>");
368 ret->args = RtlAllocateHeap(ntdll_get_process_heap(),
370 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
372 DPRINTF(") ret=%08lx\n",(DWORD)ret->origreturn);
376 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
378 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
379 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
381 /* We haven't found out the nrofargs yet. If we called a cdecl
382 * function it is too late anyway and we can just set '0' (which
383 * will be the difference between orig and current ESP
384 * If stdcall -> everything ok.
386 if (ret->dll->funs[ret->ordinal].nrofargs<0)
387 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
388 context->Eip = (DWORD)ret->origreturn;
393 DPRINTF("%04lx:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
395 DPRINTF("%04lx:RET %s.%ld(", GetCurrentThreadId(),
396 ret->dll->name,ret->dll->ordbase+ret->ordinal);
403 SNOOP_PrintArg(ret->args[i]);
404 if (i<max-1) DPRINTF(",");
406 DPRINTF(") retval=%08lx ret=%08lx\n",
407 context->Eax,(DWORD)ret->origreturn );
408 RtlFreeHeap(ntdll_get_process_heap(),0,ret->args);
414 DPRINTF("%04lx:RET %s.%s() retval=%08lx ret=%08lx\n",
415 GetCurrentThreadId(),
416 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
418 DPRINTF("%04lx:RET %s.%ld() retval=%08lx ret=%08lx\n",
419 GetCurrentThreadId(),
420 ret->dll->name,ret->dll->ordbase+ret->ordinal,
421 context->Eax, (DWORD)ret->origreturn);
423 ret->origreturn = NULL; /* mark as empty */
426 /* assembly wrappers that save the context */
427 __ASM_GLOBAL_FUNC( SNOOP_Entry,
428 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
429 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
430 __ASM_GLOBAL_FUNC( SNOOP_Return,
431 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
432 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
434 #else /* !__i386__ */
435 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals, DWORD dw) {
436 if (!TRACE_ON(snoop)) return;
437 FIXME("snooping works only on i386 for now.\n");
440 FARPROC SNOOP_GetProcAddress( HMODULE hmod, IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
441 FARPROC origfun, DWORD ordinal )
445 #endif /* !__i386__ */