shell32: Fix the errors in two Chinese (Simplified) resources.
[wine/hacks.git] / dlls / krnl386.exe16 / snoop.c
blob42fa8d18a69d9fbab95509b9a42a2b0ce14ef1a2
1 /*
2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
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 "winternl.h"
33 #include "wine/library.h"
34 #include "kernel16_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
39 #include "pshpack1.h"
41 static void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
42 static void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
44 typedef struct tagSNOOP16_FUN {
45 /* code part */
46 BYTE lcall; /* 0x9a call absolute with segment */
47 DWORD snr;
48 /* unreached */
49 int nrofargs;
50 FARPROC16 origfun;
51 char *name;
52 } SNOOP16_FUN;
54 typedef struct tagSNOOP16_DLL {
55 HMODULE16 hmod;
56 HANDLE16 funhandle;
57 SNOOP16_FUN *funs;
58 struct tagSNOOP16_DLL *next;
59 char name[1];
60 } SNOOP16_DLL;
62 typedef struct tagSNOOP16_RETURNENTRY {
63 /* code part */
64 BYTE lcall; /* 0x9a call absolute with segment */
65 DWORD snr;
66 /* unreached */
67 FARPROC16 origreturn;
68 SNOOP16_DLL *dll;
69 DWORD ordinal;
70 WORD origSP;
71 WORD *args; /* saved args across a stdcall */
72 } SNOOP16_RETURNENTRY;
74 typedef struct tagSNOOP16_RETURNENTRIES {
75 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
76 HANDLE16 rethandle;
77 struct tagSNOOP16_RETURNENTRIES *next;
78 } SNOOP16_RETURNENTRIES;
80 typedef struct tagSNOOP16_RELAY {
81 WORD pushbp; /* 0x5566 */
82 BYTE pusheax; /* 0x50 */
83 WORD pushax; /* 0x5066 */
84 BYTE pushl; /* 0x68 */
85 DWORD realfun; /* SNOOP16_Return */
86 BYTE lcall; /* 0x9a call absolute with segment */
87 DWORD callfromregs;
88 WORD seg;
89 WORD lret; /* 0xcb66 */
90 } SNOOP16_RELAY;
92 #include "poppack.h"
94 static SNOOP16_DLL *firstdll = NULL;
95 static SNOOP16_RETURNENTRIES *firstrets = NULL;
96 static SNOOP16_RELAY *snr;
97 static HANDLE16 xsnr = 0;
99 void
100 SNOOP16_RegisterDLL(HMODULE16 hModule,LPCSTR name) {
101 SNOOP16_DLL **dll = &(firstdll);
102 char *s;
104 if (!TRACE_ON(snoop)) return;
106 TRACE("hmod=%x, name=%s\n", hModule, name);
108 if (!snr) {
109 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
110 snr = GlobalLock16(xsnr);
111 snr[0].pushbp = 0x5566;
112 snr[0].pusheax = 0x50;
113 snr[0].pushax = 0x5066;
114 snr[0].pushl = 0x68;
115 snr[0].realfun = (DWORD)SNOOP16_Entry;
116 snr[0].lcall = 0x9a;
117 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
118 snr[0].seg = wine_get_cs();
119 snr[0].lret = 0xcb66;
121 snr[1].pushbp = 0x5566;
122 snr[1].pusheax = 0x50;
123 snr[1].pushax = 0x5066;
124 snr[1].pushl = 0x68;
125 snr[1].realfun = (DWORD)SNOOP16_Return;
126 snr[1].lcall = 0x9a;
127 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
128 snr[1].seg = wine_get_cs();
129 snr[1].lret = 0xcb66;
131 while (*dll) {
132 if ((*dll)->hmod == hModule)
134 /* another dll, loaded at the same address */
135 GlobalUnlock16((*dll)->funhandle);
136 GlobalFree16((*dll)->funhandle);
137 break;
139 dll = &((*dll)->next);
142 if (*dll)
143 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
144 else
145 *dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name));
147 (*dll)->next = NULL;
148 (*dll)->hmod = hModule;
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;
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];
171 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
172 return origfun;
173 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an impossible opcode, possible dataref. */
174 return origfun;
175 while (dll) {
176 if (hmod == dll->hmod)
177 break;
178 dll=dll->next;
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->ne_restab;
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;
199 /* Now search the non-resident names table */
201 if (!*cpnt && pModule->nrname_handle) {
202 cpnt = 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;
211 if (*cpnt)
213 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
214 strcpy( fun->name, name );
216 else
217 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
219 if (!SNOOP16_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
220 return origfun;
222 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
223 if (strchr(fun->name,'_')) {
224 char *s=strchr(fun->name,'_');
226 if (!strncasecmp(s,"_thunkdata",10)) {
227 HeapFree(GetProcessHeap(),0,fun->name);
228 fun->name = NULL;
229 return origfun;
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);
239 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
240 static 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 unsigned i=0;
249 int max;
251 while (dll) {
252 if (xcs == dll->funhandle) {
253 fun = (SNOOP16_FUN*)entry;
254 ordinal = fun-dll->funs;
255 break;
257 dll=dll->next;
259 if (!dll) {
260 FIXME("entrypoint 0x%08x not found\n",entry);
261 return; /* oops */
263 while (*rets) {
264 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
265 if (!(*rets)->entry[i].origreturn)
266 break;
267 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
268 break;
269 rets = &((*rets)->next);
271 if (!*rets) {
272 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
273 *rets = GlobalLock16(hand);
274 (*rets)->rethandle = hand;
275 i = 0; /* entry 0 is free */
277 ret = &((*rets)->entry[i]);
278 ret->lcall = 0x9a;
279 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
280 ret->origreturn = (FARPROC16)CALLER1REF;
281 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
282 ret->dll = dll;
283 ret->args = NULL;
284 ret->ordinal = ordinal;
285 ret->origSP = LOWORD(context->Esp);
287 context->Eip= LOWORD(fun->origfun);
288 context->SegCs = HIWORD(fun->origfun);
291 DPRINTF("%04x:CALL %s.%d: %s(",GetCurrentThreadId(), dll->name,ordinal,fun->name);
292 if (fun->nrofargs>0) {
293 max = fun->nrofargs;
294 if (max>16) max=16;
295 for (i=max;i--;)
296 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
297 if (max!=fun->nrofargs)
298 DPRINTF(" ...");
299 } else if (fun->nrofargs<0) {
300 DPRINTF("<unknown, check return>");
301 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
302 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
304 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
307 static void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
308 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
310 /* We haven't found out the nrofargs yet. If we called a cdecl
311 * function it is too late anyway and we can just set '0' (which
312 * will be the difference between orig and current SP
313 * If pascal -> everything ok.
315 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
316 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
318 context->Eip = LOWORD(ret->origreturn);
319 context->SegCs = HIWORD(ret->origreturn);
320 DPRINTF("%04x:RET %s.%d: %s(",
321 GetCurrentThreadId(),ret->dll->name,ret->ordinal,
322 ret->dll->funs[ret->ordinal].name);
323 if (ret->args) {
324 int i,max;
326 max = ret->dll->funs[ret->ordinal].nrofargs;
327 if (max>16)
328 max=16;
329 if (max<0)
330 max=0;
332 for (i=max;i--;)
333 DPRINTF("%04x%s",ret->args[i],i?",":"");
334 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
335 DPRINTF(" ...");
336 HeapFree(GetProcessHeap(),0,ret->args);
337 ret->args = NULL;
339 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
340 (WORD)context->Edx,(WORD)context->Eax,
341 HIWORD(ret->origreturn),LOWORD(ret->origreturn));
342 ret->origreturn = NULL; /* mark as empty */