Fixed some undocumented functions.
[wine] / if1632 / snoop.c
1 /*
2  * 386-specific Win16 dll<->dll snooping functions
3  *
4  * Copyright 1998 Marcus Meissner
5  */
6
7 #ifdef __i386__
8
9 #include <assert.h>
10 #include "windows.h"
11 #include "winbase.h"
12 #include "winnt.h"
13 #include "heap.h"
14 #include "global.h"
15 #include "selectors.h"
16 #include "stackframe.h"
17 #include "snoop.h"
18 #include "debugstr.h"
19 #include "debug.h"
20
21 #pragma pack(1)
22
23 void WINAPI SNOOP16_Entry(CONTEXT *context);
24 void WINAPI SNOOP16_Return(CONTEXT *context);
25 extern void CallFrom16_p_regs_();
26
27 /* Generic callfrom16_p_regs function entry.
28  *      pushw %bp                       0x55
29  *      pushl $DOS3Call                 DWORD fun32
30  *      .byte 0x9a                      0x9a
31  *      .long CallFrom16_p_regs_        DWORD addr
32  *      .long 0x90900023                WORD seg;nop;nop
33  */
34
35 typedef struct tagSNOOP16_FUN {
36         /* code part */
37         BYTE            lcall;          /* 0x9a call absolute with segment */
38         DWORD           snr;
39         /* unreached */
40         int             nrofargs;
41         FARPROC16       origfun;
42         char            *name;
43 } SNOOP16_FUN;
44
45 typedef struct tagSNOOP16_DLL {
46         HMODULE16       hmod;
47         HANDLE16        funhandle;
48         SNOOP16_FUN     *funs;
49         LPCSTR          name;
50         struct tagSNOOP16_DLL   *next;
51 } SNOOP16_DLL;
52
53 typedef struct tagSNOOP16_RETURNENTRY {
54         /* code part */
55         BYTE            lcall;          /* 0x9a call absolute with segment */
56         DWORD           snr;
57         /* unreached */
58         FARPROC16       origreturn;
59         SNOOP16_DLL     *dll;
60         DWORD           ordinal;
61         WORD            origSP;
62         WORD            *args;          /* saved args across a stdcall */
63         BYTE            show;
64 } SNOOP16_RETURNENTRY;
65
66 typedef struct tagSNOOP16_RETURNENTRIES {
67         SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
68         HANDLE16        rethandle;
69         struct tagSNOOP16_RETURNENTRIES *next;
70 } SNOOP16_RETURNENTRIES;
71
72 typedef struct tagSNOOP16_RELAY {
73         /* code part */
74         BYTE            prefix;         /* 0x66 , 32bit prefix */
75         BYTE            pushbp;         /* 0x55 */
76         BYTE            pushl;          /* 0x68 */
77         DWORD           realfun;        /* SNOOP16_Return */
78         BYTE            lcall;          /* 0x9a call absolute with segment */
79         DWORD           callfromregs;
80         WORD            seg;
81         /* unreached */
82 } SNOOP16_RELAY;
83
84 #pragma pack(4)
85
86 static  SNOOP16_DLL             *firstdll = NULL;
87 static  SNOOP16_RETURNENTRIES   *firstrets = NULL;
88 static  SNOOP16_RELAY           *snr;
89 static  HANDLE16                xsnr = 0;
90
91 void
92 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
93         SNOOP16_DLL     **dll = &(firstdll);
94         char            *s;
95
96         if (!TRACE_ON(snoop)) return;
97         if (!snr) {
98                 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,TRUE,TRUE,FALSE);
99                 snr = GlobalLock16(xsnr);
100                 snr[0].prefix   = 0x66;
101                 snr[0].pushbp   = 0x55;
102                 snr[0].pushl    = 0x68;
103                 snr[0].realfun  = (DWORD)SNOOP16_Entry;
104                 snr[0].lcall    = 0x9a;
105                 snr[0].callfromregs = (DWORD)CallFrom16_p_regs_;
106                 GET_CS(snr[0].seg);
107                 snr[1].prefix   = 0x66;
108                 snr[1].pushbp   = 0x55;
109                 snr[1].pushl    = 0x68;
110                 snr[1].realfun  = (DWORD)SNOOP16_Return;
111                 snr[1].lcall    = 0x9a;
112                 snr[1].callfromregs = (DWORD)CallFrom16_p_regs_;
113                 GET_CS(snr[1].seg);
114         }
115         while (*dll) {
116                 if ((*dll)->hmod == pModule->self)
117                         return; /* already registered */
118                 dll = &((*dll)->next);
119         }
120         *dll = (SNOOP16_DLL*)HeapAlloc(SystemHeap,HEAP_ZERO_MEMORY,sizeof(SNOOP16_DLL));
121         (*dll)->next    = NULL;
122         (*dll)->hmod    = pModule->self;
123         if ((s=strrchr(name,'\\')))
124                 name = s+1;
125         (*dll)->name    = HEAP_strdupA(SystemHeap,0,name);
126         if ((s=strrchr((*dll)->name,'.')))
127                 *s='\0';
128         (*dll)->funhandle = GlobalHandleToSel(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
129         (*dll)->funs = GlobalLock16((*dll)->funhandle);
130         if (!(*dll)->funs) {
131                 HeapFree(SystemHeap,0,*dll);
132                 FIXME(snoop,"out of memory\n");
133                 return;
134         }
135         memset((*dll)->funs,0,65535);
136 }
137
138 FARPROC16
139 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
140         SNOOP16_DLL                     *dll = firstdll;
141         SNOOP16_FUN                     *fun;
142         NE_MODULE                       *pModule = NE_GetPtr(hmod);
143         unsigned char                   *cpnt;
144         char                            name[200];
145
146         if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
147                 return origfun;
148         if (!*(LPBYTE)PTR_SEG_TO_LIN(origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
149                 return origfun;
150         while (dll) {
151                 if (hmod == dll->hmod)
152                         break;
153                 dll=dll->next;
154         }
155         if (!dll)       /* probably internal */
156                 return origfun;
157         if (ordinal>65535/sizeof(SNOOP16_FUN))
158                 return origfun;
159         fun = dll->funs+ordinal;
160         /* already done? */
161         fun->lcall      = 0x9a;
162         fun->snr        = MAKELONG(0,xsnr);
163         fun->origfun    = origfun;
164         if (fun->name)
165                 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
166         cpnt = (unsigned char *)pModule + pModule->name_table;
167         while (*cpnt) {
168                 cpnt += *cpnt + 1 + sizeof(WORD);
169                 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
170                         sprintf(name,"%.*s",*cpnt,cpnt+1);
171                         break;
172                 }
173         }
174         /* Now search the non-resident names table */
175
176         if (!*cpnt && pModule->nrname_handle) {
177                 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
178                 while (*cpnt) {
179                         cpnt += *cpnt + 1 + sizeof(WORD);
180                         if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
181                                     sprintf(name,"%.*s",*cpnt,cpnt+1);
182                                     break;
183                         }
184                 }
185         }
186         if (*cpnt)
187                 fun->name = HEAP_strdupA(SystemHeap,0,name);
188         else
189                 fun->name = HEAP_strdupA(SystemHeap,0,"");
190         /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
191         if (strchr(fun->name,'_')) {
192                 char *s=strchr(fun->name,'_');
193
194                 if (!strncasecmp(s,"_thunkdata",10)) {
195                         HeapFree(SystemHeap,0,fun->name);
196                         fun->name = NULL;
197                         return origfun;
198                 }
199         }
200         fun->lcall      = 0x9a;
201         fun->snr        = MAKELONG(0,xsnr);
202         fun->origfun    = origfun;
203         fun->nrofargs   = -1;
204         return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
205 }
206
207 #define CALLER1REF (*(DWORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),SP_reg(context)+4)))
208 void WINAPI SNOOP16_Entry(CONTEXT *context) {
209         DWORD           ordinal=0;
210         DWORD           entry=(DWORD)PTR_SEG_OFF_TO_LIN(CS_reg(context),IP_reg(context))-5;
211         WORD            xcs = CS_reg(context);
212         SNOOP16_DLL     *dll = firstdll;
213         SNOOP16_FUN     *fun = NULL;
214         SNOOP16_RETURNENTRIES   **rets = &firstrets;
215         SNOOP16_RETURNENTRY     *ret;
216         int             i,max;
217         /* from relay32/snoop.c */
218         extern int SNOOP_ShowDebugmsgSnoop(const char *dll,int ord,const char *fname);
219
220         while (dll) {
221                 if (xcs == dll->funhandle) {
222                         fun = (SNOOP16_FUN*)entry;
223                         ordinal = fun-dll->funs;
224                         break;
225                 }
226                 dll=dll->next;
227         }
228         if (!dll) {
229                 FIXME(snoop,"entrypoint 0x%08lx not found\n",entry);
230                 return; /* oops */
231         }
232         while (*rets) {
233                 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
234                         if (!(*rets)->entry[i].origreturn)
235                                 break;
236                 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
237                         break;
238                 rets = &((*rets)->next);
239         }
240         if (!*rets) {
241                 HANDLE16        hand = GlobalHandleToSel(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
242                 *rets = GlobalLock16(hand);
243                 memset(*rets,0,65535);
244                 (*rets)->rethandle = hand;
245                 i = 0;  /* entry 0 is free */
246         }
247         ret = &((*rets)->entry[i]);
248         ret->lcall      = 0x9a;
249         ret->snr        = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
250         ret->origreturn = (FARPROC16)CALLER1REF;
251         CALLER1REF      = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
252         ret->dll        = dll;
253         ret->args       = NULL;
254         ret->ordinal    = ordinal;
255         ret->origSP     = SP_reg(context);
256
257         IP_reg(context)= LOWORD(fun->origfun);
258         CS_reg(context)= HIWORD(fun->origfun);
259
260         ret->show = SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name);
261         if(!ret->show) return;
262         DPRINTF("Call %s.%ld: %s(",dll->name,ordinal,fun->name);
263         if (fun->nrofargs>0) {
264                 max = fun->nrofargs;
265                 if (max>16) max=16;
266                 for (i=max;i--;)
267                         DPRINTF("%04x%s",*(WORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),SP_reg(context))+8+sizeof(WORD)*i),i?",":"");
268                 if (max!=fun->nrofargs)
269                         DPRINTF(" ...");
270         } else if (fun->nrofargs<0) {
271                 DPRINTF("<unknown, check return>");
272                 ret->args = HeapAlloc(SystemHeap,0,16*sizeof(WORD));
273                 memcpy(ret->args,(LPBYTE)(PTR_SEG_OFF_TO_LIN(SS_reg(context),SP_reg(context))+8),sizeof(WORD)*16);
274         }
275         DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
276 }
277
278 void WINAPI SNOOP16_Return(CONTEXT *context) {
279         SNOOP16_RETURNENTRY     *ret = (SNOOP16_RETURNENTRY*)(PTR_SEG_OFF_TO_LIN(CS_reg(context),IP_reg(context))-5);
280
281         /* We haven't found out the nrofargs yet. If we called a cdecl
282          * function it is too late anyway and we can just set '0' (which 
283          * will be the difference between orig and current SP
284          * If pascal -> everything ok.
285          */
286         if (ret->dll->funs[ret->ordinal].nrofargs<0) {
287                 ret->dll->funs[ret->ordinal].nrofargs=(SP_reg(context)-ret->origSP-4)/2;
288         }
289         IP_reg(context) = LOWORD(ret->origreturn);
290         CS_reg(context) = HIWORD(ret->origreturn);
291         if(!ret->show) {
292                 ;
293         } else if (ret->args) {
294                 int     i,max;
295
296                 DPRINTF("Ret  %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
297                 max = ret->dll->funs[ret->ordinal].nrofargs;
298                 if (max>16)
299                         max=16;
300                 if (max<0) 
301                         max=0;
302
303                 for (i=max;i--;)
304                         DPRINTF("%04x%s",ret->args[i],i?",":"");
305                 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
306                         DPRINTF(" ...");
307                 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
308                         DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
309                 );
310                 HeapFree(SystemHeap,0,ret->args);
311                 ret->args = NULL;
312         } else
313                 DPRINTF("Ret  %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
314                         ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
315                         DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
316                 );
317         ret->origreturn = NULL; /* mark as empty */
318 }
319 #else   /* !__i386__ */
320 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
321         FIXME(snoop,"snooping works only on i386 for now.\n");
322         return;
323 }
324
325 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
326         return origfun;
327 }
328 #endif  /* !__i386__ */
329
330 void
331 SNOOP16_Init() {
332         fnSNOOP16_GetProcAddress16=SNOOP16_GetProcAddress16;
333         fnSNOOP16_RegisterDLL=SNOOP16_RegisterDLL;
334 }