Move the name directive from the .spec file to the Makefile.
[wine/gsoc_dplay.git] / if1632 / snoop.c
blob9d9dbf25081f42a2a3cb7bd8d7a691460fd1138e
1 /*
2 * 386-specific Win16 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 <string.h>
26 #include <stdio.h>
27 #include "winbase.h"
28 #include "windef.h"
29 #include "winnt.h"
30 #include "wine/winbase16.h"
31 #include "wine/library.h"
32 #include "global.h"
33 #include "stackframe.h"
34 #include "builtin16.h"
35 #include "toolhelp.h"
36 #include "snoop.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
41 #ifdef __i386__
43 #include "pshpack1.h"
45 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
46 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
48 typedef struct tagSNOOP16_FUN {
49 /* code part */
50 BYTE lcall; /* 0x9a call absolute with segment */
51 DWORD snr;
52 /* unreached */
53 int nrofargs;
54 FARPROC16 origfun;
55 char *name;
56 } SNOOP16_FUN;
58 typedef struct tagSNOOP16_DLL {
59 HMODULE16 hmod;
60 HANDLE16 funhandle;
61 SNOOP16_FUN *funs;
62 struct tagSNOOP16_DLL *next;
63 char name[1];
64 } SNOOP16_DLL;
66 typedef struct tagSNOOP16_RETURNENTRY {
67 /* code part */
68 BYTE lcall; /* 0x9a call absolute with segment */
69 DWORD snr;
70 /* unreached */
71 FARPROC16 origreturn;
72 SNOOP16_DLL *dll;
73 DWORD ordinal;
74 WORD origSP;
75 WORD *args; /* saved args across a stdcall */
76 } SNOOP16_RETURNENTRY;
78 typedef struct tagSNOOP16_RETURNENTRIES {
79 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
80 HANDLE16 rethandle;
81 struct tagSNOOP16_RETURNENTRIES *next;
82 } SNOOP16_RETURNENTRIES;
84 typedef struct tagSNOOP16_RELAY {
85 WORD pushbp; /* 0x5566 */
86 BYTE pusheax; /* 0x50 */
87 WORD pushax; /* 0x5066 */
88 BYTE pushl; /* 0x68 */
89 DWORD realfun; /* SNOOP16_Return */
90 BYTE lcall; /* 0x9a call absolute with segment */
91 DWORD callfromregs;
92 WORD seg;
93 WORD lret; /* 0xcb66 */
94 } SNOOP16_RELAY;
96 #include "poppack.h"
98 static SNOOP16_DLL *firstdll = NULL;
99 static SNOOP16_RETURNENTRIES *firstrets = NULL;
100 static SNOOP16_RELAY *snr;
101 static HANDLE16 xsnr = 0;
103 void
104 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
105 SNOOP16_DLL **dll = &(firstdll);
106 char *s;
108 if (!TRACE_ON(snoop)) return;
110 TRACE("hmod=%x, name=%s\n", pModule->self, name);
112 if (!snr) {
113 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
114 snr = GlobalLock16(xsnr);
115 snr[0].pushbp = 0x5566;
116 snr[0].pusheax = 0x50;
117 snr[0].pushax = 0x5066;
118 snr[0].pushl = 0x68;
119 snr[0].realfun = (DWORD)SNOOP16_Entry;
120 snr[0].lcall = 0x9a;
121 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
122 snr[0].seg = wine_get_cs();
123 snr[0].lret = 0xcb66;
125 snr[1].pushbp = 0x5566;
126 snr[1].pusheax = 0x50;
127 snr[1].pushax = 0x5066;
128 snr[1].pushl = 0x68;
129 snr[1].realfun = (DWORD)SNOOP16_Return;
130 snr[1].lcall = 0x9a;
131 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
132 snr[1].seg = wine_get_cs();
133 snr[1].lret = 0xcb66;
135 while (*dll) {
136 if ((*dll)->hmod == pModule->self)
138 /* another dll, loaded at the same address */
139 GlobalUnlock16((*dll)->funhandle);
140 GlobalFree16((*dll)->funhandle);
141 break;
143 dll = &((*dll)->next);
145 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
146 (*dll)->next = NULL;
147 (*dll)->hmod = pModule->self;
148 if ((s=strrchr(name,'\\')))
149 name = s+1;
150 strcpy( (*dll)->name, name );
151 if ((s=strrchr((*dll)->name,'.')))
152 *s='\0';
153 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
154 (*dll)->funs = GlobalLock16((*dll)->funhandle);
155 if (!(*dll)->funs) {
156 HeapFree(GetProcessHeap(),0,*dll);
157 FIXME("out of memory\n");
158 return;
162 FARPROC16
163 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
164 SNOOP16_DLL *dll = firstdll;
165 SNOOP16_FUN *fun;
166 NE_MODULE *pModule = NE_GetPtr(hmod);
167 unsigned char *cpnt;
168 char name[200];
170 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
171 return origfun;
172 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
173 return origfun;
174 while (dll) {
175 if (hmod == dll->hmod)
176 break;
177 dll=dll->next;
179 if (!dll) /* probably internal */
180 return origfun;
181 if (ordinal>65535/sizeof(SNOOP16_FUN))
182 return origfun;
183 fun = dll->funs+ordinal;
184 /* already done? */
185 fun->lcall = 0x9a;
186 fun->snr = MAKELONG(0,xsnr);
187 fun->origfun = origfun;
188 if (fun->name)
189 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
190 cpnt = (unsigned char *)pModule + pModule->name_table;
191 while (*cpnt) {
192 cpnt += *cpnt + 1 + sizeof(WORD);
193 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
194 sprintf(name,"%.*s",*cpnt,cpnt+1);
195 break;
198 /* Now search the non-resident names table */
200 if (!*cpnt && pModule->nrname_handle) {
201 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
202 while (*cpnt) {
203 cpnt += *cpnt + 1 + sizeof(WORD);
204 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
205 sprintf(name,"%.*s",*cpnt,cpnt+1);
206 break;
210 if (*cpnt)
212 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
213 strcpy( fun->name, name );
215 else
216 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
218 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
219 return origfun;
221 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
222 if (strchr(fun->name,'_')) {
223 char *s=strchr(fun->name,'_');
225 if (!strncasecmp(s,"_thunkdata",10)) {
226 HeapFree(GetProcessHeap(),0,fun->name);
227 fun->name = NULL;
228 return origfun;
231 fun->lcall = 0x9a;
232 fun->snr = MAKELONG(0,xsnr);
233 fun->origfun = origfun;
234 fun->nrofargs = -1;
235 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
238 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
239 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
240 DWORD ordinal=0;
241 DWORD entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
242 WORD xcs = context->SegCs;
243 SNOOP16_DLL *dll = firstdll;
244 SNOOP16_FUN *fun = NULL;
245 SNOOP16_RETURNENTRIES **rets = &firstrets;
246 SNOOP16_RETURNENTRY *ret;
247 int i=0, max;
249 while (dll) {
250 if (xcs == dll->funhandle) {
251 fun = (SNOOP16_FUN*)entry;
252 ordinal = fun-dll->funs;
253 break;
255 dll=dll->next;
257 if (!dll) {
258 FIXME("entrypoint 0x%08lx not found\n",entry);
259 return; /* oops */
261 while (*rets) {
262 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
263 if (!(*rets)->entry[i].origreturn)
264 break;
265 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
266 break;
267 rets = &((*rets)->next);
269 if (!*rets) {
270 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
271 *rets = GlobalLock16(hand);
272 (*rets)->rethandle = hand;
273 i = 0; /* entry 0 is free */
275 ret = &((*rets)->entry[i]);
276 ret->lcall = 0x9a;
277 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
278 ret->origreturn = (FARPROC16)CALLER1REF;
279 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
280 ret->dll = dll;
281 ret->args = NULL;
282 ret->ordinal = ordinal;
283 ret->origSP = LOWORD(context->Esp);
285 context->Eip= LOWORD(fun->origfun);
286 context->SegCs = HIWORD(fun->origfun);
289 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
290 if (fun->nrofargs>0) {
291 max = fun->nrofargs;
292 if (max>16) max=16;
293 for (i=max;i--;)
294 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
295 if (max!=fun->nrofargs)
296 DPRINTF(" ...");
297 } else if (fun->nrofargs<0) {
298 DPRINTF("<unknown, check return>");
299 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
300 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
302 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
305 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
306 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
308 /* We haven't found out the nrofargs yet. If we called a cdecl
309 * function it is too late anyway and we can just set '0' (which
310 * will be the difference between orig and current SP
311 * If pascal -> everything ok.
313 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
314 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
316 context->Eip = LOWORD(ret->origreturn);
317 context->SegCs = HIWORD(ret->origreturn);
318 if (ret->args) {
319 int i,max;
321 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
322 max = ret->dll->funs[ret->ordinal].nrofargs;
323 if (max>16)
324 max=16;
325 if (max<0)
326 max=0;
328 for (i=max;i--;)
329 DPRINTF("%04x%s",ret->args[i],i?",":"");
330 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
331 DPRINTF(" ...");
332 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
333 (WORD)context->Edx,(WORD)context->Eax,
334 HIWORD(ret->origreturn),LOWORD(ret->origreturn)
336 HeapFree(GetProcessHeap(),0,ret->args);
337 ret->args = NULL;
338 } else
339 DPRINTF("RET %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
340 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
341 (WORD)context->Edx,(WORD)context->Eax,
342 HIWORD(ret->origreturn),LOWORD(ret->origreturn)
344 ret->origreturn = NULL; /* mark as empty */
346 #else /* !__i386__ */
347 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
348 if (!TRACE_ON(snoop)) return;
349 FIXME("snooping works only on i386 for now.\n");
352 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
353 return origfun;
355 #endif /* !__i386__ */