#pragma pack(?) changed to #include "*pack*.h"
[wine.git] / relay32 / snoop.c
blob7950984171b578b2046a8efef0ebd7c8cf157654
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 DEFAULT_DEBUG_CHANNEL(snoop)
24 char **debug_snoop_excludelist = NULL, **debug_snoop_includelist = NULL;
26 #ifdef __i386__
28 #ifdef NEED_UNDERSCORE_PREFIX
29 # define PREFIX "_"
30 #else
31 # define PREFIX
32 #endif
34 /* Well, not exactly extern since they are in the same file (in the lines
35 * below). But the C Compiler doesn't see them there, so we have to help a bit.
37 extern void SNOOP_Return();
38 extern void SNOOP_Entry();
39 __asm__(".align 4\n\t"
40 ".globl "PREFIX"SNOOP_Entry\n\t"
41 ".type "PREFIX"SNOOP_Entry,@function\n\t"
42 PREFIX"SNOOP_Entry:\n\t"
43 "pushl $"PREFIX"__regs_SNOOP_Entry\n\t"
44 "pushl $"PREFIX"CALL32_Regs\n\t"
45 "ret\n\t"
46 ".align 4\n\t"
47 ".globl "PREFIX"SNOOP_Return\n\t"
48 ".type "PREFIX"SNOOP_Return,@function\n\t"
49 PREFIX"SNOOP_Return:\n\t"
50 "pushl $"PREFIX"__regs_SNOOP_Return\n\t"
51 "pushl $"PREFIX"CALL32_Regs\n\t"
52 "ret"
55 #include "pshpack1.h"
57 typedef struct tagSNOOP_FUN {
58 /* code part */
59 BYTE lcall; /* 0xe8 call snoopentry (relative) */
60 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
61 * calculation!
63 DWORD snoopentry; /* SNOOP_Entry relative */
64 /* unreached */
65 int nrofargs;
66 FARPROC origfun;
67 char *name;
68 } SNOOP_FUN;
70 typedef struct tagSNOOP_DLL {
71 HMODULE hmod;
72 SNOOP_FUN *funs;
73 LPCSTR name;
74 int nrofordinals;
75 struct tagSNOOP_DLL *next;
76 } SNOOP_DLL;
77 typedef struct tagSNOOP_RETURNENTRY {
78 /* code part */
79 BYTE lcall; /* 0xe8 call snoopret relative*/
80 /* NOTE: If you move snoopret OR origreturn fix the relative offset
81 * calculation!
83 DWORD snoopret; /* SNOOP_Ret relative */
84 /* unreached */
85 FARPROC origreturn;
86 SNOOP_DLL *dll;
87 DWORD ordinal;
88 DWORD origESP;
89 DWORD *args; /* saved args across a stdcall */
90 } SNOOP_RETURNENTRY;
92 typedef struct tagSNOOP_RETURNENTRIES {
93 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
94 struct tagSNOOP_RETURNENTRIES *next;
95 } SNOOP_RETURNENTRIES;
97 #include "poppack.h"
99 static SNOOP_DLL *firstdll = NULL;
100 static SNOOP_RETURNENTRIES *firstrets = NULL;
102 /***********************************************************************
103 * SNOOP_ShowDebugmsgSnoop
105 * Simple function to decide if a particular debugging message is
106 * wanted.
108 int SNOOP_ShowDebugmsgSnoop(const char *dll, int ord, const char *fname) {
110 if(debug_snoop_excludelist || debug_snoop_includelist) {
111 char **listitem;
112 char buf[80];
113 int len, len2, itemlen, show;
115 if(debug_snoop_excludelist) {
116 show = 1;
117 listitem = debug_snoop_excludelist;
118 } else {
119 show = 0;
120 listitem = debug_snoop_includelist;
122 len = strlen(dll);
123 assert(len < 64);
124 sprintf(buf, "%s.%d", dll, ord);
125 len2 = strlen(buf);
126 for(; *listitem; listitem++) {
127 itemlen = strlen(*listitem);
128 if((itemlen == len && !strncmp(*listitem, buf, len)) ||
129 (itemlen == len2 && !strncmp(*listitem, buf, len2)) ||
130 !strcmp(*listitem, fname)) {
131 show = !show;
132 break;
135 return show;
137 return 1;
140 void
141 SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
142 SNOOP_DLL **dll = &(firstdll);
143 char *s;
145 if (!TRACE_ON(snoop)) return;
146 while (*dll) {
147 if ((*dll)->hmod == hmod)
148 return; /* already registered */
149 dll = &((*dll)->next);
151 *dll = (SNOOP_DLL*)HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(SNOOP_DLL));
152 (*dll)->next = NULL;
153 (*dll)->hmod = hmod;
154 (*dll)->nrofordinals = nrofordinals;
155 (*dll)->name = HEAP_strdupA(SystemHeap,0,name);
156 if ((s=strrchr((*dll)->name,'.')))
157 *s='\0';
158 (*dll)->funs = VirtualAlloc(NULL,nrofordinals*sizeof(SNOOP_FUN),MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
159 memset((*dll)->funs,0,nrofordinals*sizeof(SNOOP_FUN));
160 if (!(*dll)->funs) {
161 HeapFree(SystemHeap,0,*dll);
162 FIXME(snoop,"out of memory\n");
163 return;
167 FARPROC
168 SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
169 SNOOP_DLL *dll = firstdll;
170 SNOOP_FUN *fun;
171 int j;
172 IMAGE_SECTION_HEADER *pe_seg = PE_SECTIONS(hmod);
174 if (!TRACE_ON(snoop)) return origfun;
175 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
176 return origfun;
177 for (j=0;j<PE_HEADER(hmod)->FileHeader.NumberOfSections;j++)
178 /* 0x42 is special ELF loader tag */
179 if ((pe_seg[j].VirtualAddress==0x42) ||
180 (((DWORD)origfun-hmod>=pe_seg[j].VirtualAddress)&&
181 ((DWORD)origfun-hmod <pe_seg[j].VirtualAddress+
182 pe_seg[j].SizeOfRawData
185 break;
186 /* If we looked through all sections (and didn't find one)
187 * or if the sectionname contains "data", we return the
188 * original function since it is most likely a datareference.
190 if ( (j==PE_HEADER(hmod)->FileHeader.NumberOfSections) ||
191 (strstr(pe_seg[j].Name,"data")) ||
192 !(pe_seg[j].Characteristics & IMAGE_SCN_CNT_CODE)
194 return origfun;
196 while (dll) {
197 if (hmod == dll->hmod)
198 break;
199 dll=dll->next;
201 if (!dll) /* probably internal */
202 return origfun;
203 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,name))
204 return origfun;
205 assert(ordinal<dll->nrofordinals);
206 fun = dll->funs+ordinal;
207 if (!fun->name) fun->name = HEAP_strdupA(SystemHeap,0,name);
208 fun->lcall = 0xe8;
209 /* NOTE: origreturn struct member MUST come directly after snoopentry */
210 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
211 fun->origfun = origfun;
212 fun->nrofargs = -1;
213 return (FARPROC)&(fun->lcall);
216 static char*
217 SNOOP_PrintArg(DWORD x) {
218 static char buf[200];
219 int i,nostring;
220 MEMORY_BASIC_INFORMATION mbi;
222 if ( !HIWORD(x) ||
223 !VirtualQuery((LPVOID)x,&mbi,sizeof(mbi)) ||
224 !mbi.Type
226 sprintf(buf,"%08lx",x);
227 return buf;
229 i=0;nostring=0;
230 if (!IsBadStringPtrA((LPSTR)x,80)) {
231 while (i<80) {
232 LPBYTE s=(LPBYTE)x;
234 if (s[i]==0) break;
235 if (s[i]<0x20) {nostring=1;break;}
236 if (s[i]>=0x80) {nostring=1;break;}
237 i++;
239 if (!nostring) {
240 if (i>5) {
241 sprintf(buf,"%08lx \"",x);
242 strncat(buf,(LPSTR)x,198-strlen(buf)-2);
243 strcat(buf,"\"");
244 return buf;
248 i=0;nostring=0;
249 if (!IsBadStringPtrW((LPWSTR)x,80)) {
250 while (i<80) {
251 LPWSTR s=(LPWSTR)x;
253 if (s[i]==0) break;
254 if (s[i]<0x20) {nostring=1;break;}
255 if (s[i]>0x100) {nostring=1;break;}
256 i++;
258 if (!nostring) {
259 if (i>5) {
260 sprintf(buf,"%08lx L",x);
261 strcat(buf,debugstr_wn((LPWSTR)x,198-strlen(buf)-2));
262 return buf;
266 sprintf(buf,"%08lx",x);
267 return buf;
270 #define CALLER1REF (*(DWORD*)(ESP_reg(context)+4))
271 REGS_ENTRYPOINT(SNOOP_Entry) {
272 DWORD ordinal=0,entry = EIP_reg(context)-5;
273 SNOOP_DLL *dll = firstdll;
274 SNOOP_FUN *fun = NULL;
275 SNOOP_RETURNENTRIES **rets = &firstrets;
276 SNOOP_RETURNENTRY *ret;
277 int i,max;
279 while (dll) {
280 if ( ((char*)entry>=(char*)dll->funs) &&
281 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
283 fun = (SNOOP_FUN*)entry;
284 ordinal = fun-dll->funs;
285 break;
287 dll=dll->next;
289 if (!dll) {
290 FIXME(snoop,"entrypoint 0x%08lx not found\n",entry);
291 return; /* oops */
293 /* guess cdecl ... */
294 if (fun->nrofargs<0) {
295 /* Typical cdecl return frame is:
296 * add esp, xxxxxxxx
297 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
298 * (after that 81 C2 xx xx xx xx)
300 LPBYTE reteip = (LPBYTE)CALLER1REF;
302 if (reteip) {
303 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
304 fun->nrofargs=reteip[2]/4;
309 while (*rets) {
310 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
311 if (!(*rets)->entry[i].origreturn)
312 break;
313 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
314 break;
315 rets = &((*rets)->next);
317 if (!*rets) {
318 *rets = VirtualAlloc(NULL,4096,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
319 memset(*rets,0,4096);
320 i = 0; /* entry 0 is free */
322 ret = &((*rets)->entry[i]);
323 ret->lcall = 0xe8;
324 /* NOTE: origreturn struct member MUST come directly after snoopret */
325 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
326 ret->origreturn = (FARPROC)CALLER1REF;
327 CALLER1REF = (DWORD)&ret->lcall;
328 ret->dll = dll;
329 ret->args = NULL;
330 ret->ordinal = ordinal;
331 ret->origESP = ESP_reg(context);
333 EIP_reg(context)= (DWORD)fun->origfun;
335 DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
336 if (fun->nrofargs>0) {
337 max = fun->nrofargs; if (max>16) max=16;
338 for (i=0;i<max;i++)
339 DPRINTF("%s%s",SNOOP_PrintArg(*(DWORD*)(ESP_reg(context)+8+sizeof(DWORD)*i)),(i<fun->nrofargs-1)?",":"");
340 if (max!=fun->nrofargs)
341 DPRINTF(" ...");
342 } else if (fun->nrofargs<0) {
343 DPRINTF("<unknown, check return>");
344 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(DWORD));
345 memcpy(ret->args,(LPBYTE)(ESP_reg(context)+8),sizeof(DWORD)*16);
347 DPRINTF(") ret=%08lx fs=%04lx\n",(DWORD)ret->origreturn,FS_reg(context));
350 REGS_ENTRYPOINT(SNOOP_Return) {
351 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(EIP_reg(context)-5);
353 /* We haven't found out the nrofargs yet. If we called a cdecl
354 * function it is too late anyway and we can just set '0' (which
355 * will be the difference between orig and current ESP
356 * If stdcall -> everything ok.
358 if (ret->dll->funs[ret->ordinal].nrofargs<0)
359 ret->dll->funs[ret->ordinal].nrofargs=(ESP_reg(context)-ret->origESP-4)/4;
360 EIP_reg(context) = (DWORD)ret->origreturn;
361 if (ret->args) {
362 int i,max;
364 DPRINTF("Ret %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
365 max = ret->dll->funs[ret->ordinal].nrofargs;
366 if (max>16) max=16;
368 for (i=0;i<max;i++)
369 DPRINTF("%s%s",SNOOP_PrintArg(ret->args[i]),(i<max-1)?",":"");
370 DPRINTF(") retval = %08lx ret=%08lx fs=%04lx\n",
371 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
373 HeapFree(SystemHeap,0,ret->args);
374 ret->args = NULL;
375 } else
376 DPRINTF("Ret %s.%ld: %s() retval = %08lx ret=%08lx fs=%04lx\n",
377 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
378 EAX_reg(context),(DWORD)ret->origreturn,FS_reg(context)
380 ret->origreturn = NULL; /* mark as empty */
382 #else /* !__i386__ */
383 void SNOOP_RegisterDLL(HMODULE hmod,LPCSTR name,DWORD nrofordinals) {
384 FIXME(snoop,"snooping works only on i386 for now.\n");
385 return;
388 FARPROC SNOOP_GetProcAddress(HMODULE hmod,LPCSTR name,DWORD ordinal,FARPROC origfun) {
389 return origfun;
391 #endif /* !__i386__ */