Removed CURSORICON_IconToCursor now that we support color cursors.
[wine/wine64.git] / relay32 / snoop.c
blob59f8ba32cca17ccd405707b9a6dce951d7aacd10
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 void SNOOP_PrintArg(DWORD x)
234 int i,nostring;
236 DPRINTF("%08lx",x);
237 if ( !HIWORD(x) ) return; /* trivial reject to avoid faults */
238 __TRY
240 LPBYTE s=(LPBYTE)x;
241 i=0;nostring=0;
242 while (i<80) {
243 if (s[i]==0) break;
244 if (s[i]<0x20) {nostring=1;break;}
245 if (s[i]>=0x80) {nostring=1;break;}
246 i++;
248 if (!nostring && i > 5)
249 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
250 else /* try unicode */
252 LPWSTR s=(LPWSTR)x;
253 i=0;nostring=0;
254 while (i<80) {
255 if (s[i]==0) break;
256 if (s[i]<0x20) {nostring=1;break;}
257 if (s[i]>0x100) {nostring=1;break;}
258 i++;
260 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
263 __EXCEPT(page_fault)
266 __ENDTRY
269 #define CALLER1REF (*(DWORD*)context->Esp)
271 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
273 DWORD ordinal=0,entry = context->Eip - 5;
274 SNOOP_DLL *dll = firstdll;
275 SNOOP_FUN *fun = NULL;
276 SNOOP_RETURNENTRIES **rets = &firstrets;
277 SNOOP_RETURNENTRY *ret;
278 int i=0, max;
280 while (dll) {
281 if ( ((char*)entry>=(char*)dll->funs) &&
282 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
284 fun = (SNOOP_FUN*)entry;
285 ordinal = fun-dll->funs;
286 break;
288 dll=dll->next;
290 if (!dll) {
291 FIXME("entrypoint 0x%08lx not found\n",entry);
292 return; /* oops */
294 /* guess cdecl ... */
295 if (fun->nrofargs<0) {
296 /* Typical cdecl return frame is:
297 * add esp, xxxxxxxx
298 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
299 * (after that 81 C2 xx xx xx xx)
301 LPBYTE reteip = (LPBYTE)CALLER1REF;
303 if (reteip) {
304 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
305 fun->nrofargs=reteip[2]/4;
310 while (*rets) {
311 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
312 if (!(*rets)->entry[i].origreturn)
313 break;
314 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
315 break;
316 rets = &((*rets)->next);
318 if (!*rets) {
319 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
320 memset(*rets,0,4096);
321 i = 0; /* entry 0 is free */
323 ret = &((*rets)->entry[i]);
324 ret->lcall = 0xe8;
325 /* NOTE: origreturn struct member MUST come directly after snoopret */
326 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
327 ret->origreturn = (FARPROC)CALLER1REF;
328 CALLER1REF = (DWORD)&ret->lcall;
329 ret->dll = dll;
330 ret->args = NULL;
331 ret->ordinal = ordinal;
332 ret->origESP = context->Esp;
334 context->Eip = (DWORD)fun->origfun;
336 DPRINTF("%08lx:CALL %s.%ld: %s(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal,fun->name);
337 if (fun->nrofargs>0) {
338 max = fun->nrofargs; if (max>16) max=16;
339 for (i=0;i<max;i++)
341 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
342 if (i<fun->nrofargs-1) DPRINTF(",");
344 if (max!=fun->nrofargs)
345 DPRINTF(" ...");
346 } else if (fun->nrofargs<0) {
347 DPRINTF("<unknown, check return>");
348 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD));
349 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
351 DPRINTF(") ret=%08lx\n",(DWORD)ret->origreturn);
355 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
357 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
359 /* We haven't found out the nrofargs yet. If we called a cdecl
360 * function it is too late anyway and we can just set '0' (which
361 * will be the difference between orig and current ESP
362 * If stdcall -> everything ok.
364 if (ret->dll->funs[ret->ordinal].nrofargs<0)
365 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
366 context->Eip = (DWORD)ret->origreturn;
367 if (ret->args) {
368 int i,max;
370 DPRINTF("%08lx:RET %s.%ld: %s(",
371 GetCurrentThreadId(),
372 ret->dll->name,ret->dll->ordbase+ret->ordinal,ret->dll->funs[ret->ordinal].name);
373 max = ret->dll->funs[ret->ordinal].nrofargs;
374 if (max>16) max=16;
376 for (i=0;i<max;i++)
378 SNOOP_PrintArg(ret->args[i]);
379 if (i<max-1) DPRINTF(",");
381 DPRINTF(") retval = %08lx ret=%08lx\n",
382 context->Eax,(DWORD)ret->origreturn );
383 HeapFree(GetProcessHeap(),0,ret->args);
384 ret->args = NULL;
385 } else
386 DPRINTF("%08lx:RET %s.%ld: %s() retval = %08lx ret=%08lx\n",
387 GetCurrentThreadId(),
388 ret->dll->name,ret->dll->ordbase+ret->ordinal,ret->dll->funs[ret->ordinal].name,
389 context->Eax, (DWORD)ret->origreturn);
390 ret->origreturn = NULL; /* mark as empty */
393 /* assembly wrappers that save the context */
394 __ASM_GLOBAL_FUNC( SNOOP_Entry,
395 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
396 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
397 __ASM_GLOBAL_FUNC( SNOOP_Return,
398 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
399 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
401 #else /* !__i386__ */
402 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals, DWORD dw) {
403 if (!TRACE_ON(snoop)) return;
404 FIXME("snooping works only on i386 for now.\n");
407 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
408 return origfun;
410 #endif /* !__i386__ */