Avoid infinite loop if QueryPathOfRegTypeLib is called with lcid=0.
[wine/dcerpc.git] / if1632 / snoop.c
blob94b1d37ca421754f89b713d32b8af53b7486f7bd
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 "winnt.h"
13 #include "heap.h"
14 #include "global.h"
15 #include "selectors.h"
16 #include "stackframe.h"
17 #include "builtin16.h"
18 #include "toolhelp.h"
19 #include "snoop.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(snoop);
24 #ifdef __i386__
26 #include "pshpack1.h"
28 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
29 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
31 typedef struct tagSNOOP16_FUN {
32 /* code part */
33 BYTE lcall; /* 0x9a call absolute with segment */
34 DWORD snr;
35 /* unreached */
36 int nrofargs;
37 FARPROC16 origfun;
38 char *name;
39 } SNOOP16_FUN;
41 typedef struct tagSNOOP16_DLL {
42 HMODULE16 hmod;
43 HANDLE16 funhandle;
44 SNOOP16_FUN *funs;
45 LPCSTR name;
46 struct tagSNOOP16_DLL *next;
47 } SNOOP16_DLL;
49 typedef struct tagSNOOP16_RETURNENTRY {
50 /* code part */
51 BYTE lcall; /* 0x9a call absolute with segment */
52 DWORD snr;
53 /* unreached */
54 FARPROC16 origreturn;
55 SNOOP16_DLL *dll;
56 DWORD ordinal;
57 WORD origSP;
58 WORD *args; /* saved args across a stdcall */
59 } SNOOP16_RETURNENTRY;
61 typedef struct tagSNOOP16_RETURNENTRIES {
62 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
63 HANDLE16 rethandle;
64 struct tagSNOOP16_RETURNENTRIES *next;
65 } SNOOP16_RETURNENTRIES;
67 typedef struct tagSNOOP16_RELAY {
68 WORD pushbp; /* 0x5566 */
69 BYTE pusheax; /* 0x50 */
70 WORD pushax; /* 0x5066 */
71 BYTE pushl; /* 0x68 */
72 DWORD realfun; /* SNOOP16_Return */
73 BYTE lcall; /* 0x9a call absolute with segment */
74 DWORD callfromregs;
75 WORD seg;
76 WORD lret; /* 0xcb66 */
77 } SNOOP16_RELAY;
79 #include "poppack.h"
81 static SNOOP16_DLL *firstdll = NULL;
82 static SNOOP16_RETURNENTRIES *firstrets = NULL;
83 static SNOOP16_RELAY *snr;
84 static HANDLE16 xsnr = 0;
86 void
87 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
88 SNOOP16_DLL **dll = &(firstdll);
89 char *s;
91 if (!TRACE_ON(snoop)) return;
92 if (!snr) {
93 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,TRUE,TRUE,FALSE);
94 snr = GlobalLock16(xsnr);
95 snr[0].pushbp = 0x5566;
96 snr[0].pusheax = 0x50;
97 snr[0].pushax = 0x5066;
98 snr[0].pushl = 0x68;
99 snr[0].realfun = (DWORD)SNOOP16_Entry;
100 snr[0].lcall = 0x9a;
101 snr[0].callfromregs = (DWORD)CallFrom16Register;
102 snr[0].seg = __get_cs();
103 snr[0].lret = 0xcb66;
105 snr[1].pushbp = 0x5566;
106 snr[1].pusheax = 0x50;
107 snr[1].pushax = 0x5066;
108 snr[1].pushl = 0x68;
109 snr[1].realfun = (DWORD)SNOOP16_Return;
110 snr[1].lcall = 0x9a;
111 snr[1].callfromregs = (DWORD)CallFrom16Register;
112 snr[1].seg = __get_cs();
113 snr[1].lret = 0xcb66;
115 while (*dll) {
116 if ((*dll)->hmod == pModule->self)
117 return; /* already registered */
118 dll = &((*dll)->next);
120 *dll = (SNOOP16_DLL*)HeapAlloc(GetProcessHeap(),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(GetProcessHeap(),0,name);
126 if ((s=strrchr((*dll)->name,'.')))
127 *s='\0';
128 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
129 (*dll)->funs = GlobalLock16((*dll)->funhandle);
130 if (!(*dll)->funs) {
131 HeapFree(GetProcessHeap(),0,*dll);
132 FIXME("out of memory\n");
133 return;
135 memset((*dll)->funs,0,65535);
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];
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;
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;
174 /* Now search the non-resident names table */
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;
186 if (*cpnt)
187 fun->name = HEAP_strdupA(GetProcessHeap(),0,name);
188 else
189 fun->name = HEAP_strdupA(GetProcessHeap(),0,"");
190 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
191 return origfun;
193 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
194 if (strchr(fun->name,'_')) {
195 char *s=strchr(fun->name,'_');
197 if (!strncasecmp(s,"_thunkdata",10)) {
198 HeapFree(GetProcessHeap(),0,fun->name);
199 fun->name = NULL;
200 return origfun;
203 fun->lcall = 0x9a;
204 fun->snr = MAKELONG(0,xsnr);
205 fun->origfun = origfun;
206 fun->nrofargs = -1;
207 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
210 #define CALLER1REF (*(DWORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_reg(context))+4)))
211 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
212 DWORD ordinal=0;
213 DWORD entry=(DWORD)PTR_SEG_OFF_TO_LIN(CS_reg(context),LOWORD(EIP_reg(context)))-5;
214 WORD xcs = CS_reg(context);
215 SNOOP16_DLL *dll = firstdll;
216 SNOOP16_FUN *fun = NULL;
217 SNOOP16_RETURNENTRIES **rets = &firstrets;
218 SNOOP16_RETURNENTRY *ret;
219 int i,max;
221 while (dll) {
222 if (xcs == dll->funhandle) {
223 fun = (SNOOP16_FUN*)entry;
224 ordinal = fun-dll->funs;
225 break;
227 dll=dll->next;
229 if (!dll) {
230 FIXME("entrypoint 0x%08lx not found\n",entry);
231 return; /* oops */
233 while (*rets) {
234 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
235 if (!(*rets)->entry[i].origreturn)
236 break;
237 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
238 break;
239 rets = &((*rets)->next);
241 if (!*rets) {
242 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,TRUE,FALSE,FALSE));
243 *rets = GlobalLock16(hand);
244 memset(*rets,0,65535);
245 (*rets)->rethandle = hand;
246 i = 0; /* entry 0 is free */
248 ret = &((*rets)->entry[i]);
249 ret->lcall = 0x9a;
250 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
251 ret->origreturn = (FARPROC16)CALLER1REF;
252 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
253 ret->dll = dll;
254 ret->args = NULL;
255 ret->ordinal = ordinal;
256 ret->origSP = LOWORD(ESP_reg(context));
258 EIP_reg(context)= LOWORD(fun->origfun);
259 CS_reg(context) = HIWORD(fun->origfun);
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*)((char *) PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_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(GetProcessHeap(),0,16*sizeof(WORD));
273 memcpy(ret->args,(LPBYTE)((char *) PTR_SEG_OFF_TO_LIN(SS_reg(context),LOWORD(ESP_reg(context)))+8),sizeof(WORD)*16);
275 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
278 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
279 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) PTR_SEG_OFF_TO_LIN(CS_reg(context),LOWORD(EIP_reg(context)))-5);
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.
286 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
287 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(ESP_reg(context))-ret->origSP-4)/2;
289 EIP_reg(context) = LOWORD(ret->origreturn);
290 CS_reg(context) = HIWORD(ret->origreturn);
291 if (ret->args) {
292 int i,max;
294 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
295 max = ret->dll->funs[ret->ordinal].nrofargs;
296 if (max>16)
297 max=16;
298 if (max<0)
299 max=0;
301 for (i=max;i--;)
302 DPRINTF("%04x%s",ret->args[i],i?",":"");
303 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
304 DPRINTF(" ...");
305 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
306 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
308 HeapFree(GetProcessHeap(),0,ret->args);
309 ret->args = NULL;
310 } else
311 DPRINTF("RET %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
312 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
313 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
315 ret->origreturn = NULL; /* mark as empty */
317 #else /* !__i386__ */
318 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
319 FIXME("snooping works only on i386 for now.\n");
320 return;
323 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
324 return origfun;
326 #endif /* !__i386__ */