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