Moved parts of the DC initialisation and bitmap selection out of the
[wine/multimedia.git] / relay32 / snoop.c
blobd9215b6c81d3ee2fafe440be61ee45df44f39caf
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 "snoop.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;
47 #ifdef __i386__
49 extern void WINAPI SNOOP_Entry();
50 extern void WINAPI SNOOP_Return();
52 #ifdef NEED_UNDERSCORE_PREFIX
53 # define PREFIX "_"
54 #else
55 # define PREFIX
56 #endif
58 #include "pshpack1.h"
60 typedef struct tagSNOOP_FUN {
61 /* code part */
62 BYTE lcall; /* 0xe8 call snoopentry (relative) */
63 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
64 * calculation!
66 DWORD snoopentry; /* SNOOP_Entry relative */
67 /* unreached */
68 int nrofargs;
69 FARPROC origfun;
70 char *name;
71 } SNOOP_FUN;
73 typedef struct tagSNOOP_DLL {
74 HMODULE hmod;
75 SNOOP_FUN *funs;
76 DWORD ordbase;
77 DWORD nrofordinals;
78 struct tagSNOOP_DLL *next;
79 char name[1];
80 } SNOOP_DLL;
82 typedef struct tagSNOOP_RETURNENTRY {
83 /* code part */
84 BYTE lcall; /* 0xe8 call snoopret relative*/
85 /* NOTE: If you move snoopret OR origreturn fix the relative offset
86 * calculation!
88 DWORD snoopret; /* SNOOP_Ret relative */
89 /* unreached */
90 FARPROC origreturn;
91 SNOOP_DLL *dll;
92 DWORD ordinal;
93 DWORD origESP;
94 DWORD *args; /* saved args across a stdcall */
95 } SNOOP_RETURNENTRY;
97 typedef struct tagSNOOP_RETURNENTRIES {
98 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
99 struct tagSNOOP_RETURNENTRIES *next;
100 } SNOOP_RETURNENTRIES;
102 #include "poppack.h"
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
111 * wanted.
113 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
115 if(debug_snoop_excludelist || debug_snoop_includelist) {
116 char **listitem;
117 char buf[80];
118 int len, len2, itemlen, show;
120 if(debug_snoop_excludelist) {
121 show = 1;
122 listitem = debug_snoop_excludelist;
123 } else {
124 show = 0;
125 listitem = debug_snoop_includelist;
127 len = strlen(dll);
128 assert(len < 64);
129 sprintf(buf, "%s.%d", dll, ord);
130 len2 = strlen(buf);
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)) {
136 show = !show;
137 break;
140 return show;
142 return 1;
145 void
146 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD ordbase,DWORD nrofordinals) {
147 SNOOP_DLL **dll = &(firstdll);
148 char *s;
150 TRACE("hmod=%x, name=%s, ordbase=%ld, nrofordinals=%ld\n",
151 hmod, name, ordbase, nrofordinals);
153 if (!TRACE_ON(snoop)) return;
154 while (*dll) {
155 if ((*dll)->hmod == hmod)
157 /* another dll, loaded at the same address */
158 VirtualFree((*dll)->funs, (*dll)->nrofordinals*sizeof(SNOOP_FUN), MEM_RELEASE);
159 break;
161 dll = &((*dll)->next);
163 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP_DLL)+strlen(name));
164 (*dll)->hmod = hmod;
165 (*dll)->ordbase = ordbase;
166 (*dll)->nrofordinals = nrofordinals;
167 strcpy( (*dll)->name, name );
168 if ((s=strrchr((*dll)->name,'.')))
169 *s='\0';
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));
172 if (!(*dll)->funs) {
173 HeapFree(GetProcessHeap(),0,*dll);
174 FIXME("out of memory\n");
175 return;
179 FARPROC
180 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
181 SNOOP_DLL *dll = firstdll;
182 SNOOP_FUN *fun;
183 int j;
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. */
188 return origfun;
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
197 break;
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)
206 return origfun;
208 while (dll) {
209 if (hmod == dll->hmod)
210 break;
211 dll=dll->next;
213 if (!dll) /* probably internal */
214 return origfun;
215 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
216 return origfun;
217 assert(ordinal < dll->nrofordinals);
218 fun = dll->funs+ordinal;
219 if (!fun->name)
221 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
222 strcpy( fun->name, name );
223 fun->lcall = 0xe8;
224 /* NOTE: origreturn struct member MUST come directly after snoopentry */
225 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
226 fun->origfun = origfun;
227 fun->nrofargs = -1;
229 return (FARPROC)&(fun->lcall);
232 static char*
233 SNOOP_PrintArg(DWORD x) {
234 static char buf[200];
235 int i,nostring;
236 char * volatile ret=0;
238 if ( !HIWORD(x) ) { /* trivial reject to avoid faults */
239 sprintf(buf,"%08lx",x);
240 return buf;
242 __TRY{
243 LPBYTE s=(LPBYTE)x;
244 i=0;nostring=0;
245 while (i<80) {
246 if (s[i]==0) break;
247 if (s[i]<0x20) {nostring=1;break;}
248 if (s[i]>=0x80) {nostring=1;break;}
249 i++;
251 if (!nostring) {
252 if (i>5) {
253 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_an((LPSTR)x,sizeof(buf)-10));
254 ret=buf;
258 __EXCEPT(page_fault){}
259 __ENDTRY
260 if (ret)
261 return ret;
262 __TRY{
263 LPWSTR s=(LPWSTR)x;
264 i=0;nostring=0;
265 while (i<80) {
266 if (s[i]==0) break;
267 if (s[i]<0x20) {nostring=1;break;}
268 if (s[i]>0x100) {nostring=1;break;}
269 i++;
271 if (!nostring) {
272 if (i>5) {
273 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_wn((LPWSTR)x,sizeof(buf)-10));
274 ret=buf;
278 __EXCEPT(page_fault){}
279 __ENDTRY
280 if (ret)
281 return ret;
282 sprintf(buf,"%08lx",x);
283 return buf;
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;
295 int i=0, max;
297 while (dll) {
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;
303 break;
305 dll=dll->next;
307 if (!dll) {
308 FIXME("entrypoint 0x%08lx not found\n",entry);
309 return; /* oops */
311 /* guess cdecl ... */
312 if (fun->nrofargs<0) {
313 /* Typical cdecl return frame is:
314 * add esp, xxxxxxxx
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;
320 if (reteip) {
321 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
322 fun->nrofargs=reteip[2]/4;
327 while (*rets) {
328 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
329 if (!(*rets)->entry[i].origreturn)
330 break;
331 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
332 break;
333 rets = &((*rets)->next);
335 if (!*rets) {
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]);
341 ret->lcall = 0xe8;
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;
346 ret->dll = dll;
347 ret->args = NULL;
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;
356 for (i=0;i<max;i++)
357 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
358 if (max!=fun->nrofargs)
359 DPRINTF(" ...");
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;
381 if (ret->args) {
382 int i,max;
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;
388 if (max>16) max=16;
390 for (i=0;i<max;i++)
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);
395 ret->args = NULL;
396 } else
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) {
419 return origfun;
421 #endif /* !__i386__ */