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