Added checks to guard against NULL active window.
[wine/gsoc_dplay.git] / relay32 / snoop.c
blob645ecd5d29e0ba2b0eaa88cba4ccb2c1e31c99d5
1 /*
2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
5 */
8 #include <assert.h>
9 #include <string.h>
10 #include "winbase.h"
11 #include "winnt.h"
12 #include "heap.h"
13 #include "builtin32.h"
14 #include "snoop.h"
15 #include "neexe.h"
16 #include "peexe.h"
17 #include "selectors.h"
18 #include "stackframe.h"
19 #include "debugstr.h"
20 #include "debug.h"
22 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
24 #ifdef __i386__
26 #ifdef NEED_UNDERSCORE_PREFIX
27 # define PREFIX "_"
28 #else
29 # define PREFIX
30 #endif
32 /* Well, not exactly extern since they are in the same file (in the lines
33 * below). But the C Compiler doesn't see them there, so we have to help a bit.
35 extern void SNOOP_Return();
36 extern void SNOOP_Entry();
37 __asm__(".align 4\n\t"
38 ".globl "PREFIX"SNOOP_Entry\n\t"
39 ".type "PREFIX"SNOOP_Entry,@function\n\t"
40 PREFIX"SNOOP_Entry:\n\t"
41 "pushl $"PREFIX"__regs_SNOOP_Entry\n\t"
42 "pushl $"PREFIX"CALL32_Regs\n\t"
43 "ret\n\t"
44 ".align 4\n\t"
45 ".globl "PREFIX"SNOOP_Return\n\t"
46 ".type "PREFIX"SNOOP_Return,@function\n\t"
47 PREFIX"SNOOP_Return:\n\t"
48 "pushl $"PREFIX"__regs_SNOOP_Return\n\t"
49 "pushl $"PREFIX"CALL32_Regs\n\t"
50 "ret"
53 #pragma pack(1)
55 typedef struct tagSNOOP_FUN {
56 /* code part */
57 BYTE lcall; /* 0xe8 call snoopentry (relative) */
58 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
59 * calculation!
61 DWORD snoopentry; /* SNOOP_Entry relative */
62 /* unreached */
63 int nrofargs;
64 FARPROC origfun;
65 char *name;
66 } SNOOP_FUN;
68 typedef struct tagSNOOP_DLL {
69 HMODULE hmod;
70 SNOOP_FUN *funs;
71 LPCSTR name;
72 int nrofordinals;
73 struct tagSNOOP_DLL *next;
74 } SNOOP_DLL;
75 typedef struct tagSNOOP_RETURNENTRY {
76 /* code part */
77 BYTE lcall; /* 0xe8 call snoopret relative*/
78 /* NOTE: If you move snoopret OR origreturn fix the relative offset
79 * calculation!
81 DWORD snoopret; /* SNOOP_Ret relative */
82 /* unreached */
83 FARPROC origreturn;
84 SNOOP_DLL *dll;
85 DWORD ordinal;
86 DWORD origESP;
87 DWORD *args; /* saved args across a stdcall */
88 } SNOOP_RETURNENTRY;
90 typedef struct tagSNOOP_RETURNENTRIES {
91 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
92 struct tagSNOOP_RETURNENTRIES *next;
93 } SNOOP_RETURNENTRIES;
95 #pragma pack(4)
97 static SNOOP_DLL *firstdll = NULL;
98 static SNOOP_RETURNENTRIES *firstrets = NULL;
100 /***********************************************************************
101 * SNOOP_ShowDebugmsgSnoop
103 * Simple function to decide if a particular debugging message is
104 * wanted.
106 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
108 if(debug_snoop_excludelist || debug_snoop_includelist) {
109 char **listitem;
110 char buf[80];
111 int len, len2, itemlen, show;
113 if(debug_snoop_excludelist) {
114 show = 1;
115 listitem = debug_snoop_excludelist;
116 } else {
117 show = 0;
118 listitem = debug_snoop_includelist;
120 len = strlen(dll);
121 assert(len < 64);
122 sprintf(buf, "%s.%d", dll, ord);
123 len2 = strlen(buf);
124 for(; *listitem; listitem++) {
125 itemlen = strlen(*listitem);
126 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
127 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
128 !strcmp(*listitem, fname)) {
129 show = !show;
130 break;
133 return show;
135 return 1;
138 void
139 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
140 SNOOP_DLL **dll = &(firstdll);
141 char *s;
143 if (!TRACE_ON(snoop)) return;
144 while (*dll) {
145 if ((*dll)->hmod == hmod)
146 return; /* already registered */
147 dll = &((*dll)->next);
149 *dll = (SNOOP_DLL*)HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
150 (*dll)->next = NULL;
151 (*dll)->hmod = hmod;
152 (*dll)->nrofordinals = nrofordinals;
153 (*dll)->name = HEAP_strdupA(SystemHeap,0,name);
154 if ((s=strrchr((*dll)->name,'.')))
155 *s='\0';
156 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
157 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
158 if (!(*dll)->funs) {
159 HeapFree(SystemHeap,0,*dll);
160 FIXME(snoop,"out of memory\n");
161 return;
165 FARPROC
166 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
167 SNOOP_DLL *dll = firstdll;
168 SNOOP_FUN *fun;
169 int j;
170 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
172 if (!TRACE_ON(snoop)) return origfun;
173 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
174 return origfun;
175 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
176 /* 0x42 is special ELF loader tag */
177 if ((pe_seg[j].VirtualAddress==0x42) ||
178 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
179 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
180 pe_seg[j].SizeOfRawData
183 break;
184 /* If we looked through all sections (and didn't find one)
185 * or if the sectionname contains "data", we return the
186 * original function since it is most likely a datareference.
188 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
189 (strstr(pe_seg[j].Name,"data")) ||
190 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
192 return origfun;
194 while (dll) {
195 if (hmod == dll->hmod)
196 break;
197 dll=dll->next;
199 if (!dll) /* probably internal */
200 return origfun;
201 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
202 return origfun;
203 assert(ordinal<dll->nrofordinals);
204 fun = dll->funs+ordinal;
205 if (!fun->name) fun->name = HEAP_strdupA(SystemHeap,0,name);
206 fun->lcall = 0xe8;
207 /* NOTE: origreturn struct member MUST come directly after snoopentry */
208 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
209 fun->origfun = origfun;
210 fun->nrofargs = -1;
211 return (FARPROC)&(fun->lcall);
214 static char*
215 SNOOP_PrintArg(DWORD x) {
216 static char buf[200];
217 int i,nostring;
218 MEMORY_BASIC_INFORMATION mbi;
220 if ( !HIWORD(x) ||
221 !VirtualQuery((LPVOID)x,&mbi,sizeof(mbi)) ||
222 !mbi.Type
224 sprintf(buf,"%08lx",x);
225 return buf;
227 i=0;nostring=0;
228 if (!IsBadStringPtrA((LPSTR)x,80)) {
229 while (i<80) {
230 LPBYTE s=(LPBYTE)x;
232 if (s[i]==0) break;
233 if (s[i]<0x20) {nostring=1;break;}
234 if (s[i]>=0x80) {nostring=1;break;}
235 i++;
237 if (!nostring) {
238 if (i>5) {
239 sprintf(buf,"%08lx \"",x);
240 strncat(buf,(LPSTR)x,198-strlen(buf)-2);
241 strcat(buf,"\"");
242 return buf;
246 i=0;nostring=0;
247 if (!IsBadStringPtrW((LPWSTR)x,80)) {
248 while (i<80) {
249 LPWSTR s=(LPWSTR)x;
251 if (s[i]==0) break;
252 if (s[i]<0x20) {nostring=1;break;}
253 if (s[i]>0x100) {nostring=1;break;}
254 i++;
256 if (!nostring) {
257 if (i>5) {
258 sprintf(buf,"%08lx L",x);
259 strcat(buf,debugstr_wn((LPWSTR)x,198-strlen(buf)-2));
260 return buf;
264 sprintf(buf,"%08lx",x);
265 return buf;
268 #define CALLER1REF (*(DWORD*)(ESP_reg(context)+4))
269 REGS_ENTRYPOINT(SNOOP_Entry) {
270 DWORD ordinal=0,entry = EIP_reg(context)-5;
271 SNOOP_DLL *dll = firstdll;
272 SNOOP_FUN *fun = NULL;
273 SNOOP_RETURNENTRIES **rets = &firstrets;
274 SNOOP_RETURNENTRY *ret;
275 int i,max;
277 while (dll) {
278 if ( ((char*)entry>=(char*)dll->funs) &&
279 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
281 fun = (SNOOP_FUN*)entry;
282 ordinal = fun-dll->funs;
283 break;
285 dll=dll->next;
287 if (!dll) {
288 FIXME(snoop,"entrypoint 0x%08lx not found\n",entry);
289 return; /* oops */
291 /* guess cdecl ... */
292 if (fun->nrofargs<0) {
293 /* Typical cdecl return frame is:
294 * add esp, xxxxxxxx
295 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
296 * (after that 81 C2 xx xx xx xx)
298 LPBYTE reteip = (LPBYTE)CALLER1REF;
300 if (reteip) {
301 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
302 fun->nrofargs=reteip[2]/4;
307 while (*rets) {
308 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
309 if (!(*rets)->entry[i].origreturn)
310 break;
311 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
312 break;
313 rets = &((*rets)->next);
315 if (!*rets) {
316 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
317 memset(*rets,0,4096);
318 i = 0; /* entry 0 is free */
320 ret = &((*rets)->entry[i]);
321 ret->lcall = 0xe8;
322 /* NOTE: origreturn struct member MUST come directly after snoopret */
323 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
324 ret->origreturn = (FARPROC)CALLER1REF;
325 CALLER1REF = (DWORD)&ret->lcall;
326 ret->dll = dll;
327 ret->args = NULL;
328 ret->ordinal = ordinal;
329 ret->origESP = ESP_reg(context);
331 EIP_reg(context)= (DWORD)fun->origfun;
333 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
334 if (fun->nrofargs>0) {
335 max = fun->nrofargs; if (max>16) max=16;
336 for (i=0;i<max;i++)
337 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+8+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
338 if (max!=fun->nrofargs)
339 DPRINTF(" ...");
340 } else if (fun->nrofargs<0) {
341 DPRINTF("<unknown, check return>");
342 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(DWORD));
343 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+8),sizeof(DWORD)*16);
345 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
348 REGS_ENTRYPOINT(SNOOP_Return) {
349 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(EIP_reg(context)-5);
351 /* We haven't found out the nrofargs yet. If we called a cdecl
352 * function it is too late anyway and we can just set '0' (which
353 * will be the difference between orig and current ESP
354 * If stdcall -> everything ok.
356 if (ret->dll->funs[ret->ordinal].nrofargs<0)
357 ret->dll->funs[ret->ordinal].nrofargs=(ESP_reg(context)-ret->origESP-4)/4;
358 EIP_reg(context) = (DWORD)ret->origreturn;
359 if (ret->args) {
360 int i,max;
362 DPRINTF("Ret %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
363 max = ret->dll->funs[ret->ordinal].nrofargs;
364 if (max>16) max=16;
366 for (i=0;i<max;i++)
367 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
368 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
369 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
371 HeapFree(SystemHeap,0,ret->args);
372 ret->args = NULL;
373 } else
374 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
375 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
376 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
378 ret->origreturn = NULL; /* mark as empty */
380 #else /* !__i386__ */
381 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
382 FIXME(snoop,"snooping works only on i386 for now.\n");
383 return;
386 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
387 return origfun;
389 #endif /* !__i386__ */