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
);
37 WINE_DECLARE_DEBUG_CHANNEL(seh
);
39 static WINE_EXCEPTION_FILTER(page_fault
)
41 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
42 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
43 return EXCEPTION_EXECUTE_HANDLER
;
44 return EXCEPTION_CONTINUE_SEARCH
;
47 extern const char **debug_snoop_excludelist
;
48 extern const char **debug_snoop_includelist
;
52 extern void WINAPI
SNOOP_Entry();
53 extern void WINAPI
SNOOP_Return();
57 typedef struct tagSNOOP_FUN
{
59 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
60 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
63 DWORD snoopentry
; /* SNOOP_Entry relative */
70 typedef struct tagSNOOP_DLL
{
75 struct tagSNOOP_DLL
*next
;
79 typedef struct tagSNOOP_RETURNENTRY
{
81 BYTE lcall
; /* 0xe8 call snoopret relative*/
82 /* NOTE: If you move snoopret OR origreturn fix the relative offset
85 DWORD snoopret
; /* SNOOP_Ret relative */
91 DWORD
*args
; /* saved args across a stdcall */
94 typedef struct tagSNOOP_RETURNENTRIES
{
95 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
96 struct tagSNOOP_RETURNENTRIES
*next
;
97 } SNOOP_RETURNENTRIES
;
101 static SNOOP_DLL
*firstdll
= NULL
;
102 static SNOOP_RETURNENTRIES
*firstrets
= NULL
;
104 /***********************************************************************
105 * SNOOP_ShowDebugmsgSnoop
107 * Simple function to decide if a particular debugging message is
110 int SNOOP_ShowDebugmsgSnoop(const char *dll
, int ord
, const char *fname
) {
112 if(debug_snoop_excludelist
|| debug_snoop_includelist
) {
113 const char **listitem
;
115 int len
, len2
, itemlen
, show
;
117 if(debug_snoop_excludelist
) {
119 listitem
= debug_snoop_excludelist
;
122 listitem
= debug_snoop_includelist
;
126 sprintf(buf
, "%s.%d", dll
, ord
);
128 for(; *listitem
; listitem
++) {
129 itemlen
= strlen(*listitem
);
130 if((itemlen
== len
&& !strncasecmp(*listitem
, buf
, len
)) ||
131 (itemlen
== len2
&& !strncasecmp(*listitem
, buf
, len2
)) ||
132 !strcasecmp(*listitem
, fname
)) {
143 SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD ordbase
,DWORD nrofordinals
) {
144 SNOOP_DLL
**dll
= &(firstdll
);
147 TRACE("hmod=%p, name=%s, ordbase=%ld, nrofordinals=%ld\n",
148 hmod
, name
, ordbase
, nrofordinals
);
150 if (!TRACE_ON(snoop
)) return;
152 if ((*dll
)->hmod
== hmod
)
154 /* another dll, loaded at the same address */
155 VirtualFree((*dll
)->funs
, (*dll
)->nrofordinals
*sizeof(SNOOP_FUN
), MEM_RELEASE
);
158 dll
= &((*dll
)->next
);
160 *dll
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *dll
, sizeof(SNOOP_DLL
)+strlen(name
));
162 (*dll
)->ordbase
= ordbase
;
163 (*dll
)->nrofordinals
= nrofordinals
;
164 strcpy( (*dll
)->name
, name
);
165 if ((s
=strrchr((*dll
)->name
,'.')))
167 (*dll
)->funs
= VirtualAlloc(NULL
,nrofordinals
*sizeof(SNOOP_FUN
),MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
168 memset((*dll
)->funs
,0,nrofordinals
*sizeof(SNOOP_FUN
));
170 HeapFree(GetProcessHeap(),0,*dll
);
171 FIXME("out of memory\n");
177 SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
178 SNOOP_DLL
*dll
= firstdll
;
180 IMAGE_SECTION_HEADER
*sec
;
182 if (!TRACE_ON(snoop
)) return origfun
;
183 if (!*(LPBYTE
)origfun
) /* 0x00 is an imposs. opcode, poss. dataref. */
186 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
188 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
189 return origfun
; /* most likely a data reference */
192 if (hmod
== dll
->hmod
)
196 if (!dll
) /* probably internal */
198 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,name
))
200 assert(ordinal
< dll
->nrofordinals
);
201 fun
= dll
->funs
+ordinal
;
204 fun
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
)+1);
205 strcpy( fun
->name
, name
);
207 /* NOTE: origreturn struct member MUST come directly after snoopentry */
208 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
209 fun
->origfun
= origfun
;
212 return (FARPROC
)&(fun
->lcall
);
215 static void SNOOP_PrintArg(DWORD x
)
220 if (!HIWORD(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
227 if (s
[i
]<0x20) {nostring
=1;break;}
228 if (s
[i
]>=0x80) {nostring
=1;break;}
231 if (!nostring
&& i
> 5)
232 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
233 else /* try unicode */
239 if (s
[i
]<0x20) {nostring
=1;break;}
240 if (s
[i
]>0x100) {nostring
=1;break;}
243 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
252 #define CALLER1REF (*(DWORD*)context->Esp)
254 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
)
256 DWORD ordinal
=0,entry
= context
->Eip
- 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
= context
->Esp
;
317 context
->Eip
= (DWORD
)fun
->origfun
;
319 DPRINTF("%04lx:CALL %s.%ld: %s(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
,fun
->name
);
320 if (fun
->nrofargs
>0) {
321 max
= fun
->nrofargs
; if (max
>16) max
=16;
324 SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
));
325 if (i
<fun
->nrofargs
-1) DPRINTF(",");
327 if (max
!=fun
->nrofargs
)
329 } else if (fun
->nrofargs
<0) {
330 DPRINTF("<unknown, check return>");
331 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD
));
332 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
334 DPRINTF(") ret=%08lx\n",(DWORD
)ret
->origreturn
);
338 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
)
340 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
342 /* We haven't found out the nrofargs yet. If we called a cdecl
343 * function it is too late anyway and we can just set '0' (which
344 * will be the difference between orig and current ESP
345 * If stdcall -> everything ok.
347 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
348 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
349 context
->Eip
= (DWORD
)ret
->origreturn
;
353 DPRINTF("%04lx:RET %s.%ld: %s(",
354 GetCurrentThreadId(),
355 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
356 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
361 SNOOP_PrintArg(ret
->args
[i
]);
362 if (i
<max
-1) DPRINTF(",");
364 DPRINTF(") retval = %08lx ret=%08lx\n",
365 context
->Eax
,(DWORD
)ret
->origreturn
);
366 HeapFree(GetProcessHeap(),0,ret
->args
);
369 DPRINTF("%04lx:RET %s.%ld: %s() retval = %08lx ret=%08lx\n",
370 GetCurrentThreadId(),
371 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
372 context
->Eax
, (DWORD
)ret
->origreturn
);
373 ret
->origreturn
= NULL
; /* mark as empty */
376 /* assembly wrappers that save the context */
377 __ASM_GLOBAL_FUNC( SNOOP_Entry
,
378 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
379 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
380 __ASM_GLOBAL_FUNC( SNOOP_Return
,
381 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
382 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
384 #else /* !__i386__ */
385 void SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
, DWORD dw
) {
386 if (!TRACE_ON(snoop
)) return;
387 FIXME("snooping works only on i386 for now.\n");
390 FARPROC
SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
393 #endif /* !__i386__ */