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
;
170 static const WCHAR configW
[] = {'S','o','f','t','w','a','r','e','\\',
171 'W','i','n','e','\\',
172 'D','e','b','u','g',0};
173 static const WCHAR RelayIncludeW
[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
174 static const WCHAR RelayExcludeW
[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
175 static const WCHAR SnoopIncludeW
[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
176 static const WCHAR SnoopExcludeW
[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
177 static const WCHAR RelayFromIncludeW
[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
178 static const WCHAR RelayFromExcludeW
[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
179 static const WCHAR SnoopFromIncludeW
[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
180 static const WCHAR SnoopFromExcludeW
[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
182 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
183 attr
.Length
= sizeof(attr
);
184 attr
.RootDirectory
= root
;
185 attr
.ObjectName
= &name
;
187 attr
.SecurityDescriptor
= NULL
;
188 attr
.SecurityQualityOfService
= NULL
;
189 RtlInitUnicodeString( &name
, configW
);
191 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
192 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
194 if (!hkey
) return TRUE
;
196 debug_relay_includelist
= load_list( hkey
, RelayIncludeW
);
197 debug_relay_excludelist
= load_list( hkey
, RelayExcludeW
);
198 debug_snoop_includelist
= load_list( hkey
, SnoopIncludeW
);
199 debug_snoop_excludelist
= load_list( hkey
, SnoopExcludeW
);
200 debug_from_relay_includelist
= load_list( hkey
, RelayFromIncludeW
);
201 debug_from_relay_excludelist
= load_list( hkey
, RelayFromExcludeW
);
202 debug_from_snoop_includelist
= load_list( hkey
, SnoopFromIncludeW
);
203 debug_from_snoop_excludelist
= load_list( hkey
, SnoopFromExcludeW
);
210 /***********************************************************************
213 * Check if a given module and function is in the list.
215 static BOOL
check_list( const WCHAR
*module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
219 sprintf( ord_str
, "%d", ordinal
);
222 const WCHAR
*p
= wcsrchr( *list
, '.' );
223 if (p
&& p
> *list
) /* check module and function */
226 if (wcsnicmp( module
, *list
, len
- 1 ) || module
[len
]) continue;
227 if (p
[1] == '*' && !p
[2]) return TRUE
;
228 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
229 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
231 else /* function only */
233 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
240 /***********************************************************************
241 * check_relay_include
243 * Check if a given function must be included in the relay output.
245 static BOOL
check_relay_include( const WCHAR
*module
, int ordinal
, const char *func
)
247 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
249 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
254 /***********************************************************************
257 * Check if calls from a given module must be included in the relay/snoop output,
258 * given the exclusion and inclusion lists.
260 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
262 static const WCHAR dllW
[] = {'.','d','l','l',0 };
263 const WCHAR
**listitem
;
266 if (!module
) return TRUE
;
267 if (!includelist
&& !excludelist
) return TRUE
;
271 listitem
= excludelist
;
276 listitem
= includelist
;
278 for(; *listitem
; listitem
++)
282 if (!wcsicmp( *listitem
, module
)) return !show
;
283 len
= wcslen( *listitem
);
284 if (!wcsnicmp( *listitem
, module
, len
) && !wcsicmp( module
+ len
, dllW
))
291 static BOOL
is_ret_val( char type
)
293 return type
>= 'A' && type
<= 'Z';
296 static const char *func_name( struct relay_private_data
*data
, unsigned int ordinal
)
298 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
300 if (entry_point
->name
)
301 return wine_dbg_sprintf( "%s.%s", data
->dllname
, entry_point
->name
);
303 return wine_dbg_sprintf( "%s.%u", data
->dllname
, data
->base
+ ordinal
);
306 static void trace_string_a( INT_PTR ptr
)
308 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_a( (char *)ptr
));
309 else TRACE( "%08Ix", ptr
);
312 static void trace_string_w( INT_PTR ptr
)
314 if (!IS_INTARG( ptr
)) TRACE( "%08Ix %s", ptr
, debugstr_w( (WCHAR
*)ptr
));
315 else TRACE( "%08Ix", ptr
);
320 /***********************************************************************
323 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
324 const DWORD
*stack
, unsigned int *nb_args
)
326 WORD ordinal
= LOWORD(idx
);
327 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
328 struct relay_private_data
*data
= descr
->private;
329 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
332 TRACE( "\1Call %s(", func_name( data
, ordinal
));
334 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
336 switch (arg_types
[i
])
338 case 'j': /* int64 */
339 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
342 case 'k': /* int128 */
343 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
347 trace_string_a( stack
[pos
++] );
350 trace_string_w( stack
[pos
++] );
352 case 'f': /* float */
353 TRACE( "%g", *(const float *)&stack
[pos
++] );
355 case 'd': /* double */
356 TRACE( "%g", *(const double *)&stack
[pos
] );
361 TRACE( "%08x", stack
[pos
++] );
364 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
367 if (arg_types
[0] == 't')
369 *nb_args
|= 0x80000000; /* thiscall/fastcall */
370 if (arg_types
[1] == 't') *nb_args
|= 0x40000000; /* fastcall */
372 TRACE( ") ret=%08x\n", stack
[-1] );
373 return entry_point
->orig_func
;
376 /***********************************************************************
379 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
380 void *retaddr
, LONGLONG retval
)
382 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
384 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
386 while (!is_ret_val( *arg_types
)) arg_types
++;
387 if (*arg_types
== 'J') /* int64 return value */
388 TRACE( " retval=%08x%08x ret=%08x\n",
389 (UINT
)(retval
>> 32), (UINT
)retval
, (UINT
)retaddr
);
391 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, (UINT
)retaddr
);
394 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
);
395 __ASM_STDCALL_FUNC( relay_call
, 8,
397 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
398 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
400 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
402 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
404 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
406 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
407 /* trace the parameters */
409 "pushl %esp\n\t" /* number of args return ptr */
410 "leal 20(%ebp),%esi\n\t" /* stack */
414 "call " __ASM_STDCALL("relay_trace_entry",16) "\n\t"
415 /* copy the arguments*/
416 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
418 "leal 0(,%ecx,4),%edx\n\t"
424 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
427 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
430 /* call the entry point */
434 /* trace the return value */
435 "leal -20(%ebp),%esp\n\t"
441 "call " __ASM_STDCALL("relay_trace_exit",20) "\n\t"
442 /* restore return value and return */
443 "leal -12(%ebp),%esp\n\t"
447 __ASM_CFI(".cfi_same_value %ecx\n\t")
449 __ASM_CFI(".cfi_same_value %edi\n\t")
451 __ASM_CFI(".cfi_same_value %esi\n\t")
453 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
454 __ASM_CFI(".cfi_same_value %ebp\n\t")
457 #elif defined(__arm__)
459 /***********************************************************************
462 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
463 const DWORD
*stack
, unsigned int *nb_args
)
465 WORD ordinal
= LOWORD(idx
);
466 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
467 struct relay_private_data
*data
= descr
->private;
468 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
471 unsigned int float_pos
= 0, double_pos
= 0;
472 const union fpregs
{ float s
[16]; double d
[8]; } *fpstack
= (const union fpregs
*)stack
- 1;
475 TRACE( "\1Call %s(", func_name( data
, ordinal
));
477 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
479 switch (arg_types
[i
])
481 case 'j': /* int64 */
482 pos
= (pos
+ 1) & ~1;
483 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
486 case 'k': /* int128 */
487 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
491 trace_string_a( stack
[pos
++] );
494 trace_string_w( stack
[pos
++] );
496 case 'f': /* float */
498 if (!(float_pos
% 2)) float_pos
= max( float_pos
, double_pos
* 2 );
501 TRACE( "%g", fpstack
->s
[float_pos
++] );
505 TRACE( "%g", *(const float *)&stack
[pos
++] );
507 case 'd': /* double */
509 double_pos
= max( (float_pos
+ 1) / 2, double_pos
);
512 TRACE( "%g", fpstack
->d
[double_pos
++] );
516 pos
= (pos
+ 1) & ~1;
517 TRACE( "%g", *(const double *)&stack
[pos
] );
522 TRACE( "%08x", stack
[pos
++] );
525 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
529 if (float_pos
|| double_pos
)
532 stack
= (const DWORD
*)fpstack
; /* retaddr is below the fp regs */
536 TRACE( ") ret=%08x\n", stack
[-1] );
537 return entry_point
->orig_func
;
540 /***********************************************************************
543 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
544 DWORD retaddr
, LONGLONG retval
)
546 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
548 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
550 while (!is_ret_val( *arg_types
)) arg_types
++;
551 if (*arg_types
== 'J') /* int64 return value */
552 TRACE( " retval=%08x%08x ret=%08x\n",
553 (UINT
)(retval
>> 32), (UINT
)retval
, retaddr
);
555 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, retaddr
);
558 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const DWORD
*stack
);
559 __ASM_GLOBAL_FUNC( relay_call
,
561 "push {r4-r8,lr}\n\t"
564 "add r3, sp, #12\n\t"
567 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
568 "mov ip, r0\n\t" /* entry point */
570 "ldr r1, [sp, #12]\n\t" /* number of args */
572 "subs r3, #16\n\t" /* first 4 args are in registers */
576 "add r2, r6, #16\n\t" /* skip r0-r3 */
577 "1:\tsubs r3, r3, #4\n\t"
578 "ldr r0, [r2, r3]\n\t"
579 "str r0, [sp, r3]\n\t"
583 "tst r1, #0x80000000\n\t"
584 "ldm r6, {r0-r3}\n\t"
585 "vldmdbne r6!, {s0-s15}\n\t"
587 "ldm r6, {r0-r3}\n\t"
591 "ldr r2, [r6, #-4]\n\t" /* retaddr */
597 "str r5, [sp, #4]\n\t"
599 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
600 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
601 "vldr d0, [sp, #8]\n\t"
603 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
610 #elif defined(__aarch64__)
612 /***********************************************************************
615 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
616 const INT_PTR
*stack
, unsigned int *nb_args
)
618 WORD ordinal
= LOWORD(idx
);
619 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
620 struct relay_private_data
*data
= descr
->private;
621 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
624 TRACE( "\1Call %s(", func_name( data
, ordinal
));
626 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
628 switch (arg_types
[i
])
631 trace_string_a( stack
[i
] );
634 trace_string_w( stack
[i
] );
638 TRACE( "%08zx", stack
[i
] );
641 if (!is_ret_val( arg_types
[i
+ 1] )) TRACE( "," );
644 TRACE( ") ret=%08zx\n", stack
[-1] );
645 return entry_point
->orig_func
;
648 /***********************************************************************
651 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
652 INT_PTR retaddr
, INT_PTR retval
)
654 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
655 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
658 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
);
659 __ASM_GLOBAL_FUNC( call_entry_point
,
660 "stp x29, x30, [SP,#-16]!\n\t"
661 "stp x19, x20, [SP,#-16]!\n\t"
664 "ldp x8, x9, [x19, #-32]\n\t"
670 "ldp x0, x1, [x11], #16\n\t"
671 "add w12, w12, #2\n\t"
674 "ldp x2, x3, [x11], #16\n\t"
675 "add w12, w12, #2\n\t"
678 "ldp x4, x5, [x11], #16\n\t"
679 "add w12, w12, #2\n\t"
682 "ldp x6, x7, [x11], #16\n\t"
683 "add w12, w12, #2\n\t"
686 "sub w12, w10, #8\n\t"
687 "lsl w12, w12, #3\n\t"
688 "sub SP, SP, w12, uxtw\n\t"
689 "tbz w12, #3, 1f\n\t"
691 "1: sub w12, w12, #8\n\t"
692 "ldr x13, [x11, x12]\n\t"
693 "str x13, [SP, x12]\n\t"
697 "ldp x19, x20, [SP], #16\n\t"
698 "ldp x29, x30, [SP], #16\n\t"
701 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
703 unsigned int nb_args
;
704 void *func
= relay_trace_entry( descr
, idx
, stack
, &nb_args
);
705 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
);
706 relay_trace_exit( descr
, idx
, stack
[-1], ret
);
710 #elif defined(__x86_64__)
712 /***********************************************************************
715 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
716 const INT_PTR
*stack
, unsigned int *nb_args
)
718 WORD ordinal
= LOWORD(idx
);
719 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
720 struct relay_private_data
*data
= descr
->private;
721 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
724 TRACE( "\1Call %s(", func_name( data
, ordinal
));
726 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
728 switch (arg_types
[i
])
731 trace_string_a( stack
[i
] );
734 trace_string_w( stack
[i
] );
736 case 'f': /* float */
737 TRACE( "%g", *(const float *)&stack
[i
] );
739 case 'd': /* double */
740 TRACE( "%g", *(const double *)&stack
[i
] );
744 TRACE( "%08zx", stack
[i
] );
747 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
750 TRACE( ") ret=%08zx\n", stack
[-1] );
751 return entry_point
->orig_func
;
754 /***********************************************************************
757 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
758 INT_PTR retaddr
, INT_PTR retval
)
760 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
761 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
764 extern INT_PTR WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
765 __ASM_GLOBAL_FUNC( relay_call
,
767 __ASM_SEH(".seh_pushreg %rbp\n\t")
768 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
769 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
771 __ASM_SEH(".seh_setframe %rbp,0\n\t")
772 __ASM_SEH(".seh_endprologue\n\t")
773 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
774 "leaq -0x48(%rbp),%rsp\n\t"
776 "movq %rcx,-32(%rbp)\n\t"
777 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
778 "movq %rdx,-24(%rbp)\n\t"
779 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
780 "movq %rsi,-16(%rbp)\n\t"
781 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
782 "movq %rdi,-8(%rbp)\n\t"
783 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
784 /* trace the parameters */
785 "leaq 24(%rbp),%r8\n\t" /* stack */
786 "leaq -40(%rbp),%r9\n\t"
787 "call " __ASM_NAME("relay_trace_entry") "\n\t"
788 /* copy the arguments */
789 "movl -40(%rbp),%edx\n\t" /* number of args */
792 "cmovgq %rdx,%rcx\n\t"
793 "leaq -16(,%rcx,8),%rdx\n\t"
796 "leaq 24(%rbp),%rsi\n\t" /* original stack */
799 /* call the entry point */
800 "movq 0(%rsp),%rcx\n\t"
801 "movq 8(%rsp),%rdx\n\t"
802 "movq 16(%rsp),%r8\n\t"
803 "movq 24(%rsp),%r9\n\t"
804 "movq 0(%rsp),%xmm0\n\t"
805 "movq 8(%rsp),%xmm1\n\t"
806 "movq 16(%rsp),%xmm2\n\t"
807 "movq 24(%rsp),%xmm3\n\t"
809 /* trace the return value */
810 "movq -32(%rbp),%rcx\n\t"
811 "movq -24(%rbp),%rdx\n\t"
812 "movq 16(%rbp),%r8\n\t" /* retaddr */
814 "movaps %xmm0,32(%rsp)\n\t"
816 "call " __ASM_NAME("relay_trace_exit") "\n\t"
817 /* restore return value and return */
819 "movaps 32(%rsp),%xmm0\n\t"
820 "movq -16(%rbp),%rsi\n\t"
821 __ASM_CFI(".cfi_same_value %rsi\n\t")
822 "movq -8(%rbp),%rdi\n\t"
823 __ASM_CFI(".cfi_same_value %rdi\n\t")
824 "leaq 0(%rbp),%rsp\n\t"
825 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
827 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
828 __ASM_CFI(".cfi_same_value %rbp\n\t")
832 #error Not supported on this CPU
836 static struct relay_descr
*get_relay_descr( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
839 struct relay_descr
*descr
;
840 struct relay_descr_rva
*rva
;
841 ULONG_PTR ptr
= (ULONG_PTR
)module
+ exports
->Name
;
844 if (ptr
<= (ULONG_PTR
)(exports
+ 1)) return NULL
;
845 if (ptr
> (ULONG_PTR
)exports
+ exp_size
) return NULL
;
846 if (ptr
% sizeof(DWORD
)) return NULL
;
848 rva
= (struct relay_descr_rva
*)ptr
- 1;
849 if (rva
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
850 if (rva
->descr
) descr
= (struct relay_descr
*)((char *)module
+ rva
->descr
);
851 else descr
= (struct relay_descr
*)((const char *)exports
+ exp_size
);
852 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
856 /***********************************************************************
857 * RELAY_GetProcAddress
859 * Return the proc address to use for a given function.
861 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
862 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
864 struct relay_private_data
*data
;
865 const struct relay_descr
*descr
= get_relay_descr( module
, exports
, exp_size
);
867 if (!descr
|| !(data
= descr
->private)) return proc
; /* no relay data */
868 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
869 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
870 return proc
; /* we want to relay it */
871 return data
->entry_points
[ordinal
].orig_func
;
875 /***********************************************************************
878 * Setup relay debugging for a built-in dll.
880 void RELAY_SetupDLL( HMODULE module
)
882 IMAGE_EXPORT_DIRECTORY
*exports
;
885 DWORD size
, entry_point_rva
, old_prot
;
886 struct relay_descr
*descr
;
887 struct relay_private_data
*data
;
888 WCHAR dllnameW
[sizeof(data
->dllname
)];
893 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
895 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
896 if (!exports
) return;
898 if (!(descr
= get_relay_descr( module
, exports
, size
))) return;
900 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
901 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
904 descr
->relay_call
= relay_call
;
905 descr
->private = data
;
907 data
->module
= module
;
908 data
->base
= exports
->Base
;
909 len
= strlen( (char *)module
+ exports
->Name
);
910 if (len
> 4 && !_stricmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
911 len
= min( len
, sizeof(data
->dllname
) - 1 );
912 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
913 data
->dllname
[len
] = 0;
914 ascii_to_unicode( dllnameW
, data
->dllname
, len
+ 1 );
916 /* fetch name pointer for all entry points and store them in the private structure */
918 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
919 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
921 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
922 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
925 /* patch the functions in the export table to point to the relay thunks */
927 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
928 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
931 func_size
= exports
->NumberOfFunctions
* sizeof(*funcs
);
932 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, PAGE_READWRITE
, &old_prot
);
933 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
935 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
936 if (!check_relay_include( dllnameW
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
937 continue; /* don't include this entry point */
939 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
940 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
942 if (old_prot
!= PAGE_READWRITE
)
943 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, old_prot
, &old_prot
);
946 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
948 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
949 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
954 void RELAY_SetupDLL( HMODULE module
)
958 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
961 /***********************************************************************/
963 /***********************************************************************/
967 WINE_DECLARE_DEBUG_CHANNEL(seh
);
968 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
970 #include "pshpack1.h"
975 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
976 DWORD snoopentry
; /* SNOOP_Entry relative */
983 typedef struct tagSNOOP_DLL
{
988 struct tagSNOOP_DLL
*next
;
995 BYTE lcall
; /* 0xe8 call snoopret relative*/
996 DWORD snoopret
; /* SNOOP_Ret relative */
1002 DWORD
*args
; /* saved args across a stdcall */
1003 } SNOOP_RETURNENTRY
;
1005 typedef struct tagSNOOP_RETURNENTRIES
{
1006 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
1007 struct tagSNOOP_RETURNENTRIES
*next
;
1008 } SNOOP_RETURNENTRIES
;
1010 #include "poppack.h"
1012 extern void WINAPI
SNOOP_Entry(void);
1013 extern void WINAPI
SNOOP_Return(void);
1015 static SNOOP_DLL
*firstdll
;
1016 static SNOOP_RETURNENTRIES
*firstrets
;
1019 /***********************************************************************
1020 * SNOOP_ShowDebugmsgSnoop
1022 * Simple function to decide if a particular debugging message is
1025 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
1028 int len
= strlen(module
);
1030 if (len
>= ARRAY_SIZE( moduleW
)) return FALSE
;
1031 ascii_to_unicode( moduleW
, module
, len
+ 1 );
1032 if (debug_snoop_excludelist
&& check_list( moduleW
, ordinal
, func
, debug_snoop_excludelist
))
1034 if (debug_snoop_includelist
&& !check_list( moduleW
, ordinal
, func
, debug_snoop_includelist
))
1040 /***********************************************************************
1043 * Setup snoop debugging for a native dll.
1045 void SNOOP_SetupDLL(HMODULE hmod
)
1047 SNOOP_DLL
**dll
= &firstdll
;
1052 IMAGE_EXPORT_DIRECTORY
*exports
;
1054 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
1056 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
1057 if (!exports
|| !exports
->NumberOfFunctions
) return;
1058 name
= (char *)hmod
+ exports
->Name
;
1061 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
1064 if ((*dll
)->hmod
== hmod
)
1066 /* another dll, loaded at the same address */
1067 addr
= (*dll
)->funs
;
1068 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
1069 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1072 dll
= &((*dll
)->next
);
1075 *dll
= RtlReAllocateHeap(GetProcessHeap(),
1076 HEAP_ZERO_MEMORY
, *dll
,
1077 sizeof(SNOOP_DLL
) + strlen(name
));
1079 *dll
= RtlAllocateHeap(GetProcessHeap(),
1081 sizeof(SNOOP_DLL
) + strlen(name
));
1082 (*dll
)->hmod
= hmod
;
1083 (*dll
)->ordbase
= exports
->Base
;
1084 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
1085 strcpy( (*dll
)->name
, name
);
1086 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
1087 if (p
> (*dll
)->name
&& !_stricmp( p
, ".dll" )) *p
= 0;
1089 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
1091 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1092 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
1094 RtlFreeHeap(GetProcessHeap(),0,*dll
);
1095 FIXME("out of memory\n");
1098 (*dll
)->funs
= addr
;
1099 memset((*dll
)->funs
,0,size
);
1103 /***********************************************************************
1104 * SNOOP_GetProcAddress
1106 * Return the proc address to use for a given function.
1108 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
1109 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
1114 const WORD
*ordinals
;
1116 SNOOP_DLL
*dll
= firstdll
;
1118 const IMAGE_SECTION_HEADER
*sec
;
1120 if (!TRACE_ON(snoop
)) return origfun
;
1121 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
1122 return origfun
; /* the calling module was explicitly excluded */
1124 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
1127 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
1129 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
1130 return origfun
; /* most likely a data reference */
1133 if (hmod
== dll
->hmod
)
1137 if (!dll
) /* probably internal */
1140 /* try to find a name for it */
1142 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
1143 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
1144 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
1146 if (ordinals
[i
] == ordinal
)
1148 ename
= (const char *)hmod
+ names
[i
];
1152 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
1154 assert(ordinal
< dll
->nrofordinals
);
1155 fun
= dll
->funs
+ ordinal
;
1160 fun
->snoopentry
= (char *)SNOOP_Entry
- (char *)(&fun
->snoopentry
+ 1);
1161 fun
->origfun
= origfun
;
1164 return (FARPROC
)&(fun
->lcall
);
1167 static void SNOOP_PrintArg(DWORD x
)
1171 TRACE_(snoop
)("%08x",x
);
1172 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
1179 if (s
[i
]<0x20) {nostring
=1;break;}
1180 if (s
[i
]>=0x80) {nostring
=1;break;}
1183 if (!nostring
&& i
> 5)
1184 TRACE_(snoop
)(" %s",debugstr_an((LPSTR
)x
,i
));
1185 else /* try unicode */
1191 if (s
[i
]<0x20) {nostring
=1;break;}
1192 if (s
[i
]>0x100) {nostring
=1;break;}
1195 if (!nostring
&& i
> 5) TRACE_(snoop
)(" %s",debugstr_wn((LPWSTR
)x
,i
));
1204 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Entry( void **stack
)
1207 SNOOP_FUN
*fun
= (SNOOP_FUN
*)((char *)stack
[0] - 5);
1208 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1209 SNOOP_RETURNENTRY
*ret
;
1212 for (dll
= firstdll
; dll
; dll
= dll
->next
)
1213 if (fun
>= dll
->funs
&& fun
< dll
->funs
+ dll
->nrofordinals
) break;
1216 FIXME("entrypoint %p not found\n", fun
);
1219 /* guess cdecl ... */
1220 if (fun
->nrofargs
<0) {
1221 /* Typical cdecl return frame is:
1223 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1224 * (after that 81 C2 xx xx xx xx)
1226 LPBYTE reteip
= stack
[1];
1229 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1230 fun
->nrofargs
=reteip
[2]/4;
1236 for (i
=0;i
<ARRAY_SIZE( (*rets
)->entry
);i
++)
1237 if (!(*rets
)->entry
[i
].origreturn
)
1239 if (i
!=ARRAY_SIZE( (*rets
)->entry
))
1241 rets
= &((*rets
)->next
);
1247 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1248 MEM_COMMIT
| MEM_RESERVE
,
1249 PAGE_EXECUTE_READWRITE
);
1252 i
= 0; /* entry 0 is free */
1254 ret
= &((*rets
)->entry
[i
]);
1256 ret
->snoopret
= (char *)SNOOP_Return
- (char *)(&ret
->snoopret
+ 1);
1257 ret
->origreturn
= stack
[1];
1258 stack
[1] = &ret
->lcall
;
1261 ret
->ordinal
= fun
- dll
->funs
;
1262 ret
->origESP
= stack
;
1264 stack
[0] = fun
->origfun
;
1266 if (!TRACE_ON(snoop
)) return;
1268 if (fun
->name
) TRACE_(snoop
)("\1CALL %s.%s(", dll
->name
, fun
->name
);
1269 else TRACE_(snoop
)("\1CALL %s.%d(", dll
->name
, dll
->ordbase
+ret
->ordinal
);
1270 if (fun
->nrofargs
>0) {
1271 max
= fun
->nrofargs
; if (max
>16) max
=16;
1274 SNOOP_PrintArg( (DWORD
)stack
[i
+ 2] );
1275 if (i
<fun
->nrofargs
-1) TRACE_(snoop
)(",");
1277 if (max
!=fun
->nrofargs
)
1278 TRACE_(snoop
)(" ...");
1279 } else if (fun
->nrofargs
<0) {
1280 TRACE_(snoop
)("<unknown, check return>");
1281 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1282 0,16*sizeof(DWORD
));
1283 memcpy(ret
->args
, stack
+ 2, sizeof(DWORD
)*16);
1285 TRACE_(snoop
)(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1288 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Return( void **stack
)
1290 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)((char *)stack
[0] - 5);
1291 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1292 DWORD retval
= (DWORD
)stack
[-1]; /* get saved %eax from the stack */
1294 /* We haven't found out the nrofargs yet. If we called a cdecl
1295 * function it is too late anyway and we can just set '0' (which
1296 * will be the difference between orig and current ESP
1297 * If stdcall -> everything ok.
1299 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1300 ret
->dll
->funs
[ret
->ordinal
].nrofargs
= stack
- ret
->origESP
- 1;
1301 stack
[0] = ret
->origreturn
;
1303 if (!TRACE_ON(snoop
)) {
1304 ret
->origreturn
= NULL
; /* mark as empty */
1312 TRACE_(snoop
)("\1RET %s.%s(", ret
->dll
->name
, fun
->name
);
1314 TRACE_(snoop
)("\1RET %s.%d(", ret
->dll
->name
, ret
->dll
->ordbase
+ret
->ordinal
);
1316 max
= fun
->nrofargs
;
1321 SNOOP_PrintArg(ret
->args
[i
]);
1322 if (i
<max
-1) TRACE_(snoop
)(",");
1324 TRACE_(snoop
)(") retval=%08x ret=%08x\n", retval
, (DWORD
)ret
->origreturn
);
1325 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1331 TRACE_(snoop
)("\1RET %s.%s() retval=%08x ret=%08x\n",
1332 ret
->dll
->name
, fun
->name
, retval
, (DWORD
)ret
->origreturn
);
1334 TRACE_(snoop
)("\1RET %s.%d() retval=%08x ret=%08x\n",
1335 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1336 retval
, (DWORD
)ret
->origreturn
);
1338 ret
->origreturn
= NULL
; /* mark as empty */
1341 /* small wrappers that save registers and get the stack pointer */
1342 #define SNOOP_WRAPPER(name) \
1343 __ASM_STDCALL_FUNC( name, 0, \
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") \
1350 "leal 12(%esp),%eax\n\t" \
1352 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1353 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1354 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1356 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1358 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1360 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1363 SNOOP_WRAPPER( SNOOP_Entry
)
1364 SNOOP_WRAPPER( SNOOP_Return
)
1366 #else /* __i386__ */
1368 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1369 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1374 void SNOOP_SetupDLL( HMODULE hmod
)
1376 FIXME("snooping works only on i386 for now.\n");
1379 #endif /* __i386__ */