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 WINE_DECLARE_DEBUG_CHANNEL(timestamp
);
44 WINE_DECLARE_DEBUG_CHANNEL(pid
);
46 struct relay_descr
/* descriptor for a module */
48 void *magic
; /* signature */
49 void *relay_call
; /* functions to call from relay thunks */
50 void *relay_call_regs
; /* no longer used */
51 void *private; /* reserved for the relay code private data */
52 const char *entry_point_base
; /* base address of entry point thunks */
53 const unsigned int *entry_point_offsets
; /* offsets of entry points thunks */
54 const unsigned int *arg_types
; /* table of argument types for all entry points */
57 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
58 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
60 /* private data built at dll load time */
62 struct relay_entry_point
64 void *orig_func
; /* original entry point function */
65 const char *name
; /* function name (if any) */
68 struct relay_private_data
70 HMODULE module
; /* module handle of this dll */
71 unsigned int base
; /* ordinal base */
72 char dllname
[40]; /* dll name (without .dll extension) */
73 struct relay_entry_point entry_points
[1]; /* list of dll entry points */
76 static const WCHAR
**debug_relay_excludelist
;
77 static const WCHAR
**debug_relay_includelist
;
78 static const WCHAR
**debug_snoop_excludelist
;
79 static const WCHAR
**debug_snoop_includelist
;
80 static const WCHAR
**debug_from_relay_excludelist
;
81 static const WCHAR
**debug_from_relay_includelist
;
82 static const WCHAR
**debug_from_snoop_excludelist
;
83 static const WCHAR
**debug_from_snoop_includelist
;
85 static RTL_RUN_ONCE init_once
= RTL_RUN_ONCE_INIT
;
87 /* compare an ASCII and a Unicode string without depending on the current codepage */
88 static inline int strcmpAW( const char *strA
, const WCHAR
*strW
)
90 while (*strA
&& ((unsigned char)*strA
== *strW
)) { strA
++; strW
++; }
91 return (unsigned char)*strA
- *strW
;
94 /* compare an ASCII and a Unicode string without depending on the current codepage */
95 static inline int strncmpiAW( const char *strA
, const WCHAR
*strW
, int n
)
98 for ( ; n
> 0; n
--, strA
++, strW
++)
99 if ((ret
= toupperW((unsigned char)*strA
) - toupperW(*strW
)) || !*strA
) break;
103 /***********************************************************************
106 * Build a function list from a ';'-separated string.
108 static const WCHAR
**build_list( const WCHAR
*buffer
)
111 const WCHAR
*p
= buffer
;
114 while ((p
= strchrW( p
, ';' )))
119 /* allocate count+1 pointers, plus the space for a copy of the string */
120 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0,
121 (count
+1) * sizeof(WCHAR
*) + (strlenW(buffer
)+1) * sizeof(WCHAR
) )))
123 WCHAR
*str
= (WCHAR
*)(ret
+ count
+ 1);
126 strcpyW( str
, buffer
);
131 if (!(q
= strchrW( q
, ';' ))) break;
139 /***********************************************************************
142 * Load a function list from a registry value.
144 static const WCHAR
**load_list( HKEY hkey
, const WCHAR
*value
)
146 char initial_buffer
[4096];
147 char *buffer
= initial_buffer
;
151 const WCHAR
**list
= NULL
;
153 RtlInitUnicodeString( &name
, value
);
154 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, sizeof(initial_buffer
), &count
);
155 if (status
== STATUS_BUFFER_OVERFLOW
)
157 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, count
);
158 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, count
, &count
);
160 if (status
== STATUS_SUCCESS
)
162 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
163 list
= build_list( str
);
164 if (list
) TRACE( "%s = %s\n", debugstr_w(value
), debugstr_w(str
) );
167 if (buffer
!= initial_buffer
) RtlFreeHeap( GetProcessHeap(), 0, buffer
);
171 /***********************************************************************
174 * Build the relay include/exclude function lists.
176 static DWORD WINAPI
init_debug_lists( RTL_RUN_ONCE
*once
, void *param
, void **context
)
178 OBJECT_ATTRIBUTES attr
;
181 static const WCHAR configW
[] = {'S','o','f','t','w','a','r','e','\\',
182 'W','i','n','e','\\',
183 'D','e','b','u','g',0};
184 static const WCHAR RelayIncludeW
[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
185 static const WCHAR RelayExcludeW
[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
186 static const WCHAR SnoopIncludeW
[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
187 static const WCHAR SnoopExcludeW
[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
188 static const WCHAR RelayFromIncludeW
[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
189 static const WCHAR RelayFromExcludeW
[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
190 static const WCHAR SnoopFromIncludeW
[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
191 static const WCHAR SnoopFromExcludeW
[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
193 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
194 attr
.Length
= sizeof(attr
);
195 attr
.RootDirectory
= root
;
196 attr
.ObjectName
= &name
;
198 attr
.SecurityDescriptor
= NULL
;
199 attr
.SecurityQualityOfService
= NULL
;
200 RtlInitUnicodeString( &name
, configW
);
202 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
203 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
205 if (!hkey
) return TRUE
;
207 debug_relay_includelist
= load_list( hkey
, RelayIncludeW
);
208 debug_relay_excludelist
= load_list( hkey
, RelayExcludeW
);
209 debug_snoop_includelist
= load_list( hkey
, SnoopIncludeW
);
210 debug_snoop_excludelist
= load_list( hkey
, SnoopExcludeW
);
211 debug_from_relay_includelist
= load_list( hkey
, RelayFromIncludeW
);
212 debug_from_relay_excludelist
= load_list( hkey
, RelayFromExcludeW
);
213 debug_from_snoop_includelist
= load_list( hkey
, SnoopFromIncludeW
);
214 debug_from_snoop_excludelist
= load_list( hkey
, SnoopFromExcludeW
);
221 /***********************************************************************
224 * Check if a given module and function is in the list.
226 static BOOL
check_list( const char *module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
230 sprintf( ord_str
, "%d", ordinal
);
233 const WCHAR
*p
= strrchrW( *list
, '.' );
234 if (p
&& p
> *list
) /* check module and function */
237 if (strncmpiAW( module
, *list
, len
-1 ) || module
[len
]) continue;
238 if (p
[1] == '*' && !p
[2]) return TRUE
;
239 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
240 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
242 else /* function only */
244 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
251 /***********************************************************************
252 * check_relay_include
254 * Check if a given function must be included in the relay output.
256 static BOOL
check_relay_include( const char *module
, int ordinal
, const char *func
)
258 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
260 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
265 /***********************************************************************
268 * Check if calls from a given module must be included in the relay/snoop output,
269 * given the exclusion and inclusion lists.
271 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
273 static const WCHAR dllW
[] = {'.','d','l','l',0 };
274 const WCHAR
**listitem
;
277 if (!module
) return TRUE
;
278 if (!includelist
&& !excludelist
) return TRUE
;
282 listitem
= excludelist
;
287 listitem
= includelist
;
289 for(; *listitem
; listitem
++)
293 if (!strcmpiW( *listitem
, module
)) return !show
;
294 len
= strlenW( *listitem
);
295 if (!strncmpiW( *listitem
, module
, len
) && !strcmpiW( module
+ len
, dllW
))
301 /***********************************************************************
304 static inline void RELAY_PrintArgs( const INT_PTR
*args
, int nb_args
, unsigned int typemask
)
308 if ((typemask
& 3) && !IS_INTARG(*args
))
311 DPRINTF( "%08lx %s", *args
, debugstr_w((LPCWSTR
)*args
) );
313 DPRINTF( "%08lx %s", *args
, debugstr_a((LPCSTR
)*args
) );
315 else DPRINTF( "%08lx", *args
);
316 if (nb_args
) DPRINTF( "," );
322 static void print_timestamp(void)
324 ULONG ticks
= NtGetTickCount();
325 DPRINTF( "%3u.%03u:", ticks
/ 1000, ticks
% 1000 );
328 /***********************************************************************
331 * stack points to the return address, i.e. the first argument is stack[1].
333 DECLSPEC_HIDDEN
void * WINAPI
relay_trace_entry( struct relay_descr
*descr
,
334 unsigned int idx
, const INT_PTR
*stack
)
336 WORD ordinal
= LOWORD(idx
);
337 BYTE nb_args
= LOBYTE(HIWORD(idx
));
338 struct relay_private_data
*data
= descr
->private;
339 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
343 if (TRACE_ON(timestamp
)) print_timestamp();
346 DPRINTF( "%04x:", GetCurrentProcessId() );
348 if (entry_point
->name
)
349 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
351 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
352 RELAY_PrintArgs( stack
+ 1, nb_args
, descr
->arg_types
[ordinal
] );
353 DPRINTF( ") ret=%08lx\n", stack
[0] );
355 return entry_point
->orig_func
;
358 /***********************************************************************
361 DECLSPEC_HIDDEN
void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
362 const INT_PTR
*stack
, LONGLONG retval
)
364 WORD ordinal
= LOWORD(idx
);
365 BYTE flags
= HIBYTE(HIWORD(idx
));
366 struct relay_private_data
*data
= descr
->private;
367 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
369 if (!TRACE_ON(relay
)) return;
371 if (TRACE_ON(timestamp
)) print_timestamp();
374 DPRINTF( "%04x:", GetCurrentProcessId() );
376 if (entry_point
->name
)
377 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
379 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
381 if (flags
& 1) /* 64-bit return value */
382 DPRINTF( " retval=%08x%08x ret=%08lx\n",
383 (UINT
)(retval
>> 32), (UINT
)retval
, stack
[0] );
385 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR
)retval
, stack
[0] );
390 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
391 __ASM_GLOBAL_FUNC( call_entry_point
,
393 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
394 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
396 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
398 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
400 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
401 "movl 12(%ebp),%edx\n\t"
406 "movl 12(%ebp),%ecx\n\t"
407 "movl 16(%ebp),%esi\n\t"
411 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
414 "1:\tcall *8(%ebp)\n\t"
415 "leal -8(%ebp),%esp\n\t"
417 __ASM_CFI(".cfi_same_value %edi\n\t")
419 __ASM_CFI(".cfi_same_value %esi\n\t")
421 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
422 __ASM_CFI(".cfi_same_value %ebp\n\t")
425 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
426 __ASM_GLOBAL_FUNC( relay_call
,
428 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
429 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
431 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
433 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
435 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
437 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
438 /* trace the parameters */
442 "call " __ASM_NAME("relay_trace_entry") "\n\t"
443 /* copy the arguments*/
444 "movzbl 14(%ebp),%ecx\n\t" /* number of args */
446 "leal 0(,%ecx,4),%edx\n\t"
449 "movl 16(%ebp),%esi\n\t"
454 "testb $2,15(%ebp)\n\t" /* (flags & 2) -> thiscall */
457 /* call the entry point */
461 /* trace the return value */
462 "leal -20(%ebp),%esp\n\t"
468 "call " __ASM_NAME("relay_trace_exit") "\n\t"
469 /* restore return value and return */
470 "leal -12(%ebp),%esp\n\t"
474 __ASM_CFI(".cfi_same_value %ecx\n\t")
476 __ASM_CFI(".cfi_same_value %edi\n\t")
478 __ASM_CFI(".cfi_same_value %esi\n\t")
480 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
481 __ASM_CFI(".cfi_same_value %ebp\n\t")
484 #elif defined(__arm__)
486 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
487 __ASM_GLOBAL_FUNC( call_entry_point
,
489 "push {r4, r5, LR}\n\t"
497 "subeq SP, SP, #4\n\t"
498 "1:\tsub r3, r3, #4\n\t"
499 "ldr r0, [r2, r3]\n\t"
500 "str r0, [SP, r3]\n\t"
515 "4:\tpop {r0-r3}\n\t"
520 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
522 BYTE nb_args
= LOBYTE(HIWORD(idx
));
523 BYTE flags
= HIBYTE(HIWORD(idx
));
524 void *func
= relay_trace_entry( descr
, idx
, stack
);
525 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
+ 1, flags
);
526 relay_trace_exit( descr
, idx
, stack
, ret
);
530 #elif defined(__aarch64__)
532 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
533 __ASM_GLOBAL_FUNC( call_entry_point
,
534 "stp x29, x30, [SP,#-16]!\n\t"
535 "stp x19, x20, [SP,#-16]!\n\t"
538 "ldp x8, x9, [x19, #-32]\n\t"
544 "ldp x0, x1, [x11], #16\n\t"
545 "add w12, w12, #2\n\t"
548 "ldp x2, x3, [x11], #16\n\t"
549 "add w12, w12, #2\n\t"
552 "ldp x4, x5, [x11], #16\n\t"
553 "add w12, w12, #2\n\t"
556 "ldp x6, x7, [x11], #16\n\t"
557 "add w12, w12, #2\n\t"
560 "sub w12, w10, #8\n\t"
561 "lsl w12, w12, #3\n\t"
562 "sub SP, SP, w12, uxtw\n\t"
563 "tbz w12, #3, 1f\n\t"
565 "1: sub w12, w12, #8\n\t"
566 "ldr x13, [x11, x12]\n\t"
567 "str x13, [SP, x12]\n\t"
571 "ldp x19, x20, [SP], #16\n\t"
572 "ldp x29, x30, [SP], #16\n\t"
575 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
577 BYTE nb_args
= LOBYTE(HIWORD(idx
));
578 BYTE flags
= HIBYTE(HIWORD(idx
));
579 void *func
= relay_trace_entry( descr
, idx
, stack
+ 3 );
580 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
+ 4, flags
);
581 relay_trace_exit( descr
, idx
, stack
+ 3, ret
);
585 #elif defined(__x86_64__)
587 extern void * WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
588 __ASM_GLOBAL_FUNC( relay_call
,
590 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
591 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
593 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
594 "subq $0x30,%rsp\n\t"
595 "movq %rsi,0x20(%rsp)\n\t"
596 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
597 "movq %rdi,0x28(%rsp)\n\t"
598 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
599 /* trace the parameters */
600 "movq %rcx,0x10(%rbp)\n\t"
601 "movq %rdx,0x18(%rbp)\n\t"
602 "movq %r8,0x20(%rbp)\n\t"
603 "call " __ASM_NAME("relay_trace_entry") "\n\t"
604 /* copy the arguments */
605 "movzbq 0x1a(%rbp),%rdx\n\t" /* number of args */
608 "cmovgq %rdx,%rcx\n\t"
609 "leaq -16(,%rcx,8),%rdx\n\t"
612 "movq 0x20(%rbp),%r8\n\t" /* original stack */
613 "leaq 8(%r8),%rsi\n\t"
616 /* call the entry point */
617 "movq 0(%rsp),%rcx\n\t"
618 "movq 8(%rsp),%rdx\n\t"
619 "movq 16(%rsp),%r8\n\t"
620 "movq 24(%rsp),%r9\n\t"
621 "movq 0(%rsp),%xmm0\n\t"
622 "movq 8(%rsp),%xmm1\n\t"
623 "movq 16(%rsp),%xmm2\n\t"
624 "movq 24(%rsp),%xmm3\n\t"
626 /* trace the return value */
627 "leaq -0x30(%rbp),%rsp\n\t"
628 "movq 0x10(%rbp),%rcx\n\t"
629 "movq 0x18(%rbp),%rdx\n\t"
630 "movq 0x20(%rbp),%r8\n\t"
632 "movaps %xmm0,0x10(%rbp)\n\t"
634 "call " __ASM_NAME("relay_trace_exit") "\n\t"
635 /* restore return value and return */
637 "movaps 0x10(%rbp),%xmm0\n\t"
638 "movq 0x20(%rsp),%rsi\n\t"
639 __ASM_CFI(".cfi_same_value %rsi\n\t")
640 "movq 0x28(%rsp),%rdi\n\t"
641 __ASM_CFI(".cfi_same_value %rdi\n\t")
643 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
645 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
646 __ASM_CFI(".cfi_same_value %rbp\n\t")
650 #error Not supported on this CPU
654 /***********************************************************************
655 * RELAY_GetProcAddress
657 * Return the proc address to use for a given function.
659 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
660 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
662 struct relay_private_data
*data
;
663 const struct relay_descr
*descr
= (const struct relay_descr
*)((const char *)exports
+ exp_size
);
665 if (descr
->magic
!= RELAY_DESCR_MAGIC
|| !(data
= descr
->private)) return proc
; /* no relay data */
666 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
667 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
668 return proc
; /* we want to relay it */
669 return data
->entry_points
[ordinal
].orig_func
;
673 /***********************************************************************
676 * Setup relay debugging for a built-in dll.
678 void RELAY_SetupDLL( HMODULE module
)
680 IMAGE_EXPORT_DIRECTORY
*exports
;
683 DWORD size
, entry_point_rva
;
684 struct relay_descr
*descr
;
685 struct relay_private_data
*data
;
688 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
690 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
691 if (!exports
) return;
693 descr
= (struct relay_descr
*)((char *)exports
+ size
);
694 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return;
696 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
697 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
700 descr
->relay_call
= relay_call
;
701 descr
->private = data
;
703 data
->module
= module
;
704 data
->base
= exports
->Base
;
705 len
= strlen( (char *)module
+ exports
->Name
);
706 if (len
> 4 && !strcasecmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
707 len
= min( len
, sizeof(data
->dllname
) - 1 );
708 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
709 data
->dllname
[len
] = 0;
711 /* fetch name pointer for all entry points and store them in the private structure */
713 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
714 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
716 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
717 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
720 /* patch the functions in the export table to point to the relay thunks */
722 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
723 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
724 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
726 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
727 if (!check_relay_include( data
->dllname
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
728 continue; /* don't include this entry point */
730 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
731 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
735 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
737 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
738 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
743 void RELAY_SetupDLL( HMODULE module
)
747 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
750 /***********************************************************************/
752 /***********************************************************************/
756 WINE_DECLARE_DEBUG_CHANNEL(seh
);
757 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
759 #include "pshpack1.h"
764 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
765 DWORD snoopentry
; /* SNOOP_Entry relative */
772 typedef struct tagSNOOP_DLL
{
777 struct tagSNOOP_DLL
*next
;
784 BYTE lcall
; /* 0xe8 call snoopret relative*/
785 DWORD snoopret
; /* SNOOP_Ret relative */
791 DWORD
*args
; /* saved args across a stdcall */
794 typedef struct tagSNOOP_RETURNENTRIES
{
795 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
796 struct tagSNOOP_RETURNENTRIES
*next
;
797 } SNOOP_RETURNENTRIES
;
801 extern void WINAPI
SNOOP_Entry(void);
802 extern void WINAPI
SNOOP_Return(void);
804 static SNOOP_DLL
*firstdll
;
805 static SNOOP_RETURNENTRIES
*firstrets
;
808 /***********************************************************************
809 * SNOOP_ShowDebugmsgSnoop
811 * Simple function to decide if a particular debugging message is
814 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
816 if (debug_snoop_excludelist
&& check_list( module
, ordinal
, func
, debug_snoop_excludelist
))
818 if (debug_snoop_includelist
&& !check_list( module
, ordinal
, func
, debug_snoop_includelist
))
824 /***********************************************************************
827 * Setup snoop debugging for a native dll.
829 void SNOOP_SetupDLL(HMODULE hmod
)
831 SNOOP_DLL
**dll
= &firstdll
;
836 IMAGE_EXPORT_DIRECTORY
*exports
;
838 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
840 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
841 if (!exports
|| !exports
->NumberOfFunctions
) return;
842 name
= (char *)hmod
+ exports
->Name
;
845 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
848 if ((*dll
)->hmod
== hmod
)
850 /* another dll, loaded at the same address */
852 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
853 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
856 dll
= &((*dll
)->next
);
859 *dll
= RtlReAllocateHeap(GetProcessHeap(),
860 HEAP_ZERO_MEMORY
, *dll
,
861 sizeof(SNOOP_DLL
) + strlen(name
));
863 *dll
= RtlAllocateHeap(GetProcessHeap(),
865 sizeof(SNOOP_DLL
) + strlen(name
));
867 (*dll
)->ordbase
= exports
->Base
;
868 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
869 strcpy( (*dll
)->name
, name
);
870 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
871 if (p
> (*dll
)->name
&& !strcasecmp( p
, ".dll" )) *p
= 0;
873 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
875 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
876 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
878 RtlFreeHeap(GetProcessHeap(),0,*dll
);
879 FIXME("out of memory\n");
883 memset((*dll
)->funs
,0,size
);
887 /***********************************************************************
888 * SNOOP_GetProcAddress
890 * Return the proc address to use for a given function.
892 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
893 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
898 const WORD
*ordinals
;
900 SNOOP_DLL
*dll
= firstdll
;
902 const IMAGE_SECTION_HEADER
*sec
;
904 if (!TRACE_ON(snoop
)) return origfun
;
905 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
906 return origfun
; /* the calling module was explicitly excluded */
908 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
911 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
913 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
914 return origfun
; /* most likely a data reference */
917 if (hmod
== dll
->hmod
)
921 if (!dll
) /* probably internal */
924 /* try to find a name for it */
926 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
927 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
928 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
930 if (ordinals
[i
] == ordinal
)
932 ename
= (const char *)hmod
+ names
[i
];
936 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
938 assert(ordinal
< dll
->nrofordinals
);
939 fun
= dll
->funs
+ ordinal
;
944 fun
->snoopentry
= (char *)SNOOP_Entry
- (char *)(&fun
->snoopentry
+ 1);
945 fun
->origfun
= origfun
;
948 return (FARPROC
)&(fun
->lcall
);
951 static void SNOOP_PrintArg(DWORD x
)
956 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
963 if (s
[i
]<0x20) {nostring
=1;break;}
964 if (s
[i
]>=0x80) {nostring
=1;break;}
967 if (!nostring
&& i
> 5)
968 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
969 else /* try unicode */
975 if (s
[i
]<0x20) {nostring
=1;break;}
976 if (s
[i
]>0x100) {nostring
=1;break;}
979 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
988 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Entry( void **stack
)
991 SNOOP_FUN
*fun
= (SNOOP_FUN
*)((char *)stack
[0] - 5);
992 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
993 SNOOP_RETURNENTRY
*ret
;
996 for (dll
= firstdll
; dll
; dll
= dll
->next
)
997 if (fun
>= dll
->funs
&& fun
< dll
->funs
+ dll
->nrofordinals
) break;
1000 FIXME("entrypoint %p not found\n", fun
);
1003 /* guess cdecl ... */
1004 if (fun
->nrofargs
<0) {
1005 /* Typical cdecl return frame is:
1007 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1008 * (after that 81 C2 xx xx xx xx)
1010 LPBYTE reteip
= stack
[1];
1013 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1014 fun
->nrofargs
=reteip
[2]/4;
1020 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
1021 if (!(*rets
)->entry
[i
].origreturn
)
1023 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
1025 rets
= &((*rets
)->next
);
1031 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1032 MEM_COMMIT
| MEM_RESERVE
,
1033 PAGE_EXECUTE_READWRITE
);
1036 i
= 0; /* entry 0 is free */
1038 ret
= &((*rets
)->entry
[i
]);
1040 ret
->snoopret
= (char *)SNOOP_Return
- (char *)(&ret
->snoopret
+ 1);
1041 ret
->origreturn
= stack
[1];
1042 stack
[1] = &ret
->lcall
;
1045 ret
->ordinal
= fun
- dll
->funs
;
1046 ret
->origESP
= stack
;
1048 stack
[0] = fun
->origfun
;
1050 if (!TRACE_ON(snoop
)) return;
1052 if (TRACE_ON(timestamp
))
1054 if (fun
->name
) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll
->name
,fun
->name
);
1055 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ret
->ordinal
);
1056 if (fun
->nrofargs
>0) {
1057 max
= fun
->nrofargs
; if (max
>16) max
=16;
1060 SNOOP_PrintArg( (DWORD
)stack
[i
+ 2] );
1061 if (i
<fun
->nrofargs
-1) DPRINTF(",");
1063 if (max
!=fun
->nrofargs
)
1065 } else if (fun
->nrofargs
<0) {
1066 DPRINTF("<unknown, check return>");
1067 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1068 0,16*sizeof(DWORD
));
1069 memcpy(ret
->args
, stack
+ 2, sizeof(DWORD
)*16);
1071 DPRINTF(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1074 void WINAPI DECLSPEC_HIDDEN
__regs_SNOOP_Return( void **stack
)
1076 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)((char *)stack
[0] - 5);
1077 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1078 DWORD retval
= (DWORD
)stack
[-1]; /* get saved %eax from the stack */
1080 /* We haven't found out the nrofargs yet. If we called a cdecl
1081 * function it is too late anyway and we can just set '0' (which
1082 * will be the difference between orig and current ESP
1083 * If stdcall -> everything ok.
1085 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1086 ret
->dll
->funs
[ret
->ordinal
].nrofargs
= stack
- ret
->origESP
- 1;
1087 stack
[0] = ret
->origreturn
;
1089 if (!TRACE_ON(snoop
)) {
1090 ret
->origreturn
= NULL
; /* mark as empty */
1094 if (TRACE_ON(timestamp
))
1100 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret
->dll
->name
, fun
->name
);
1102 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1103 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
);
1105 max
= fun
->nrofargs
;
1110 SNOOP_PrintArg(ret
->args
[i
]);
1111 if (i
<max
-1) DPRINTF(",");
1113 DPRINTF(") retval=%08x ret=%08x\n", retval
, (DWORD
)ret
->origreturn
);
1114 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1120 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1121 GetCurrentThreadId(),
1122 ret
->dll
->name
, fun
->name
, retval
, (DWORD
)ret
->origreturn
);
1124 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1125 GetCurrentThreadId(),
1126 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1127 retval
, (DWORD
)ret
->origreturn
);
1129 ret
->origreturn
= NULL
; /* mark as empty */
1132 /* small wrappers that save registers and get the stack pointer */
1133 #define SNOOP_WRAPPER(name) \
1134 __ASM_STDCALL_FUNC( name, 0, \
1136 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1138 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1140 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1141 "leal 12(%esp),%eax\n\t" \
1143 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1144 "call " __ASM_NAME("__regs_" #name) __ASM_STDCALL(4) "\n\t" \
1145 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1147 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1149 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1151 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1154 SNOOP_WRAPPER( SNOOP_Entry
)
1155 SNOOP_WRAPPER( SNOOP_Return
)
1157 #else /* __i386__ */
1159 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1160 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1165 void SNOOP_SetupDLL( HMODULE hmod
)
1167 FIXME("snooping works only on i386 for now.\n");
1170 #endif /* __i386__ */