2 * Win32 relay and snoop functions
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1998 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * 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
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
34 #include "wine/exception.h"
35 #include "ntdll_misc.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(relay
);
41 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
43 WINE_DECLARE_DEBUG_CHANNEL(timestamp
);
45 struct relay_descr
/* descriptor for a module */
47 void *magic
; /* signature */
48 void *relay_call
; /* functions to call from relay thunks */
49 void *relay_call_regs
;
50 void *private; /* reserved for the relay code private data */
51 const char *entry_point_base
; /* base address of entry point thunks */
52 const unsigned int *entry_point_offsets
; /* offsets of entry points thunks */
53 const unsigned int *arg_types
; /* table of argument types for all entry points */
56 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
57 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
59 /* private data built at dll load time */
61 struct relay_entry_point
63 void *orig_func
; /* original entry point function */
64 const char *name
; /* function name (if any) */
67 struct relay_private_data
69 HMODULE module
; /* module handle of this dll */
70 unsigned int base
; /* ordinal base */
71 char dllname
[40]; /* dll name (without .dll extension) */
72 struct relay_entry_point entry_points
[1]; /* list of dll entry points */
75 static const WCHAR
**debug_relay_excludelist
;
76 static const WCHAR
**debug_relay_includelist
;
77 static const WCHAR
**debug_snoop_excludelist
;
78 static const WCHAR
**debug_snoop_includelist
;
79 static const WCHAR
**debug_from_relay_excludelist
;
80 static const WCHAR
**debug_from_relay_includelist
;
81 static const WCHAR
**debug_from_snoop_excludelist
;
82 static const WCHAR
**debug_from_snoop_includelist
;
84 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
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
)
89 while (*strA
&& ((unsigned char)*strA
== *strW
)) { strA
++; strW
++; }
90 return (unsigned char)*strA
- *strW
;
93 /* 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
)
97 for ( ; n
> 0; n
--, strA
++, strW
++)
98 if ((ret
= toupperW((unsigned char)*strA
) - toupperW(*strW
)) || !*strA
) break;
102 /***********************************************************************
105 * Build a function list from a ';'-separated string.
107 static const WCHAR
**build_list( const WCHAR
*buffer
)
110 const WCHAR
*p
= buffer
;
113 while ((p
= strchrW( p
, ';' )))
118 /* allocate count+1 pointers, plus the space for a copy of the string */
119 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0,
120 (count
+1) * sizeof(WCHAR
*) + (strlenW(buffer
)+1) * sizeof(WCHAR
) )))
122 WCHAR
*str
= (WCHAR
*)(ret
+ count
+ 1);
125 strcpyW( str
, buffer
);
130 if (!(q
= strchrW( q
, ';' ))) break;
138 /***********************************************************************
141 * Load a function list from a registry value.
143 static const WCHAR
**load_list( HKEY hkey
, const WCHAR
*value
)
145 char initial_buffer
[4096];
146 char *buffer
= initial_buffer
;
150 const WCHAR
**list
= NULL
;
152 RtlInitUnicodeString( &name
, value
);
153 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, sizeof(initial_buffer
), &count
);
154 if (status
== STATUS_BUFFER_OVERFLOW
)
156 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, count
);
157 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, count
, &count
);
159 if (status
== STATUS_SUCCESS
)
161 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
162 list
= build_list( str
);
163 if (list
) TRACE( "%s = %s\n", debugstr_w(value
), debugstr_w(str
) );
166 if (buffer
!= initial_buffer
) RtlFreeHeap( GetProcessHeap(), 0, buffer
);
170 /***********************************************************************
173 * Build the relay include/exclude function lists.
175 static DWORD WINAPI
init_debug_lists( RTL_RUN_ONCE
*once
, void *param
, void **context
)
177 OBJECT_ATTRIBUTES attr
;
180 static const WCHAR configW
[] = {'S','o','f','t','w','a','r','e','\\',
181 'W','i','n','e','\\',
182 'D','e','b','u','g',0};
183 static const WCHAR RelayIncludeW
[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
184 static const WCHAR RelayExcludeW
[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
185 static const WCHAR SnoopIncludeW
[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
186 static const WCHAR SnoopExcludeW
[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
187 static const WCHAR RelayFromIncludeW
[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
188 static const WCHAR RelayFromExcludeW
[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
189 static const WCHAR SnoopFromIncludeW
[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
190 static const WCHAR SnoopFromExcludeW
[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
192 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
193 attr
.Length
= sizeof(attr
);
194 attr
.RootDirectory
= root
;
195 attr
.ObjectName
= &name
;
197 attr
.SecurityDescriptor
= NULL
;
198 attr
.SecurityQualityOfService
= NULL
;
199 RtlInitUnicodeString( &name
, configW
);
201 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
202 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
204 if (!hkey
) return TRUE
;
206 debug_relay_includelist
= load_list( hkey
, RelayIncludeW
);
207 debug_relay_excludelist
= load_list( hkey
, RelayExcludeW
);
208 debug_snoop_includelist
= load_list( hkey
, SnoopIncludeW
);
209 debug_snoop_excludelist
= load_list( hkey
, SnoopExcludeW
);
210 debug_from_relay_includelist
= load_list( hkey
, RelayFromIncludeW
);
211 debug_from_relay_excludelist
= load_list( hkey
, RelayFromExcludeW
);
212 debug_from_snoop_includelist
= load_list( hkey
, SnoopFromIncludeW
);
213 debug_from_snoop_excludelist
= load_list( hkey
, SnoopFromExcludeW
);
220 /***********************************************************************
223 * Check if a given module and function is in the list.
225 static BOOL
check_list( const char *module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
229 sprintf( ord_str
, "%d", ordinal
);
232 const WCHAR
*p
= strrchrW( *list
, '.' );
233 if (p
&& p
> *list
) /* check module and function */
236 if (strncmpiAW( module
, *list
, len
-1 ) || module
[len
]) continue;
237 if (p
[1] == '*' && !p
[2]) return TRUE
;
238 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
239 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
241 else /* function only */
243 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
250 /***********************************************************************
251 * check_relay_include
253 * Check if a given function must be included in the relay output.
255 static BOOL
check_relay_include( const char *module
, int ordinal
, const char *func
)
257 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
259 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
264 /***********************************************************************
267 * Check if calls from a given module must be included in the relay/snoop output,
268 * given the exclusion and inclusion lists.
270 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
272 static const WCHAR dllW
[] = {'.','d','l','l',0 };
273 const WCHAR
**listitem
;
276 if (!module
) return TRUE
;
277 if (!includelist
&& !excludelist
) return TRUE
;
281 listitem
= excludelist
;
286 listitem
= includelist
;
288 for(; *listitem
; listitem
++)
292 if (!strcmpiW( *listitem
, module
)) return !show
;
293 len
= strlenW( *listitem
);
294 if (!strncmpiW( *listitem
, module
, len
) && !strcmpiW( module
+ len
, dllW
))
300 /***********************************************************************
303 static inline void RELAY_PrintArgs( const INT_PTR
*args
, int nb_args
, unsigned int typemask
)
307 if ((typemask
& 3) && !IS_INTARG(*args
))
310 DPRINTF( "%08lx %s", *args
, debugstr_w((LPCWSTR
)*args
) );
312 DPRINTF( "%08lx %s", *args
, debugstr_a((LPCSTR
)*args
) );
314 else DPRINTF( "%08lx", *args
);
315 if (nb_args
) DPRINTF( "," );
321 static void print_timestamp(void)
323 ULONG ticks
= NtGetTickCount();
324 DPRINTF( "%3u.%03u:", ticks
/ 1000, ticks
% 1000 );
327 /***********************************************************************
330 * stack points to the return address, i.e. the first argument is stack[1].
332 void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
334 WORD ordinal
= LOWORD(idx
);
335 BYTE nb_args
= LOBYTE(HIWORD(idx
));
336 struct relay_private_data
*data
= descr
->private;
337 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
341 if (TRACE_ON(timestamp
)) print_timestamp();
343 if (entry_point
->name
)
344 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
346 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
347 RELAY_PrintArgs( stack
+ 1, nb_args
, descr
->arg_types
[ordinal
] );
348 DPRINTF( ") ret=%08lx\n", stack
[0] );
350 return entry_point
->orig_func
;
353 /***********************************************************************
356 void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
357 const INT_PTR
*stack
, LONGLONG retval
)
359 WORD ordinal
= LOWORD(idx
);
360 BYTE flags
= HIBYTE(HIWORD(idx
));
361 struct relay_private_data
*data
= descr
->private;
362 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
364 if (!TRACE_ON(relay
)) return;
366 if (TRACE_ON(timestamp
)) print_timestamp();
368 if (entry_point
->name
)
369 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
371 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
373 if (flags
& 1) /* 64-bit return value */
374 DPRINTF( " retval=%08x%08x ret=%08lx\n",
375 (UINT
)(retval
>> 32), (UINT
)retval
, stack
[0] );
377 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR
)retval
, stack
[0] );
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
,
385 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
386 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
388 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
390 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
392 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
393 "movl 12(%ebp),%edx\n\t"
398 "movl 12(%ebp),%ecx\n\t"
399 "movl 16(%ebp),%esi\n\t"
403 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
406 "1:\tcall *8(%ebp)\n\t"
407 "leal -8(%ebp),%esp\n\t"
409 __ASM_CFI(".cfi_same_value %edi\n\t")
411 __ASM_CFI(".cfi_same_value %esi\n\t")
413 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
414 __ASM_CFI(".cfi_same_value %ebp\n\t")
417 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
418 __ASM_GLOBAL_FUNC( relay_call
,
420 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
421 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
423 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
425 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
427 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
429 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
430 /* trace the parameters */
434 "call " __ASM_NAME("relay_trace_entry") "\n\t"
435 /* copy the arguments*/
436 "movzbl 14(%ebp),%ecx\n\t" /* number of args */
438 "leal 0(,%ecx,4),%edx\n\t"
441 "movl 16(%ebp),%esi\n\t"
446 "testb $2,15(%ebp)\n\t" /* (flags & 2) -> thiscall */
449 /* call the entry point */
453 /* trace the return value */
454 "leal -20(%ebp),%esp\n\t"
460 "call " __ASM_NAME("relay_trace_exit") "\n\t"
461 /* restore return value and return */
462 "leal -12(%ebp),%esp\n\t"
466 __ASM_CFI(".cfi_same_value %ecx\n\t")
468 __ASM_CFI(".cfi_same_value %edi\n\t")
470 __ASM_CFI(".cfi_same_value %esi\n\t")
472 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
473 __ASM_CFI(".cfi_same_value %ebp\n\t")
476 void WINAPI
__regs_relay_call_regs( struct relay_descr
*descr
, unsigned int idx
,
477 unsigned int orig_eax
, unsigned int ret_addr
,
480 WORD ordinal
= LOWORD(idx
);
481 BYTE nb_args
= LOBYTE(HIWORD(idx
));
482 struct relay_private_data
*data
= descr
->private;
483 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
484 BYTE
*orig_func
= entry_point
->orig_func
;
485 INT_PTR
*args
= (INT_PTR
*)context
->Esp
;
486 INT_PTR args_copy
[32];
488 /* restore the context to what it was before the relay thunk */
489 context
->Eax
= orig_eax
;
490 context
->Eip
= ret_addr
;
491 context
->Esp
+= nb_args
* sizeof(int);
495 if (entry_point
->name
)
496 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
498 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
499 RELAY_PrintArgs( args
, nb_args
, descr
->arg_types
[ordinal
] );
500 DPRINTF( ") ret=%08x\n", ret_addr
);
502 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
503 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
504 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
505 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
506 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
508 assert( orig_func
[0] == 0x68 /* pushl func */ );
509 assert( orig_func
[5] == 0x6a /* pushl args */ );
510 assert( orig_func
[7] == 0xe8 /* call */ );
513 /* now call the real function */
515 memcpy( args_copy
, args
, nb_args
* sizeof(args
[0]) );
516 args_copy
[nb_args
++] = (INT_PTR
)context
; /* append context argument */
518 call_entry_point( orig_func
+ 12 + *(int *)(orig_func
+ 1), nb_args
, args_copy
, 0 );
522 if (entry_point
->name
)
523 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
524 GetCurrentThreadId(), data
->dllname
, entry_point
->name
,
525 context
->Eax
, context
->Eip
);
527 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
528 GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
,
529 context
->Eax
, context
->Eip
);
530 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
531 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
532 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
533 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
534 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
537 extern void WINAPI
relay_call_regs(void);
538 DEFINE_REGS_ENTRYPOINT( relay_call_regs
, 4 )
540 #elif defined(__arm__)
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
,
545 "push {r4, r5, LR}\n\t"
553 "subeq SP, SP, #4\n\t"
554 "1:\tsub r3, r3, #4\n\t"
555 "ldr r0, [r2, r3]\n\t"
556 "str r0, [SP, r3]\n\t"
571 "4:\tpop {r0-r3}\n\t"
576 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
578 BYTE nb_args
= LOBYTE(HIWORD(idx
));
579 BYTE flags
= HIBYTE(HIWORD(idx
));
580 void *func
= relay_trace_entry( descr
, idx
, stack
);
581 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
+ 1, flags
);
582 relay_trace_exit( descr
, idx
, stack
, ret
);
586 static void WINAPI
relay_call_regs( struct relay_descr
*descr
, INT_PTR idx
, INT_PTR
*stack
)
588 assert(0); /* should never be called */
591 #elif defined(__x86_64__)
593 extern void * WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
594 __ASM_GLOBAL_FUNC( relay_call
,
596 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
597 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
599 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
600 "subq $0x30,%rsp\n\t"
601 "movq %rsi,0x20(%rsp)\n\t"
602 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
603 "movq %rdi,0x28(%rsp)\n\t"
604 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
605 /* trace the parameters */
606 "movq %rcx,0x10(%rbp)\n\t"
607 "movq %rdx,0x18(%rbp)\n\t"
608 "movq %r8,0x20(%rbp)\n\t"
609 "call " __ASM_NAME("relay_trace_entry") "\n\t"
610 /* copy the arguments */
611 "movzbq 0x1a(%rbp),%rdx\n\t" /* number of args */
614 "cmovgq %rdx,%rcx\n\t"
615 "leaq -16(,%rcx,8),%rdx\n\t"
618 "movq 0x20(%rbp),%r8\n\t" /* original stack */
619 "leaq 8(%r8),%rsi\n\t"
622 /* call the entry point */
623 "movq 0(%rsp),%rcx\n\t"
624 "movq 8(%rsp),%rdx\n\t"
625 "movq 16(%rsp),%r8\n\t"
626 "movq 24(%rsp),%r9\n\t"
627 "movq 0(%rsp),%xmm0\n\t"
628 "movq 8(%rsp),%xmm1\n\t"
629 "movq 16(%rsp),%xmm2\n\t"
630 "movq 24(%rsp),%xmm3\n\t"
632 /* trace the return value */
633 "leaq -0x30(%rbp),%rsp\n\t"
634 "movq 0x10(%rbp),%rcx\n\t"
635 "movq 0x18(%rbp),%rdx\n\t"
636 "movq 0x20(%rbp),%r8\n\t"
638 "movaps %xmm0,0x10(%rbp)\n\t"
640 "call " __ASM_NAME("relay_trace_exit") "\n\t"
641 /* restore return value and return */
643 "movaps 0x10(%rbp),%xmm0\n\t"
644 "movq 0x20(%rsp),%rsi\n\t"
645 __ASM_CFI(".cfi_same_value %rsi\n\t")
646 "movq 0x28(%rsp),%rdi\n\t"
647 __ASM_CFI(".cfi_same_value %rdi\n\t")
649 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
651 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
652 __ASM_CFI(".cfi_same_value %rbp\n\t")
655 static void WINAPI
relay_call_regs( struct relay_descr
*descr
, INT_PTR idx
, INT_PTR
*stack
)
657 assert(0); /* should never be called */
661 #error Not supported on this CPU
665 /***********************************************************************
666 * RELAY_GetProcAddress
668 * Return the proc address to use for a given function.
670 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
671 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
673 struct relay_private_data
*data
;
674 const struct relay_descr
*descr
= (const struct relay_descr
*)((const char *)exports
+ exp_size
);
676 if (descr
->magic
!= RELAY_DESCR_MAGIC
|| !(data
= descr
->private)) return proc
; /* no relay data */
677 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 return proc
; /* we want to relay it */
680 return data
->entry_points
[ordinal
].orig_func
;
684 /***********************************************************************
687 * Setup relay debugging for a built-in dll.
689 void RELAY_SetupDLL( HMODULE module
)
691 IMAGE_EXPORT_DIRECTORY
*exports
;
694 DWORD size
, entry_point_rva
;
695 struct relay_descr
*descr
;
696 struct relay_private_data
*data
;
699 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
701 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
702 if (!exports
) return;
704 descr
= (struct relay_descr
*)((char *)exports
+ size
);
705 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return;
707 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
708 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
711 descr
->relay_call
= relay_call
;
712 descr
->relay_call_regs
= relay_call_regs
;
713 descr
->private = data
;
715 data
->module
= module
;
716 data
->base
= exports
->Base
;
717 len
= strlen( (char *)module
+ exports
->Name
);
718 if (len
> 4 && !strcasecmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
719 len
= min( len
, sizeof(data
->dllname
) - 1 );
720 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
721 data
->dllname
[len
] = 0;
723 /* fetch name pointer for all entry points and store them in the private structure */
725 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
726 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
728 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
729 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
732 /* patch the functions in the export table to point to the relay thunks */
734 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
735 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
736 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
738 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
739 if (!check_relay_include( data
->dllname
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
740 continue; /* don't include this entry point */
742 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
743 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
747 #else /* __i386__ || __x86_64__ || __arm__ */
749 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
750 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
755 void RELAY_SetupDLL( HMODULE module
)
759 #endif /* __i386__ || __x86_64__ || __arm__ */
762 /***********************************************************************/
764 /***********************************************************************/
768 WINE_DECLARE_DEBUG_CHANNEL(seh
);
769 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
771 #include "pshpack1.h"
776 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
777 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
780 DWORD snoopentry
; /* SNOOP_Entry relative */
787 typedef struct tagSNOOP_DLL
{
792 struct tagSNOOP_DLL
*next
;
799 BYTE lcall
; /* 0xe8 call snoopret relative*/
800 /* NOTE: If you move snoopret OR origreturn fix the relative offset
803 DWORD snoopret
; /* SNOOP_Ret relative */
809 DWORD
*args
; /* saved args across a stdcall */
812 typedef struct tagSNOOP_RETURNENTRIES
{
813 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
814 struct tagSNOOP_RETURNENTRIES
*next
;
815 } SNOOP_RETURNENTRIES
;
819 extern void WINAPI
SNOOP_Entry(void);
820 extern void WINAPI
SNOOP_Return(void);
822 static SNOOP_DLL
*firstdll
;
823 static SNOOP_RETURNENTRIES
*firstrets
;
826 /***********************************************************************
827 * SNOOP_ShowDebugmsgSnoop
829 * Simple function to decide if a particular debugging message is
832 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
834 if (debug_snoop_excludelist
&& check_list( module
, ordinal
, func
, debug_snoop_excludelist
))
836 if (debug_snoop_includelist
&& !check_list( module
, ordinal
, func
, debug_snoop_includelist
))
842 /***********************************************************************
845 * Setup snoop debugging for a native dll.
847 void SNOOP_SetupDLL(HMODULE hmod
)
849 SNOOP_DLL
**dll
= &firstdll
;
854 IMAGE_EXPORT_DIRECTORY
*exports
;
856 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
858 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
859 if (!exports
|| !exports
->NumberOfFunctions
) return;
860 name
= (char *)hmod
+ exports
->Name
;
863 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
866 if ((*dll
)->hmod
== hmod
)
868 /* another dll, loaded at the same address */
870 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
871 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
874 dll
= &((*dll
)->next
);
877 *dll
= RtlReAllocateHeap(GetProcessHeap(),
878 HEAP_ZERO_MEMORY
, *dll
,
879 sizeof(SNOOP_DLL
) + strlen(name
));
881 *dll
= RtlAllocateHeap(GetProcessHeap(),
883 sizeof(SNOOP_DLL
) + strlen(name
));
885 (*dll
)->ordbase
= exports
->Base
;
886 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
887 strcpy( (*dll
)->name
, name
);
888 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
889 if (p
> (*dll
)->name
&& !strcasecmp( p
, ".dll" )) *p
= 0;
891 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
893 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
894 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
896 RtlFreeHeap(GetProcessHeap(),0,*dll
);
897 FIXME("out of memory\n");
901 memset((*dll
)->funs
,0,size
);
905 /***********************************************************************
906 * SNOOP_GetProcAddress
908 * Return the proc address to use for a given function.
910 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
911 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
916 const WORD
*ordinals
;
918 SNOOP_DLL
*dll
= firstdll
;
920 const IMAGE_SECTION_HEADER
*sec
;
922 if (!TRACE_ON(snoop
)) return origfun
;
923 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
924 return origfun
; /* the calling module was explicitly excluded */
926 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
929 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
931 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
932 return origfun
; /* most likely a data reference */
935 if (hmod
== dll
->hmod
)
939 if (!dll
) /* probably internal */
942 /* try to find a name for it */
944 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
945 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
946 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
948 if (ordinals
[i
] == ordinal
)
950 ename
= (const char *)hmod
+ names
[i
];
954 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
956 assert(ordinal
< dll
->nrofordinals
);
957 fun
= dll
->funs
+ ordinal
;
962 /* NOTE: origreturn struct member MUST come directly after snoopentry */
963 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
964 fun
->origfun
= origfun
;
967 return (FARPROC
)&(fun
->lcall
);
970 static void SNOOP_PrintArg(DWORD x
)
975 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
982 if (s
[i
]<0x20) {nostring
=1;break;}
983 if (s
[i
]>=0x80) {nostring
=1;break;}
986 if (!nostring
&& i
> 5)
987 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
988 else /* try unicode */
994 if (s
[i
]<0x20) {nostring
=1;break;}
995 if (s
[i
]>0x100) {nostring
=1;break;}
998 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
1007 #define CALLER1REF (*(DWORD*)context->Esp)
1009 void WINAPI
__regs_SNOOP_Entry( CONTEXT
*context
)
1011 DWORD ordinal
=0,entry
= context
->Eip
- 5;
1012 SNOOP_DLL
*dll
= firstdll
;
1013 SNOOP_FUN
*fun
= NULL
;
1014 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1015 SNOOP_RETURNENTRY
*ret
;
1019 if ( ((char*)entry
>=(char*)dll
->funs
) &&
1020 ((char*)entry
<=(char*)(dll
->funs
+dll
->nrofordinals
))
1022 fun
= (SNOOP_FUN
*)entry
;
1023 ordinal
= fun
-dll
->funs
;
1029 FIXME("entrypoint 0x%08x not found\n",entry
);
1032 /* guess cdecl ... */
1033 if (fun
->nrofargs
<0) {
1034 /* Typical cdecl return frame is:
1036 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1037 * (after that 81 C2 xx xx xx xx)
1039 LPBYTE reteip
= (LPBYTE
)CALLER1REF
;
1042 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1043 fun
->nrofargs
=reteip
[2]/4;
1049 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
1050 if (!(*rets
)->entry
[i
].origreturn
)
1052 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
1054 rets
= &((*rets
)->next
);
1060 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1061 MEM_COMMIT
| MEM_RESERVE
,
1062 PAGE_EXECUTE_READWRITE
);
1065 memset(*rets
,0,4096);
1066 i
= 0; /* entry 0 is free */
1068 ret
= &((*rets
)->entry
[i
]);
1070 /* NOTE: origreturn struct member MUST come directly after snoopret */
1071 ret
->snoopret
= ((char*)SNOOP_Return
)-(char*)(&ret
->origreturn
);
1072 ret
->origreturn
= (FARPROC
)CALLER1REF
;
1073 CALLER1REF
= (DWORD
)&ret
->lcall
;
1076 ret
->ordinal
= ordinal
;
1077 ret
->origESP
= context
->Esp
;
1079 context
->Eip
= (DWORD
)fun
->origfun
;
1081 if (TRACE_ON(timestamp
))
1083 if (fun
->name
) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll
->name
,fun
->name
);
1084 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
);
1085 if (fun
->nrofargs
>0) {
1086 max
= fun
->nrofargs
; if (max
>16) max
=16;
1089 SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
));
1090 if (i
<fun
->nrofargs
-1) DPRINTF(",");
1092 if (max
!=fun
->nrofargs
)
1094 } else if (fun
->nrofargs
<0) {
1095 DPRINTF("<unknown, check return>");
1096 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1097 0,16*sizeof(DWORD
));
1098 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
1100 DPRINTF(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1104 void WINAPI
__regs_SNOOP_Return( CONTEXT
*context
)
1106 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
1107 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1109 /* We haven't found out the nrofargs yet. If we called a cdecl
1110 * function it is too late anyway and we can just set '0' (which
1111 * will be the difference between orig and current ESP
1112 * If stdcall -> everything ok.
1114 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1115 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
1116 context
->Eip
= (DWORD
)ret
->origreturn
;
1117 if (TRACE_ON(timestamp
))
1123 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret
->dll
->name
, fun
->name
);
1125 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1126 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
);
1128 max
= fun
->nrofargs
;
1133 SNOOP_PrintArg(ret
->args
[i
]);
1134 if (i
<max
-1) DPRINTF(",");
1136 DPRINTF(") retval=%08x ret=%08x\n",
1137 context
->Eax
,(DWORD
)ret
->origreturn
);
1138 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1144 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1145 GetCurrentThreadId(),
1146 ret
->dll
->name
, fun
->name
, context
->Eax
, (DWORD
)ret
->origreturn
);
1148 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1149 GetCurrentThreadId(),
1150 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1151 context
->Eax
, (DWORD
)ret
->origreturn
);
1153 ret
->origreturn
= NULL
; /* mark as empty */
1156 /* assembly wrappers that save the context */
1157 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry
, 0 )
1158 DEFINE_REGS_ENTRYPOINT( SNOOP_Return
, 0 )
1160 #else /* __i386__ */
1162 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1163 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1168 void SNOOP_SetupDLL( HMODULE hmod
)
1170 FIXME("snooping works only on i386 for now.\n");
1173 #endif /* __i386__ */