XCloseIM sometimes crashes in Xlib, don't call it.
[wine/multimedia.git] / relay32 / snoop.c
blobb7007f7bf39a5f5d797e63fe6e3c55ded3f08dfa
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "winternl.h"
30 #include "snoop.h"
31 #include "stackframe.h"
32 #include "wine/debug.h"
33 #include "wine/exception.h"
34 #include "excpt.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;
49 #ifdef __i386__
51 extern void WINAPI SNOOP_Entry();
52 extern void WINAPI SNOOP_Return();
54 #include "pshpack1.h"
56 typedef struct tagSNOOP_FUN {
57 /* code part */
58 BYTE lcall; /* 0xe8 call snoopentry (relative) */
59 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
60 * calculation!
62 DWORD snoopentry; /* SNOOP_Entry relative */
63 /* unreached */
64 int nrofargs;
65 FARPROC origfun;
66 char *name;
67 } SNOOP_FUN;
69 typedef struct tagSNOOP_DLL {
70 HMODULE hmod;
71 SNOOP_FUN *funs;
72 DWORD ordbase;
73 DWORD nrofordinals;
74 struct tagSNOOP_DLL *next;
75 char name[1];
76 } SNOOP_DLL;
78 typedef struct tagSNOOP_RETURNENTRY {
79 /* code part */
80 BYTE lcall; /* 0xe8 call snoopret relative*/
81 /* NOTE: If you move snoopret OR origreturn fix the relative offset
82 * calculation!
84 DWORD snoopret; /* SNOOP_Ret relative */
85 /* unreached */
86 FARPROC origreturn;
87 SNOOP_DLL *dll;
88 DWORD ordinal;
89 DWORD origESP;
90 DWORD *args; /* saved args across a stdcall */
91 } SNOOP_RETURNENTRY;
93 typedef struct tagSNOOP_RETURNENTRIES {
94 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
95 struct tagSNOOP_RETURNENTRIES *next;
96 } SNOOP_RETURNENTRIES;
98 #include "poppack.h"
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
107 * wanted.
109 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
111 if(debug_snoop_excludelist || debug_snoop_includelist) {
112 const char **listitem;
113 char buf[80];
114 int len, len2, itemlen, show;
116 if(debug_snoop_excludelist) {
117 show = 1;
118 listitem = debug_snoop_excludelist;
119 } else {
120 show = 0;
121 listitem = debug_snoop_includelist;
123 len = strlen(dll);
124 assert(len < 64);
125 sprintf(buf, "%s.%d", dll, ord);
126 len2 = strlen(buf);
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)) {
132 show = !show;
133 break;
136 return show;
138 return 1;
141 void
142 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD ordbase,DWORD nrofordinals) {
143 SNOOP_DLL **dll = &(firstdll);
144 char *s;
146 TRACE("hmod=%p, name=%s, ordbase=%ld, nrofordinals=%ld\n",
147 hmod, name, ordbase, nrofordinals);
149 if (!TRACE_ON(snoop)) return;
150 while (*dll) {
151 if ((*dll)->hmod == hmod)
153 /* another dll, loaded at the same address */
154 VirtualFree((*dll)->funs, (*dll)->nrofordinals*sizeof(SNOOP_FUN), MEM_RELEASE);
155 break;
157 dll = &((*dll)->next);
159 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP_DLL)+strlen(name));
160 (*dll)->hmod = hmod;
161 (*dll)->ordbase = ordbase;
162 (*dll)->nrofordinals = nrofordinals;
163 strcpy( (*dll)->name, name );
164 if ((s=strrchr((*dll)->name,'.')))
165 *s='\0';
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));
168 if (!(*dll)->funs) {
169 HeapFree(GetProcessHeap(),0,*dll);
170 FIXME("out of memory\n");
171 return;
175 FARPROC
176 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
177 SNOOP_DLL *dll = firstdll;
178 SNOOP_FUN *fun;
179 IMAGE_SECTION_HEADER *sec;
181 if (!TRACE_ON(snoop)) return origfun;
182 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
183 return origfun;
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 */
190 while (dll) {
191 if (hmod == dll->hmod)
192 break;
193 dll=dll->next;
195 if (!dll) /* probably internal */
196 return origfun;
197 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
198 return origfun;
199 assert(ordinal < dll->nrofordinals);
200 fun = dll->funs+ordinal;
201 if (!fun->name)
203 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
204 strcpy( fun->name, name );
205 fun->lcall = 0xe8;
206 /* NOTE: origreturn struct member MUST come directly after snoopentry */
207 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
208 fun->origfun = origfun;
209 fun->nrofargs = -1;
211 return (FARPROC)&(fun->lcall);
214 static void SNOOP_PrintArg(DWORD x)
216 int i,nostring;
218 DPRINTF("%08lx",x);
219 if ( !HIWORD(x) ) return; /* trivial reject to avoid faults */
220 __TRY
222 LPBYTE s=(LPBYTE)x;
223 i=0;nostring=0;
224 while (i<80) {
225 if (s[i]==0) break;
226 if (s[i]<0x20) {nostring=1;break;}
227 if (s[i]>=0x80) {nostring=1;break;}
228 i++;
230 if (!nostring && i > 5)
231 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
232 else /* try unicode */
234 LPWSTR s=(LPWSTR)x;
235 i=0;nostring=0;
236 while (i<80) {
237 if (s[i]==0) break;
238 if (s[i]<0x20) {nostring=1;break;}
239 if (s[i]>0x100) {nostring=1;break;}
240 i++;
242 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
245 __EXCEPT(page_fault)
248 __ENDTRY
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;
260 int i=0, max;
262 while (dll) {
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;
268 break;
270 dll=dll->next;
272 if (!dll) {
273 FIXME("entrypoint 0x%08lx not found\n",entry);
274 return; /* oops */
276 /* guess cdecl ... */
277 if (fun->nrofargs<0) {
278 /* Typical cdecl return frame is:
279 * add esp, xxxxxxxx
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;
285 if (reteip) {
286 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
287 fun->nrofargs=reteip[2]/4;
292 while (*rets) {
293 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
294 if (!(*rets)->entry[i].origreturn)
295 break;
296 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
297 break;
298 rets = &((*rets)->next);
300 if (!*rets) {
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]);
306 ret->lcall = 0xe8;
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;
311 ret->dll = dll;
312 ret->args = NULL;
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;
321 for (i=0;i<max;i++)
323 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
324 if (i<fun->nrofargs-1) DPRINTF(",");
326 if (max!=fun->nrofargs)
327 DPRINTF(" ...");
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;
349 if (ret->args) {
350 int i,max;
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;
356 if (max>16) max=16;
358 for (i=0;i<max;i++)
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);
366 ret->args = NULL;
367 } else
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) {
390 return origfun;
392 #endif /* !__i386__ */