relay.c 40.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2
 * Win32 relay and snoop functions
Alexandre Julliard's avatar
Alexandre Julliard committed
3 4
 *
 * Copyright 1997 Alexandre Julliard
5
 * Copyright 1998 Marcus Meissner
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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

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

Alexandre Julliard's avatar
Alexandre Julliard committed
25
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <string.h>
27
#include <stdarg.h>
28
#include <stdio.h>
29

30 31
#include "ntstatus.h"
#define WIN32_NO_STATUS
32
#include "windef.h"
33
#include "winternl.h"
34 35
#include "wine/exception.h"
#include "ntdll_misc.h"
36
#include "wine/unicode.h"
37
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38

39
WINE_DEFAULT_DEBUG_CHANNEL(relay);
40

41
#if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
42

43 44
WINE_DECLARE_DEBUG_CHANNEL(timestamp);

45 46 47
struct relay_descr  /* descriptor for a module */
{
    void               *magic;               /* signature */
48 49
    void               *relay_call;          /* functions to call from relay thunks */
    void               *relay_call_regs;
50 51 52 53 54 55 56
    void               *private;             /* reserved for the relay code private data */
    const char         *entry_point_base;    /* base address of entry point thunks */
    const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
    const unsigned int *arg_types;           /* table of argument types for all entry points */
};

#define RELAY_DESCR_MAGIC  ((void *)0xdeb90001)
57
#define IS_INTARG(x)       (((ULONG_PTR)(x) >> 16) == 0)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

/* private data built at dll load time */

struct relay_entry_point
{
    void       *orig_func;    /* original entry point function */
    const char *name;         /* function name (if any) */
};

struct relay_private_data
{
    HMODULE                  module;            /* module handle of this dll */
    unsigned int             base;              /* ordinal base */
    char                     dllname[40];       /* dll name (without .dll extension) */
    struct relay_entry_point entry_points[1];   /* list of dll entry points */
};

75 76 77 78
static const WCHAR **debug_relay_excludelist;
static const WCHAR **debug_relay_includelist;
static const WCHAR **debug_snoop_excludelist;
static const WCHAR **debug_snoop_includelist;
79 80
static const WCHAR **debug_from_relay_excludelist;
static const WCHAR **debug_from_relay_includelist;
81 82
static const WCHAR **debug_from_snoop_excludelist;
static const WCHAR **debug_from_snoop_includelist;
83

84
static RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
85

86
/* compare an ASCII and a Unicode string without depending on the current codepage */
87
static inline int strcmpAW( const char *strA, const WCHAR *strW )
88 89 90 91 92 93
{
    while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
    return (unsigned char)*strA - *strW;
}

/* compare an ASCII and a Unicode string without depending on the current codepage */
94
static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
95 96 97 98 99 100
{
    int ret = 0;
    for ( ; n > 0; n--, strA++, strW++)
        if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
    return ret;
}
101

Alexandre Julliard's avatar
Alexandre Julliard committed
102
/***********************************************************************
103
 *           build_list
Alexandre Julliard's avatar
Alexandre Julliard committed
104
 *
105
 * Build a function list from a ';'-separated string.
Alexandre Julliard's avatar
Alexandre Julliard committed
106
 */
107
static const WCHAR **build_list( const WCHAR *buffer )
108 109
{
    int count = 1;
110 111
    const WCHAR *p = buffer;
    const WCHAR **ret;
112

113
    while ((p = strchrW( p, ';' )))
114
    {
115
        count++;
116
        p++;
Alexandre Julliard's avatar
Alexandre Julliard committed
117
    }
118
    /* allocate count+1 pointers, plus the space for a copy of the string */
119 120
    if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
                                (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
121
    {
122
        WCHAR *str = (WCHAR *)(ret + count + 1);
123
        WCHAR *q = str;
124

125
        strcpyW( str, buffer );
126 127 128
        count = 0;
        for (;;)
        {
129 130 131
            ret[count++] = q;
            if (!(q = strchrW( q, ';' ))) break;
            *q++ = 0;
132 133
        }
        ret[count++] = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
134
    }
135 136 137
    return ret;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/***********************************************************************
 *           load_list_value
 *
 * Load a function list from a registry value.
 */
static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
{
    char initial_buffer[4096];
    char *buffer = initial_buffer;
    DWORD count;
    NTSTATUS status;
    UNICODE_STRING name;
    const WCHAR **list = NULL;

    RtlInitUnicodeString( &name, value );
153
    status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(initial_buffer), &count );
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    if (status == STATUS_BUFFER_OVERFLOW)
    {
        buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
        status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
    }
    if (status == STATUS_SUCCESS)
    {
        WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
        list = build_list( str );
        if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
    }

    if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
    return list;
}
169 170

/***********************************************************************
171
 *           init_debug_lists
172 173 174
 *
 * Build the relay include/exclude function lists.
 */
175
static DWORD WINAPI init_debug_lists( RTL_RUN_ONCE *once, void *param, void **context )
176
{
177 178
    OBJECT_ATTRIBUTES attr;
    UNICODE_STRING name;
179
    HANDLE root, hkey;
180
    static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
181 182 183 184 185 186
                                    'W','i','n','e','\\',
                                    'D','e','b','u','g',0};
    static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
    static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
    static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
    static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
187 188
    static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
    static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
189 190
    static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
    static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
191

192
    RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
193
    attr.Length = sizeof(attr);
194
    attr.RootDirectory = root;
195 196 197 198 199 200
    attr.ObjectName = &name;
    attr.Attributes = 0;
    attr.SecurityDescriptor = NULL;
    attr.SecurityQualityOfService = NULL;
    RtlInitUnicodeString( &name, configW );

201 202 203
    /* @@ Wine registry key: HKCU\Software\Wine\Debug */
    if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
    NtClose( root );
204
    if (!hkey) return TRUE;
205

206 207 208 209 210 211 212 213
    debug_relay_includelist = load_list( hkey, RelayIncludeW );
    debug_relay_excludelist = load_list( hkey, RelayExcludeW );
    debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
    debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
    debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
    debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
    debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
    debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
214

215
    NtClose( hkey );
216
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
217 218
}

219

220
/***********************************************************************
221
 *           check_list
222
 *
223
 * Check if a given module and function is in the list.
224
 */
225
static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
226
{
227
    char ord_str[10];
228

229 230
    sprintf( ord_str, "%d", ordinal );
    for(; *list; list++)
231
    {
232 233
        const WCHAR *p = strrchrW( *list, '.' );
        if (p && p > *list)  /* check module and function */
234
        {
235 236 237 238 239
            int len = p - *list;
            if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
            if (p[1] == '*' && !p[2]) return TRUE;
            if (!strcmpAW( ord_str, p + 1 )) return TRUE;
            if (func && !strcmpAW( func, p + 1 )) return TRUE;
240 241 242
        }
        else  /* function only */
        {
243
            if (func && !strcmpAW( func, *list )) return TRUE;
244 245
        }
    }
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    return FALSE;
}


/***********************************************************************
 *           check_relay_include
 *
 * Check if a given function must be included in the relay output.
 */
static BOOL check_relay_include( const char *module, int ordinal, const char *func )
{
    if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
        return FALSE;
    if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
        return FALSE;
    return TRUE;
262 263
}

264
/***********************************************************************
265
 *           check_from_module
266
 *
267 268
 * Check if calls from a given module must be included in the relay/snoop output,
 * given the exclusion and inclusion lists.
269
 */
270
static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
271
{
272 273
    static const WCHAR dllW[] = {'.','d','l','l',0 };
    const WCHAR **listitem;
274 275
    BOOL show;

276
    if (!module) return TRUE;
277 278
    if (!includelist && !excludelist) return TRUE;
    if (excludelist)
279 280
    {
        show = TRUE;
281
        listitem = excludelist;
282 283 284 285
    }
    else
    {
        show = FALSE;
286
        listitem = includelist;
287 288 289 290 291
    }
    for(; *listitem; listitem++)
    {
        int len;

292 293 294
        if (!strcmpiW( *listitem, module )) return !show;
        len = strlenW( *listitem );
        if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
295 296 297 298 299
            return !show;
    }
    return show;
}

300 301 302
/***********************************************************************
 *           RELAY_PrintArgs
 */
303
static inline void RELAY_PrintArgs( const INT_PTR *args, int nb_args, unsigned int typemask )
304 305 306
{
    while (nb_args--)
    {
307
        if ((typemask & 3) && !IS_INTARG(*args))
308 309
        {
	    if (typemask & 2)
310
                DPRINTF( "%08lx %s", *args, debugstr_w((LPCWSTR)*args) );
311
            else
312
                DPRINTF( "%08lx %s", *args, debugstr_a((LPCSTR)*args) );
313
	}
314
        else DPRINTF( "%08lx", *args );
315 316 317 318 319 320
        if (nb_args) DPRINTF( "," );
        args++;
        typemask >>= 2;
    }
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
static void print_timestamp(void)
{
    ULONG ticks = NtGetTickCount();
    DPRINTF( "%3u.%03u:", ticks / 1000, ticks % 1000 );
}

/***********************************************************************
 *           relay_trace_entry
 *
 * stack points to the return address, i.e. the first argument is stack[1].
 */
void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
{
    WORD ordinal = LOWORD(idx);
    BYTE nb_args = LOBYTE(HIWORD(idx));
    struct relay_private_data *data = descr->private;
    struct relay_entry_point *entry_point = data->entry_points + ordinal;

    if (TRACE_ON(relay))
    {
        if (TRACE_ON(timestamp)) print_timestamp();

        if (entry_point->name)
            DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
        else
            DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
        RELAY_PrintArgs( stack + 1, nb_args, descr->arg_types[ordinal] );
        DPRINTF( ") ret=%08lx\n", stack[0] );
    }
    return entry_point->orig_func;
}

/***********************************************************************
 *           relay_trace_exit
 */
void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
                              const INT_PTR *stack, LONGLONG retval )
{
    WORD ordinal = LOWORD(idx);
    BYTE flags   = HIBYTE(HIWORD(idx));
    struct relay_private_data *data = descr->private;
    struct relay_entry_point *entry_point = data->entry_points + ordinal;

    if (!TRACE_ON(relay)) return;

    if (TRACE_ON(timestamp)) print_timestamp();

    if (entry_point->name)
        DPRINTF( "%04x:Ret  %s.%s()", GetCurrentThreadId(), data->dllname, entry_point->name );
    else
        DPRINTF( "%04x:Ret  %s.%u()", GetCurrentThreadId(), data->dllname, data->base + ordinal );

    if (flags & 1)  /* 64-bit return value */
        DPRINTF( " retval=%08x%08x ret=%08lx\n",
                 (UINT)(retval >> 32), (UINT)retval, stack[0] );
    else
        DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR)retval, stack[0] );
}

380
#ifdef __i386__
381 382

extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
383
__ASM_GLOBAL_FUNC( call_entry_point,
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
                   "pushl %ebp\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
                   "movl %esp,%ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
                   "pushl %esi\n\t"
                  __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
                   "pushl %edi\n\t"
                  __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
                   "movl 12(%ebp),%edx\n\t"
                   "shll $2,%edx\n\t"
                   "jz 1f\n\t"
                   "subl %edx,%esp\n\t"
                   "andl $~15,%esp\n\t"
                   "movl 12(%ebp),%ecx\n\t"
                   "movl 16(%ebp),%esi\n\t"
                   "movl %esp,%edi\n\t"
                   "cld\n\t"
                   "rep; movsl\n"
403 404 405
                   "testl $2,20(%ebp)\n\t"  /* (flags & 2) -> thiscall */
                   "jz 1f\n\t"
                   "popl %ecx\n\t"
406 407 408 409 410 411 412 413 414 415
                   "1:\tcall *8(%ebp)\n\t"
                   "leal -8(%ebp),%esp\n\t"
                   "popl %edi\n\t"
                   __ASM_CFI(".cfi_same_value %edi\n\t")
                   "popl %esi\n\t"
                   __ASM_CFI(".cfi_same_value %esi\n\t")
                   "popl %ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
                   __ASM_CFI(".cfi_same_value %ebp\n\t")
                   "ret" )
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
__ASM_GLOBAL_FUNC( relay_call,
                   "pushl %ebp\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
                   "movl %esp,%ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
                   "pushl %esi\n\t"
                  __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
                   "pushl %edi\n\t"
                  __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
                   "pushl %ecx\n\t"
                  __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
                   /* trace the parameters */
                   "pushl 16(%ebp)\n\t"
                   "pushl 12(%ebp)\n\t"
                   "pushl 8(%ebp)\n\t"
                   "call " __ASM_NAME("relay_trace_entry") "\n\t"
                   /* copy the arguments*/
                   "movzbl 14(%ebp),%ecx\n\t"  /* number of args */
                   "jecxz 1f\n\t"
                   "leal 0(,%ecx,4),%edx\n\t"
                   "subl %edx,%esp\n\t"
                   "andl $~15,%esp\n\t"
                   "movl 16(%ebp),%esi\n\t"
                   "addl $4,%esi\n\t"
                   "movl %esp,%edi\n\t"
                   "cld\n\t"
                   "rep; movsl\n\t"
                   "testb $2,15(%ebp)\n\t"  /* (flags & 2) -> thiscall */
                   "jz 1f\n\t"
                   "popl %ecx\n"
                   /* call the entry point */
                   "1:\tcall *%eax\n\t"
                   "movl %eax,%esi\n\t"
                   "movl %edx,%edi\n\t"
                   /* trace the return value */
                   "leal -20(%ebp),%esp\n\t"
                   "pushl %edx\n\t"
                   "pushl %eax\n\t"
                   "pushl 16(%ebp)\n\t"
                   "pushl 12(%ebp)\n\t"
                   "pushl 8(%ebp)\n\t"
                   "call " __ASM_NAME("relay_trace_exit") "\n\t"
                   /* restore return value and return */
                   "leal -12(%ebp),%esp\n\t"
                   "movl %esi,%eax\n\t"
                   "movl %edi,%edx\n\t"
                   "popl %ecx\n\t"
                   __ASM_CFI(".cfi_same_value %ecx\n\t")
                   "popl %edi\n\t"
                   __ASM_CFI(".cfi_same_value %edi\n\t")
                   "popl %esi\n\t"
                   __ASM_CFI(".cfi_same_value %esi\n\t")
                   "popl %ebp\n\t"
                   __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
                   __ASM_CFI(".cfi_same_value %ebp\n\t")
                   "ret $12" )
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

void WINAPI __regs_relay_call_regs( struct relay_descr *descr, unsigned int idx,
                                    unsigned int orig_eax, unsigned int ret_addr,
                                    CONTEXT *context )
{
    WORD ordinal = LOWORD(idx);
    BYTE nb_args = LOBYTE(HIWORD(idx));
    struct relay_private_data *data = descr->private;
    struct relay_entry_point *entry_point = data->entry_points + ordinal;
    BYTE *orig_func = entry_point->orig_func;
    INT_PTR *args = (INT_PTR *)context->Esp;
    INT_PTR args_copy[32];

    /* restore the context to what it was before the relay thunk */
    context->Eax = orig_eax;
    context->Eip = ret_addr;
    context->Esp += nb_args * sizeof(int);

    if (TRACE_ON(relay))
    {
        if (entry_point->name)
            DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
        else
            DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
        RELAY_PrintArgs( args, nb_args, descr->arg_types[ordinal] );
        DPRINTF( ") ret=%08x\n", ret_addr );

        DPRINTF( "%04x:  eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
                 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
                 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
                 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
                 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );

        assert( orig_func[0] == 0x68 /* pushl func */ );
        assert( orig_func[5] == 0x6a /* pushl args */ );
        assert( orig_func[7] == 0xe8 /* call */ );
    }

    /* now call the real function */

    memcpy( args_copy, args, nb_args * sizeof(args[0]) );
    args_copy[nb_args++] = (INT_PTR)context;  /* append context argument */

    call_entry_point( orig_func + 12 + *(int *)(orig_func + 1), nb_args, args_copy, 0 );

    if (TRACE_ON(relay))
    {
        if (entry_point->name)
            DPRINTF( "%04x:Ret  %s.%s() retval=%08x ret=%08x\n",
                     GetCurrentThreadId(), data->dllname, entry_point->name,
                     context->Eax, context->Eip );
        else
            DPRINTF( "%04x:Ret  %s.%u() retval=%08x ret=%08x\n",
                     GetCurrentThreadId(), data->dllname, data->base + ordinal,
                     context->Eax, context->Eip );
        DPRINTF( "%04x:  eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
                 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
                 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
                 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
                 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
    }
}
extern void WINAPI relay_call_regs(void);
DEFINE_REGS_ENTRYPOINT( relay_call_regs, 4 )

540
#elif defined(__arm__)
541 542

extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
543
__ASM_GLOBAL_FUNC( call_entry_point,
544
                   ".arm\n\t"
545 546 547 548 549 550 551
                   "push {r4, r5, LR}\n\t"
                   "mov r4, r0\n\t"
                   "mov r5, SP\n\t"
                   "lsl r3, r1, #2\n\t"
                   "cmp r3, #0\n\t"
                   "beq 5f\n\t"
                   "sub SP, SP, r3\n\t"
552 553
                   "tst r1, #1\n\t"
                   "subeq SP, SP, #4\n\t"
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
                   "1:\tsub r3, r3, #4\n\t"
                   "ldr r0, [r2, r3]\n\t"
                   "str r0, [SP, r3]\n\t"
                   "cmp r3, #0\n\t"
                   "bgt 1b\n\t"
                   "cmp r1, #1\n\t"
                   "bgt 2f\n\t"
                   "pop {r0}\n\t"
                   "b 5f\n\t"
                   "2:\tcmp r1, #2\n\t"
                   "bgt 3f\n\t"
                   "pop {r0-r1}\n\t"
                   "b 5f\n\t"
                   "3:\tcmp r1, #3\n\t"
                   "bgt 4f\n\t"
                   "pop {r0-r2}\n\t"
                   "b 5f\n\t"
                   "4:\tpop {r0-r3}\n\t"
                   "5:\tblx r4\n\t"
                   "mov SP, r5\n\t"
                   "pop {r4, r5, PC}" )
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
{
    BYTE nb_args = LOBYTE(HIWORD(idx));
    BYTE flags   = HIBYTE(HIWORD(idx));
    void *func = relay_trace_entry( descr, idx, stack );
    LONGLONG ret = call_entry_point( func, nb_args, stack + 1, flags );
    relay_trace_exit( descr, idx, stack, ret );
    return ret;
}

static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
{
    assert(0);  /* should never be called */
}

#elif defined(__x86_64__)

593 594
extern void * WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
__ASM_GLOBAL_FUNC( relay_call,
595
                   "pushq %rbp\n\t"
596 597
                   __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
                   __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
598
                   "movq %rsp,%rbp\n\t"
599
                   __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
600 601 602 603 604 605 606 607 608 609 610 611
                   "subq $0x30,%rsp\n\t"
                   "movq %rsi,0x20(%rsp)\n\t"
                   __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
                   "movq %rdi,0x28(%rsp)\n\t"
                   __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
                   /* trace the parameters */
                   "movq %rcx,0x10(%rbp)\n\t"
                   "movq %rdx,0x18(%rbp)\n\t"
                   "movq %r8,0x20(%rbp)\n\t"
                   "call " __ASM_NAME("relay_trace_entry") "\n\t"
                   /* copy the arguments */
                   "movzbq 0x1a(%rbp),%rdx\n\t"  /* number of args */
612 613 614
                   "movq $4,%rcx\n\t"
                   "cmp %rcx,%rdx\n\t"
                   "cmovgq %rdx,%rcx\n\t"
615 616
                   "leaq -16(,%rcx,8),%rdx\n\t"
                   "andq $~15,%rdx\n\t"
617
                   "subq %rdx,%rsp\n\t"
618 619
                   "movq 0x20(%rbp),%r8\n\t"  /* original stack */
                   "leaq 8(%r8),%rsi\n\t"
620 621
                   "movq %rsp,%rdi\n\t"
                   "rep; movsq\n\t"
622
                   /* call the entry point */
623 624 625 626
                   "movq 0(%rsp),%rcx\n\t"
                   "movq 8(%rsp),%rdx\n\t"
                   "movq 16(%rsp),%r8\n\t"
                   "movq 24(%rsp),%r9\n\t"
627 628 629 630
                   "movq 0(%rsp),%xmm0\n\t"
                   "movq 8(%rsp),%xmm1\n\t"
                   "movq 16(%rsp),%xmm2\n\t"
                   "movq 24(%rsp),%xmm3\n\t"
631
                   "callq *%rax\n\t"
632 633 634 635 636 637 638 639 640 641 642 643 644
                   /* trace the return value */
                   "leaq -0x30(%rbp),%rsp\n\t"
                   "movq 0x10(%rbp),%rcx\n\t"
                   "movq 0x18(%rbp),%rdx\n\t"
                   "movq 0x20(%rbp),%r8\n\t"
                   "movq %rax,%rsi\n\t"
                   "movaps %xmm0,0x10(%rbp)\n\t"
                   "movq %rax,%r9\n\t"
                   "call " __ASM_NAME("relay_trace_exit") "\n\t"
                   /* restore return value and return */
                   "movq %rsi,%rax\n\t"
                   "movaps 0x10(%rbp),%xmm0\n\t"
                   "movq 0x20(%rsp),%rsi\n\t"
645
                   __ASM_CFI(".cfi_same_value %rsi\n\t")
646 647 648
                   "movq 0x28(%rsp),%rdi\n\t"
                   __ASM_CFI(".cfi_same_value %rdi\n\t")
                   "movq %rbp,%rsp\n\t"
649
                   __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
650
                   "popq %rbp\n\t"
651 652
                   __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
                   __ASM_CFI(".cfi_same_value %rbp\n\t")
653
                   "ret")
654

655
static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
656
{
657
    assert(0);  /* should never be called */
658 659
}

660 661 662
#else
#error Not supported on this CPU
#endif
663 664


665 666 667 668 669
/***********************************************************************
 *           RELAY_GetProcAddress
 *
 * Return the proc address to use for a given function.
 */
Eric Pouech's avatar
Eric Pouech committed
670
FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
671
                              DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
672
{
673 674
    struct relay_private_data *data;
    const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
675

676 677
    if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc;  /* no relay data */
    if (!data->entry_points[ordinal].orig_func) return proc;  /* not a relayed function */
678
    if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
679 680
        return proc;  /* we want to relay it */
    return data->entry_points[ordinal].orig_func;
681 682 683
}


684 685 686 687 688
/***********************************************************************
 *           RELAY_SetupDLL
 *
 * Setup relay debugging for a built-in dll.
 */
689
void RELAY_SetupDLL( HMODULE module )
690 691 692
{
    IMAGE_EXPORT_DIRECTORY *exports;
    DWORD *funcs;
693 694 695 696 697
    unsigned int i, len;
    DWORD size, entry_point_rva;
    struct relay_descr *descr;
    struct relay_private_data *data;
    const WORD *ordptr;
698

699
    RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
700

701
    exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
702
    if (!exports) return;
703

704 705 706 707 708 709 710
    descr = (struct relay_descr *)((char *)exports + size);
    if (descr->magic != RELAY_DESCR_MAGIC) return;

    if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
                                  (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
        return;

711 712
    descr->relay_call = relay_call;
    descr->relay_call_regs = relay_call_regs;
713 714 715 716 717 718 719 720 721 722 723 724 725 726
    descr->private = data;

    data->module = module;
    data->base   = exports->Base;
    len = strlen( (char *)module + exports->Name );
    if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
    len = min( len, sizeof(data->dllname) - 1 );
    memcpy( data->dllname, (char *)module + exports->Name, len );
    data->dllname[len] = 0;

    /* fetch name pointer for all entry points and store them in the private structure */

    ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
    for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
727
    {
728 729 730
        DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
        data->entry_points[*ordptr].name = (const char *)module + name_rva;
    }
731

732
    /* patch the functions in the export table to point to the relay thunks */
733

734
    funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
735
    entry_point_rva = descr->entry_point_base - (const char *)module;
736 737 738 739 740
    for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
    {
        if (!descr->entry_point_offsets[i]) continue;   /* not a normal function */
        if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
            continue;  /* don't include this entry point */
741

742 743
        data->entry_points[i].orig_func = (char *)module + *funcs;
        *funcs = entry_point_rva + descr->entry_point_offsets[i];
744 745 746
    }
}

747
#else  /* __i386__ || __x86_64__ || __arm__ */
748 749 750 751 752 753 754 755 756 757 758

FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
                              DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
{
    return proc;
}

void RELAY_SetupDLL( HMODULE module )
{
}

759
#endif  /* __i386__ || __x86_64__ || __arm__ */
760

761 762 763 764 765

/***********************************************************************/
/* snoop support */
/***********************************************************************/

766 767 768 769 770
#ifdef __i386__

WINE_DECLARE_DEBUG_CHANNEL(seh);
WINE_DECLARE_DEBUG_CHANNEL(snoop);

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
#include "pshpack1.h"

typedef	struct
{
	/* code part */
	BYTE		lcall;		/* 0xe8 call snoopentry (relative) */
	/* NOTE: If you move snoopentry OR nrofargs fix the relative offset
	 * calculation!
	 */
	DWORD		snoopentry;	/* SNOOP_Entry relative */
	/* unreached */
	int		nrofargs;
	FARPROC	origfun;
	const char *name;
} SNOOP_FUN;

typedef struct tagSNOOP_DLL {
	HMODULE	hmod;
	SNOOP_FUN	*funs;
	DWORD		ordbase;
	DWORD		nrofordinals;
	struct tagSNOOP_DLL	*next;
	char name[1];
} SNOOP_DLL;

typedef struct
{
	/* code part */
	BYTE		lcall;		/* 0xe8 call snoopret relative*/
	/* NOTE: If you move snoopret OR origreturn fix the relative offset
	 * calculation!
	 */
	DWORD		snoopret;	/* SNOOP_Ret relative */
	/* unreached */
	FARPROC	origreturn;
	SNOOP_DLL	*dll;
	DWORD		ordinal;
	DWORD		origESP;
	DWORD		*args;		/* saved args across a stdcall */
} SNOOP_RETURNENTRY;

typedef struct tagSNOOP_RETURNENTRIES {
	SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
	struct tagSNOOP_RETURNENTRIES	*next;
} SNOOP_RETURNENTRIES;

#include "poppack.h"

extern void WINAPI SNOOP_Entry(void);
extern void WINAPI SNOOP_Return(void);

static SNOOP_DLL *firstdll;
static SNOOP_RETURNENTRIES *firstrets;


826 827 828 829 830 831
/***********************************************************************
 *          SNOOP_ShowDebugmsgSnoop
 *
 * Simple function to decide if a particular debugging message is
 * wanted.
 */
832
static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
833
{
834 835 836 837 838
    if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
        return FALSE;
    if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
        return FALSE;
    return TRUE;
839 840 841 842 843 844 845 846 847 848 849 850 851 852
}


/***********************************************************************
 *           SNOOP_SetupDLL
 *
 * Setup snoop debugging for a native dll.
 */
void SNOOP_SetupDLL(HMODULE hmod)
{
    SNOOP_DLL **dll = &firstdll;
    char *p, *name;
    void *addr;
    SIZE_T size;
853
    ULONG size32;
854 855
    IMAGE_EXPORT_DIRECTORY *exports;

856
    RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
857

858
    exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
859
    if (!exports || !exports->NumberOfFunctions) return;
860
    name = (char *)hmod + exports->Name;
861
    size = size32;
862

863
    TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
864 865 866 867 868 869 870

    while (*dll) {
        if ((*dll)->hmod == hmod)
        {
            /* another dll, loaded at the same address */
            addr = (*dll)->funs;
            size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
871
            NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
872 873 874 875
            break;
        }
        dll = &((*dll)->next);
    }
876 877
    if (*dll)
        *dll = RtlReAllocateHeap(GetProcessHeap(),
878 879
                             HEAP_ZERO_MEMORY, *dll,
                             sizeof(SNOOP_DLL) + strlen(name));
880 881 882 883
    else
        *dll = RtlAllocateHeap(GetProcessHeap(),
                             HEAP_ZERO_MEMORY,
                             sizeof(SNOOP_DLL) + strlen(name));
884 885 886 887 888 889 890 891
    (*dll)->hmod	= hmod;
    (*dll)->ordbase = exports->Base;
    (*dll)->nrofordinals = exports->NumberOfFunctions;
    strcpy( (*dll)->name, name );
    p = (*dll)->name + strlen((*dll)->name) - 4;
    if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;

    size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
892
    addr = NULL;
893
    NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
894 895
                            MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    if (!addr) {
896
        RtlFreeHeap(GetProcessHeap(),0,*dll);
897 898 899 900 901 902 903 904 905 906 907 908 909
        FIXME("out of memory\n");
        return;
    }
    (*dll)->funs = addr;
    memset((*dll)->funs,0,size);
}


/***********************************************************************
 *           SNOOP_GetProcAddress
 *
 * Return the proc address to use for a given function.
 */
Eric Pouech's avatar
Eric Pouech committed
910
FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
911 912
                              DWORD exp_size, FARPROC origfun, DWORD ordinal,
                              const WCHAR *user)
913
{
914
    unsigned int i;
Eric Pouech's avatar
Eric Pouech committed
915 916 917 918 919 920 921 922 923 924 925
    const char *ename;
    const WORD *ordinals;
    const DWORD *names;
    SNOOP_DLL *dll = firstdll;
    SNOOP_FUN *fun;
    const IMAGE_SECTION_HEADER *sec;

    if (!TRACE_ON(snoop)) return origfun;
    if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
        return origfun; /* the calling module was explicitly excluded */

Austin English's avatar
Austin English committed
926
    if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
Eric Pouech's avatar
Eric Pouech committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
        return origfun;

    sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );

    if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
        return origfun;  /* most likely a data reference */

    while (dll) {
        if (hmod == dll->hmod)
            break;
        dll = dll->next;
    }
    if (!dll)	/* probably internal */
        return origfun;

    /* try to find a name for it */
    ename = NULL;
    names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
    ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
    if (names) for (i = 0; i < exports->NumberOfNames; i++)
    {
        if (ordinals[i] == ordinal)
949
        {
Eric Pouech's avatar
Eric Pouech committed
950 951
            ename = (const char *)hmod + names[i];
            break;
952
        }
Eric Pouech's avatar
Eric Pouech committed
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    }
    if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
        return origfun;
    assert(ordinal < dll->nrofordinals);
    fun = dll->funs + ordinal;
    if (!fun->name)
    {
        fun->name       = ename;
        fun->lcall	= 0xe8;
        /* NOTE: origreturn struct member MUST come directly after snoopentry */
        fun->snoopentry	= (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
        fun->origfun	= origfun;
        fun->nrofargs	= -1;
    }
    return (FARPROC)&(fun->lcall);
968 969 970 971 972 973
}

static void SNOOP_PrintArg(DWORD x)
{
    int i,nostring;

974
    DPRINTF("%08x",x);
975
    if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    __TRY
    {
        LPBYTE s=(LPBYTE)x;
        i=0;nostring=0;
        while (i<80) {
            if (s[i]==0) break;
            if (s[i]<0x20) {nostring=1;break;}
            if (s[i]>=0x80) {nostring=1;break;}
            i++;
        }
        if (!nostring && i > 5)
            DPRINTF(" %s",debugstr_an((LPSTR)x,i));
        else  /* try unicode */
        {
            LPWSTR s=(LPWSTR)x;
            i=0;nostring=0;
            while (i<80) {
                if (s[i]==0) break;
                if (s[i]<0x20) {nostring=1;break;}
                if (s[i]>0x100) {nostring=1;break;}
                i++;
            }
            if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
        }
    }
1001
    __EXCEPT_PAGE_FAULT
1002
    {
1003
    }
1004
    __ENDTRY
1005 1006
}

1007 1008
#define CALLER1REF (*(DWORD*)context->Esp)

1009
void WINAPI __regs_SNOOP_Entry( CONTEXT *context )
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
{
	DWORD		ordinal=0,entry = context->Eip - 5;
	SNOOP_DLL	*dll = firstdll;
	SNOOP_FUN	*fun = NULL;
	SNOOP_RETURNENTRIES	**rets = &firstrets;
	SNOOP_RETURNENTRY	*ret;
	int		i=0, max;

	while (dll) {
		if (	((char*)entry>=(char*)dll->funs)	&&
			((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
		) {
			fun = (SNOOP_FUN*)entry;
			ordinal = fun-dll->funs;
			break;
		}
		dll=dll->next;
	}
	if (!dll) {
1029
		FIXME("entrypoint 0x%08x not found\n",entry);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
		return; /* oops */
	}
	/* guess cdecl ... */
	if (fun->nrofargs<0) {
		/* Typical cdecl return frame is:
		 *     add esp, xxxxxxxx
		 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
		 * (after that 81 C2 xx xx xx xx)
		 */
		LPBYTE	reteip = (LPBYTE)CALLER1REF;

		if (reteip) {
			if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
				fun->nrofargs=reteip[2]/4;
		}
	}


	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) {
                SIZE_T size = 4096;
1058
                VOID* addr = NULL;
1059

1060
                NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size, 
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
                                        MEM_COMMIT | MEM_RESERVE,
                                        PAGE_EXECUTE_READWRITE);
                if (!addr) return;
                *rets = addr;
		memset(*rets,0,4096);
		i = 0;	/* entry 0 is free */
	}
	ret = &((*rets)->entry[i]);
	ret->lcall	= 0xe8;
	/* NOTE: origreturn struct member MUST come directly after snoopret */
	ret->snoopret	= ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
	ret->origreturn	= (FARPROC)CALLER1REF;
	CALLER1REF	= (DWORD)&ret->lcall;
	ret->dll	= dll;
	ret->args	= NULL;
	ret->ordinal	= ordinal;
	ret->origESP	= context->Esp;

	context->Eip = (DWORD)fun->origfun;

1081 1082
	if (TRACE_ON(timestamp))
		print_timestamp();
1083 1084
	if (fun->name) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
	else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	if (fun->nrofargs>0) {
		max = fun->nrofargs; if (max>16) max=16;
		for (i=0;i<max;i++)
                {
                    SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
                    if (i<fun->nrofargs-1) DPRINTF(",");
                }
		if (max!=fun->nrofargs)
			DPRINTF(" ...");
	} else if (fun->nrofargs<0) {
		DPRINTF("<unknown, check return>");
1096
		ret->args = RtlAllocateHeap(GetProcessHeap(),
1097 1098 1099
                                            0,16*sizeof(DWORD));
		memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
	}
1100
	DPRINTF(") ret=%08x\n",(DWORD)ret->origreturn);
1101 1102 1103
}


1104
void WINAPI __regs_SNOOP_Return( CONTEXT *context )
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
{
	SNOOP_RETURNENTRY	*ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
        SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];

	/* We haven't found out the nrofargs yet. If we called a cdecl
	 * function it is too late anyway and we can just set '0' (which
	 * will be the difference between orig and current ESP
	 * If stdcall -> everything ok.
	 */
	if (ret->dll->funs[ret->ordinal].nrofargs<0)
		ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
	context->Eip = (DWORD)ret->origreturn;
1117 1118
	if (TRACE_ON(timestamp))
		print_timestamp();
1119 1120 1121 1122
	if (ret->args) {
		int	i,max;

                if (fun->name)
1123
                    DPRINTF("%04x:RET  %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1124
                else
1125
                    DPRINTF("%04x:RET  %s.%d(", GetCurrentThreadId(),
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
                            ret->dll->name,ret->dll->ordbase+ret->ordinal);

		max = fun->nrofargs;
		if (max>16) max=16;

		for (i=0;i<max;i++)
                {
                    SNOOP_PrintArg(ret->args[i]);
                    if (i<max-1) DPRINTF(",");
                }
1136
		DPRINTF(") retval=%08x ret=%08x\n",
1137
			context->Eax,(DWORD)ret->origreturn );
1138
		RtlFreeHeap(GetProcessHeap(),0,ret->args);
1139 1140 1141 1142 1143
		ret->args = NULL;
	}
        else
        {
            if (fun->name)
1144
		DPRINTF("%04x:RET  %s.%s() retval=%08x ret=%08x\n",
1145 1146 1147
			GetCurrentThreadId(),
			ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
            else
1148
		DPRINTF("%04x:RET  %s.%d() retval=%08x ret=%08x\n",
1149 1150 1151 1152 1153 1154 1155 1156
			GetCurrentThreadId(),
			ret->dll->name,ret->dll->ordbase+ret->ordinal,
			context->Eax, (DWORD)ret->origreturn);
        }
	ret->origreturn = NULL; /* mark as empty */
}

/* assembly wrappers that save the context */
1157 1158
DEFINE_REGS_ENTRYPOINT( SNOOP_Entry, 0 )
DEFINE_REGS_ENTRYPOINT( SNOOP_Return, 0 )
1159

1160 1161
#else  /* __i386__ */

1162
FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1163
                              FARPROC origfun, DWORD ordinal, const WCHAR *user )
1164 1165 1166 1167 1168 1169 1170 1171 1172
{
    return origfun;
}

void SNOOP_SetupDLL( HMODULE hmod )
{
    FIXME("snooping works only on i386 for now.\n");
}

1173
#endif /* __i386__ */