2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
13 #include "wine/winbase16.h"
14 #include "wine/library.h"
16 #include "stackframe.h"
17 #include "builtin16.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(snoop);
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 {
33 BYTE lcall; /* 0x9a call absolute with segment */
41 typedef struct tagSNOOP16_DLL {
45 struct tagSNOOP16_DLL *next;
49 typedef struct tagSNOOP16_RETURNENTRY {
51 BYTE lcall; /* 0x9a call absolute with segment */
58 WORD *args; /* saved args across a stdcall */
59 } SNOOP16_RETURNENTRY;
61 typedef struct tagSNOOP16_RETURNENTRIES {
62 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
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 */
76 WORD lret; /* 0xcb66 */
81 static SNOOP16_DLL *firstdll = NULL;
82 static SNOOP16_RETURNENTRIES *firstrets = NULL;
83 static SNOOP16_RELAY *snr;
84 static HANDLE16 xsnr = 0;
87 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
88 SNOOP16_DLL **dll = &(firstdll);
91 if (!TRACE_ON(snoop)) return;
93 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
94 snr = GlobalLock16(xsnr);
95 snr[0].pushbp = 0x5566;
96 snr[0].pusheax = 0x50;
97 snr[0].pushax = 0x5066;
99 snr[0].realfun = (DWORD)SNOOP16_Entry;
101 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
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;
109 snr[1].realfun = (DWORD)SNOOP16_Return;
111 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
112 snr[1].seg = __get_cs();
113 snr[1].lret = 0xcb66;
116 if ((*dll)->hmod == pModule->self)
117 return; /* already registered */
118 dll = &((*dll)->next);
120 *dll = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SNOOP16_DLL)+strlen(name));
122 (*dll)->hmod = pModule->self;
123 if ((s=strrchr(name,'\\')))
125 strcpy( (*dll)->name, name );
126 if ((s=strrchr((*dll)->name,'.')))
128 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
129 (*dll)->funs = GlobalLock16((*dll)->funhandle);
131 HeapFree(GetProcessHeap(),0,*dll);
132 FIXME("out of memory\n");
138 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
139 SNOOP16_DLL *dll = firstdll;
141 NE_MODULE *pModule = NE_GetPtr(hmod);
145 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
147 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
150 if (hmod == dll->hmod)
154 if (!dll) /* probably internal */
156 if (ordinal>65535/sizeof(SNOOP16_FUN))
158 fun = dll->funs+ordinal;
161 fun->snr = MAKELONG(0,xsnr);
162 fun->origfun = origfun;
164 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
165 cpnt = (unsigned char *)pModule + pModule->name_table;
167 cpnt += *cpnt + 1 + sizeof(WORD);
168 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
169 sprintf(name,"%.*s",*cpnt,cpnt+1);
173 /* Now search the non-resident names table */
175 if (!*cpnt && pModule->nrname_handle) {
176 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
178 cpnt += *cpnt + 1 + sizeof(WORD);
179 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
180 sprintf(name,"%.*s",*cpnt,cpnt+1);
187 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
188 strcpy( fun->name, name );
191 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
193 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
196 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
197 if (strchr(fun->name,'_')) {
198 char *s=strchr(fun->name,'_');
200 if (!strncasecmp(s,"_thunkdata",10)) {
201 HeapFree(GetProcessHeap(),0,fun->name);
207 fun->snr = MAKELONG(0,xsnr);
208 fun->origfun = origfun;
210 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
213 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
214 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
216 DWORD entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
217 WORD xcs = context->SegCs;
218 SNOOP16_DLL *dll = firstdll;
219 SNOOP16_FUN *fun = NULL;
220 SNOOP16_RETURNENTRIES **rets = &firstrets;
221 SNOOP16_RETURNENTRY *ret;
225 if (xcs == dll->funhandle) {
226 fun = (SNOOP16_FUN*)entry;
227 ordinal = fun-dll->funs;
233 FIXME("entrypoint 0x%08lx not found\n",entry);
237 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
238 if (!(*rets)->entry[i].origreturn)
240 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
242 rets = &((*rets)->next);
245 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
246 *rets = GlobalLock16(hand);
247 (*rets)->rethandle = hand;
248 i = 0; /* entry 0 is free */
250 ret = &((*rets)->entry[i]);
252 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
253 ret->origreturn = (FARPROC16)CALLER1REF;
254 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
257 ret->ordinal = ordinal;
258 ret->origSP = LOWORD(context->Esp);
260 context->Eip= LOWORD(fun->origfun);
261 context->SegCs = HIWORD(fun->origfun);
264 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
265 if (fun->nrofargs>0) {
269 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
270 if (max!=fun->nrofargs)
272 } else if (fun->nrofargs<0) {
273 DPRINTF("<unknown, check return>");
274 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
275 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
277 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
280 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
281 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
283 /* We haven't found out the nrofargs yet. If we called a cdecl
284 * function it is too late anyway and we can just set '0' (which
285 * will be the difference between orig and current SP
286 * If pascal -> everything ok.
288 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
289 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
291 context->Eip = LOWORD(ret->origreturn);
292 context->SegCs = HIWORD(ret->origreturn);
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;
304 DPRINTF("%04x%s",ret->args[i],i?",":"");
305 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
307 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
308 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
310 HeapFree(GetProcessHeap(),0,ret->args);
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)
317 ret->origreturn = NULL; /* mark as empty */
319 #else /* !__i386__ */
320 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
321 if (!TRACE_ON(snoop)) return;
322 FIXME("snooping works only on i386 for now.\n");
325 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
328 #endif /* !__i386__ */