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