Avoid using TIOCM_* constants if not available.
[wine/multimedia.git] / relay32 / snoop.c
blobed7a402e6c4aaf77295d1e7e8d94123e58932a4f
1 /*
2 * 386-specific Win32 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "winbase.h"
13 #include "winnt.h"
14 #include "heap.h"
15 #include "snoop.h"
16 #include "neexe.h"
17 #include "selectors.h"
18 #include "stackframe.h"
19 #include "debugtools.h"
20 #include "wine/exception.h"
22 DEFAULT_DEBUG_CHANNEL(snoop);
24 static WINE_EXCEPTION_FILTER(page_fault)
26 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
27 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
28 return EXCEPTION_EXECUTE_HANDLER;
29 return EXCEPTION_CONTINUE_SEARCH;
32 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
34 #ifdef __i386__
36 extern void WINAPI SNOOP_Entry();
37 extern void WINAPI SNOOP_Return();
39 #ifdef NEED_UNDERSCORE_PREFIX
40 # define PREFIX "_"
41 #else
42 # define PREFIX
43 #endif
45 #include "pshpack1.h"
47 typedef struct tagSNOOP_FUN {
48 /* code part */
49 BYTE lcall; /* 0xe8 call snoopentry (relative) */
50 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
51 * calculation!
53 DWORD snoopentry; /* SNOOP_Entry relative */
54 /* unreached */
55 int nrofargs;
56 FARPROC origfun;
57 char *name;
58 } SNOOP_FUN;
60 typedef struct tagSNOOP_DLL {
61 HMODULE hmod;
62 SNOOP_FUN *funs;
63 LPCSTR name;
64 DWORD nrofordinals;
65 struct tagSNOOP_DLL *next;
66 } SNOOP_DLL;
67 typedef struct tagSNOOP_RETURNENTRY {
68 /* code part */
69 BYTE lcall; /* 0xe8 call snoopret relative*/
70 /* NOTE: If you move snoopret OR origreturn fix the relative offset
71 * calculation!
73 DWORD snoopret; /* SNOOP_Ret relative */
74 /* unreached */
75 FARPROC origreturn;
76 SNOOP_DLL *dll;
77 DWORD ordinal;
78 DWORD origESP;
79 DWORD *args; /* saved args across a stdcall */
80 } SNOOP_RETURNENTRY;
82 typedef struct tagSNOOP_RETURNENTRIES {
83 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
84 struct tagSNOOP_RETURNENTRIES *next;
85 } SNOOP_RETURNENTRIES;
87 #include "poppack.h"
89 static SNOOP_DLL *firstdll = NULL;
90 static SNOOP_RETURNENTRIES *firstrets = NULL;
92 /***********************************************************************
93 * SNOOP_ShowDebugmsgSnoop
95 * Simple function to decide if a particular debugging message is
96 * wanted.
98 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
100 if(debug_snoop_excludelist || debug_snoop_includelist) {
101 char **listitem;
102 char buf[80];
103 int len, len2, itemlen, show;
105 if(debug_snoop_excludelist) {
106 show = 1;
107 listitem = debug_snoop_excludelist;
108 } else {
109 show = 0;
110 listitem = debug_snoop_includelist;
112 len = strlen(dll);
113 assert(len < 64);
114 sprintf(buf, "%s.%d", dll, ord);
115 len2 = strlen(buf);
116 for(; *listitem; listitem++) {
117 itemlen = strlen(*listitem);
118 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
119 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
120 !strcmp(*listitem, fname)) {
121 show = !show;
122 break;
125 return show;
127 return 1;
130 void
131 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
132 SNOOP_DLL **dll = &(firstdll);
133 char *s;
135 if (!TRACE_ON(snoop)) return;
136 while (*dll) {
137 if ((*dll)->hmod == hmod)
138 return; /* already registered */
139 dll = &((*dll)->next);
141 *dll = (SNOOP_DLL*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
142 (*dll)->next = NULL;
143 (*dll)->hmod = hmod;
144 (*dll)->nrofordinals = nrofordinals;
145 (*dll)->name = HEAP_strdupA(GetProcessHeap(),0,name);
146 if ((s=strrchr((*dll)->name,'.')))
147 *s='\0';
148 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
149 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
150 if (!(*dll)->funs) {
151 HeapFree(GetProcessHeap(),0,*dll);
152 FIXME("out of memory\n");
153 return;
157 FARPROC
158 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
159 SNOOP_DLL *dll = firstdll;
160 SNOOP_FUN *fun;
161 int j;
162 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
164 if (!TRACE_ON(snoop)) return origfun;
165 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
166 return origfun;
167 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
168 /* 0x42 is special ELF loader tag */
169 if ((pe_seg[j].VirtualAddress==0x42) ||
170 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
171 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
172 pe_seg[j].SizeOfRawData
175 break;
176 /* If we looked through all sections (and didn't find one)
177 * or if the sectionname contains "data", we return the
178 * original function since it is most likely a datareference.
180 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
181 (strstr(pe_seg[j].Name,"data")) ||
182 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
184 return origfun;
186 while (dll) {
187 if (hmod == dll->hmod)
188 break;
189 dll=dll->next;
191 if (!dll) /* probably internal */
192 return origfun;
193 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
194 return origfun;
195 assert(ordinal < dll->nrofordinals);
196 fun = dll->funs+ordinal;
197 if (!fun->name) fun->name = HEAP_strdupA(GetProcessHeap(),0,name);
198 fun->lcall = 0xe8;
199 /* NOTE: origreturn struct member MUST come directly after snoopentry */
200 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
201 fun->origfun = origfun;
202 fun->nrofargs = -1;
203 return (FARPROC)&(fun->lcall);
206 static char*
207 SNOOP_PrintArg(DWORD x) {
208 static char buf[200];
209 int i,nostring;
210 char * volatile ret=0;
212 if ( !HIWORD(x) ) { /* trivial reject to avoid faults */
213 sprintf(buf,"%08lx",x);
214 return buf;
216 __TRY{
217 LPBYTE s=(LPBYTE)x;
218 i=0;nostring=0;
219 while (i<80) {
220 if (s[i]==0) break;
221 if (s[i]<0x20) {nostring=1;break;}
222 if (s[i]>=0x80) {nostring=1;break;}
223 i++;
225 if (!nostring) {
226 if (i>5) {
227 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_an((LPSTR)x,sizeof(buf)-10));
228 ret=buf;
232 __EXCEPT(page_fault){}
233 __ENDTRY
234 if (ret)
235 return ret;
236 __TRY{
237 LPWSTR s=(LPWSTR)x;
238 i=0;nostring=0;
239 while (i<80) {
240 if (s[i]==0) break;
241 if (s[i]<0x20) {nostring=1;break;}
242 if (s[i]>0x100) {nostring=1;break;}
243 i++;
245 if (!nostring) {
246 if (i>5) {
247 snprintf(buf,sizeof(buf),"%08lx %s",x,debugstr_wn((LPWSTR)x,sizeof(buf)-10));
248 ret=buf;
252 __EXCEPT(page_fault){}
253 __ENDTRY
254 if (ret)
255 return ret;
256 sprintf(buf,"%08lx",x);
257 return buf;
260 #define CALLER1REF (*(DWORD*)context->Esp)
262 void WINAPI SNOOP_DoEntry( CONTEXT86 *context );
263 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Entry, SNOOP_DoEntry );
264 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
266 DWORD ordinal=0,entry = context->Eip - 5;
267 SNOOP_DLL *dll = firstdll;
268 SNOOP_FUN *fun = NULL;
269 SNOOP_RETURNENTRIES **rets = &firstrets;
270 SNOOP_RETURNENTRY *ret;
271 int i=0, max;
273 while (dll) {
274 if ( ((char*)entry>=(char*)dll->funs) &&
275 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
277 fun = (SNOOP_FUN*)entry;
278 ordinal = fun-dll->funs;
279 break;
281 dll=dll->next;
283 if (!dll) {
284 FIXME("entrypoint 0x%08lx not found\n",entry);
285 return; /* oops */
287 /* guess cdecl ... */
288 if (fun->nrofargs<0) {
289 /* Typical cdecl return frame is:
290 * add esp, xxxxxxxx
291 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
292 * (after that 81 C2 xx xx xx xx)
294 LPBYTE reteip = (LPBYTE)CALLER1REF;
296 if (reteip) {
297 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
298 fun->nrofargs=reteip[2]/4;
303 while (*rets) {
304 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
305 if (!(*rets)->entry[i].origreturn)
306 break;
307 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
308 break;
309 rets = &((*rets)->next);
311 if (!*rets) {
312 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
313 memset(*rets,0,4096);
314 i = 0; /* entry 0 is free */
316 ret = &((*rets)->entry[i]);
317 ret->lcall = 0xe8;
318 /* NOTE: origreturn struct member MUST come directly after snoopret */
319 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
320 ret->origreturn = (FARPROC)CALLER1REF;
321 CALLER1REF = (DWORD)&ret->lcall;
322 ret->dll = dll;
323 ret->args = NULL;
324 ret->ordinal = ordinal;
325 ret->origESP = context->Esp;
327 context->Eip = (DWORD)fun->origfun;
329 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
330 if (fun->nrofargs>0) {
331 max = fun->nrofargs; if (max>16) max=16;
332 for (i=0;i<max;i++)
333 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
334 if (max!=fun->nrofargs)
335 DPRINTF(" ...");
336 } else if (fun->nrofargs<0) {
337 DPRINTF("<unknown, check return>");
338 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(DWORD));
339 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
341 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,context->SegFs);
344 void WINAPI SNOOP_DoReturn( CONTEXT86 *context );
345 DEFINE_REGS_ENTRYPOINT_0( SNOOP_Return, SNOOP_DoReturn );
346 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
348 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
350 /* We haven't found out the nrofargs yet. If we called a cdecl
351 * function it is too late anyway and we can just set '0' (which
352 * will be the difference between orig and current ESP
353 * If stdcall -> everything ok.
355 if (ret->dll->funs[ret->ordinal].nrofargs<0)
356 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
357 context->Eip = (DWORD)ret->origreturn;
358 if (ret->args) {
359 int i,max;
361 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
362 max = ret->dll->funs[ret->ordinal].nrofargs;
363 if (max>16) max=16;
365 for (i=0;i<max;i++)
366 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
367 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
368 context->Eax,(DWORD)ret->origreturn,context->SegFs );
369 HeapFree(GetProcessHeap(),0,ret->args);
370 ret->args = NULL;
371 } else
372 DPRINTF("RET %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
373 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
374 context->Eax,(DWORD)ret->origreturn,context->SegFs );
375 ret->origreturn = NULL; /* mark as empty */
377 #else /* !__i386__ */
378 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
379 FIXME("snooping works only on i386 for now.\n");
380 return;
383 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
384 return origfun;
386 #endif /* !__i386__ */