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
;
168 UNICODE_STRING name
= RTL_CONSTANT_STRING( L
"Software\\Wine\\Debug" );
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
;
179 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
180 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
182 if (!hkey
) return TRUE
;
184 debug_relay_includelist
= load_list( hkey
, L
"RelayInclude" );
185 debug_relay_excludelist
= load_list( hkey
, L
"RelayExclude" );
186 debug_snoop_includelist
= load_list( hkey
, L
"SnoopInclude" );
187 debug_snoop_excludelist
= load_list( hkey
, L
"SnoopExclude" );
188 debug_from_relay_includelist
= load_list( hkey
, L
"RelayFromInclude" );
189 debug_from_relay_excludelist
= load_list( hkey
, L
"RelayFromExclude" );
190 debug_from_snoop_includelist
= load_list( hkey
, L
"SnoopFromInclude" );
191 debug_from_snoop_excludelist
= load_list( hkey
, L
"SnoopFromExclude" );
198 /***********************************************************************
201 * Check if a given module and function is in the list.
203 static BOOL
check_list( const WCHAR
*module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
207 sprintf( ord_str
, "%d", ordinal
);
210 const WCHAR
*p
= wcsrchr( *list
, '.' );
211 if (p
&& p
> *list
) /* check module and function */
214 if (wcsnicmp( module
, *list
, len
- 1 ) || module
[len
]) continue;
215 if (p
[1] == '*' && !p
[2]) return TRUE
;
216 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
217 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
219 else /* function only */
221 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
228 /***********************************************************************
229 * check_relay_include
231 * Check if a given function must be included in the relay output.
233 static BOOL
check_relay_include( const WCHAR
*module
, int ordinal
, const char *func
)
235 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
237 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
242 /***********************************************************************
245 * Check if calls from a given module must be included in the relay/snoop output,
246 * given the exclusion and inclusion lists.
248 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
250 const WCHAR
**listitem
;
253 if (!module
) return TRUE
;
254 if (!includelist
&& !excludelist
) return TRUE
;
258 listitem
= excludelist
;
263 listitem
= includelist
;
265 for(; *listitem
; listitem
++)
269 if (!wcsicmp( *listitem
, module
)) return !show
;
270 len
= wcslen( *listitem
);
271 if (!wcsnicmp( *listitem
, module
, len
) && !wcsicmp( module
+ len
, L
".dll" ))
278 static BOOL
is_ret_val( char type
)
280 return type
>= 'A' && type
<= 'Z';
283 static const char *func_name( struct relay_private_data
*data
, unsigned int ordinal
)
285 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
287 if (entry_point
->name
)
288 return wine_dbg_sprintf( "%s.%s", data
->dllname
, entry_point
->name
);
290 return wine_dbg_sprintf( "%s.%u", data
->dllname
, data
->base
+ ordinal
);
293 static void trace_string_a( INT_PTR ptr
)
295 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_a( (char *)ptr
));
296 else TRACE( "%08Ix", ptr
);
299 static void trace_string_w( INT_PTR ptr
)
301 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_w( (WCHAR
*)ptr
));
302 else TRACE( "%08Ix", ptr
);
307 /***********************************************************************
310 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
311 const DWORD
*stack
, unsigned int *nb_args
)
313 WORD ordinal
= LOWORD(idx
);
314 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
315 struct relay_private_data
*data
= descr
->private;
316 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
319 TRACE( "\1Call %s(", func_name( data
, ordinal
));
321 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
323 switch (arg_types
[i
])
325 case 'j': /* int64 */
326 TRACE( "%lx%08lx", stack
[pos
+1], stack
[pos
] );
329 case 'k': /* int128 */
330 TRACE( "{%08lx,%08lx,%08lx,%08lx}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
334 trace_string_a( stack
[pos
++] );
337 trace_string_w( stack
[pos
++] );
339 case 'f': /* float */
340 TRACE( "%g", *(const float *)&stack
[pos
++] );
342 case 'd': /* double */
343 TRACE( "%g", *(const double *)&stack
[pos
] );
348 TRACE( "%08lx", stack
[pos
++] );
351 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
354 if (arg_types
[0] == 't')
356 *nb_args
|= 0x80000000; /* thiscall/fastcall */
357 if (arg_types
[1] == 't') *nb_args
|= 0x40000000; /* fastcall */
359 TRACE( ") ret=%08lx\n", stack
[-1] );
360 return entry_point
->orig_func
;
363 /***********************************************************************
366 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
367 void *retaddr
, LONGLONG retval
)
369 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
371 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
373 while (!is_ret_val( *arg_types
)) arg_types
++;
374 if (*arg_types
== 'J') /* int64 return value */
375 TRACE( " retval=%08x%08x ret=%08x\n",
376 (UINT
)(retval
>> 32), (UINT
)retval
, (UINT
)retaddr
);
378 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, (UINT
)retaddr
);
381 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
);
382 __ASM_STDCALL_FUNC( relay_call
, 8,
384 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
385 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
387 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
389 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
391 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
393 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
394 /* trace the parameters */
396 "pushl %esp\n\t" /* number of args return ptr */
397 "leal 20(%ebp),%esi\n\t" /* stack */
401 "call " __ASM_STDCALL("relay_trace_entry",16) "\n\t"
402 /* copy the arguments*/
403 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
405 "leal 0(,%ecx,4),%edx\n\t"
411 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
414 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
417 /* call the entry point */
421 /* trace the return value */
422 "leal -20(%ebp),%esp\n\t"
428 "call " __ASM_STDCALL("relay_trace_exit",20) "\n\t"
429 /* restore return value and return */
430 "leal -12(%ebp),%esp\n\t"
434 __ASM_CFI(".cfi_same_value %ecx\n\t")
436 __ASM_CFI(".cfi_same_value %edi\n\t")
438 __ASM_CFI(".cfi_same_value %esi\n\t")
440 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
441 __ASM_CFI(".cfi_same_value %ebp\n\t")
444 #elif defined(__arm__)
446 /***********************************************************************
449 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
450 const DWORD
*stack
, unsigned int *nb_args
)
452 WORD ordinal
= LOWORD(idx
);
453 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
454 struct relay_private_data
*data
= descr
->private;
455 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
458 unsigned int float_pos
= 0, double_pos
= 0;
459 const union fpregs
{ float s
[16]; double d
[8]; } *fpstack
= (const union fpregs
*)stack
- 1;
462 TRACE( "\1Call %s(", func_name( data
, ordinal
));
464 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
466 switch (arg_types
[i
])
468 case 'j': /* int64 */
469 pos
= (pos
+ 1) & ~1;
470 TRACE( "%lx%08lx", stack
[pos
+1], stack
[pos
] );
473 case 'k': /* int128 */
474 TRACE( "{%08lx,%08lx,%08lx,%08lx}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
478 trace_string_a( stack
[pos
++] );
481 trace_string_w( stack
[pos
++] );
483 case 'f': /* float */
485 if (!(float_pos
% 2)) float_pos
= max( float_pos
, double_pos
* 2 );
488 TRACE( "%g", fpstack
->s
[float_pos
++] );
492 TRACE( "%g", *(const float *)&stack
[pos
++] );
494 case 'd': /* double */
496 double_pos
= max( (float_pos
+ 1) / 2, double_pos
);
499 TRACE( "%g", fpstack
->d
[double_pos
++] );
503 pos
= (pos
+ 1) & ~1;
504 TRACE( "%g", *(const double *)&stack
[pos
] );
509 TRACE( "%08lx", stack
[pos
++] );
512 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
516 if (float_pos
|| double_pos
)
519 stack
= (const DWORD
*)fpstack
; /* retaddr is below the fp regs */
523 TRACE( ") ret=%08lx\n", stack
[-1] );
524 return entry_point
->orig_func
;
527 /***********************************************************************
530 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
531 DWORD retaddr
, LONGLONG retval
)
533 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
535 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
537 while (!is_ret_val( *arg_types
)) arg_types
++;
538 if (*arg_types
== 'J') /* int64 return value */
539 TRACE( " retval=%08x%08x ret=%08lx\n",
540 (UINT
)(retval
>> 32), (UINT
)retval
, retaddr
);
542 TRACE( " retval=%08x ret=%08lx\n", (UINT
)retval
, retaddr
);
545 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const DWORD
*stack
);
546 __ASM_GLOBAL_FUNC( relay_call
,
547 "push {r4-r8,lr}\n\t"
550 "add r3, sp, #12\n\t"
553 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
554 "mov ip, r0\n\t" /* entry point */
556 "ldr r1, [sp, #12]\n\t" /* number of args */
558 "subs r3, #16\n\t" /* first 4 args are in registers */
563 "add r2, r6, #16\n\t" /* skip r0-r3 */
564 "1:\tsubs r3, r3, #4\n\t"
565 "ldr r0, [r2, r3]\n\t"
566 "str r0, [sp, r3]\n\t"
570 "tst r1, #0x80000000\n\t"
571 "ldm r6, {r0-r3}\n\t"
573 "vldmdbne r6!, {s0-s15}\n\t"
575 "ldm r6, {r0-r3}\n\t"
579 "ldr r2, [r6, #-4]\n\t" /* retaddr */
585 "str r5, [sp, #4]\n\t"
587 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
588 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
589 "vldr d0, [sp, #8]\n\t"
591 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
598 #elif defined(__aarch64__)
600 /***********************************************************************
603 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
604 const INT_PTR
*stack
, unsigned int *nb_args
)
606 WORD ordinal
= LOWORD(idx
);
607 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
608 struct relay_private_data
*data
= descr
->private;
609 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
612 TRACE( "\1Call %s(", func_name( data
, ordinal
));
614 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
616 switch (arg_types
[i
])
619 trace_string_a( stack
[i
] );
622 trace_string_w( stack
[i
] );
626 TRACE( "%08Ix", stack
[i
] );
629 if (!is_ret_val( arg_types
[i
+ 1] )) TRACE( "," );
632 TRACE( ") ret=%08Ix\n", stack
[-1] );
633 return entry_point
->orig_func
;
636 /***********************************************************************
639 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
640 INT_PTR retaddr
, INT_PTR retval
)
642 TRACE( "\1Ret %s() retval=%08Ix ret=%08Ix\n",
643 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
646 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
);
647 __ASM_GLOBAL_FUNC( call_entry_point
,
648 "stp x29, x30, [SP,#-16]!\n\t"
649 "stp x19, x20, [SP,#-16]!\n\t"
652 "ldp x8, x9, [x19, #-32]\n\t"
658 "ldp x0, x1, [x11], #16\n\t"
659 "add w12, w12, #2\n\t"
662 "ldp x2, x3, [x11], #16\n\t"
663 "add w12, w12, #2\n\t"
666 "ldp x4, x5, [x11], #16\n\t"
667 "add w12, w12, #2\n\t"
670 "ldp x6, x7, [x11], #16\n\t"
671 "add w12, w12, #2\n\t"
674 "sub w12, w10, #8\n\t"
675 "lsl w12, w12, #3\n\t"
676 "sub SP, SP, w12, uxtw\n\t"
677 "tbz w12, #3, 1f\n\t"
679 "1: sub w12, w12, #8\n\t"
680 "ldr x13, [x11, x12]\n\t"
681 "str x13, [SP, x12]\n\t"
685 "ldp x19, x20, [SP], #16\n\t"
686 "ldp x29, x30, [SP], #16\n\t"
689 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
691 unsigned int nb_args
;
692 void *func
= relay_trace_entry( descr
, idx
, stack
, &nb_args
);
693 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
);
694 relay_trace_exit( descr
, idx
, stack
[-1], ret
);
698 #elif defined(__x86_64__)
700 /***********************************************************************
703 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
704 const INT_PTR
*stack
, unsigned int *nb_args
)
706 WORD ordinal
= LOWORD(idx
);
707 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
708 struct relay_private_data
*data
= descr
->private;
709 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
712 TRACE( "\1Call %s(", func_name( data
, ordinal
));
714 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
716 switch (arg_types
[i
])
719 trace_string_a( stack
[i
] );
722 trace_string_w( stack
[i
] );
724 case 'f': /* float */
725 TRACE( "%g", *(const float *)&stack
[i
] );
727 case 'd': /* double */
728 TRACE( "%g", *(const double *)&stack
[i
] );
732 TRACE( "%08Ix", stack
[i
] );
735 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
738 TRACE( ") ret=%08Ix\n", stack
[-1] );
739 return entry_point
->orig_func
;
742 /***********************************************************************
745 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
746 INT_PTR retaddr
, INT_PTR retval
)
748 TRACE( "\1Ret %s() retval=%08Ix ret=%08Ix\n",
749 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
752 extern INT_PTR WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
753 __ASM_GLOBAL_FUNC( relay_call
,
755 __ASM_SEH(".seh_pushreg %rbp\n\t")
756 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
757 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
759 __ASM_SEH(".seh_setframe %rbp,0\n\t")
760 __ASM_SEH(".seh_endprologue\n\t")
761 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
762 "leaq -0x48(%rbp),%rsp\n\t"
764 "movq %rcx,-32(%rbp)\n\t"
765 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
766 "movq %rdx,-24(%rbp)\n\t"
767 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
768 "movq %rsi,-16(%rbp)\n\t"
769 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
770 "movq %rdi,-8(%rbp)\n\t"
771 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
772 /* trace the parameters */
773 "leaq 24(%rbp),%r8\n\t" /* stack */
774 "leaq -40(%rbp),%r9\n\t"
775 "call " __ASM_NAME("relay_trace_entry") "\n\t"
776 /* copy the arguments */
777 "movl -40(%rbp),%edx\n\t" /* number of args */
780 "cmovgq %rdx,%rcx\n\t"
781 "leaq -16(,%rcx,8),%rdx\n\t"
784 "leaq 24(%rbp),%rsi\n\t" /* original stack */
787 /* call the entry point */
788 "movq 0(%rsp),%rcx\n\t"
789 "movq 8(%rsp),%rdx\n\t"
790 "movq 16(%rsp),%r8\n\t"
791 "movq 24(%rsp),%r9\n\t"
792 "movq 0(%rsp),%xmm0\n\t"
793 "movq 8(%rsp),%xmm1\n\t"
794 "movq 16(%rsp),%xmm2\n\t"
795 "movq 24(%rsp),%xmm3\n\t"
797 /* trace the return value */
798 "movq -32(%rbp),%rcx\n\t"
799 "movq -24(%rbp),%rdx\n\t"
800 "movq 16(%rbp),%r8\n\t" /* retaddr */
802 "movaps %xmm0,32(%rsp)\n\t"
804 "call " __ASM_NAME("relay_trace_exit") "\n\t"
805 /* restore return value and return */
807 "movaps 32(%rsp),%xmm0\n\t"
808 "movq -16(%rbp),%rsi\n\t"
809 __ASM_CFI(".cfi_same_value %rsi\n\t")
810 "movq -8(%rbp),%rdi\n\t"
811 __ASM_CFI(".cfi_same_value %rdi\n\t")
812 "leaq 0(%rbp),%rsp\n\t"
813 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
815 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
816 __ASM_CFI(".cfi_same_value %rbp\n\t")
820 #error Not supported on this CPU
824 static struct relay_descr
*get_relay_descr( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
827 struct relay_descr
*descr
;
828 struct relay_descr_rva
*rva
;
829 ULONG_PTR ptr
= (ULONG_PTR
)module
+ exports
->Name
;
832 if (ptr
<= (ULONG_PTR
)(exports
+ 1)) return NULL
;
833 if (ptr
> (ULONG_PTR
)exports
+ exp_size
) return NULL
;
834 if (ptr
% sizeof(DWORD
)) return NULL
;
836 rva
= (struct relay_descr_rva
*)ptr
- 1;
837 if (rva
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
838 if (rva
->descr
) descr
= (struct relay_descr
*)((char *)module
+ rva
->descr
);
839 else descr
= (struct relay_descr
*)((const char *)exports
+ exp_size
);
840 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
844 /***********************************************************************
845 * RELAY_GetProcAddress
847 * Return the proc address to use for a given function.
849 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
850 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
852 struct relay_private_data
*data
;
853 const struct relay_descr
*descr
= get_relay_descr( module
, exports
, exp_size
);
855 if (!descr
|| !(data
= descr
->private)) return proc
; /* no relay data */
856 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
857 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
858 return proc
; /* we want to relay it */
859 return data
->entry_points
[ordinal
].orig_func
;
863 /***********************************************************************
866 * Setup relay debugging for a built-in dll.
868 void RELAY_SetupDLL( HMODULE module
)
870 IMAGE_EXPORT_DIRECTORY
*exports
;
873 DWORD size
, entry_point_rva
, old_prot
;
874 struct relay_descr
*descr
;
875 struct relay_private_data
*data
;
876 WCHAR dllnameW
[sizeof(data
->dllname
)];
881 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
883 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
884 if (!exports
) return;
886 if (!(descr
= get_relay_descr( module
, exports
, size
))) return;
888 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
889 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
892 descr
->relay_call
= relay_call
;
893 descr
->private = data
;
895 data
->module
= module
;
896 data
->base
= exports
->Base
;
897 len
= strlen( (char *)module
+ exports
->Name
);
898 if (len
> 4 && !_stricmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
899 len
= min( len
, sizeof(data
->dllname
) - 1 );
900 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
901 data
->dllname
[len
] = 0;
902 ascii_to_unicode( dllnameW
, data
->dllname
, len
+ 1 );
904 /* fetch name pointer for all entry points and store them in the private structure */
906 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
907 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
909 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
910 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
913 /* patch the functions in the export table to point to the relay thunks */
915 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
916 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
919 func_size
= exports
->NumberOfFunctions
* sizeof(*funcs
);
920 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, PAGE_READWRITE
, &old_prot
);
921 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
923 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
924 if (!check_relay_include( dllnameW
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
925 continue; /* don't include this entry point */
927 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
928 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
930 if (old_prot
!= PAGE_READWRITE
)
931 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, old_prot
, &old_prot
);
934 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
936 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
937 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
942 void RELAY_SetupDLL( HMODULE module
)
946 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
949 /***********************************************************************/
951 /***********************************************************************/
955 WINE_DECLARE_DEBUG_CHANNEL(seh
);
956 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
958 #include "pshpack1.h"
963 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
964 DWORD snoopentry
; /* SNOOP_Entry relative */
971 typedef struct tagSNOOP_DLL
{
976 struct tagSNOOP_DLL
*next
;
983 BYTE lcall
; /* 0xe8 call snoopret relative*/
984 DWORD snoopret
; /* SNOOP_Ret relative */
990 DWORD
*args
; /* saved args across a stdcall */
993 typedef struct tagSNOOP_RETURNENTRIES
{
994 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
995 struct tagSNOOP_RETURNENTRIES
*next
;
996 } SNOOP_RETURNENTRIES
;
1000 extern void WINAPI
SNOOP_Entry(void);
1001 extern void WINAPI
SNOOP_Return(void);
1003 static SNOOP_DLL
*firstdll
;
1004 static SNOOP_RETURNENTRIES
*firstrets
;
1007 /***********************************************************************
1008 * SNOOP_ShowDebugmsgSnoop
1010 * Simple function to decide if a particular debugging message is
1013 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
1016 int len
= strlen(module
);
1018 if (len
>= ARRAY_SIZE( moduleW
)) return FALSE
;
1019 ascii_to_unicode( moduleW
, module
, len
+ 1 );
1020 if (debug_snoop_excludelist
&& check_list( moduleW
, ordinal
, func
, debug_snoop_excludelist
))
1022 if (debug_snoop_includelist
&& !check_list( moduleW
, ordinal
, func
, debug_snoop_includelist
))
1028 /***********************************************************************
1031 * Setup snoop debugging for a native dll.
1033 void SNOOP_SetupDLL(HMODULE hmod
)
1035 SNOOP_DLL
**dll
= &firstdll
;
1040 IMAGE_EXPORT_DIRECTORY
*exports
;
1042 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
1044 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
1045 if (!exports
|| !exports
->NumberOfFunctions
) return;
1046 name
= (char *)hmod
+ exports
->Name
;
1049 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
1052 if ((*dll
)->hmod
== hmod
)
1054 /* another dll, loaded at the same address */
1055 addr
= (*dll
)->funs
;
1056 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
1057 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1060 dll
= &((*dll
)->next
);
1063 *dll
= RtlReAllocateHeap(GetProcessHeap(),
1064 HEAP_ZERO_MEMORY
, *dll
,
1065 sizeof(SNOOP_DLL
) + strlen(name
));
1067 *dll
= RtlAllocateHeap(GetProcessHeap(),
1069 sizeof(SNOOP_DLL
) + strlen(name
));
1070 (*dll
)->hmod
= hmod
;
1071 (*dll
)->ordbase
= exports
->Base
;
1072 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
1073 strcpy( (*dll
)->name
, name
);
1074 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
1075 if (p
> (*dll
)->name
&& !_stricmp( p
, ".dll" )) *p
= 0;
1077 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
1079 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1080 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
1082 RtlFreeHeap(GetProcessHeap(),0,*dll
);
1083 FIXME("out of memory\n");
1086 (*dll
)->funs
= addr
;
1087 memset((*dll
)->funs
,0,size
);
1091 /***********************************************************************
1092 * SNOOP_GetProcAddress
1094 * Return the proc address to use for a given function.
1096 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
1097 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
1102 const WORD
*ordinals
;
1104 SNOOP_DLL
*dll
= firstdll
;
1106 const IMAGE_SECTION_HEADER
*sec
;
1108 if (!TRACE_ON(snoop
)) return origfun
;
1109 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
1110 return origfun
; /* the calling module was explicitly excluded */
1112 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
1115 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
1117 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
1118 return origfun
; /* most likely a data reference */
1121 if (hmod
== dll
->hmod
)
1125 if (!dll
) /* probably internal */
1128 /* try to find a name for it */
1130 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
1131 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
1132 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
1134 if (ordinals
[i
] == ordinal
)
1136 ename
= (const char *)hmod
+ names
[i
];
1140 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
1142 assert(ordinal
< dll
->nrofordinals
);
1143 fun
= dll
->funs
+ ordinal
;
1148 fun
->snoopentry
= (char *)SNOOP_Entry
- (char *)(&fun
->snoopentry
+ 1);
1149 fun
->origfun
= origfun
;
1152 return (FARPROC
)&(fun
->lcall
);
1155 static void SNOOP_PrintArg(DWORD x
)
1159 TRACE_(snoop
)("%08lx",x
);
1160 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
1167 if (s
[i
]<0x20) {nostring
=1;break;}
1168 if (s
[i
]>=0x80) {nostring
=1;break;}
1171 if (!nostring
&& i
> 5)
1172 TRACE_(snoop
)(" %s",debugstr_an((LPSTR
)x
,i
));
1173 else /* try unicode */
1179 if (s
[i
]<0x20) {nostring
=1;break;}
1180 if (s
[i
]>0x100) {nostring
=1;break;}
1183 if (!nostring
&& i
> 5) TRACE_(snoop
)(" %s",debugstr_wn((LPWSTR
)x
,i
));
1192 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Entry( void **stack
)
1195 SNOOP_FUN
*fun
= (SNOOP_FUN
*)((char *)stack
[0] - 5);
1196 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1197 SNOOP_RETURNENTRY
*ret
;
1200 for (dll
= firstdll
; dll
; dll
= dll
->next
)
1201 if (fun
>= dll
->funs
&& fun
< dll
->funs
+ dll
->nrofordinals
) break;
1204 FIXME("entrypoint %p not found\n", fun
);
1207 /* guess cdecl ... */
1208 if (fun
->nrofargs
<0) {
1209 /* Typical cdecl return frame is:
1211 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1212 * (after that 81 C2 xx xx xx xx)
1214 LPBYTE reteip
= stack
[1];
1217 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1218 fun
->nrofargs
=reteip
[2]/4;
1224 for (i
=0;i
<ARRAY_SIZE( (*rets
)->entry
);i
++)
1225 if (!(*rets
)->entry
[i
].origreturn
)
1227 if (i
!=ARRAY_SIZE( (*rets
)->entry
))
1229 rets
= &((*rets
)->next
);
1235 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1236 MEM_COMMIT
| MEM_RESERVE
,
1237 PAGE_EXECUTE_READWRITE
);
1240 i
= 0; /* entry 0 is free */
1242 ret
= &((*rets
)->entry
[i
]);
1244 ret
->snoopret
= (char *)SNOOP_Return
- (char *)(&ret
->snoopret
+ 1);
1245 ret
->origreturn
= stack
[1];
1246 stack
[1] = &ret
->lcall
;
1249 ret
->ordinal
= fun
- dll
->funs
;
1250 ret
->origESP
= stack
;
1252 stack
[0] = fun
->origfun
;
1254 if (!TRACE_ON(snoop
)) return;
1256 if (fun
->name
) TRACE_(snoop
)("\1CALL %s.%s(", dll
->name
, fun
->name
);
1257 else TRACE_(snoop
)("\1CALL %s.%ld(", dll
->name
, dll
->ordbase
+ret
->ordinal
);
1258 if (fun
->nrofargs
>0) {
1259 max
= fun
->nrofargs
; if (max
>16) max
=16;
1262 SNOOP_PrintArg( (DWORD
)stack
[i
+ 2] );
1263 if (i
<fun
->nrofargs
-1) TRACE_(snoop
)(",");
1265 if (max
!=fun
->nrofargs
)
1266 TRACE_(snoop
)(" ...");
1267 } else if (fun
->nrofargs
<0) {
1268 TRACE_(snoop
)("<unknown, check return>");
1269 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1270 0,16*sizeof(DWORD
));
1271 memcpy(ret
->args
, stack
+ 2, sizeof(DWORD
)*16);
1273 TRACE_(snoop
)(") ret=%08lx\n",(DWORD
)ret
->origreturn
);
1276 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Return( void **stack
)
1278 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)((char *)stack
[0] - 5);
1279 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1280 DWORD retval
= (DWORD
)stack
[-1]; /* get saved %eax from the stack */
1282 /* We haven't found out the nrofargs yet. If we called a cdecl
1283 * function it is too late anyway and we can just set '0' (which
1284 * will be the difference between orig and current ESP
1285 * If stdcall -> everything ok.
1287 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1288 ret
->dll
->funs
[ret
->ordinal
].nrofargs
= stack
- ret
->origESP
- 1;
1289 stack
[0] = ret
->origreturn
;
1291 if (!TRACE_ON(snoop
)) {
1292 ret
->origreturn
= NULL
; /* mark as empty */
1300 TRACE_(snoop
)("\1RET %s.%s(", ret
->dll
->name
, fun
->name
);
1302 TRACE_(snoop
)("\1RET %s.%ld(", ret
->dll
->name
, ret
->dll
->ordbase
+ret
->ordinal
);
1304 max
= fun
->nrofargs
;
1309 SNOOP_PrintArg(ret
->args
[i
]);
1310 if (i
<max
-1) TRACE_(snoop
)(",");
1312 TRACE_(snoop
)(") retval=%08lx ret=%08lx\n", retval
, (DWORD
)ret
->origreturn
);
1313 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1319 TRACE_(snoop
)("\1RET %s.%s() retval=%08lx ret=%08lx\n",
1320 ret
->dll
->name
, fun
->name
, retval
, (DWORD
)ret
->origreturn
);
1322 TRACE_(snoop
)("\1RET %s.%ld() retval=%08lx ret=%08lx\n",
1323 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1324 retval
, (DWORD
)ret
->origreturn
);
1326 ret
->origreturn
= NULL
; /* mark as empty */
1329 /* small wrappers that save registers and get the stack pointer */
1330 #define SNOOP_WRAPPER(name) \
1331 __ASM_STDCALL_FUNC( name, 0, \
1333 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1335 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1337 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1338 "leal 12(%esp),%eax\n\t" \
1340 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1341 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1342 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1344 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1346 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1348 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1351 SNOOP_WRAPPER( SNOOP_Entry
)
1352 SNOOP_WRAPPER( SNOOP_Return
)
1354 #else /* __i386__ */
1356 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1357 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1362 void SNOOP_SetupDLL( HMODULE hmod
)
1364 FIXME("snooping works only on i386 for now.\n");
1367 #endif /* __i386__ */