snoop16.c 10.3 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * 386-specific Win16 dll<->dll snooping functions
 *
 * Copyright 1998 Marcus Meissner
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <assert.h>
25
#include <string.h>
26
#include <stdarg.h>
27 28
#include <stdio.h>
#include "windef.h"
29
#include "winbase.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
30
#include "winnt.h"
31
#include "wine/winbase16.h"
32
#include "winternl.h"
33
#include "wine/library.h"
34
#include "kernel_private.h"
35
#include "kernel16_private.h"
Marcus Meissner's avatar
Marcus Meissner committed
36
#include "toolhelp.h"
37
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38

39
WINE_DEFAULT_DEBUG_CHANNEL(snoop);
40

41 42
#ifdef __i386__

43
#include "pshpack1.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
44

45 46
void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
Alexandre Julliard's avatar
Alexandre Julliard committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

typedef	struct tagSNOOP16_FUN {
	/* code part */
	BYTE		lcall;		/* 0x9a call absolute with segment */
	DWORD		snr;
	/* unreached */
	int		nrofargs;
	FARPROC16	origfun;
	char		*name;
} SNOOP16_FUN;

typedef struct tagSNOOP16_DLL {
	HMODULE16	hmod;
	HANDLE16	funhandle;
	SNOOP16_FUN	*funs;
	struct tagSNOOP16_DLL	*next;
63
	char name[1];
Alexandre Julliard's avatar
Alexandre Julliard committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
} SNOOP16_DLL;

typedef struct tagSNOOP16_RETURNENTRY {
	/* code part */
	BYTE		lcall;		/* 0x9a call absolute with segment */
	DWORD		snr;
	/* unreached */
	FARPROC16	origreturn;
	SNOOP16_DLL	*dll;
	DWORD		ordinal;
	WORD		origSP;
	WORD		*args;		/* saved args across a stdcall */
} SNOOP16_RETURNENTRY;

typedef struct tagSNOOP16_RETURNENTRIES {
	SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
	HANDLE16	rethandle;
	struct tagSNOOP16_RETURNENTRIES	*next;
} SNOOP16_RETURNENTRIES;

typedef struct tagSNOOP16_RELAY {
85 86 87
	WORD		pushbp;		/* 0x5566 */
	BYTE		pusheax;	/* 0x50 */
	WORD		pushax;		/* 0x5066 */
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89 90 91 92
	BYTE		pushl;		/* 0x68 */
	DWORD		realfun;	/* SNOOP16_Return */
	BYTE		lcall;		/* 0x9a call absolute with segment */
	DWORD		callfromregs;
	WORD		seg;
93
        WORD		lret;           /* 0xcb66 */
Alexandre Julliard's avatar
Alexandre Julliard committed
94 95
} SNOOP16_RELAY;

96
#include "poppack.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
97 98 99 100 101 102 103

static	SNOOP16_DLL		*firstdll = NULL;
static	SNOOP16_RETURNENTRIES 	*firstrets = NULL;
static	SNOOP16_RELAY		*snr;
static	HANDLE16		xsnr = 0;

void
104
SNOOP16_RegisterDLL(HMODULE16 hModule,LPCSTR name) {
Alexandre Julliard's avatar
Alexandre Julliard committed
105 106 107 108
	SNOOP16_DLL	**dll = &(firstdll);
	char		*s;

	if (!TRACE_ON(snoop)) return;
109

110
        TRACE("hmod=%x, name=%s\n", hModule, name);
111

Alexandre Julliard's avatar
Alexandre Julliard committed
112
	if (!snr) {
113
		xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
Alexandre Julliard's avatar
Alexandre Julliard committed
114
		snr = GlobalLock16(xsnr);
115 116 117
		snr[0].pushbp	= 0x5566;
		snr[0].pusheax	= 0x50;
		snr[0].pushax	= 0x5066;
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119 120
		snr[0].pushl	= 0x68;
		snr[0].realfun	= (DWORD)SNOOP16_Entry;
		snr[0].lcall 	= 0x9a;
121
		snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
122
		snr[0].seg      = wine_get_cs();
123 124 125 126 127
		snr[0].lret     = 0xcb66;

		snr[1].pushbp	= 0x5566;
		snr[1].pusheax	= 0x50;
		snr[1].pushax	= 0x5066;
Alexandre Julliard's avatar
Alexandre Julliard committed
128 129 130
		snr[1].pushl	= 0x68;
		snr[1].realfun	= (DWORD)SNOOP16_Return;
		snr[1].lcall 	= 0x9a;
131
		snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
132
		snr[1].seg      = wine_get_cs();
133
		snr[1].lret     = 0xcb66;
Alexandre Julliard's avatar
Alexandre Julliard committed
134 135
	}
	while (*dll) {
136
		if ((*dll)->hmod == hModule)
137 138 139 140 141 142
                {
                    /* another dll, loaded at the same address */
                    GlobalUnlock16((*dll)->funhandle);
                    GlobalFree16((*dll)->funhandle);
                    break;
                }
Alexandre Julliard's avatar
Alexandre Julliard committed
143 144
		dll = &((*dll)->next);
	}
145 146 147 148 149 150

	if (*dll)
		*dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
	else
		*dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name));	

Alexandre Julliard's avatar
Alexandre Julliard committed
151
	(*dll)->next	= NULL;
152
	(*dll)->hmod	= hModule;
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154
	if ((s=strrchr(name,'\\')))
		name = s+1;
155
	strcpy( (*dll)->name, name );
Alexandre Julliard's avatar
Alexandre Julliard committed
156 157
	if ((s=strrchr((*dll)->name,'.')))
		*s='\0';
158
	(*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
Alexandre Julliard's avatar
Alexandre Julliard committed
159 160
	(*dll)->funs = GlobalLock16((*dll)->funhandle);
	if (!(*dll)->funs) {
161
		HeapFree(GetProcessHeap(),0,*dll);
162
		FIXME("out of memory\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176
		return;
	}
}

FARPROC16
SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
	SNOOP16_DLL			*dll = firstdll;
	SNOOP16_FUN			*fun;
	NE_MODULE			*pModule = NE_GetPtr(hmod);
	unsigned char			*cpnt;
	char				name[200];

	if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
		return origfun;
177
	if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
Alexandre Julliard's avatar
Alexandre Julliard committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		return origfun;
	while (dll) {
		if (hmod == dll->hmod)
			break;
		dll=dll->next;
	}
	if (!dll)	/* probably internal */
		return origfun;
	if (ordinal>65535/sizeof(SNOOP16_FUN))
		return origfun;
	fun = dll->funs+ordinal;
	/* already done? */
	fun->lcall 	= 0x9a;
	fun->snr	= MAKELONG(0,xsnr);
	fun->origfun	= origfun;
	if (fun->name)
		return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
195
	cpnt = (unsigned char *)pModule + pModule->ne_restab;
Alexandre Julliard's avatar
Alexandre Julliard committed
196 197 198 199 200 201 202 203 204 205
	while (*cpnt) {
		cpnt += *cpnt + 1 + sizeof(WORD);
		if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
			sprintf(name,"%.*s",*cpnt,cpnt+1);
			break;
		}
	}
	/* Now search the non-resident names table */

	if (!*cpnt && pModule->nrname_handle) {
Mike McCormack's avatar
Mike McCormack committed
206
		cpnt = (unsigned char *)GlobalLock16( pModule->nrname_handle );
Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209 210 211 212 213 214 215
		while (*cpnt) {
			cpnt += *cpnt + 1 + sizeof(WORD);
			if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
				    sprintf(name,"%.*s",*cpnt,cpnt+1);
				    break;
			}
		}
	}
	if (*cpnt)
216 217 218 219
        {
            fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
            strcpy( fun->name, name );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
220
	else
221 222
            fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */

223
	if (!SNOOP16_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
224 225
		return origfun;

Alexandre Julliard's avatar
Alexandre Julliard committed
226 227 228 229 230
	/* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
	if (strchr(fun->name,'_')) {
		char *s=strchr(fun->name,'_');

		if (!strncasecmp(s,"_thunkdata",10)) {
231
			HeapFree(GetProcessHeap(),0,fun->name);
Alexandre Julliard's avatar
Alexandre Julliard committed
232 233 234 235
			fun->name = NULL;
			return origfun;
		}
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
236 237 238 239 240 241 242
	fun->lcall 	= 0x9a;
	fun->snr	= MAKELONG(0,xsnr);
	fun->origfun	= origfun;
	fun->nrofargs	= -1;
	return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
}

243
#define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
244
void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
Alexandre Julliard's avatar
Alexandre Julliard committed
245
	DWORD		ordinal=0;
246
	DWORD		entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
247
	WORD		xcs = context->SegCs;
Alexandre Julliard's avatar
Alexandre Julliard committed
248 249 250 251
	SNOOP16_DLL	*dll = firstdll;
	SNOOP16_FUN	*fun = NULL;
	SNOOP16_RETURNENTRIES	**rets = &firstrets;
	SNOOP16_RETURNENTRY	*ret;
252
	int		i=0, max;
Alexandre Julliard's avatar
Alexandre Julliard committed
253 254 255 256 257 258 259 260 261 262

	while (dll) {
		if (xcs == dll->funhandle) {
			fun = (SNOOP16_FUN*)entry;
			ordinal = fun-dll->funs;
			break;
		}
		dll=dll->next;
	}
	if (!dll) {
263
		FIXME("entrypoint 0x%08lx not found\n",entry);
Alexandre Julliard's avatar
Alexandre Julliard committed
264 265 266 267 268 269 270 271 272 273 274
		return; /* oops */
	}
	while (*rets) {
		for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
			if (!(*rets)->entry[i].origreturn)
				break;
		if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
			break;
		rets = &((*rets)->next);
	}
	if (!*rets) {
275
		HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
Alexandre Julliard's avatar
Alexandre Julliard committed
276 277 278 279 280 281 282 283 284 285 286 287
		*rets = GlobalLock16(hand);
		(*rets)->rethandle = hand;
		i = 0;	/* entry 0 is free */
	}
	ret = &((*rets)->entry[i]);
	ret->lcall 	= 0x9a;
	ret->snr	= MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
	ret->origreturn	= (FARPROC16)CALLER1REF;
	CALLER1REF	= MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
	ret->dll	= dll;
	ret->args	= NULL;
	ret->ordinal	= ordinal;
288
	ret->origSP	= LOWORD(context->Esp);
Alexandre Julliard's avatar
Alexandre Julliard committed
289

290 291
	context->Eip= LOWORD(fun->origfun);
	context->SegCs = HIWORD(fun->origfun);
Alexandre Julliard's avatar
Alexandre Julliard committed
292

293

294
	DPRINTF("%04lx:CALL %s.%ld: %s(",GetCurrentThreadId(), dll->name,ordinal,fun->name);
Alexandre Julliard's avatar
Alexandre Julliard committed
295
	if (fun->nrofargs>0) {
Alexandre Julliard's avatar
Alexandre Julliard committed
296 297
		max = fun->nrofargs;
		if (max>16) max=16;
Alexandre Julliard's avatar
Alexandre Julliard committed
298
		for (i=max;i--;)
299
			DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
Alexandre Julliard's avatar
Alexandre Julliard committed
300 301 302 303
		if (max!=fun->nrofargs)
			DPRINTF(" ...");
	} else if (fun->nrofargs<0) {
		DPRINTF("<unknown, check return>");
304
		ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
305
		memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
Alexandre Julliard's avatar
Alexandre Julliard committed
306
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
307
	DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
Alexandre Julliard's avatar
Alexandre Julliard committed
308 309
}

310
void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
311
	SNOOP16_RETURNENTRY	*ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
Alexandre Julliard's avatar
Alexandre Julliard committed
312 313

	/* We haven't found out the nrofargs yet. If we called a cdecl
314
	 * function it is too late anyway and we can just set '0' (which
Alexandre Julliard's avatar
Alexandre Julliard committed
315 316 317
	 * will be the difference between orig and current SP
	 * If pascal -> everything ok.
	 */
Alexandre Julliard's avatar
Alexandre Julliard committed
318
	if (ret->dll->funs[ret->ordinal].nrofargs<0) {
319
		ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
Alexandre Julliard's avatar
Alexandre Julliard committed
320
	}
321 322
	context->Eip = LOWORD(ret->origreturn);
	context->SegCs  = HIWORD(ret->origreturn);
323 324 325
        DPRINTF("%04lx:RET  %s.%ld: %s(",
                GetCurrentThreadId(),ret->dll->name,ret->ordinal,
                ret->dll->funs[ret->ordinal].name);
326
	if (ret->args) {
Alexandre Julliard's avatar
Alexandre Julliard committed
327 328 329
		int	i,max;

		max = ret->dll->funs[ret->ordinal].nrofargs;
Alexandre Julliard's avatar
Alexandre Julliard committed
330 331
		if (max>16)
			max=16;
332
		if (max<0)
Alexandre Julliard's avatar
Alexandre Julliard committed
333
			max=0;
Alexandre Julliard's avatar
Alexandre Julliard committed
334 335 336

		for (i=max;i--;)
			DPRINTF("%04x%s",ret->args[i],i?",":"");
Alexandre Julliard's avatar
Alexandre Julliard committed
337 338
		if (max!=ret->dll->funs[ret->ordinal].nrofargs)
			DPRINTF(" ...");
339
		HeapFree(GetProcessHeap(),0,ret->args);
Alexandre Julliard's avatar
Alexandre Julliard committed
340
		ret->args = NULL;
341 342 343 344
	}
        DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
                (WORD)context->Edx,(WORD)context->Eax,
                HIWORD(ret->origreturn),LOWORD(ret->origreturn));
Alexandre Julliard's avatar
Alexandre Julliard committed
345 346 347
	ret->origreturn = NULL; /* mark as empty */
}
#else	/* !__i386__ */
348
void SNOOP16_RegisterDLL(HMODULE16 hModule,LPCSTR name) {
349
	if (!TRACE_ON(snoop)) return;
350
	FIXME("snooping works only on i386 for now.\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
351 352 353 354 355 356
}

FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
	return origfun;
}
#endif	/* !__i386__ */