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"
31 #include "stackframe.h"
32 #include "wine/debug.h"
33 #include "wine/exception.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(snoop
);
38 static WINE_EXCEPTION_FILTER(page_fault
)
40 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
41 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
42 return EXCEPTION_EXECUTE_HANDLER
;
43 return EXCEPTION_CONTINUE_SEARCH
;
46 extern const char **debug_snoop_excludelist
;
47 extern const char **debug_snoop_includelist
;
51 extern void WINAPI
SNOOP_Entry();
52 extern void WINAPI
SNOOP_Return();
56 typedef struct tagSNOOP_FUN
{
58 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
59 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
62 DWORD snoopentry
; /* SNOOP_Entry relative */
69 typedef struct tagSNOOP_DLL
{
74 struct tagSNOOP_DLL
*next
;
78 typedef struct tagSNOOP_RETURNENTRY
{
80 BYTE lcall
; /* 0xe8 call snoopret relative*/
81 /* NOTE: If you move snoopret OR origreturn fix the relative offset
84 DWORD snoopret
; /* SNOOP_Ret relative */
90 DWORD
*args
; /* saved args across a stdcall */
93 typedef struct tagSNOOP_RETURNENTRIES
{
94 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
95 struct tagSNOOP_RETURNENTRIES
*next
;
96 } SNOOP_RETURNENTRIES
;
100 static SNOOP_DLL
*firstdll
= NULL
;
101 static SNOOP_RETURNENTRIES
*firstrets
= NULL
;
103 /***********************************************************************
104 * SNOOP_ShowDebugmsgSnoop
106 * Simple function to decide if a particular debugging message is
109 int SNOOP_ShowDebugmsgSnoop(const char *dll
, int ord
, const char *fname
) {
111 if(debug_snoop_excludelist
|| debug_snoop_includelist
) {
112 const char **listitem
;
114 int len
, len2
, itemlen
, show
;
116 if(debug_snoop_excludelist
) {
118 listitem
= debug_snoop_excludelist
;
121 listitem
= debug_snoop_includelist
;
125 sprintf(buf
, "%s.%d", dll
, ord
);
127 for(; *listitem
; listitem
++) {
128 itemlen
= strlen(*listitem
);
129 if((itemlen
== len
&& !strncasecmp(*listitem
, buf
, len
)) ||
130 (itemlen
== len2
&& !strncasecmp(*listitem
, buf
, len2
)) ||
131 !strcasecmp(*listitem
, fname
)) {
142 SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD ordbase
,DWORD nrofordinals
) {
143 SNOOP_DLL
**dll
= &(firstdll
);
146 TRACE("hmod=%p, name=%s, ordbase=%ld, nrofordinals=%ld\n",
147 hmod
, name
, ordbase
, nrofordinals
);
149 if (!TRACE_ON(snoop
)) return;
151 if ((*dll
)->hmod
== hmod
)
153 /* another dll, loaded at the same address */
154 VirtualFree((*dll
)->funs
, (*dll
)->nrofordinals
*sizeof(SNOOP_FUN
), MEM_RELEASE
);
157 dll
= &((*dll
)->next
);
159 *dll
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *dll
, sizeof(SNOOP_DLL
)+strlen(name
));
161 (*dll
)->ordbase
= ordbase
;
162 (*dll
)->nrofordinals
= nrofordinals
;
163 strcpy( (*dll
)->name
, name
);
164 if ((s
=strrchr((*dll
)->name
,'.')))
166 (*dll
)->funs
= VirtualAlloc(NULL
,nrofordinals
*sizeof(SNOOP_FUN
),MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
167 memset((*dll
)->funs
,0,nrofordinals
*sizeof(SNOOP_FUN
));
169 HeapFree(GetProcessHeap(),0,*dll
);
170 FIXME("out of memory\n");
176 SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
177 SNOOP_DLL
*dll
= firstdll
;
179 IMAGE_SECTION_HEADER
*sec
;
181 if (!TRACE_ON(snoop
)) return origfun
;
182 if (!*(LPBYTE
)origfun
) /* 0x00 is an imposs. opcode, poss. dataref. */
185 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
187 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
188 return origfun
; /* most likely a data reference */
191 if (hmod
== dll
->hmod
)
195 if (!dll
) /* probably internal */
197 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,name
))
199 assert(ordinal
< dll
->nrofordinals
);
200 fun
= dll
->funs
+ordinal
;
203 fun
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
)+1);
204 strcpy( fun
->name
, name
);
206 /* NOTE: origreturn struct member MUST come directly after snoopentry */
207 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
208 fun
->origfun
= origfun
;
211 return (FARPROC
)&(fun
->lcall
);
214 static void SNOOP_PrintArg(DWORD x
)
219 if ( !HIWORD(x
) ) return; /* trivial reject to avoid faults */
226 if (s
[i
]<0x20) {nostring
=1;break;}
227 if (s
[i
]>=0x80) {nostring
=1;break;}
230 if (!nostring
&& i
> 5)
231 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
232 else /* try unicode */
238 if (s
[i
]<0x20) {nostring
=1;break;}
239 if (s
[i
]>0x100) {nostring
=1;break;}
242 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
251 #define CALLER1REF (*(DWORD*)context->Esp)
253 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
)
255 DWORD ordinal
=0,entry
= context
->Eip
- 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
= context
->Esp
;
316 context
->Eip
= (DWORD
)fun
->origfun
;
318 DPRINTF("%08lx:CALL %s.%ld: %s(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
,fun
->name
);
319 if (fun
->nrofargs
>0) {
320 max
= fun
->nrofargs
; if (max
>16) max
=16;
323 SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
));
324 if (i
<fun
->nrofargs
-1) DPRINTF(",");
326 if (max
!=fun
->nrofargs
)
328 } else if (fun
->nrofargs
<0) {
329 DPRINTF("<unknown, check return>");
330 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD
));
331 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
333 DPRINTF(") ret=%08lx\n",(DWORD
)ret
->origreturn
);
337 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
)
339 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
341 /* We haven't found out the nrofargs yet. If we called a cdecl
342 * function it is too late anyway and we can just set '0' (which
343 * will be the difference between orig and current ESP
344 * If stdcall -> everything ok.
346 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
347 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
348 context
->Eip
= (DWORD
)ret
->origreturn
;
352 DPRINTF("%08lx:RET %s.%ld: %s(",
353 GetCurrentThreadId(),
354 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
355 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
360 SNOOP_PrintArg(ret
->args
[i
]);
361 if (i
<max
-1) DPRINTF(",");
363 DPRINTF(") retval = %08lx ret=%08lx\n",
364 context
->Eax
,(DWORD
)ret
->origreturn
);
365 HeapFree(GetProcessHeap(),0,ret
->args
);
368 DPRINTF("%08lx:RET %s.%ld: %s() retval = %08lx ret=%08lx\n",
369 GetCurrentThreadId(),
370 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
371 context
->Eax
, (DWORD
)ret
->origreturn
);
372 ret
->origreturn
= NULL
; /* mark as empty */
375 /* assembly wrappers that save the context */
376 __ASM_GLOBAL_FUNC( SNOOP_Entry
,
377 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
378 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
379 __ASM_GLOBAL_FUNC( SNOOP_Return
,
380 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
381 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
383 #else /* !__i386__ */
384 void SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
, DWORD dw
) {
385 if (!TRACE_ON(snoop
)) return;
386 FIXME("snooping works only on i386 for now.\n");
389 FARPROC
SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
392 #endif /* !__i386__ */