2 * Win32 relay and snoop functions
4 * Copyright 1997 Alexandre Julliard
5 * Copyright 1998 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
34 #include "wine/exception.h"
35 #include "ntdll_misc.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(relay
);
41 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
43 struct relay_descr
/* descriptor for a module */
45 ULONG_PTR magic
; /* signature */
46 void *relay_call
; /* functions to call from relay thunks */
47 void *private; /* reserved for the relay code private data */
48 const char *entry_point_base
; /* base address of entry point thunks */
49 const unsigned int *entry_point_offsets
; /* offsets of entry points thunks */
50 const char *args_string
; /* string describing the arguments */
53 struct relay_descr_rva
/* RVA to the descriptor for PE dlls */
59 #define RELAY_DESCR_MAGIC 0xdeb90002
60 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
62 /* private data built at dll load time */
64 struct relay_entry_point
66 void *orig_func
; /* original entry point function */
67 const char *name
; /* function name (if any) */
70 struct relay_private_data
72 HMODULE module
; /* module handle of this dll */
73 unsigned int base
; /* ordinal base */
74 char dllname
[40]; /* dll name (without .dll extension) */
75 struct relay_entry_point entry_points
[1]; /* list of dll entry points */
78 static const WCHAR
**debug_relay_excludelist
;
79 static const WCHAR
**debug_relay_includelist
;
80 static const WCHAR
**debug_snoop_excludelist
;
81 static const WCHAR
**debug_snoop_includelist
;
82 static const WCHAR
**debug_from_relay_excludelist
;
83 static const WCHAR
**debug_from_relay_includelist
;
84 static const WCHAR
**debug_from_snoop_excludelist
;
85 static const WCHAR
**debug_from_snoop_includelist
;
87 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
89 /* compare an ASCII and a Unicode string without depending on the current codepage */
90 static inline int strcmpAW( const char *strA
, const WCHAR
*strW
)
92 while (*strA
&& ((unsigned char)*strA
== *strW
)) { strA
++; strW
++; }
93 return (unsigned char)*strA
- *strW
;
96 /* compare an ASCII and a Unicode string without depending on the current codepage */
97 static inline int strncmpiAW( const char *strA
, const WCHAR
*strW
, int n
)
100 for ( ; n
> 0; n
--, strA
++, strW
++)
101 if ((ret
= toupperW((unsigned char)*strA
) - toupperW(*strW
)) || !*strA
) break;
105 /***********************************************************************
108 * Build a function list from a ';'-separated string.
110 static const WCHAR
**build_list( const WCHAR
*buffer
)
113 const WCHAR
*p
= buffer
;
116 while ((p
= strchrW( p
, ';' )))
121 /* allocate count+1 pointers, plus the space for a copy of the string */
122 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0,
123 (count
+1) * sizeof(WCHAR
*) + (strlenW(buffer
)+1) * sizeof(WCHAR
) )))
125 WCHAR
*str
= (WCHAR
*)(ret
+ count
+ 1);
128 strcpyW( str
, buffer
);
133 if (!(q
= strchrW( q
, ';' ))) break;
141 /***********************************************************************
144 * Load a function list from a registry value.
146 static const WCHAR
**load_list( HKEY hkey
, const WCHAR
*value
)
148 char initial_buffer
[4096];
149 char *buffer
= initial_buffer
;
153 const WCHAR
**list
= NULL
;
155 RtlInitUnicodeString( &name
, value
);
156 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, sizeof(initial_buffer
), &count
);
157 if (status
== STATUS_BUFFER_OVERFLOW
)
159 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, count
);
160 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, count
, &count
);
162 if (status
== STATUS_SUCCESS
)
164 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
165 list
= build_list( str
);
166 if (list
) TRACE( "%s = %s\n", debugstr_w(value
), debugstr_w(str
) );
169 if (buffer
!= initial_buffer
) RtlFreeHeap( GetProcessHeap(), 0, buffer
);
173 /***********************************************************************
176 * Build the relay include/exclude function lists.
178 static DWORD WINAPI
init_debug_lists( RTL_RUN_ONCE
*once
, void *param
, void **context
)
180 OBJECT_ATTRIBUTES attr
;
183 static const WCHAR configW
[] = {'S','o','f','t','w','a','r','e','\\',
184 'W','i','n','e','\\',
185 'D','e','b','u','g',0};
186 static const WCHAR RelayIncludeW
[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
187 static const WCHAR RelayExcludeW
[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
188 static const WCHAR SnoopIncludeW
[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
189 static const WCHAR SnoopExcludeW
[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
190 static const WCHAR RelayFromIncludeW
[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
191 static const WCHAR RelayFromExcludeW
[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
192 static const WCHAR SnoopFromIncludeW
[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
193 static const WCHAR SnoopFromExcludeW
[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
195 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
196 attr
.Length
= sizeof(attr
);
197 attr
.RootDirectory
= root
;
198 attr
.ObjectName
= &name
;
200 attr
.SecurityDescriptor
= NULL
;
201 attr
.SecurityQualityOfService
= NULL
;
202 RtlInitUnicodeString( &name
, configW
);
204 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
205 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
207 if (!hkey
) return TRUE
;
209 debug_relay_includelist
= load_list( hkey
, RelayIncludeW
);
210 debug_relay_excludelist
= load_list( hkey
, RelayExcludeW
);
211 debug_snoop_includelist
= load_list( hkey
, SnoopIncludeW
);
212 debug_snoop_excludelist
= load_list( hkey
, SnoopExcludeW
);
213 debug_from_relay_includelist
= load_list( hkey
, RelayFromIncludeW
);
214 debug_from_relay_excludelist
= load_list( hkey
, RelayFromExcludeW
);
215 debug_from_snoop_includelist
= load_list( hkey
, SnoopFromIncludeW
);
216 debug_from_snoop_excludelist
= load_list( hkey
, SnoopFromExcludeW
);
223 /***********************************************************************
226 * Check if a given module and function is in the list.
228 static BOOL
check_list( const char *module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
232 sprintf( ord_str
, "%d", ordinal
);
235 const WCHAR
*p
= strrchrW( *list
, '.' );
236 if (p
&& p
> *list
) /* check module and function */
239 if (strncmpiAW( module
, *list
, len
-1 ) || module
[len
]) continue;
240 if (p
[1] == '*' && !p
[2]) return TRUE
;
241 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
242 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
244 else /* function only */
246 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
253 /***********************************************************************
254 * check_relay_include
256 * Check if a given function must be included in the relay output.
258 static BOOL
check_relay_include( const char *module
, int ordinal
, const char *func
)
260 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
262 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
267 /***********************************************************************
270 * Check if calls from a given module must be included in the relay/snoop output,
271 * given the exclusion and inclusion lists.
273 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
275 static const WCHAR dllW
[] = {'.','d','l','l',0 };
276 const WCHAR
**listitem
;
279 if (!module
) return TRUE
;
280 if (!includelist
&& !excludelist
) return TRUE
;
284 listitem
= excludelist
;
289 listitem
= includelist
;
291 for(; *listitem
; listitem
++)
295 if (!strcmpiW( *listitem
, module
)) return !show
;
296 len
= strlenW( *listitem
);
297 if (!strncmpiW( *listitem
, module
, len
) && !strcmpiW( module
+ len
, dllW
))
304 static BOOL
is_ret_val( char type
)
306 return type
>= 'A' && type
<= 'Z';
309 static const char *func_name( struct relay_private_data
*data
, unsigned int ordinal
)
311 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
313 if (entry_point
->name
)
314 return wine_dbg_sprintf( "%s.%s", data
->dllname
, entry_point
->name
);
316 return wine_dbg_sprintf( "%s.%u", data
->dllname
, data
->base
+ ordinal
);
319 static void trace_string_a( INT_PTR ptr
)
321 if (!IS_INTARG( ptr
)) TRACE( "%08lx %s", ptr
, debugstr_a( (char *)ptr
));
322 else TRACE( "%08lx", ptr
);
325 static void trace_string_w( INT_PTR ptr
)
327 if (!IS_INTARG( ptr
)) TRACE( "%08lx %s", ptr
, debugstr_w( (WCHAR
*)ptr
));
328 else TRACE( "%08lx", ptr
);
333 /***********************************************************************
336 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
337 const DWORD
*stack
, unsigned int *nb_args
)
339 WORD ordinal
= LOWORD(idx
);
340 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
341 struct relay_private_data
*data
= descr
->private;
342 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
345 TRACE( "\1Call %s(", func_name( data
, ordinal
));
347 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
349 switch (arg_types
[i
])
351 case 'j': /* int64 */
352 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
355 case 'k': /* int128 */
356 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
360 trace_string_a( stack
[pos
++] );
363 trace_string_w( stack
[pos
++] );
365 case 'f': /* float */
366 TRACE( "%g", *(const float *)&stack
[pos
++] );
368 case 'd': /* double */
369 TRACE( "%g", *(const double *)&stack
[pos
] );
374 TRACE( "%08x", stack
[pos
++] );
377 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
380 if (arg_types
[0] == 't')
382 *nb_args
|= 0x80000000; /* thiscall/fastcall */
383 if (arg_types
[1] == 't') *nb_args
|= 0x40000000; /* fastcall */
385 TRACE( ") ret=%08x\n", stack
[-1] );
386 return entry_point
->orig_func
;
389 /***********************************************************************
392 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
393 void *retaddr
, LONGLONG retval
)
395 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
397 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
399 while (!is_ret_val( *arg_types
)) arg_types
++;
400 if (*arg_types
== 'J') /* int64 return value */
401 TRACE( " retval=%08x%08x ret=%08x\n",
402 (UINT
)(retval
>> 32), (UINT
)retval
, (UINT
)retaddr
);
404 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, (UINT
)retaddr
);
407 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
);
408 __ASM_GLOBAL_FUNC( relay_call
,
410 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
411 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
413 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
415 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
417 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
419 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
420 /* trace the parameters */
422 "pushl %esp\n\t" /* number of args return ptr */
423 "leal 20(%ebp),%esi\n\t" /* stack */
427 "call " __ASM_NAME("relay_trace_entry") "\n\t"
428 /* copy the arguments*/
429 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
431 "leal 0(,%ecx,4),%edx\n\t"
437 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
440 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
443 /* call the entry point */
447 /* trace the return value */
448 "leal -20(%ebp),%esp\n\t"
454 "call " __ASM_NAME("relay_trace_exit") "\n\t"
455 /* restore return value and return */
456 "leal -12(%ebp),%esp\n\t"
460 __ASM_CFI(".cfi_same_value %ecx\n\t")
462 __ASM_CFI(".cfi_same_value %edi\n\t")
464 __ASM_CFI(".cfi_same_value %esi\n\t")
466 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
467 __ASM_CFI(".cfi_same_value %ebp\n\t")
470 #elif defined(__arm__)
472 /***********************************************************************
475 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
476 const DWORD
*stack
, unsigned int *nb_args
)
478 WORD ordinal
= LOWORD(idx
);
479 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
480 struct relay_private_data
*data
= descr
->private;
481 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
484 unsigned int float_pos
= 0, double_pos
= 0;
485 const union fpregs
{ float s
[16]; double d
[8]; } *fpstack
= (const union fpregs
*)stack
- 1;
488 TRACE( "\1Call %s(", func_name( data
, ordinal
));
490 for (i
= pos
= 0; !is_ret_val( arg_types
[i
] ); i
++)
492 switch (arg_types
[i
])
494 case 'j': /* int64 */
495 pos
= (pos
+ 1) & ~1;
496 TRACE( "%x%08x", stack
[pos
+1], stack
[pos
] );
499 case 'k': /* int128 */
500 TRACE( "{%08x,%08x,%08x,%08x}", stack
[pos
], stack
[pos
+1], stack
[pos
+2], stack
[pos
+3] );
504 trace_string_a( stack
[pos
++] );
507 trace_string_w( stack
[pos
++] );
509 case 'f': /* float */
511 if (!(float_pos
% 2)) float_pos
= max( float_pos
, double_pos
* 2 );
514 TRACE( "%g", fpstack
->s
[float_pos
++] );
518 TRACE( "%g", *(const float *)&stack
[pos
++] );
520 case 'd': /* double */
522 double_pos
= max( (float_pos
+ 1) / 2, double_pos
);
525 TRACE( "%g", fpstack
->d
[double_pos
++] );
529 pos
= (pos
+ 1) & ~1;
530 TRACE( "%g", *(const double *)&stack
[pos
] );
535 TRACE( "%08x", stack
[pos
++] );
538 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
542 if (float_pos
|| double_pos
)
545 stack
= (const DWORD
*)fpstack
; /* retaddr is below the fp regs */
549 TRACE( ") ret=%08x\n", stack
[-1] );
550 return entry_point
->orig_func
;
553 /***********************************************************************
556 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
557 DWORD retaddr
, LONGLONG retval
)
559 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
561 TRACE( "\1Ret %s()", func_name( descr
->private, LOWORD(idx
) ));
563 while (!is_ret_val( *arg_types
)) arg_types
++;
564 if (*arg_types
== 'J') /* int64 return value */
565 TRACE( " retval=%08x%08x ret=%08x\n",
566 (UINT
)(retval
>> 32), (UINT
)retval
, retaddr
);
568 TRACE( " retval=%08x ret=%08x\n", (UINT
)retval
, retaddr
);
571 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const DWORD
*stack
);
572 __ASM_GLOBAL_FUNC( relay_call
,
574 "push {r4-r8,lr}\n\t"
577 "add r3, sp, #12\n\t"
580 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
581 "mov ip, r0\n\t" /* entry point */
583 "ldr r1, [sp, #12]\n\t" /* number of args */
585 "subs r3, #16\n\t" /* first 4 args are in registers */
589 "add r2, r6, #16\n\t" /* skip r0-r3 */
590 "1:\tsubs r3, r3, #4\n\t"
591 "ldr r0, [r2, r3]\n\t"
592 "str r0, [sp, r3]\n\t"
596 "tst r1, #0x80000000\n\t"
597 "ldm r6, {r0-r3}\n\t"
598 "vldmdbne r6!, {s0-s15}\n\t"
600 "ldm r6, {r0-r3}\n\t"
604 "ldr r2, [r6, #-4]\n\t" /* retaddr */
610 "str r5, [sp, #4]\n\t"
612 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
613 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
614 "vldr d0, [sp, #8]\n\t"
616 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
623 #elif defined(__aarch64__)
625 /***********************************************************************
628 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
629 const INT_PTR
*stack
, unsigned int *nb_args
)
631 WORD ordinal
= LOWORD(idx
);
632 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
633 struct relay_private_data
*data
= descr
->private;
634 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
637 TRACE( "\1Call %s(", func_name( data
, ordinal
));
639 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
641 switch (arg_types
[i
])
644 trace_string_a( stack
[i
] );
647 trace_string_w( stack
[i
] );
651 TRACE( "%08lx", stack
[i
] );
654 if (!is_ret_val( arg_types
[i
+ 1] )) TRACE( "," );
657 TRACE( ") ret=%08lx\n", stack
[-1] );
658 return entry_point
->orig_func
;
661 /***********************************************************************
664 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
665 INT_PTR retaddr
, INT_PTR retval
)
667 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
668 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
671 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
);
672 __ASM_GLOBAL_FUNC( call_entry_point
,
673 "stp x29, x30, [SP,#-16]!\n\t"
674 "stp x19, x20, [SP,#-16]!\n\t"
677 "ldp x8, x9, [x19, #-32]\n\t"
683 "ldp x0, x1, [x11], #16\n\t"
684 "add w12, w12, #2\n\t"
687 "ldp x2, x3, [x11], #16\n\t"
688 "add w12, w12, #2\n\t"
691 "ldp x4, x5, [x11], #16\n\t"
692 "add w12, w12, #2\n\t"
695 "ldp x6, x7, [x11], #16\n\t"
696 "add w12, w12, #2\n\t"
699 "sub w12, w10, #8\n\t"
700 "lsl w12, w12, #3\n\t"
701 "sub SP, SP, w12, uxtw\n\t"
702 "tbz w12, #3, 1f\n\t"
704 "1: sub w12, w12, #8\n\t"
705 "ldr x13, [x11, x12]\n\t"
706 "str x13, [SP, x12]\n\t"
710 "ldp x19, x20, [SP], #16\n\t"
711 "ldp x29, x30, [SP], #16\n\t"
714 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
716 unsigned int nb_args
;
717 void *func
= relay_trace_entry( descr
, idx
, stack
, &nb_args
);
718 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
);
719 relay_trace_exit( descr
, idx
, stack
[-1], ret
);
723 #elif defined(__x86_64__)
725 /***********************************************************************
728 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
,
729 const INT_PTR
*stack
, unsigned int *nb_args
)
731 WORD ordinal
= LOWORD(idx
);
732 const char *arg_types
= descr
->args_string
+ HIWORD(idx
);
733 struct relay_private_data
*data
= descr
->private;
734 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
737 TRACE( "\1Call %s(", func_name( data
, ordinal
));
739 for (i
= 0; !is_ret_val( arg_types
[i
] ); i
++)
741 switch (arg_types
[i
])
744 trace_string_a( stack
[i
] );
747 trace_string_w( stack
[i
] );
749 case 'f': /* float */
750 TRACE( "%g", *(const float *)&stack
[i
] );
752 case 'd': /* double */
753 TRACE( "%g", *(const double *)&stack
[i
] );
757 TRACE( "%08lx", stack
[i
] );
760 if (!is_ret_val( arg_types
[i
+1] )) TRACE( "," );
763 TRACE( ") ret=%08lx\n", stack
[-1] );
764 return entry_point
->orig_func
;
767 /***********************************************************************
770 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
771 INT_PTR retaddr
, INT_PTR retval
)
773 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
774 func_name( descr
->private, LOWORD(idx
) ), retval
, retaddr
);
777 extern INT_PTR WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
778 __ASM_GLOBAL_FUNC( relay_call
,
780 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
781 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
783 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
784 "leaq -0x48(%rbp),%rsp\n\t"
786 "movq %rcx,-32(%rbp)\n\t"
787 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
788 "movq %rdx,-24(%rbp)\n\t"
789 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
790 "movq %rsi,-16(%rbp)\n\t"
791 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
792 "movq %rdi,-8(%rbp)\n\t"
793 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
794 /* trace the parameters */
795 "leaq 24(%rbp),%r8\n\t" /* stack */
796 "leaq -40(%rbp),%r9\n\t"
797 "call " __ASM_NAME("relay_trace_entry") "\n\t"
798 /* copy the arguments */
799 "movl -40(%rbp),%edx\n\t" /* number of args */
802 "cmovgq %rdx,%rcx\n\t"
803 "leaq -16(,%rcx,8),%rdx\n\t"
806 "leaq 24(%rbp),%rsi\n\t" /* original stack */
809 /* call the entry point */
810 "movq 0(%rsp),%rcx\n\t"
811 "movq 8(%rsp),%rdx\n\t"
812 "movq 16(%rsp),%r8\n\t"
813 "movq 24(%rsp),%r9\n\t"
814 "movq 0(%rsp),%xmm0\n\t"
815 "movq 8(%rsp),%xmm1\n\t"
816 "movq 16(%rsp),%xmm2\n\t"
817 "movq 24(%rsp),%xmm3\n\t"
819 /* trace the return value */
820 "movq -32(%rbp),%rcx\n\t"
821 "movq -24(%rbp),%rdx\n\t"
822 "movq 16(%rbp),%r8\n\t" /* retaddr */
824 "movaps %xmm0,32(%rsp)\n\t"
826 "call " __ASM_NAME("relay_trace_exit") "\n\t"
827 /* restore return value and return */
829 "movaps 32(%rsp),%xmm0\n\t"
830 "movq -16(%rbp),%rsi\n\t"
831 __ASM_CFI(".cfi_same_value %rsi\n\t")
832 "movq -8(%rbp),%rdi\n\t"
833 __ASM_CFI(".cfi_same_value %rdi\n\t")
835 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
837 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
838 __ASM_CFI(".cfi_same_value %rbp\n\t")
842 #error Not supported on this CPU
846 static struct relay_descr
*get_relay_descr( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
849 struct relay_descr
*descr
;
850 struct relay_descr_rva
*rva
;
851 ULONG_PTR ptr
= (ULONG_PTR
)module
+ exports
->Name
;
854 if (ptr
<= (ULONG_PTR
)(exports
+ 1)) return NULL
;
855 if (ptr
> (ULONG_PTR
)exports
+ exp_size
) return NULL
;
856 if (ptr
% sizeof(DWORD
)) return NULL
;
858 rva
= (struct relay_descr_rva
*)ptr
- 1;
859 if (rva
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
860 if (rva
->descr
) descr
= (struct relay_descr
*)((char *)module
+ rva
->descr
);
861 else descr
= (struct relay_descr
*)((const char *)exports
+ exp_size
);
862 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return NULL
;
866 /***********************************************************************
867 * RELAY_GetProcAddress
869 * Return the proc address to use for a given function.
871 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
872 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
874 struct relay_private_data
*data
;
875 const struct relay_descr
*descr
= get_relay_descr( module
, exports
, exp_size
);
877 if (!descr
|| !(data
= descr
->private)) return proc
; /* no relay data */
878 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
879 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
880 return proc
; /* we want to relay it */
881 return data
->entry_points
[ordinal
].orig_func
;
885 /***********************************************************************
888 * Setup relay debugging for a built-in dll.
890 void RELAY_SetupDLL( HMODULE module
)
892 IMAGE_EXPORT_DIRECTORY
*exports
;
895 DWORD size
, entry_point_rva
, old_prot
;
896 struct relay_descr
*descr
;
897 struct relay_private_data
*data
;
902 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
904 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
905 if (!exports
) return;
907 if (!(descr
= get_relay_descr( module
, exports
, size
))) return;
909 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
910 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
913 descr
->relay_call
= relay_call
;
914 descr
->private = data
;
916 data
->module
= module
;
917 data
->base
= exports
->Base
;
918 len
= strlen( (char *)module
+ exports
->Name
);
919 if (len
> 4 && !_stricmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
920 len
= min( len
, sizeof(data
->dllname
) - 1 );
921 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
922 data
->dllname
[len
] = 0;
924 /* fetch name pointer for all entry points and store them in the private structure */
926 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
927 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
929 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
930 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
933 /* patch the functions in the export table to point to the relay thunks */
935 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
936 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
939 func_size
= exports
->NumberOfFunctions
* sizeof(*funcs
);
940 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, PAGE_READWRITE
, &old_prot
);
941 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
943 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
944 if (!check_relay_include( data
->dllname
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
945 continue; /* don't include this entry point */
947 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
948 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
950 if (old_prot
!= PAGE_READWRITE
)
951 NtProtectVirtualMemory( NtCurrentProcess(), &func_base
, &func_size
, old_prot
, &old_prot
);
954 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
956 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
957 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
962 void RELAY_SetupDLL( HMODULE module
)
966 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
969 /***********************************************************************/
971 /***********************************************************************/
975 WINE_DECLARE_DEBUG_CHANNEL(seh
);
976 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
978 #include "pshpack1.h"
983 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
984 DWORD snoopentry
; /* SNOOP_Entry relative */
991 typedef struct tagSNOOP_DLL
{
996 struct tagSNOOP_DLL
*next
;
1003 BYTE lcall
; /* 0xe8 call snoopret relative*/
1004 DWORD snoopret
; /* SNOOP_Ret relative */
1010 DWORD
*args
; /* saved args across a stdcall */
1011 } SNOOP_RETURNENTRY
;
1013 typedef struct tagSNOOP_RETURNENTRIES
{
1014 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
1015 struct tagSNOOP_RETURNENTRIES
*next
;
1016 } SNOOP_RETURNENTRIES
;
1018 #include "poppack.h"
1020 extern void WINAPI
SNOOP_Entry(void);
1021 extern void WINAPI
SNOOP_Return(void);
1023 static SNOOP_DLL
*firstdll
;
1024 static SNOOP_RETURNENTRIES
*firstrets
;
1027 /***********************************************************************
1028 * SNOOP_ShowDebugmsgSnoop
1030 * Simple function to decide if a particular debugging message is
1033 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
1035 if (debug_snoop_excludelist
&& check_list( module
, ordinal
, func
, debug_snoop_excludelist
))
1037 if (debug_snoop_includelist
&& !check_list( module
, ordinal
, func
, debug_snoop_includelist
))
1043 /***********************************************************************
1046 * Setup snoop debugging for a native dll.
1048 void SNOOP_SetupDLL(HMODULE hmod
)
1050 SNOOP_DLL
**dll
= &firstdll
;
1055 IMAGE_EXPORT_DIRECTORY
*exports
;
1057 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
1059 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
1060 if (!exports
|| !exports
->NumberOfFunctions
) return;
1061 name
= (char *)hmod
+ exports
->Name
;
1064 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
1067 if ((*dll
)->hmod
== hmod
)
1069 /* another dll, loaded at the same address */
1070 addr
= (*dll
)->funs
;
1071 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
1072 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
1075 dll
= &((*dll
)->next
);
1078 *dll
= RtlReAllocateHeap(GetProcessHeap(),
1079 HEAP_ZERO_MEMORY
, *dll
,
1080 sizeof(SNOOP_DLL
) + strlen(name
));
1082 *dll
= RtlAllocateHeap(GetProcessHeap(),
1084 sizeof(SNOOP_DLL
) + strlen(name
));
1085 (*dll
)->hmod
= hmod
;
1086 (*dll
)->ordbase
= exports
->Base
;
1087 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
1088 strcpy( (*dll
)->name
, name
);
1089 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
1090 if (p
> (*dll
)->name
&& !_stricmp( p
, ".dll" )) *p
= 0;
1092 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
1094 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1095 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
1097 RtlFreeHeap(GetProcessHeap(),0,*dll
);
1098 FIXME("out of memory\n");
1101 (*dll
)->funs
= addr
;
1102 memset((*dll
)->funs
,0,size
);
1106 /***********************************************************************
1107 * SNOOP_GetProcAddress
1109 * Return the proc address to use for a given function.
1111 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
1112 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
1117 const WORD
*ordinals
;
1119 SNOOP_DLL
*dll
= firstdll
;
1121 const IMAGE_SECTION_HEADER
*sec
;
1123 if (!TRACE_ON(snoop
)) return origfun
;
1124 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
1125 return origfun
; /* the calling module was explicitly excluded */
1127 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
1130 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
1132 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
1133 return origfun
; /* most likely a data reference */
1136 if (hmod
== dll
->hmod
)
1140 if (!dll
) /* probably internal */
1143 /* try to find a name for it */
1145 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
1146 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
1147 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
1149 if (ordinals
[i
] == ordinal
)
1151 ename
= (const char *)hmod
+ names
[i
];
1155 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
1157 assert(ordinal
< dll
->nrofordinals
);
1158 fun
= dll
->funs
+ ordinal
;
1163 fun
->snoopentry
= (char *)SNOOP_Entry
- (char *)(&fun
->snoopentry
+ 1);
1164 fun
->origfun
= origfun
;
1167 return (FARPROC
)&(fun
->lcall
);
1170 static void SNOOP_PrintArg(DWORD x
)
1174 TRACE_(snoop
)("%08x",x
);
1175 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
1182 if (s
[i
]<0x20) {nostring
=1;break;}
1183 if (s
[i
]>=0x80) {nostring
=1;break;}
1186 if (!nostring
&& i
> 5)
1187 TRACE_(snoop
)(" %s",debugstr_an((LPSTR
)x
,i
));
1188 else /* try unicode */
1194 if (s
[i
]<0x20) {nostring
=1;break;}
1195 if (s
[i
]>0x100) {nostring
=1;break;}
1198 if (!nostring
&& i
> 5) TRACE_(snoop
)(" %s",debugstr_wn((LPWSTR
)x
,i
));
1207 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Entry( void **stack
)
1210 SNOOP_FUN
*fun
= (SNOOP_FUN
*)((char *)stack
[0] - 5);
1211 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1212 SNOOP_RETURNENTRY
*ret
;
1215 for (dll
= firstdll
; dll
; dll
= dll
->next
)
1216 if (fun
>= dll
->funs
&& fun
< dll
->funs
+ dll
->nrofordinals
) break;
1219 FIXME("entrypoint %p not found\n", fun
);
1222 /* guess cdecl ... */
1223 if (fun
->nrofargs
<0) {
1224 /* Typical cdecl return frame is:
1226 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1227 * (after that 81 C2 xx xx xx xx)
1229 LPBYTE reteip
= stack
[1];
1232 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1233 fun
->nrofargs
=reteip
[2]/4;
1239 for (i
=0;i
<ARRAY_SIZE( (*rets
)->entry
);i
++)
1240 if (!(*rets
)->entry
[i
].origreturn
)
1242 if (i
!=ARRAY_SIZE( (*rets
)->entry
))
1244 rets
= &((*rets
)->next
);
1250 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1251 MEM_COMMIT
| MEM_RESERVE
,
1252 PAGE_EXECUTE_READWRITE
);
1255 i
= 0; /* entry 0 is free */
1257 ret
= &((*rets
)->entry
[i
]);
1259 ret
->snoopret
= (char *)SNOOP_Return
- (char *)(&ret
->snoopret
+ 1);
1260 ret
->origreturn
= stack
[1];
1261 stack
[1] = &ret
->lcall
;
1264 ret
->ordinal
= fun
- dll
->funs
;
1265 ret
->origESP
= stack
;
1267 stack
[0] = fun
->origfun
;
1269 if (!TRACE_ON(snoop
)) return;
1271 if (fun
->name
) TRACE_(snoop
)("\1CALL %s.%s(", dll
->name
, fun
->name
);
1272 else TRACE_(snoop
)("\1CALL %s.%d(", dll
->name
, dll
->ordbase
+ret
->ordinal
);
1273 if (fun
->nrofargs
>0) {
1274 max
= fun
->nrofargs
; if (max
>16) max
=16;
1277 SNOOP_PrintArg( (DWORD
)stack
[i
+ 2] );
1278 if (i
<fun
->nrofargs
-1) TRACE_(snoop
)(",");
1280 if (max
!=fun
->nrofargs
)
1281 TRACE_(snoop
)(" ...");
1282 } else if (fun
->nrofargs
<0) {
1283 TRACE_(snoop
)("<unknown, check return>");
1284 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1285 0,16*sizeof(DWORD
));
1286 memcpy(ret
->args
, stack
+ 2, sizeof(DWORD
)*16);
1288 TRACE_(snoop
)(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1291 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Return( void **stack
)
1293 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)((char *)stack
[0] - 5);
1294 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1295 DWORD retval
= (DWORD
)stack
[-1]; /* get saved %eax from the stack */
1297 /* We haven't found out the nrofargs yet. If we called a cdecl
1298 * function it is too late anyway and we can just set '0' (which
1299 * will be the difference between orig and current ESP
1300 * If stdcall -> everything ok.
1302 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1303 ret
->dll
->funs
[ret
->ordinal
].nrofargs
= stack
- ret
->origESP
- 1;
1304 stack
[0] = ret
->origreturn
;
1306 if (!TRACE_ON(snoop
)) {
1307 ret
->origreturn
= NULL
; /* mark as empty */
1315 TRACE_(snoop
)("\1RET %s.%s(", ret
->dll
->name
, fun
->name
);
1317 TRACE_(snoop
)("\1RET %s.%d(", ret
->dll
->name
, ret
->dll
->ordbase
+ret
->ordinal
);
1319 max
= fun
->nrofargs
;
1324 SNOOP_PrintArg(ret
->args
[i
]);
1325 if (i
<max
-1) TRACE_(snoop
)(",");
1327 TRACE_(snoop
)(") retval=%08x ret=%08x\n", retval
, (DWORD
)ret
->origreturn
);
1328 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1334 TRACE_(snoop
)("\1RET %s.%s() retval=%08x ret=%08x\n",
1335 ret
->dll
->name
, fun
->name
, retval
, (DWORD
)ret
->origreturn
);
1337 TRACE_(snoop
)("\1RET %s.%d() retval=%08x ret=%08x\n",
1338 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1339 retval
, (DWORD
)ret
->origreturn
);
1341 ret
->origreturn
= NULL
; /* mark as empty */
1344 /* small wrappers that save registers and get the stack pointer */
1345 #define SNOOP_WRAPPER(name) \
1346 __ASM_STDCALL_FUNC( name, 0, \
1348 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1350 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1352 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1353 "leal 12(%esp),%eax\n\t" \
1355 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1356 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1357 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1359 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1361 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1363 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1366 SNOOP_WRAPPER( SNOOP_Entry
)
1367 SNOOP_WRAPPER( SNOOP_Return
)
1369 #else /* __i386__ */
1371 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1372 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1377 void SNOOP_SetupDLL( HMODULE hmod
)
1379 FIXME("snooping works only on i386 for now.\n");
1382 #endif /* __i386__ */