Added DBCS/unicode encoding templates.
[wine.git] / if1632 / snoop.c
blobfd6bc73c13e3aa7781e48391a15c91e61f32c0ba
1 /*
2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include "winbase.h"
11 #include "windef.h"
12 #include "wingdi.h"
13 #include "winuser.h"
14 #include "winnt.h"
15 #include "heap.h"
16 #include "global.h"
17 #include "selectors.h"
18 #include "stackframe.h"
19 #include "builtin16.h"
20 #include "snoop.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(snoop);
25 #ifdef __i386__
27 #include "pshpack1.h"
29 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
30 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
32 typedef struct tagSNOOP16_FUN {
33 /* code part */
34 BYTE lcall; /* 0x9a call absolute with segment */
35 DWORD snr;
36 /* unreached */
37 int nrofargs;
38 FARPROC16 origfun;
39 char *name;
40 } SNOOP16_FUN;
42 typedef struct tagSNOOP16_DLL {
43 HMODULE16 hmod;
44 HANDLE16 funhandle;
45 SNOOP16_FUN *funs;
46 LPCSTR name;
47 struct tagSNOOP16_DLL *next;
48 } SNOOP16_DLL;
50 typedef struct tagSNOOP16_RETURNENTRY {
51 /* code part */
52 BYTE lcall; /* 0x9a call absolute with segment */
53 DWORD snr;
54 /* unreached */
55 FARPROC16 origreturn;
56 SNOOP16_DLL *dll;
57 DWORD ordinal;
58 WORD origSP;
59 WORD *args; /* saved args across a stdcall */
60 } SNOOP16_RETURNENTRY;
62 typedef struct tagSNOOP16_RETURNENTRIES {
63 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
64 HANDLE16 rethandle;
65 struct tagSNOOP16_RETURNENTRIES *next;
66 } SNOOP16_RETURNENTRIES;
68 typedef struct tagSNOOP16_RELAY {
69 WORD pushbp; /* 0x5566 */
70 BYTE pusheax; /* 0x50 */
71 WORD pushax; /* 0x5066 */
72 BYTE pushl; /* 0x68 */
73 DWORD realfun; /* SNOOP16_Return */
74 BYTE lcall; /* 0x9a call absolute with segment */
75 DWORD callfromregs;
76 WORD seg;
77 WORD lret; /* 0xcb66 */
78 } SNOOP16_RELAY;
80 #include "poppack.h"
82 static SNOOP16_DLL *firstdll = NULL;
83 static SNOOP16_RETURNENTRIES *firstrets = NULL;
84 static SNOOP16_RELAY *snr;
85 static HANDLE16 xsnr = 0;
87 void
88 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
89 SNOOP16_DLL **dll = &(firstdll);
90 char *s;
92 if (!TRACE_ON(snoop)) return;
93 if (!snr) {
94 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,TRUE,TRUE,FALSE);
95 snr = GlobalLock16(xsnr);
96 snr[0].pushbp = 0x5566;
97 snr[0].pusheax = 0x50;
98 snr[0].pushax = 0x5066;
99 snr[0].pushl = 0x68;
100 snr[0].realfun = (DWORD)SNOOP16_Entry;
101 snr[0].lcall = 0x9a;
102 snr[0].callfromregs = (DWORD)CallFrom16Register;
103 snr[0].seg = __get_cs();
104 snr[0].lret = 0xcb66;
106 snr[1].pushbp = 0x5566;
107 snr[1].pusheax = 0x50;
108 snr[1].pushax = 0x5066;
109 snr[1].pushl = 0x68;
110 snr[1].realfun = (DWORD)SNOOP16_Return;
111 snr[1].lcall = 0x9a;
112 snr[1].callfromregs = (DWORD)CallFrom16Register;
113 snr[1].seg = __get_cs();
114 snr[1].lret = 0xcb66;
116 while (*dll) {
117 if ((*dll)->hmod == pModule->self)
118 return; /* already registered */
119 dll = &((*dll)->next);
121 *dll = (SNOOP16_DLL*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SNOOP16_DLL));
122 (*dll)->next = NULL;
123 (*dll)->hmod = pModule->self;
124 if ((s=strrchr(name,'\\')))
125 name = s+1;
126 (*dll)->name = HEAP_strdupA(GetProcessHeap(),0,name);
127 if ((s=strrchr((*dll)->name,'.')))
128 *s='\0';
129 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
130 (*dll)->funs = GlobalLock16((*dll)->funhandle);
131 if (!(*dll)->funs) {
132 HeapFree(GetProcessHeap(),0,*dll);
133 FIXME("out of memory\n");
134 return;
136 memset((*dll)->funs,0,65535);
139 FARPROC16
140 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
141 SNOOP16_DLL *dll = firstdll;
142 SNOOP16_FUN *fun;
143 NE_MODULE *pModule = NE_GetPtr(hmod);
144 unsigned char *cpnt;
145 char name[200];
147 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
148 return origfun;
149 if (!*(LPBYTE)PTR_SEG_TO_LIN(origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
150 return origfun;
151 while (dll) {
152 if (hmod == dll->hmod)
153 break;
154 dll=dll->next;
156 if (!dll) /* probably internal */
157 return origfun;
158 if (ordinal>65535/sizeof(SNOOP16_FUN))
159 return origfun;
160 fun = dll->funs+ordinal;
161 /* already done? */
162 fun->lcall = 0x9a;
163 fun->snr = MAKELONG(0,xsnr);
164 fun->origfun = origfun;
165 if (fun->name)
166 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
167 cpnt = (unsigned char *)pModule + pModule->name_table;
168 while (*cpnt) {
169 cpnt += *cpnt + 1 + sizeof(WORD);
170 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
171 sprintf(name,"%.*s",*cpnt,cpnt+1);
172 break;
175 /* Now search the non-resident names table */
177 if (!*cpnt && pModule->nrname_handle) {
178 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
179 while (*cpnt) {
180 cpnt += *cpnt + 1 + sizeof(WORD);
181 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
182 sprintf(name,"%.*s",*cpnt,cpnt+1);
183 break;
187 if (*cpnt)
188 fun->name = HEAP_strdupA(GetProcessHeap(),0,name);
189 else
190 fun->name = HEAP_strdupA(GetProcessHeap(),0,"");
191 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
192 return origfun;
194 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
195 if (strchr(fun->name,'_')) {
196 char *s=strchr(fun->name,'_');
198 if (!strncasecmp(s,"_thunkdata",10)) {
199 HeapFree(GetProcessHeap(),0,fun->name);
200 fun->name = NULL;
201 return origfun;
204 fun->lcall = 0x9a;
205 fun->snr = MAKELONG(0,xsnr);
206 fun->origfun = origfun;
207 fun->nrofargs = -1;
208 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
211 #define CALLER1REF (*(DWORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_reg(context))+4)))
212 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
213 DWORD ordinal=0;
214 DWORD entry=(DWORD)PTR_SEG_OFF_TO_LIN(CS_reg(context),LOWORD(EIP_reg(context)))-5;
215 WORD xcs = CS_reg(context);
216 SNOOP16_DLL *dll = firstdll;
217 SNOOP16_FUN *fun = NULL;
218 SNOOP16_RETURNENTRIES **rets = &firstrets;
219 SNOOP16_RETURNENTRY *ret;
220 int i,max;
222 while (dll) {
223 if (xcs == dll->funhandle) {
224 fun = (SNOOP16_FUN*)entry;
225 ordinal = fun-dll->funs;
226 break;
228 dll=dll->next;
230 if (!dll) {
231 FIXME("entrypoint 0x%08lx not found\n",entry);
232 return; /* oops */
234 while (*rets) {
235 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
236 if (!(*rets)->entry[i].origreturn)
237 break;
238 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
239 break;
240 rets = &((*rets)->next);
242 if (!*rets) {
243 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
244 *rets = GlobalLock16(hand);
245 memset(*rets,0,65535);
246 (*rets)->rethandle = hand;
247 i = 0; /* entry 0 is free */
249 ret = &((*rets)->entry[i]);
250 ret->lcall = 0x9a;
251 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
252 ret->origreturn = (FARPROC16)CALLER1REF;
253 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
254 ret->dll = dll;
255 ret->args = NULL;
256 ret->ordinal = ordinal;
257 ret->origSP = LOWORD(ESP_reg(context));
259 EIP_reg(context)= LOWORD(fun->origfun);
260 CS_reg(context) = HIWORD(fun->origfun);
263 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
264 if (fun->nrofargs>0) {
265 max = fun->nrofargs;
266 if (max>16) max=16;
267 for (i=max;i--;)
268 DPRINTF("%04x%s",*(WORD*)((char *) PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_reg(context)))+8+sizeof(WORD)*i),i?",":"");
269 if (max!=fun->nrofargs)
270 DPRINTF(" ...");
271 } else if (fun->nrofargs<0) {
272 DPRINTF("<unknown, check return>");
273 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
274 memcpy(ret->args,(LPBYTE)((char *) PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_reg(context)))+8),sizeof(WORD)*16);
276 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
279 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
280 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) PTR_SEG_OFF_TO_LIN(CS_reg(context),LOWORD(EIP_reg(context)))-5);
282 /* We haven't found out the nrofargs yet. If we called a cdecl
283 * function it is too late anyway and we can just set '0' (which
284 * will be the difference between orig and current SP
285 * If pascal -> everything ok.
287 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
288 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(ESP_reg(context))-ret->origSP-4)/2;
290 EIP_reg(context) = LOWORD(ret->origreturn);
291 CS_reg(context) = HIWORD(ret->origreturn);
292 if (ret->args) {
293 int i,max;
295 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
296 max = ret->dll->funs[ret->ordinal].nrofargs;
297 if (max>16)
298 max=16;
299 if (max<0)
300 max=0;
302 for (i=max;i--;)
303 DPRINTF("%04x%s",ret->args[i],i?",":"");
304 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
305 DPRINTF(" ...");
306 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
307 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
309 HeapFree(GetProcessHeap(),0,ret->args);
310 ret->args = NULL;
311 } else
312 DPRINTF("RET %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
313 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
314 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
316 ret->origreturn = NULL; /* mark as empty */
318 #else /* !__i386__ */
319 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
320 FIXME("snooping works only on i386 for now.\n");
321 return;
324 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
325 return origfun;
327 #endif /* !__i386__ */