2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
16 #include "stackframe.h"
17 #include "debugtools.h"
18 #include "wine/exception.h"
20 DEFAULT_DEBUG_CHANNEL(snoop
);
22 static WINE_EXCEPTION_FILTER(page_fault
)
24 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
25 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
26 return EXCEPTION_EXECUTE_HANDLER
;
27 return EXCEPTION_CONTINUE_SEARCH
;
30 char **debug_snoop_excludelist
= NULL
, **debug_snoop_includelist
= NULL
;
34 extern void WINAPI
SNOOP_Entry();
35 extern void WINAPI
SNOOP_Return();
37 #ifdef NEED_UNDERSCORE_PREFIX
45 typedef struct tagSNOOP_FUN
{
47 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
48 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
51 DWORD snoopentry
; /* SNOOP_Entry relative */
58 typedef struct tagSNOOP_DLL
{
63 struct tagSNOOP_DLL
*next
;
65 typedef struct tagSNOOP_RETURNENTRY
{
67 BYTE lcall
; /* 0xe8 call snoopret relative*/
68 /* NOTE: If you move snoopret OR origreturn fix the relative offset
71 DWORD snoopret
; /* SNOOP_Ret relative */
77 DWORD
*args
; /* saved args across a stdcall */
80 typedef struct tagSNOOP_RETURNENTRIES
{
81 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
82 struct tagSNOOP_RETURNENTRIES
*next
;
83 } SNOOP_RETURNENTRIES
;
87 static SNOOP_DLL
*firstdll
= NULL
;
88 static SNOOP_RETURNENTRIES
*firstrets
= NULL
;
90 /***********************************************************************
91 * SNOOP_ShowDebugmsgSnoop
93 * Simple function to decide if a particular debugging message is
96 int SNOOP_ShowDebugmsgSnoop(const char *dll
, int ord
, const char *fname
) {
98 if(debug_snoop_excludelist
|| debug_snoop_includelist
) {
101 int len
, len2
, itemlen
, show
;
103 if(debug_snoop_excludelist
) {
105 listitem
= debug_snoop_excludelist
;
108 listitem
= debug_snoop_includelist
;
112 sprintf(buf
, "%s.%d", dll
, ord
);
114 for(; *listitem
; listitem
++) {
115 itemlen
= strlen(*listitem
);
116 if((itemlen
== len
&& !strncmp(*listitem
, buf
, len
)) ||
117 (itemlen
== len2
&& !strncmp(*listitem
, buf
, len2
)) ||
118 !strcmp(*listitem
, fname
)) {
129 SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
) {
130 SNOOP_DLL
**dll
= &(firstdll
);
133 if (!TRACE_ON(snoop
)) return;
135 if ((*dll
)->hmod
== hmod
)
136 return; /* already registered */
137 dll
= &((*dll
)->next
);
139 *dll
= (SNOOP_DLL
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(SNOOP_DLL
));
142 (*dll
)->nrofordinals
= nrofordinals
;
143 (*dll
)->name
= HEAP_strdupA(GetProcessHeap(),0,name
);
144 if ((s
=strrchr((*dll
)->name
,'.')))
146 (*dll
)->funs
= VirtualAlloc(NULL
,nrofordinals
*sizeof(SNOOP_FUN
),MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
147 memset((*dll
)->funs
,0,nrofordinals
*sizeof(SNOOP_FUN
));
149 HeapFree(GetProcessHeap(),0,*dll
);
150 FIXME("out of memory\n");
156 SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
157 SNOOP_DLL
*dll
= firstdll
;
160 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hmod
);
162 if (!TRACE_ON(snoop
)) return origfun
;
163 if (!*(LPBYTE
)origfun
) /* 0x00 is an imposs. opcode, poss. dataref. */
165 for (j
=0;j
<PE_HEADER(hmod
)->FileHeader
.NumberOfSections
;j
++)
166 /* 0x42 is special ELF loader tag */
167 if ((pe_seg
[j
].VirtualAddress
==0x42) ||
168 (((DWORD
)origfun
-hmod
>=pe_seg
[j
].VirtualAddress
)&&
169 ((DWORD
)origfun
-hmod
<pe_seg
[j
].VirtualAddress
+
170 pe_seg
[j
].SizeOfRawData
174 /* If we looked through all sections (and didn't find one)
175 * or if the sectionname contains "data", we return the
176 * original function since it is most likely a datareference.
178 if ( (j
==PE_HEADER(hmod
)->FileHeader
.NumberOfSections
) ||
179 (strstr(pe_seg
[j
].Name
,"data")) ||
180 !(pe_seg
[j
].Characteristics
& IMAGE_SCN_CNT_CODE
)
185 if (hmod
== dll
->hmod
)
189 if (!dll
) /* probably internal */
191 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,name
))
193 assert(ordinal
< dll
->nrofordinals
);
194 fun
= dll
->funs
+ordinal
;
195 if (!fun
->name
) fun
->name
= HEAP_strdupA(GetProcessHeap(),0,name
);
197 /* NOTE: origreturn struct member MUST come directly after snoopentry */
198 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
199 fun
->origfun
= origfun
;
201 return (FARPROC
)&(fun
->lcall
);
205 SNOOP_PrintArg(DWORD x
) {
206 static char buf
[200];
208 char * volatile ret
=0;
210 if ( !HIWORD(x
) ) { /* trivial reject to avoid faults */
211 sprintf(buf
,"%08lx",x
);
219 if (s
[i
]<0x20) {nostring
=1;break;}
220 if (s
[i
]>=0x80) {nostring
=1;break;}
225 snprintf(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_an((LPSTR
)x
,sizeof(buf
)-10));
230 __EXCEPT(page_fault
){}
239 if (s
[i
]<0x20) {nostring
=1;break;}
240 if (s
[i
]>0x100) {nostring
=1;break;}
245 snprintf(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_wn((LPWSTR
)x
,sizeof(buf
)-10));
250 __EXCEPT(page_fault
){}
254 sprintf(buf
,"%08lx",x
);
258 #define CALLER1REF (*(DWORD*)context->Esp)
260 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
)
262 DWORD ordinal
=0,entry
= context
->Eip
- 5;
263 SNOOP_DLL
*dll
= firstdll
;
264 SNOOP_FUN
*fun
= NULL
;
265 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
266 SNOOP_RETURNENTRY
*ret
;
270 if ( ((char*)entry
>=(char*)dll
->funs
) &&
271 ((char*)entry
<=(char*)(dll
->funs
+dll
->nrofordinals
))
273 fun
= (SNOOP_FUN
*)entry
;
274 ordinal
= fun
-dll
->funs
;
280 FIXME("entrypoint 0x%08lx not found\n",entry
);
283 /* guess cdecl ... */
284 if (fun
->nrofargs
<0) {
285 /* Typical cdecl return frame is:
287 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
288 * (after that 81 C2 xx xx xx xx)
290 LPBYTE reteip
= (LPBYTE
)CALLER1REF
;
293 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
294 fun
->nrofargs
=reteip
[2]/4;
300 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
301 if (!(*rets
)->entry
[i
].origreturn
)
303 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
305 rets
= &((*rets
)->next
);
308 *rets
= VirtualAlloc(NULL
,4096,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
309 memset(*rets
,0,4096);
310 i
= 0; /* entry 0 is free */
312 ret
= &((*rets
)->entry
[i
]);
314 /* NOTE: origreturn struct member MUST come directly after snoopret */
315 ret
->snoopret
= ((char*)SNOOP_Return
)-(char*)(&ret
->origreturn
);
316 ret
->origreturn
= (FARPROC
)CALLER1REF
;
317 CALLER1REF
= (DWORD
)&ret
->lcall
;
320 ret
->ordinal
= ordinal
;
321 ret
->origESP
= context
->Esp
;
323 context
->Eip
= (DWORD
)fun
->origfun
;
325 DPRINTF("CALL %s.%ld: %s(",dll
->name
,ordinal
,fun
->name
);
326 if (fun
->nrofargs
>0) {
327 max
= fun
->nrofargs
; if (max
>16) max
=16;
329 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
)),(i
<fun
->nrofargs
-1)?",":"");
330 if (max
!=fun
->nrofargs
)
332 } else if (fun
->nrofargs
<0) {
333 DPRINTF("<unknown, check return>");
334 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD
));
335 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
337 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD
)ret
->origreturn
,context
->SegFs
);
341 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
)
343 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
345 /* We haven't found out the nrofargs yet. If we called a cdecl
346 * function it is too late anyway and we can just set '0' (which
347 * will be the difference between orig and current ESP
348 * If stdcall -> everything ok.
350 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
351 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
352 context
->Eip
= (DWORD
)ret
->origreturn
;
356 DPRINTF("RET %s.%ld: %s(",ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
357 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
361 DPRINTF("%s%s",SNOOP_PrintArg(ret
->args
[i
]),(i
<max
-1)?",":"");
362 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
363 context
->Eax
,(DWORD
)ret
->origreturn
,context
->SegFs
);
364 HeapFree(GetProcessHeap(),0,ret
->args
);
367 DPRINTF("RET %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
368 ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
369 context
->Eax
,(DWORD
)ret
->origreturn
,context
->SegFs
);
370 ret
->origreturn
= NULL
; /* mark as empty */
373 /* assembly wrappers that save the context */
374 __ASM_GLOBAL_FUNC( SNOOP_Entry
,
375 "call " __ASM_NAME("CALL32_Regs") "\n\t"
376 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
377 __ASM_GLOBAL_FUNC( SNOOP_Return
,
378 "call " __ASM_NAME("CALL32_Regs") "\n\t"
379 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
381 #else /* !__i386__ */
382 void SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
) {
383 FIXME("snooping works only on i386 for now.\n");
387 FARPROC
SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
390 #endif /* !__i386__ */