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"
30 #include "stackframe.h"
31 #include "wine/debug.h"
32 #include "wine/exception.h"
33 #include "msvcrt/excpt.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(snoop
);
37 static WINE_EXCEPTION_FILTER(page_fault
)
39 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
40 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
41 return EXCEPTION_EXECUTE_HANDLER
;
42 return EXCEPTION_CONTINUE_SEARCH
;
45 char **debug_snoop_excludelist
= NULL
, **debug_snoop_includelist
= NULL
;
49 extern void WINAPI
SNOOP_Entry();
50 extern void WINAPI
SNOOP_Return();
52 #ifdef NEED_UNDERSCORE_PREFIX
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
) {
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
);
150 TRACE("hmod=%x, name=%s, ordbase=%ld, nrofordinals=%ld\n",
151 hmod
, name
, ordbase
, nrofordinals
);
153 if (!TRACE_ON(snoop
)) return;
155 if ((*dll
)->hmod
== hmod
)
157 /* another dll, loaded at the same address */
158 VirtualFree((*dll
)->funs
, (*dll
)->nrofordinals
*sizeof(SNOOP_FUN
), MEM_RELEASE
);
161 dll
= &((*dll
)->next
);
163 *dll
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *dll
, sizeof(SNOOP_DLL
)+strlen(name
));
165 (*dll
)->ordbase
= ordbase
;
166 (*dll
)->nrofordinals
= nrofordinals
;
167 strcpy( (*dll
)->name
, name
);
168 if ((s
=strrchr((*dll
)->name
,'.')))
170 (*dll
)->funs
= VirtualAlloc(NULL
,nrofordinals
*sizeof(SNOOP_FUN
),MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
171 memset((*dll
)->funs
,0,nrofordinals
*sizeof(SNOOP_FUN
));
173 HeapFree(GetProcessHeap(),0,*dll
);
174 FIXME("out of memory\n");
180 SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
181 SNOOP_DLL
*dll
= firstdll
;
184 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hmod
);
186 if (!TRACE_ON(snoop
)) return origfun
;
187 if (!*(LPBYTE
)origfun
) /* 0x00 is an imposs. opcode, poss. dataref. */
189 for (j
=0;j
<PE_HEADER(hmod
)->FileHeader
.NumberOfSections
;j
++)
190 /* 0x42 is special ELF loader tag */
191 if ((pe_seg
[j
].VirtualAddress
==0x42) ||
192 (((DWORD
)origfun
-hmod
>=pe_seg
[j
].VirtualAddress
)&&
193 ((DWORD
)origfun
-hmod
<pe_seg
[j
].VirtualAddress
+
194 pe_seg
[j
].SizeOfRawData
198 /* If we looked through all sections (and didn't find one)
199 * or if the sectionname contains "data", we return the
200 * original function since it is most likely a datareference.
202 if ( (j
==PE_HEADER(hmod
)->FileHeader
.NumberOfSections
) ||
203 (strstr(pe_seg
[j
].Name
,"data")) ||
204 !(pe_seg
[j
].Characteristics
& IMAGE_SCN_CNT_CODE
)
209 if (hmod
== dll
->hmod
)
213 if (!dll
) /* probably internal */
215 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,name
))
217 assert(ordinal
< dll
->nrofordinals
);
218 fun
= dll
->funs
+ordinal
;
221 fun
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
)+1);
222 strcpy( fun
->name
, name
);
224 /* NOTE: origreturn struct member MUST come directly after snoopentry */
225 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
226 fun
->origfun
= origfun
;
229 return (FARPROC
)&(fun
->lcall
);
233 SNOOP_PrintArg(DWORD x
) {
234 static char buf
[200];
236 char * volatile ret
=0;
238 if ( !HIWORD(x
) ) { /* trivial reject to avoid faults */
239 sprintf(buf
,"%08lx",x
);
247 if (s
[i
]<0x20) {nostring
=1;break;}
248 if (s
[i
]>=0x80) {nostring
=1;break;}
253 snprintf(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_an((LPSTR
)x
,sizeof(buf
)-10));
258 __EXCEPT(page_fault
){}
267 if (s
[i
]<0x20) {nostring
=1;break;}
268 if (s
[i
]>0x100) {nostring
=1;break;}
273 snprintf(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_wn((LPWSTR
)x
,sizeof(buf
)-10));
278 __EXCEPT(page_fault
){}
282 sprintf(buf
,"%08lx",x
);
286 #define CALLER1REF (*(DWORD*)context->Esp)
288 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
)
290 DWORD ordinal
=0,entry
= context
->Eip
- 5;
291 SNOOP_DLL
*dll
= firstdll
;
292 SNOOP_FUN
*fun
= NULL
;
293 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
294 SNOOP_RETURNENTRY
*ret
;
298 if ( ((char*)entry
>=(char*)dll
->funs
) &&
299 ((char*)entry
<=(char*)(dll
->funs
+dll
->nrofordinals
))
301 fun
= (SNOOP_FUN
*)entry
;
302 ordinal
= fun
-dll
->funs
;
308 FIXME("entrypoint 0x%08lx not found\n",entry
);
311 /* guess cdecl ... */
312 if (fun
->nrofargs
<0) {
313 /* Typical cdecl return frame is:
315 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
316 * (after that 81 C2 xx xx xx xx)
318 LPBYTE reteip
= (LPBYTE
)CALLER1REF
;
321 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
322 fun
->nrofargs
=reteip
[2]/4;
328 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
329 if (!(*rets
)->entry
[i
].origreturn
)
331 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
333 rets
= &((*rets
)->next
);
336 *rets
= VirtualAlloc(NULL
,4096,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
337 memset(*rets
,0,4096);
338 i
= 0; /* entry 0 is free */
340 ret
= &((*rets
)->entry
[i
]);
342 /* NOTE: origreturn struct member MUST come directly after snoopret */
343 ret
->snoopret
= ((char*)SNOOP_Return
)-(char*)(&ret
->origreturn
);
344 ret
->origreturn
= (FARPROC
)CALLER1REF
;
345 CALLER1REF
= (DWORD
)&ret
->lcall
;
348 ret
->ordinal
= ordinal
;
349 ret
->origESP
= context
->Esp
;
351 context
->Eip
= (DWORD
)fun
->origfun
;
353 DPRINTF("%08lx:CALL %s.%ld: %s(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
,fun
->name
);
354 if (fun
->nrofargs
>0) {
355 max
= fun
->nrofargs
; if (max
>16) max
=16;
357 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
)),(i
<fun
->nrofargs
-1)?",":"");
358 if (max
!=fun
->nrofargs
)
360 } else if (fun
->nrofargs
<0) {
361 DPRINTF("<unknown, check return>");
362 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD
));
363 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
365 DPRINTF(") ret=%08lx\n",(DWORD
)ret
->origreturn
);
369 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
)
371 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
373 /* We haven't found out the nrofargs yet. If we called a cdecl
374 * function it is too late anyway and we can just set '0' (which
375 * will be the difference between orig and current ESP
376 * If stdcall -> everything ok.
378 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
379 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
380 context
->Eip
= (DWORD
)ret
->origreturn
;
384 DPRINTF("%08lx:RET %s.%ld: %s(",
385 GetCurrentThreadId(),
386 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
387 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
391 DPRINTF("%s%s",SNOOP_PrintArg(ret
->args
[i
]),(i
<max
-1)?",":"");
392 DPRINTF(") retval = %08lx ret=%08lx\n",
393 context
->Eax
,(DWORD
)ret
->origreturn
);
394 HeapFree(GetProcessHeap(),0,ret
->args
);
397 DPRINTF("%08lx:RET %s.%ld: %s() retval = %08lx ret=%08lx\n",
398 GetCurrentThreadId(),
399 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
400 context
->Eax
, (DWORD
)ret
->origreturn
);
401 ret
->origreturn
= NULL
; /* mark as empty */
404 /* assembly wrappers that save the context */
405 __ASM_GLOBAL_FUNC( SNOOP_Entry
,
406 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
407 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
408 __ASM_GLOBAL_FUNC( SNOOP_Return
,
409 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
410 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
412 #else /* !__i386__ */
413 void SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
) {
414 if (!TRACE_ON(snoop
)) return;
415 FIXME("snooping works only on i386 for now.\n");
418 FARPROC
SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
421 #endif /* !__i386__ */