2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
17 #include "builtin32.h"
20 #include "selectors.h"
21 #include "stackframe.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(snoop
);
26 char **debug_snoop_excludelist
= NULL
, **debug_snoop_includelist
= NULL
;
30 extern void WINAPI
SNOOP_Entry();
31 extern void WINAPI
SNOOP_Return();
33 #ifdef NEED_UNDERSCORE_PREFIX
41 typedef struct tagSNOOP_FUN
{
43 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
44 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
47 DWORD snoopentry
; /* SNOOP_Entry relative */
54 typedef struct tagSNOOP_DLL
{
59 struct tagSNOOP_DLL
*next
;
61 typedef struct tagSNOOP_RETURNENTRY
{
63 BYTE lcall
; /* 0xe8 call snoopret relative*/
64 /* NOTE: If you move snoopret OR origreturn fix the relative offset
67 DWORD snoopret
; /* SNOOP_Ret relative */
73 DWORD
*args
; /* saved args across a stdcall */
76 typedef struct tagSNOOP_RETURNENTRIES
{
77 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
78 struct tagSNOOP_RETURNENTRIES
*next
;
79 } SNOOP_RETURNENTRIES
;
83 static SNOOP_DLL
*firstdll
= NULL
;
84 static SNOOP_RETURNENTRIES
*firstrets
= NULL
;
86 /***********************************************************************
87 * SNOOP_ShowDebugmsgSnoop
89 * Simple function to decide if a particular debugging message is
92 int SNOOP_ShowDebugmsgSnoop(const char *dll
, int ord
, const char *fname
) {
94 if(debug_snoop_excludelist
|| debug_snoop_includelist
) {
97 int len
, len2
, itemlen
, show
;
99 if(debug_snoop_excludelist
) {
101 listitem
= debug_snoop_excludelist
;
104 listitem
= debug_snoop_includelist
;
108 sprintf(buf
, "%s.%d", dll
, ord
);
110 for(; *listitem
; listitem
++) {
111 itemlen
= strlen(*listitem
);
112 if((itemlen
== len
&& !strncmp(*listitem
, buf
, len
)) ||
113 (itemlen
== len2
&& !strncmp(*listitem
, buf
, len2
)) ||
114 !strcmp(*listitem
, fname
)) {
125 SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
) {
126 SNOOP_DLL
**dll
= &(firstdll
);
129 if (!TRACE_ON(snoop
)) return;
131 if ((*dll
)->hmod
== hmod
)
132 return; /* already registered */
133 dll
= &((*dll
)->next
);
135 *dll
= (SNOOP_DLL
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(SNOOP_DLL
));
138 (*dll
)->nrofordinals
= nrofordinals
;
139 (*dll
)->name
= HEAP_strdupA(GetProcessHeap(),0,name
);
140 if ((s
=strrchr((*dll
)->name
,'.')))
142 (*dll
)->funs
= VirtualAlloc(NULL
,nrofordinals
*sizeof(SNOOP_FUN
),MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
143 memset((*dll
)->funs
,0,nrofordinals
*sizeof(SNOOP_FUN
));
145 HeapFree(GetProcessHeap(),0,*dll
);
146 FIXME("out of memory\n");
152 SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
153 SNOOP_DLL
*dll
= firstdll
;
156 IMAGE_SECTION_HEADER
*pe_seg
= PE_SECTIONS(hmod
);
158 if (!TRACE_ON(snoop
)) return origfun
;
159 if (!*(LPBYTE
)origfun
) /* 0x00 is an imposs. opcode, poss. dataref. */
161 for (j
=0;j
<PE_HEADER(hmod
)->FileHeader
.NumberOfSections
;j
++)
162 /* 0x42 is special ELF loader tag */
163 if ((pe_seg
[j
].VirtualAddress
==0x42) ||
164 (((DWORD
)origfun
-hmod
>=pe_seg
[j
].VirtualAddress
)&&
165 ((DWORD
)origfun
-hmod
<pe_seg
[j
].VirtualAddress
+
166 pe_seg
[j
].SizeOfRawData
170 /* If we looked through all sections (and didn't find one)
171 * or if the sectionname contains "data", we return the
172 * original function since it is most likely a datareference.
174 if ( (j
==PE_HEADER(hmod
)->FileHeader
.NumberOfSections
) ||
175 (strstr(pe_seg
[j
].Name
,"data")) ||
176 !(pe_seg
[j
].Characteristics
& IMAGE_SCN_CNT_CODE
)
181 if (hmod
== dll
->hmod
)
185 if (!dll
) /* probably internal */
187 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,name
))
189 assert(ordinal
<dll
->nrofordinals
);
190 fun
= dll
->funs
+ordinal
;
191 if (!fun
->name
) fun
->name
= HEAP_strdupA(GetProcessHeap(),0,name
);
193 /* NOTE: origreturn struct member MUST come directly after snoopentry */
194 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
195 fun
->origfun
= origfun
;
197 return (FARPROC
)&(fun
->lcall
);
201 SNOOP_PrintArg(DWORD x
) {
202 static char buf
[200];
204 MEMORY_BASIC_INFORMATION mbi
;
207 !VirtualQuery((LPVOID
)x
,&mbi
,sizeof(mbi
)) ||
210 sprintf(buf
,"%08lx",x
);
213 if (!IsBadStringPtrA((LPSTR
)x
,80)) {
218 if (s
[i
]<0x20) {nostring
=1;break;}
219 if (s
[i
]>=0x80) {nostring
=1;break;}
224 wsnprintfA(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_an((LPSTR
)x
,sizeof(buf
)-10));
229 if (!IsBadStringPtrW((LPWSTR
)x
,80)) {
234 if (s
[i
]<0x20) {nostring
=1;break;}
235 if (s
[i
]>0x100) {nostring
=1;break;}
240 wsnprintfA(buf
,sizeof(buf
),"%08lx %s",x
,debugstr_wn((LPWSTR
)x
,sizeof(buf
)-10));
245 sprintf(buf
,"%08lx",x
);
249 #define CALLER1REF (*(DWORD*)ESP_reg(context))
251 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
);
252 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Entry
, SNOOP_DoEntry
);
253 void WINAPI
SNOOP_DoEntry( CONTEXT86
*context
)
255 DWORD ordinal
=0,entry
= EIP_reg(context
)-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
= ESP_reg(context
);
316 EIP_reg(context
)= (DWORD
)fun
->origfun
;
318 DPRINTF("Call %s.%ld: %s(",dll
->name
,ordinal
,fun
->name
);
319 if (fun
->nrofargs
>0) {
320 max
= fun
->nrofargs
; if (max
>16) max
=16;
322 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD
*)(ESP_reg(context
)+4+sizeof(DWORD
)*i
)),(i
<fun
->nrofargs
-1)?",":"");
323 if (max
!=fun
->nrofargs
)
325 } else if (fun
->nrofargs
<0) {
326 DPRINTF("<unknown, check return>");
327 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD
));
328 memcpy(ret
->args
,(LPBYTE
)(ESP_reg(context
)+4),sizeof(DWORD
)*16);
330 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD
)ret
->origreturn
,FS_reg(context
));
333 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
);
334 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Return
, SNOOP_DoReturn
);
335 void WINAPI
SNOOP_DoReturn( CONTEXT86
*context
)
337 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(EIP_reg(context
)-5);
339 /* We haven't found out the nrofargs yet. If we called a cdecl
340 * function it is too late anyway and we can just set '0' (which
341 * will be the difference between orig and current ESP
342 * If stdcall -> everything ok.
344 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
345 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(ESP_reg(context
)-ret
->origESP
-4)/4;
346 EIP_reg(context
) = (DWORD
)ret
->origreturn
;
350 DPRINTF("Ret %s.%ld: %s(",ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
351 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
355 DPRINTF("%s%s",SNOOP_PrintArg(ret
->args
[i
]),(i
<max
-1)?",":"");
356 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
357 EAX_reg(context
),(DWORD
)ret
->origreturn
,FS_reg(context
)
359 HeapFree(GetProcessHeap(),0,ret
->args
);
362 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
363 ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
364 EAX_reg(context
),(DWORD
)ret
->origreturn
,FS_reg(context
)
366 ret
->origreturn
= NULL
; /* mark as empty */
368 #else /* !__i386__ */
369 void SNOOP_RegisterDLL(HMODULE hmod
,LPCSTR name
,DWORD nrofordinals
) {
370 FIXME("snooping works only on i386 for now.\n");
374 FARPROC
SNOOP_GetProcAddress(HMODULE hmod
,LPCSTR name
,DWORD ordinal
,FARPROC origfun
) {
377 #endif /* !__i386__ */