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
28 #define WIN32_NO_STATUS
31 #include "wine/exception.h"
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(relay
);
37 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
39 struct relay_descr
/* descriptor for a module */
41 ULONG_PTR magic
; /* signature */
42 void *relay_call
; /* functions to call from relay thunks */
43 void *private; /* reserved for the relay code private data */
44 const char *entry_point_base
; /* base address of entry point thunks */
45 const unsigned int *entry_point_offsets
; /* offsets of entry points thunks */
46 const char *args_string
; /* string describing the arguments */
49 struct relay_descr_rva
/* RVA to the descriptor for PE dlls */
55 #define RELAY_DESCR_MAGIC 0xdeb90002
56 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
58 /* private data built at dll load time */
60 struct relay_entry_point
62 void *orig_func
; /* original entry point function */
63 const char *name
; /* function name (if any) */
66 struct relay_private_data
68 HMODULE module
; /* module handle of this dll */
69 unsigned int base
; /* ordinal base */
70 char dllname
[40]; /* dll name (without .dll extension) */
71 struct relay_entry_point entry_points
[1]; /* list of dll entry points */
74 static const WCHAR
**debug_relay_excludelist
;
75 static const WCHAR
**debug_relay_includelist
;
76 static const WCHAR
**debug_snoop_excludelist
;
77 static const WCHAR
**debug_snoop_includelist
;
78 static const WCHAR
**debug_from_relay_excludelist
;
79 static const WCHAR
**debug_from_relay_includelist
;
80 static const WCHAR
**debug_from_snoop_excludelist
;
81 static const WCHAR
**debug_from_snoop_includelist
;
83 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
85 /* compare an ASCII and a Unicode string without depending on the current codepage */
86 static inline int strcmpAW( const char *strA
, const WCHAR
*strW
)
88 while (*strA
&& ((unsigned char)*strA
== *strW
)) { strA
++; strW
++; }
89 return (unsigned char)*strA
- *strW
;
92 /***********************************************************************
95 * Build a function list from a ';'-separated string.
97 static const WCHAR
**build_list( const WCHAR
*buffer
)
100 const WCHAR
*p
= buffer
;
103 while ((p
= wcschr( p
, ';' )))
108 /* allocate count+1 pointers, plus the space for a copy of the string */
109 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0,
110 (count
+1) * sizeof(WCHAR
*) + (wcslen(buffer
)+1) * sizeof(WCHAR
) )))
112 WCHAR
*str
= (WCHAR
*)(ret
+ count
+ 1);
115 wcscpy( str
, buffer
);
120 if (!(q
= wcschr( q
, ';' ))) break;
128 /***********************************************************************
131 * Load a function list from a registry value.
133 static const WCHAR
**load_list( HKEY hkey
, const WCHAR
*value
)
135 char initial_buffer
[4096];
136 char *buffer
= initial_buffer
;
140 const WCHAR
**list
= NULL
;
142 RtlInitUnicodeString( &name
, value
);
143 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, sizeof(initial_buffer
), &count
);
144 if (status
== STATUS_BUFFER_OVERFLOW
)
146 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, count
);
147 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, count
, &count
);
149 if (status
== STATUS_SUCCESS
)
151 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
152 list
= build_list( str
);
153 if (list
) TRACE( "%s = %s\n", debugstr_w(value
), debugstr_w(str
) );
156 if (buffer
!= initial_buffer
) RtlFreeHeap( GetProcessHeap(), 0, buffer
);
160 /***********************************************************************
163 * Build the relay include/exclude function lists.
165 static DWORD WINAPI
init_debug_lists( RTL_RUN_ONCE
*once
, void *param
, void **context
)
167 OBJECT_ATTRIBUTES attr
;
171 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
172 attr
.Length
= sizeof(attr
);
173 attr
.RootDirectory
= root
;
174 attr
.ObjectName
= &name
;
176 attr
.SecurityDescriptor
= NULL
;
177 attr
.SecurityQualityOfService
= NULL
;
178 RtlInitUnicodeString( &name
, L
"Software\\Wine\\Debug" );
180 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
181 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
183 if (!hkey
) return TRUE
;
185 debug_relay_includelist
= load_list( hkey
, L
"RelayInclude" );
186 debug_relay_excludelist
= load_list( hkey
, L
"RelayExclude" );
187 debug_snoop_includelist
= load_list( hkey
, L
"SnoopInclude" );
188 debug_snoop_excludelist
= load_list( hkey
, L
"SnoopExclude" );
189 debug_from_relay_includelist
= load_list( hkey
, L
"RelayFromInclude" );
190 debug_from_relay_excludelist
= load_list( hkey
, L
"RelayFromExclude" );
191 debug_from_snoop_includelist
= load_list( hkey
, L
"SnoopFromInclude" );
192 debug_from_snoop_excludelist
= load_list( hkey
, L
"SnoopFromExclude" );
199 /***********************************************************************
202 * Check if a given module and function is in the list.
204 static BOOL
check_list( const WCHAR
*module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
208 sprintf( ord_str
, "%d", ordinal
);
211 const WCHAR
*p
= wcsrchr( *list
, '.' );
212 if (p
&& p
> *list
) /* check module and function */
215 if (wcsnicmp( module
, *list
, len
- 1 ) || module
[len
]) continue;
216 if (p
[1] == '*' && !p
[2]) return TRUE
;
217 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
218 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
220 else /* function only */
222 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
229 /***********************************************************************
230 * check_relay_include
232 * Check if a given function must be included in the relay output.
234 static BOOL
check_relay_include( const WCHAR
*module
, int ordinal
, const char *func
)
236 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
238 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
243 /***********************************************************************
246 * Check if calls from a given module must be included in the relay/snoop output,
247 * given the exclusion and inclusion lists.
249 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
251 const WCHAR
**listitem
;
254 if (!module
) return TRUE
;
255 if (!includelist
&& !excludelist
) return TRUE
;
259 listitem
= excludelist
;
264 listitem
= includelist
;
266 for(; *listitem
; listitem
++)
270 if (!wcsicmp( *listitem
, module
)) return !show
;
271 len
= wcslen( *listitem
);
272 if (!wcsnicmp( *listitem
, module
, len
) && !wcsicmp( module
+ len
, L
".dll" ))
279 static BOOL
is_ret_val( char type
)
281 return type
>= 'A' && type
<= 'Z';
284 static const char *func_name( struct relay_private_data
*data
, unsigned int ordinal
)
286 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
288 if (entry_point
->name
)
289 return wine_dbg_sprintf( "%s.%s", data
->dllname
, entry_point
->name
);
291 return wine_dbg_sprintf( "%s.%u", data
->dllname
, data
->base
+ ordinal
);
294 static void trace_string_a( INT_PTR ptr
)
296 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_a( (char *)ptr
));
297 else TRACE( "%08Ix", ptr
);
300 static void trace_string_w( INT_PTR ptr
)
302 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_w( (WCHAR
*)ptr
));
303 else TRACE( "%08Ix", ptr
);
308 /***********************************************************************
311 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
312 const DWORD
*stack
, unsigned int *nb_args
)
314 WORD ordinal
= LOWORD(idx
);
315 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
316 struct relay_private_data
*data
= descr
->private;
317 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
320 TRACE( "\1Call %s(", func_name( data
, ordinal
));
322 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
324 switch (arg_types
[i
])
326 case 'j': /* int64 */
327 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
330 case 'k': /* int128 */
331 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
335 trace_string_a( stack
[pos
++] );
338 trace_string_w( stack
[pos
++] );
340 case 'f': /* float */
341 TRACE( "%g", *(const float *)&stack
[pos
++] );
343 case 'd': /* double */
344 TRACE( "%g", *(const double *)&stack
[pos
] );
349 TRACE( "%08x", stack
[pos
++] );
352 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
355 if (arg_types
[0] == 't')
357 *nb_args
|= 0x80000000; /* thiscall/fastcall */
358 if (arg_types
[1] == 't') *nb_args
|= 0x40000000; /* fastcall */
360 TRACE( ") ret=%08x\n", stack
[-1] );
361 return entry_point
->orig_func
;
364 /***********************************************************************
367 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
368 void *retaddr
, LONGLONG retval
)
370 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
372 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
374 while (!is_ret_val( *arg_types
)) arg_types
++;
375 if (*arg_types
== 'J') /* int64 return value */
376 TRACE( " retval=%08x%08x ret=%08x\n",
377 (UINT
)(retval
>> 32), (UINT
)retval
, (UINT
)retaddr
);
379 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, (UINT
)retaddr
);
382 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
);
383 __ASM_STDCALL_FUNC( relay_call
, 8,
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")
394 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
395 /* trace the parameters */
397 "pushl %esp\n\t" /* number of args return ptr */
398 "leal 20(%ebp),%esi\n\t" /* stack */
402 "call " __ASM_STDCALL("relay_trace_entry",16) "\n\t"
403 /* copy the arguments*/
404 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
406 "leal 0(,%ecx,4),%edx\n\t"
412 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
415 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
418 /* call the entry point */
422 /* trace the return value */
423 "leal -20(%ebp),%esp\n\t"
429 "call " __ASM_STDCALL("relay_trace_exit",20) "\n\t"
430 /* restore return value and return */
431 "leal -12(%ebp),%esp\n\t"
435 __ASM_CFI(".cfi_same_value %ecx\n\t")
437 __ASM_CFI(".cfi_same_value %edi\n\t")
439 __ASM_CFI(".cfi_same_value %esi\n\t")
441 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
442 __ASM_CFI(".cfi_same_value %ebp\n\t")
445 #elif defined(__arm__)
447 /***********************************************************************
450 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
451 const DWORD
*stack
, unsigned int *nb_args
)
453 WORD ordinal
= LOWORD(idx
);
454 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
455 struct relay_private_data
*data
= descr
->private;
456 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
459 unsigned int float_pos
= 0, double_pos
= 0;
460 const union fpregs
{ float s
[16]; double d
[8]; } *fpstack
= (const union fpregs
*)stack
- 1;
463 TRACE( "\1Call %s(", func_name( data
, ordinal
));
465 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
467 switch (arg_types
[i
])
469 case 'j': /* int64 */
470 pos
= (pos
+ 1) & ~1;
471 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
474 case 'k': /* int128 */
475 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
479 trace_string_a( stack
[pos
++] );
482 trace_string_w( stack
[pos
++] );
484 case 'f': /* float */
486 if (!(float_pos
% 2)) float_pos
= max( float_pos
, double_pos
* 2 );
489 TRACE( "%g", fpstack
->s
[float_pos
++] );
493 TRACE( "%g", *(const float *)&stack
[pos
++] );
495 case 'd': /* double */
497 double_pos
= max( (float_pos
+ 1) / 2, double_pos
);
500 TRACE( "%g", fpstack
->d
[double_pos
++] );
504 pos
= (pos
+ 1) & ~1;
505 TRACE( "%g", *(const double *)&stack
[pos
] );
510 TRACE( "%08x", stack
[pos
++] );
513 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
517 if (float_pos
|| double_pos
)
520 stack
= (const DWORD
*)fpstack
; /* retaddr is below the fp regs */
524 TRACE( ") ret=%08x\n", stack
[-1] );
525 return entry_point
->orig_func
;
528 /***********************************************************************
531 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
532 DWORD retaddr
, LONGLONG retval
)
534 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
536 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
538 while (!is_ret_val( *arg_types
)) arg_types
++;
539 if (*arg_types
== 'J') /* int64 return value */
540 TRACE( " retval=%08x%08x ret=%08x\n",
541 (UINT
)(retval
>> 32), (UINT
)retval
, retaddr
);
543 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, retaddr
);
546 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const DWORD
*stack
);
547 __ASM_GLOBAL_FUNC( relay_call
,
548 "push {r4-r8,lr}\n\t"
551 "add r3, sp, #12\n\t"
554 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
555 "mov ip, r0\n\t" /* entry point */
557 "ldr r1, [sp, #12]\n\t" /* number of args */
559 "subs r3, #16\n\t" /* first 4 args are in registers */
564 "add r2, r6, #16\n\t" /* skip r0-r3 */
565 "1:\tsubs r3, r3, #4\n\t"
566 "ldr r0, [r2, r3]\n\t"
567 "str r0, [sp, r3]\n\t"
571 "tst r1, #0x80000000\n\t"
572 "ldm r6, {r0-r3}\n\t"
574 "vldmdbne r6!, {s0-s15}\n\t"
576 "ldm r6, {r0-r3}\n\t"
580 "ldr r2, [r6, #-4]\n\t" /* retaddr */
586 "str r5, [sp, #4]\n\t"
588 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
589 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
590 "vldr d0, [sp, #8]\n\t"
592 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
599 #elif defined(__aarch64__)
601 /***********************************************************************
604 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
605 const INT_PTR
*stack
, unsigned int *nb_args
)
607 WORD ordinal
= LOWORD(idx
);
608 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
609 struct relay_private_data
*data
= descr
->private;
610 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
613 TRACE( "\1Call %s(", func_name( data
, ordinal
));
615 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
617 switch (arg_types
[i
])
620 trace_string_a( stack
[i
] );
623 trace_string_w( stack
[i
] );
627 TRACE( "%08zx", stack
[i
] );
630 if (!is_ret_val( arg_types
[i
+ 1] )) TRACE( "," );
633 TRACE( ") ret=%08zx\n", stack
[-1] );
634 return entry_point
->orig_func
;
637 /***********************************************************************
640 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
641 INT_PTR retaddr
, INT_PTR retval
)
643 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
644 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
647 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
);
648 __ASM_GLOBAL_FUNC( call_entry_point
,
649 "stp x29, x30, [SP,#-16]!\n\t"
650 "stp x19, x20, [SP,#-16]!\n\t"
653 "ldp x8, x9, [x19, #-32]\n\t"
659 "ldp x0, x1, [x11], #16\n\t"
660 "add w12, w12, #2\n\t"
663 "ldp x2, x3, [x11], #16\n\t"
664 "add w12, w12, #2\n\t"
667 "ldp x4, x5, [x11], #16\n\t"
668 "add w12, w12, #2\n\t"
671 "ldp x6, x7, [x11], #16\n\t"
672 "add w12, w12, #2\n\t"
675 "sub w12, w10, #8\n\t"
676 "lsl w12, w12, #3\n\t"
677 "sub SP, SP, w12, uxtw\n\t"
678 "tbz w12, #3, 1f\n\t"
680 "1: sub w12, w12, #8\n\t"
681 "ldr x13, [x11, x12]\n\t"
682 "str x13, [SP, x12]\n\t"
686 "ldp x19, x20, [SP], #16\n\t"
687 "ldp x29, x30, [SP], #16\n\t"
690 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
692 unsigned int nb_args
;
693 void *func
= relay_trace_entry( descr
, idx
, stack
, &nb_args
);
694 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
);
695 relay_trace_exit( descr
, idx
, stack
[-1], ret
);
699 #elif defined(__x86_64__)
701 /***********************************************************************
704 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
705 const INT_PTR
*stack
, unsigned int *nb_args
)
707 WORD ordinal
= LOWORD(idx
);
708 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
709 struct relay_private_data
*data
= descr
->private;
710 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
713 TRACE( "\1Call %s(", func_name( data
, ordinal
));
715 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
717 switch (arg_types
[i
])
720 trace_string_a( stack
[i
] );
723 trace_string_w( stack
[i
] );
725 case 'f': /* float */
726 TRACE( "%g", *(const float *)&stack
[i
] );
728 case 'd': /* double */
729 TRACE( "%g", *(const double *)&stack
[i
] );
733 TRACE( "%08zx", stack
[i
] );
736 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
739 TRACE( ") ret=%08zx\n", stack
[-1] );
740 return entry_point
->orig_func
;
743 /***********************************************************************
746 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
747 INT_PTR retaddr
, INT_PTR retval
)
749 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
750 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
753 extern INT_PTR WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
754 __ASM_GLOBAL_FUNC( relay_call
,
756 __ASM_SEH(".seh_pushreg %rbp\n\t")
757 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
758 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
760 __ASM_SEH(".seh_setframe %rbp,0\n\t")
761 __ASM_SEH(".seh_endprologue\n\t")
762 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
763 "leaq -0x48(%rbp),%rsp\n\t"
765 "movq %rcx,-32(%rbp)\n\t"
766 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
767 "movq %rdx,-24(%rbp)\n\t"
768 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
769 "movq %rsi,-16(%rbp)\n\t"
770 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
771 "movq %rdi,-8(%rbp)\n\t"
772 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
773 /* trace the parameters */
774 "leaq 24(%rbp),%r8\n\t" /* stack */
775 "leaq -40(%rbp),%r9\n\t"
776 "call " __ASM_NAME("relay_trace_entry") "\n\t"
777 /* copy the arguments */
778 "movl -40(%rbp),%edx\n\t" /* number of args */
781 "cmovgq %rdx,%rcx\n\t"
782 "leaq -16(,%rcx,8),%rdx\n\t"
785 "leaq 24(%rbp),%rsi\n\t" /* original stack */
788 /* call the entry point */
789 "movq 0(%rsp),%rcx\n\t"
790 "movq 8(%rsp),%rdx\n\t"
791 "movq 16(%rsp),%r8\n\t"
792 "movq 24(%rsp),%r9\n\t"
793 "movq 0(%rsp),%xmm0\n\t"
794 "movq 8(%rsp),%xmm1\n\t"
795 "movq 16(%rsp),%xmm2\n\t"
796 "movq 24(%rsp),%xmm3\n\t"
798 /* trace the return value */
799 "movq -32(%rbp),%rcx\n\t"
800 "movq -24(%rbp),%rdx\n\t"
801 "movq 16(%rbp),%r8\n\t" /* retaddr */
803 "movaps %xmm0,32(%rsp)\n\t"
805 "call " __ASM_NAME("relay_trace_exit") "\n\t"
806 /* restore return value and return */
808 "movaps 32(%rsp),%xmm0\n\t"
809 "movq -16(%rbp),%rsi\n\t"
810 __ASM_CFI(".cfi_same_value %rsi\n\t")
811 "movq -8(%rbp),%rdi\n\t"
812 __ASM_CFI(".cfi_same_value %rdi\n\t")
813 "leaq 0(%rbp),%rsp\n\t"
814 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
816 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
817 __ASM_CFI(".cfi_same_value %rbp\n\t")
821 #error Not supported on this CPU
825 static struct relay_descr
*get_relay_descr( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
828 struct relay_descr
*descr
;
829 struct relay_descr_rva
*rva
;
830 ULONG_PTR ptr
= (ULONG_PTR
)module
+ exports
->Name
;
833 if (ptr
<= (ULONG_PTR
)(exports
+ 1)) return NULL
;
834 if (ptr
> (ULONG_PTR
)exports
+ exp_size
) return NULL
;
835 if (ptr
% sizeof(DWORD
)) return NULL
;
837 rva
= (struct relay_descr_rva
*)ptr
- 1;
838 if (rva
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
839 if (rva
->descr
) descr
= (struct relay_descr
*)((char *)module
+ rva
->descr
);
840 else descr
= (struct relay_descr
*)((const char *)exports
+ exp_size
);
841 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
845 /***********************************************************************
846 * RELAY_GetProcAddress
848 * Return the proc address to use for a given function.
850 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
851 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
853 struct relay_private_data
*data
;
854 const struct relay_descr
*descr
= get_relay_descr( module
, exports
, exp_size
);
856 if (!descr
|| !(data
= descr
->private)) return proc
; /* no relay data */
857 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
858 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
859 return proc
; /* we want to relay it */
860 return data
->entry_points
[ordinal
].orig_func
;
864 /***********************************************************************
867 * Setup relay debugging for a built-in dll.
869 void RELAY_SetupDLL( HMODULE module
)
871 IMAGE_EXPORT_DIRECTORY
*exports
;
874 DWORD size
, entry_point_rva
, old_prot
;
875 struct relay_descr
*descr
;
876 struct relay_private_data
*data
;
877 WCHAR dllnameW
[sizeof(data
->dllname
)];
882 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
884 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
885 if (!exports
) return;
887 if (!(descr
= get_relay_descr( module
, exports
, size
))) return;
889 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
890 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
893 descr
->relay_call
= relay_call
;
894 descr
->private = data
;
896 data
->module
= module
;
897 data
->base
= exports
->Base
;
898 len
= strlen( (char *)module
+ exports
->Name
);
899 if (len
> 4 && !_stricmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
900 len
= min( len
, sizeof(data
->dllname
) - 1 );
901 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
902 data
->dllname
[len
] = 0;
903 ascii_to_unicode( dllnameW
, data
->dllname
, len
+ 1 );
905 /* fetch name pointer for all entry points and store them in the private structure */
907 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
908 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
910 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
911 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
914 /* patch the functions in the export table to point to the relay thunks */
916 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
917 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
920 func_size
= exports
->NumberOfFunctions
* sizeof(*funcs
);
921 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, PAGE_READWRITE
, &old_prot
);
922 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
924 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
925 if (!check_relay_include( dllnameW
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
926 continue; /* don't include this entry point */
928 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
929 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
931 if (old_prot
!= PAGE_READWRITE
)
932 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, old_prot
, &old_prot
);
935 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
937 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
938 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
943 void RELAY_SetupDLL( HMODULE module
)
947 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
950 /***********************************************************************/
952 /***********************************************************************/
956 WINE_DECLARE_DEBUG_CHANNEL(seh
);
957 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
959 #include "pshpack1.h"
964 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
965 DWORD snoopentry
; /* SNOOP_Entry relative */
972 typedef struct tagSNOOP_DLL
{
977 struct tagSNOOP_DLL
*next
;
984 BYTE lcall
; /* 0xe8 call snoopret relative*/
985 DWORD snoopret
; /* SNOOP_Ret relative */
991 DWORD
*args
; /* saved args across a stdcall */
994 typedef struct tagSNOOP_RETURNENTRIES
{
995 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
996 struct tagSNOOP_RETURNENTRIES
*next
;
997 } SNOOP_RETURNENTRIES
;
1001 extern void WINAPI
SNOOP_Entry(void);
1002 extern void WINAPI
SNOOP_Return(void);
1004 static SNOOP_DLL
*firstdll
;
1005 static SNOOP_RETURNENTRIES
*firstrets
;
1008 /***********************************************************************
1009 * SNOOP_ShowDebugmsgSnoop
1011 * Simple function to decide if a particular debugging message is
1014 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
1017 int len
= strlen(module
);
1019 if (len
>= ARRAY_SIZE( moduleW
)) return FALSE
;
1020 ascii_to_unicode( moduleW
, module
, len
+ 1 );
1021 if (debug_snoop_excludelist
&& check_list( moduleW
, ordinal
, func
, debug_snoop_excludelist
))
1023 if (debug_snoop_includelist
&& !check_list( moduleW
, ordinal
, func
, debug_snoop_includelist
))
1029 /***********************************************************************
1032 * Setup snoop debugging for a native dll.
1034 void SNOOP_SetupDLL(HMODULE hmod
)
1036 SNOOP_DLL
**dll
= &firstdll
;
1041 IMAGE_EXPORT_DIRECTORY
*exports
;
1043 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
1045 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
1046 if (!exports
|| !exports
->NumberOfFunctions
) return;
1047 name
= (char *)hmod
+ exports
->Name
;
1050 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
1053 if ((*dll
)->hmod
== hmod
)
1055 /* another dll, loaded at the same address */
1056 addr
= (*dll
)->funs
;
1057 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
1058 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1061 dll
= &((*dll
)->next
);
1064 *dll
= RtlReAllocateHeap(GetProcessHeap(),
1065 HEAP_ZERO_MEMORY
, *dll
,
1066 sizeof(SNOOP_DLL
) + strlen(name
));
1068 *dll
= RtlAllocateHeap(GetProcessHeap(),
1070 sizeof(SNOOP_DLL
) + strlen(name
));
1071 (*dll
)->hmod
= hmod
;
1072 (*dll
)->ordbase
= exports
->Base
;
1073 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
1074 strcpy( (*dll
)->name
, name
);
1075 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
1076 if (p
> (*dll
)->name
&& !_stricmp( p
, ".dll" )) *p
= 0;
1078 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
1080 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1081 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
1083 RtlFreeHeap(GetProcessHeap(),0,*dll
);
1084 FIXME("out of memory\n");
1087 (*dll
)->funs
= addr
;
1088 memset((*dll
)->funs
,0,size
);
1092 /***********************************************************************
1093 * SNOOP_GetProcAddress
1095 * Return the proc address to use for a given function.
1097 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
1098 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
1103 const WORD
*ordinals
;
1105 SNOOP_DLL
*dll
= firstdll
;
1107 const IMAGE_SECTION_HEADER
*sec
;
1109 if (!TRACE_ON(snoop
)) return origfun
;
1110 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
1111 return origfun
; /* the calling module was explicitly excluded */
1113 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
1116 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
1118 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
1119 return origfun
; /* most likely a data reference */
1122 if (hmod
== dll
->hmod
)
1126 if (!dll
) /* probably internal */
1129 /* try to find a name for it */
1131 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
1132 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
1133 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
1135 if (ordinals
[i
] == ordinal
)
1137 ename
= (const char *)hmod
+ names
[i
];
1141 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
1143 assert(ordinal
< dll
->nrofordinals
);
1144 fun
= dll
->funs
+ ordinal
;
1149 fun
->snoopentry
= (char *)SNOOP_Entry
- (char *)(&fun
->snoopentry
+ 1);
1150 fun
->origfun
= origfun
;
1153 return (FARPROC
)&(fun
->lcall
);
1156 static void SNOOP_PrintArg(DWORD x
)
1160 TRACE_(snoop
)("%08x",x
);
1161 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
1168 if (s
[i
]<0x20) {nostring
=1;break;}
1169 if (s
[i
]>=0x80) {nostring
=1;break;}
1172 if (!nostring
&& i
> 5)
1173 TRACE_(snoop
)(" %s",debugstr_an((LPSTR
)x
,i
));
1174 else /* try unicode */
1180 if (s
[i
]<0x20) {nostring
=1;break;}
1181 if (s
[i
]>0x100) {nostring
=1;break;}
1184 if (!nostring
&& i
> 5) TRACE_(snoop
)(" %s",debugstr_wn((LPWSTR
)x
,i
));
1193 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Entry( void **stack
)
1196 SNOOP_FUN
*fun
= (SNOOP_FUN
*)((char *)stack
[0] - 5);
1197 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1198 SNOOP_RETURNENTRY
*ret
;
1201 for (dll
= firstdll
; dll
; dll
= dll
->next
)
1202 if (fun
>= dll
->funs
&& fun
< dll
->funs
+ dll
->nrofordinals
) break;
1205 FIXME("entrypoint %p not found\n", fun
);
1208 /* guess cdecl ... */
1209 if (fun
->nrofargs
<0) {
1210 /* Typical cdecl return frame is:
1212 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1213 * (after that 81 C2 xx xx xx xx)
1215 LPBYTE reteip
= stack
[1];
1218 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1219 fun
->nrofargs
=reteip
[2]/4;
1225 for (i
=0;i
<ARRAY_SIZE( (*rets
)->entry
);i
++)
1226 if (!(*rets
)->entry
[i
].origreturn
)
1228 if (i
!=ARRAY_SIZE( (*rets
)->entry
))
1230 rets
= &((*rets
)->next
);
1236 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1237 MEM_COMMIT
| MEM_RESERVE
,
1238 PAGE_EXECUTE_READWRITE
);
1241 i
= 0; /* entry 0 is free */
1243 ret
= &((*rets
)->entry
[i
]);
1245 ret
->snoopret
= (char *)SNOOP_Return
- (char *)(&ret
->snoopret
+ 1);
1246 ret
->origreturn
= stack
[1];
1247 stack
[1] = &ret
->lcall
;
1250 ret
->ordinal
= fun
- dll
->funs
;
1251 ret
->origESP
= stack
;
1253 stack
[0] = fun
->origfun
;
1255 if (!TRACE_ON(snoop
)) return;
1257 if (fun
->name
) TRACE_(snoop
)("\1CALL %s.%s(", dll
->name
, fun
->name
);
1258 else TRACE_(snoop
)("\1CALL %s.%d(", dll
->name
, dll
->ordbase
+ret
->ordinal
);
1259 if (fun
->nrofargs
>0) {
1260 max
= fun
->nrofargs
; if (max
>16) max
=16;
1263 SNOOP_PrintArg( (DWORD
)stack
[i
+ 2] );
1264 if (i
<fun
->nrofargs
-1) TRACE_(snoop
)(",");
1266 if (max
!=fun
->nrofargs
)
1267 TRACE_(snoop
)(" ...");
1268 } else if (fun
->nrofargs
<0) {
1269 TRACE_(snoop
)("<unknown, check return>");
1270 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1271 0,16*sizeof(DWORD
));
1272 memcpy(ret
->args
, stack
+ 2, sizeof(DWORD
)*16);
1274 TRACE_(snoop
)(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1277 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Return( void **stack
)
1279 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)((char *)stack
[0] - 5);
1280 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1281 DWORD retval
= (DWORD
)stack
[-1]; /* get saved %eax from the stack */
1283 /* We haven't found out the nrofargs yet. If we called a cdecl
1284 * function it is too late anyway and we can just set '0' (which
1285 * will be the difference between orig and current ESP
1286 * If stdcall -> everything ok.
1288 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1289 ret
->dll
->funs
[ret
->ordinal
].nrofargs
= stack
- ret
->origESP
- 1;
1290 stack
[0] = ret
->origreturn
;
1292 if (!TRACE_ON(snoop
)) {
1293 ret
->origreturn
= NULL
; /* mark as empty */
1301 TRACE_(snoop
)("\1RET %s.%s(", ret
->dll
->name
, fun
->name
);
1303 TRACE_(snoop
)("\1RET %s.%d(", ret
->dll
->name
, ret
->dll
->ordbase
+ret
->ordinal
);
1305 max
= fun
->nrofargs
;
1310 SNOOP_PrintArg(ret
->args
[i
]);
1311 if (i
<max
-1) TRACE_(snoop
)(",");
1313 TRACE_(snoop
)(") retval=%08x ret=%08x\n", retval
, (DWORD
)ret
->origreturn
);
1314 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1320 TRACE_(snoop
)("\1RET %s.%s() retval=%08x ret=%08x\n",
1321 ret
->dll
->name
, fun
->name
, retval
, (DWORD
)ret
->origreturn
);
1323 TRACE_(snoop
)("\1RET %s.%d() retval=%08x ret=%08x\n",
1324 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1325 retval
, (DWORD
)ret
->origreturn
);
1327 ret
->origreturn
= NULL
; /* mark as empty */
1330 /* small wrappers that save registers and get the stack pointer */
1331 #define SNOOP_WRAPPER(name) \
1332 __ASM_STDCALL_FUNC( name, 0, \
1334 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1336 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1338 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1339 "leal 12(%esp),%eax\n\t" \
1341 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1342 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1343 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1345 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1347 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1349 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1352 SNOOP_WRAPPER( SNOOP_Entry
)
1353 SNOOP_WRAPPER( SNOOP_Return
)
1355 #else /* __i386__ */
1357 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1358 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1363 void SNOOP_SetupDLL( HMODULE hmod
)
1365 FIXME("snooping works only on i386 for now.\n");
1368 #endif /* __i386__ */