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__)
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
;
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 void * WINAPI
relay_trace_entry( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
335 WORD ordinal
= LOWORD(idx
);
336 BYTE nb_args
= LOBYTE(HIWORD(idx
));
337 struct relay_private_data
*data
= descr
->private;
338 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
342 if (TRACE_ON(timestamp
)) print_timestamp();
345 DPRINTF( "%04x:", GetCurrentProcessId() );
347 if (entry_point
->name
)
348 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
350 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
351 RELAY_PrintArgs( stack
+ 1, nb_args
, descr
->arg_types
[ordinal
] );
352 DPRINTF( ") ret=%08lx\n", stack
[0] );
354 return entry_point
->orig_func
;
357 /***********************************************************************
360 void WINAPI
relay_trace_exit( struct relay_descr
*descr
, unsigned int idx
,
361 const INT_PTR
*stack
, LONGLONG retval
)
363 WORD ordinal
= LOWORD(idx
);
364 BYTE flags
= HIBYTE(HIWORD(idx
));
365 struct relay_private_data
*data
= descr
->private;
366 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
368 if (!TRACE_ON(relay
)) return;
370 if (TRACE_ON(timestamp
)) print_timestamp();
373 DPRINTF( "%04x:", GetCurrentProcessId() );
375 if (entry_point
->name
)
376 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
378 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
380 if (flags
& 1) /* 64-bit return value */
381 DPRINTF( " retval=%08x%08x ret=%08lx\n",
382 (UINT
)(retval
>> 32), (UINT
)retval
, stack
[0] );
384 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR
)retval
, stack
[0] );
389 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
390 __ASM_GLOBAL_FUNC( call_entry_point
,
392 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
393 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
395 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
397 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
399 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
400 "movl 12(%ebp),%edx\n\t"
405 "movl 12(%ebp),%ecx\n\t"
406 "movl 16(%ebp),%esi\n\t"
410 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
413 "1:\tcall *8(%ebp)\n\t"
414 "leal -8(%ebp),%esp\n\t"
416 __ASM_CFI(".cfi_same_value %edi\n\t")
418 __ASM_CFI(".cfi_same_value %esi\n\t")
420 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
421 __ASM_CFI(".cfi_same_value %ebp\n\t")
424 extern LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
425 __ASM_GLOBAL_FUNC( relay_call
,
427 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
428 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
430 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
432 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
434 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
436 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
437 /* trace the parameters */
441 "call " __ASM_NAME("relay_trace_entry") "\n\t"
442 /* copy the arguments*/
443 "movzbl 14(%ebp),%ecx\n\t" /* number of args */
445 "leal 0(,%ecx,4),%edx\n\t"
448 "movl 16(%ebp),%esi\n\t"
453 "testb $2,15(%ebp)\n\t" /* (flags & 2) -> thiscall */
456 /* call the entry point */
460 /* trace the return value */
461 "leal -20(%ebp),%esp\n\t"
467 "call " __ASM_NAME("relay_trace_exit") "\n\t"
468 /* restore return value and return */
469 "leal -12(%ebp),%esp\n\t"
473 __ASM_CFI(".cfi_same_value %ecx\n\t")
475 __ASM_CFI(".cfi_same_value %edi\n\t")
477 __ASM_CFI(".cfi_same_value %esi\n\t")
479 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
480 __ASM_CFI(".cfi_same_value %ebp\n\t")
483 void WINAPI
__regs_relay_call_regs( struct relay_descr
*descr
, unsigned int idx
,
484 unsigned int orig_eax
, unsigned int ret_addr
,
487 WORD ordinal
= LOWORD(idx
);
488 BYTE nb_args
= LOBYTE(HIWORD(idx
));
489 struct relay_private_data
*data
= descr
->private;
490 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
491 BYTE
*orig_func
= entry_point
->orig_func
;
492 INT_PTR
*args
= (INT_PTR
*)context
->Esp
;
493 INT_PTR args_copy
[32];
495 /* restore the context to what it was before the relay thunk */
496 context
->Eax
= orig_eax
;
497 context
->Eip
= ret_addr
;
498 context
->Esp
+= nb_args
* sizeof(int);
502 if (entry_point
->name
)
503 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
505 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
506 RELAY_PrintArgs( args
, nb_args
, descr
->arg_types
[ordinal
] );
507 DPRINTF( ") ret=%08x\n", ret_addr
);
509 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
510 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
511 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
512 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
513 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
515 assert( orig_func
[0] == 0x68 /* pushl func */ );
516 assert( orig_func
[5] == 0x6a /* pushl args */ );
517 assert( orig_func
[7] == 0xe8 /* call */ );
520 /* now call the real function */
522 memcpy( args_copy
, args
, nb_args
* sizeof(args
[0]) );
523 args_copy
[nb_args
++] = (INT_PTR
)context
; /* append context argument */
525 call_entry_point( orig_func
+ 12 + *(int *)(orig_func
+ 1), nb_args
, args_copy
, 0 );
529 if (entry_point
->name
)
530 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
531 GetCurrentThreadId(), data
->dllname
, entry_point
->name
,
532 context
->Eax
, context
->Eip
);
534 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
535 GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
,
536 context
->Eax
, context
->Eip
);
537 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
538 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
539 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
540 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
541 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
544 extern void WINAPI
relay_call_regs(void);
545 DEFINE_REGS_ENTRYPOINT( relay_call_regs
, 4 )
547 #elif defined(__arm__)
549 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
550 __ASM_GLOBAL_FUNC( call_entry_point
,
552 "push {r4, r5, LR}\n\t"
560 "subeq SP, SP, #4\n\t"
561 "1:\tsub r3, r3, #4\n\t"
562 "ldr r0, [r2, r3]\n\t"
563 "str r0, [SP, r3]\n\t"
578 "4:\tpop {r0-r3}\n\t"
583 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
585 BYTE nb_args
= LOBYTE(HIWORD(idx
));
586 BYTE flags
= HIBYTE(HIWORD(idx
));
587 void *func
= relay_trace_entry( descr
, idx
, stack
);
588 LONGLONG ret
= call_entry_point( func
, nb_args
, stack
+ 1, flags
);
589 relay_trace_exit( descr
, idx
, stack
, ret
);
593 static void WINAPI
relay_call_regs( struct relay_descr
*descr
, INT_PTR idx
, INT_PTR
*stack
)
595 assert(0); /* should never be called */
598 #elif defined(__x86_64__)
600 extern void * WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
);
601 __ASM_GLOBAL_FUNC( relay_call
,
603 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
604 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
606 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
607 "subq $0x30,%rsp\n\t"
608 "movq %rsi,0x20(%rsp)\n\t"
609 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
610 "movq %rdi,0x28(%rsp)\n\t"
611 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
612 /* trace the parameters */
613 "movq %rcx,0x10(%rbp)\n\t"
614 "movq %rdx,0x18(%rbp)\n\t"
615 "movq %r8,0x20(%rbp)\n\t"
616 "call " __ASM_NAME("relay_trace_entry") "\n\t"
617 /* copy the arguments */
618 "movzbq 0x1a(%rbp),%rdx\n\t" /* number of args */
621 "cmovgq %rdx,%rcx\n\t"
622 "leaq -16(,%rcx,8),%rdx\n\t"
625 "movq 0x20(%rbp),%r8\n\t" /* original stack */
626 "leaq 8(%r8),%rsi\n\t"
629 /* call the entry point */
630 "movq 0(%rsp),%rcx\n\t"
631 "movq 8(%rsp),%rdx\n\t"
632 "movq 16(%rsp),%r8\n\t"
633 "movq 24(%rsp),%r9\n\t"
634 "movq 0(%rsp),%xmm0\n\t"
635 "movq 8(%rsp),%xmm1\n\t"
636 "movq 16(%rsp),%xmm2\n\t"
637 "movq 24(%rsp),%xmm3\n\t"
639 /* trace the return value */
640 "leaq -0x30(%rbp),%rsp\n\t"
641 "movq 0x10(%rbp),%rcx\n\t"
642 "movq 0x18(%rbp),%rdx\n\t"
643 "movq 0x20(%rbp),%r8\n\t"
645 "movaps %xmm0,0x10(%rbp)\n\t"
647 "call " __ASM_NAME("relay_trace_exit") "\n\t"
648 /* restore return value and return */
650 "movaps 0x10(%rbp),%xmm0\n\t"
651 "movq 0x20(%rsp),%rsi\n\t"
652 __ASM_CFI(".cfi_same_value %rsi\n\t")
653 "movq 0x28(%rsp),%rdi\n\t"
654 __ASM_CFI(".cfi_same_value %rdi\n\t")
656 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
658 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
659 __ASM_CFI(".cfi_same_value %rbp\n\t")
662 static void WINAPI
relay_call_regs( struct relay_descr
*descr
, INT_PTR idx
, INT_PTR
*stack
)
664 assert(0); /* should never be called */
668 #error Not supported on this CPU
672 /***********************************************************************
673 * RELAY_GetProcAddress
675 * Return the proc address to use for a given function.
677 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
678 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
680 struct relay_private_data
*data
;
681 const struct relay_descr
*descr
= (const struct relay_descr
*)((const char *)exports
+ exp_size
);
683 if (descr
->magic
!= RELAY_DESCR_MAGIC
|| !(data
= descr
->private)) return proc
; /* no relay data */
684 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
685 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
686 return proc
; /* we want to relay it */
687 return data
->entry_points
[ordinal
].orig_func
;
691 /***********************************************************************
694 * Setup relay debugging for a built-in dll.
696 void RELAY_SetupDLL( HMODULE module
)
698 IMAGE_EXPORT_DIRECTORY
*exports
;
701 DWORD size
, entry_point_rva
;
702 struct relay_descr
*descr
;
703 struct relay_private_data
*data
;
706 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
708 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
709 if (!exports
) return;
711 descr
= (struct relay_descr
*)((char *)exports
+ size
);
712 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return;
714 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
715 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
718 descr
->relay_call
= relay_call
;
719 descr
->relay_call_regs
= relay_call_regs
;
720 descr
->private = data
;
722 data
->module
= module
;
723 data
->base
= exports
->Base
;
724 len
= strlen( (char *)module
+ exports
->Name
);
725 if (len
> 4 && !strcasecmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
726 len
= min( len
, sizeof(data
->dllname
) - 1 );
727 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
728 data
->dllname
[len
] = 0;
730 /* fetch name pointer for all entry points and store them in the private structure */
732 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
733 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
735 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
736 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
739 /* patch the functions in the export table to point to the relay thunks */
741 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
742 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
743 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
745 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
746 if (!check_relay_include( data
->dllname
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
747 continue; /* don't include this entry point */
749 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
750 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
754 #else /* __i386__ || __x86_64__ || __arm__ */
756 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
757 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
762 void RELAY_SetupDLL( HMODULE module
)
766 #endif /* __i386__ || __x86_64__ || __arm__ */
769 /***********************************************************************/
771 /***********************************************************************/
775 WINE_DECLARE_DEBUG_CHANNEL(seh
);
776 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
778 #include "pshpack1.h"
783 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
784 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
787 DWORD snoopentry
; /* SNOOP_Entry relative */
794 typedef struct tagSNOOP_DLL
{
799 struct tagSNOOP_DLL
*next
;
806 BYTE lcall
; /* 0xe8 call snoopret relative*/
807 /* NOTE: If you move snoopret OR origreturn fix the relative offset
810 DWORD snoopret
; /* SNOOP_Ret relative */
816 DWORD
*args
; /* saved args across a stdcall */
819 typedef struct tagSNOOP_RETURNENTRIES
{
820 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
821 struct tagSNOOP_RETURNENTRIES
*next
;
822 } SNOOP_RETURNENTRIES
;
826 extern void WINAPI
SNOOP_Entry(void);
827 extern void WINAPI
SNOOP_Return(void);
829 static SNOOP_DLL
*firstdll
;
830 static SNOOP_RETURNENTRIES
*firstrets
;
833 /***********************************************************************
834 * SNOOP_ShowDebugmsgSnoop
836 * Simple function to decide if a particular debugging message is
839 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
841 if (debug_snoop_excludelist
&& check_list( module
, ordinal
, func
, debug_snoop_excludelist
))
843 if (debug_snoop_includelist
&& !check_list( module
, ordinal
, func
, debug_snoop_includelist
))
849 /***********************************************************************
852 * Setup snoop debugging for a native dll.
854 void SNOOP_SetupDLL(HMODULE hmod
)
856 SNOOP_DLL
**dll
= &firstdll
;
861 IMAGE_EXPORT_DIRECTORY
*exports
;
863 RtlRunOnceExecuteOnce( &init_once
, init_debug_lists
, NULL
, NULL
);
865 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
866 if (!exports
|| !exports
->NumberOfFunctions
) return;
867 name
= (char *)hmod
+ exports
->Name
;
870 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
873 if ((*dll
)->hmod
== hmod
)
875 /* another dll, loaded at the same address */
877 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
878 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
881 dll
= &((*dll
)->next
);
884 *dll
= RtlReAllocateHeap(GetProcessHeap(),
885 HEAP_ZERO_MEMORY
, *dll
,
886 sizeof(SNOOP_DLL
) + strlen(name
));
888 *dll
= RtlAllocateHeap(GetProcessHeap(),
890 sizeof(SNOOP_DLL
) + strlen(name
));
892 (*dll
)->ordbase
= exports
->Base
;
893 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
894 strcpy( (*dll
)->name
, name
);
895 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
896 if (p
> (*dll
)->name
&& !strcasecmp( p
, ".dll" )) *p
= 0;
898 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
900 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
901 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
903 RtlFreeHeap(GetProcessHeap(),0,*dll
);
904 FIXME("out of memory\n");
908 memset((*dll
)->funs
,0,size
);
912 /***********************************************************************
913 * SNOOP_GetProcAddress
915 * Return the proc address to use for a given function.
917 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
918 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
923 const WORD
*ordinals
;
925 SNOOP_DLL
*dll
= firstdll
;
927 const IMAGE_SECTION_HEADER
*sec
;
929 if (!TRACE_ON(snoop
)) return origfun
;
930 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
931 return origfun
; /* the calling module was explicitly excluded */
933 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
936 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
938 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
939 return origfun
; /* most likely a data reference */
942 if (hmod
== dll
->hmod
)
946 if (!dll
) /* probably internal */
949 /* try to find a name for it */
951 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
952 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
953 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
955 if (ordinals
[i
] == ordinal
)
957 ename
= (const char *)hmod
+ names
[i
];
961 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
963 assert(ordinal
< dll
->nrofordinals
);
964 fun
= dll
->funs
+ ordinal
;
969 /* NOTE: origreturn struct member MUST come directly after snoopentry */
970 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
971 fun
->origfun
= origfun
;
974 return (FARPROC
)&(fun
->lcall
);
977 static void SNOOP_PrintArg(DWORD x
)
982 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
989 if (s
[i
]<0x20) {nostring
=1;break;}
990 if (s
[i
]>=0x80) {nostring
=1;break;}
993 if (!nostring
&& i
> 5)
994 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
995 else /* try unicode */
1001 if (s
[i
]<0x20) {nostring
=1;break;}
1002 if (s
[i
]>0x100) {nostring
=1;break;}
1005 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
1014 #define CALLER1REF (*(DWORD*)context->Esp)
1016 void WINAPI
__regs_SNOOP_Entry( CONTEXT
*context
)
1018 DWORD ordinal
=0,entry
= context
->Eip
- 5;
1019 SNOOP_DLL
*dll
= firstdll
;
1020 SNOOP_FUN
*fun
= NULL
;
1021 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
1022 SNOOP_RETURNENTRY
*ret
;
1026 if ( ((char*)entry
>=(char*)dll
->funs
) &&
1027 ((char*)entry
<=(char*)(dll
->funs
+dll
->nrofordinals
))
1029 fun
= (SNOOP_FUN
*)entry
;
1030 ordinal
= fun
-dll
->funs
;
1036 FIXME("entrypoint 0x%08x not found\n",entry
);
1039 /* guess cdecl ... */
1040 if (fun
->nrofargs
<0) {
1041 /* Typical cdecl return frame is:
1043 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1044 * (after that 81 C2 xx xx xx xx)
1046 LPBYTE reteip
= (LPBYTE
)CALLER1REF
;
1049 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
1050 fun
->nrofargs
=reteip
[2]/4;
1056 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
1057 if (!(*rets
)->entry
[i
].origreturn
)
1059 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
1061 rets
= &((*rets
)->next
);
1067 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
1068 MEM_COMMIT
| MEM_RESERVE
,
1069 PAGE_EXECUTE_READWRITE
);
1072 memset(*rets
,0,4096);
1073 i
= 0; /* entry 0 is free */
1075 ret
= &((*rets
)->entry
[i
]);
1077 /* NOTE: origreturn struct member MUST come directly after snoopret */
1078 ret
->snoopret
= ((char*)SNOOP_Return
)-(char*)(&ret
->origreturn
);
1079 ret
->origreturn
= (FARPROC
)CALLER1REF
;
1080 CALLER1REF
= (DWORD
)&ret
->lcall
;
1083 ret
->ordinal
= ordinal
;
1084 ret
->origESP
= context
->Esp
;
1086 context
->Eip
= (DWORD
)fun
->origfun
;
1088 if (!TRACE_ON(snoop
)) return;
1090 if (TRACE_ON(timestamp
))
1092 if (fun
->name
) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll
->name
,fun
->name
);
1093 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
);
1094 if (fun
->nrofargs
>0) {
1095 max
= fun
->nrofargs
; if (max
>16) max
=16;
1098 SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
));
1099 if (i
<fun
->nrofargs
-1) DPRINTF(",");
1101 if (max
!=fun
->nrofargs
)
1103 } else if (fun
->nrofargs
<0) {
1104 DPRINTF("<unknown, check return>");
1105 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
1106 0,16*sizeof(DWORD
));
1107 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
1109 DPRINTF(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1113 void WINAPI
__regs_SNOOP_Return( CONTEXT
*context
)
1115 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
1116 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1118 /* We haven't found out the nrofargs yet. If we called a cdecl
1119 * function it is too late anyway and we can just set '0' (which
1120 * will be the difference between orig and current ESP
1121 * If stdcall -> everything ok.
1123 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1124 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
1125 context
->Eip
= (DWORD
)ret
->origreturn
;
1127 if (!TRACE_ON(snoop
)) {
1128 ret
->origreturn
= NULL
; /* mark as empty */
1132 if (TRACE_ON(timestamp
))
1138 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret
->dll
->name
, fun
->name
);
1140 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1141 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
);
1143 max
= fun
->nrofargs
;
1148 SNOOP_PrintArg(ret
->args
[i
]);
1149 if (i
<max
-1) DPRINTF(",");
1151 DPRINTF(") retval=%08x ret=%08x\n",
1152 context
->Eax
,(DWORD
)ret
->origreturn
);
1153 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1159 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1160 GetCurrentThreadId(),
1161 ret
->dll
->name
, fun
->name
, context
->Eax
, (DWORD
)ret
->origreturn
);
1163 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1164 GetCurrentThreadId(),
1165 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1166 context
->Eax
, (DWORD
)ret
->origreturn
);
1168 ret
->origreturn
= NULL
; /* mark as empty */
1171 /* assembly wrappers that save the context */
1172 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry
, 0 )
1173 DEFINE_REGS_ENTRYPOINT( SNOOP_Return
, 0 )
1175 #else /* __i386__ */
1177 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1178 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1183 void SNOOP_SetupDLL( HMODULE hmod
)
1185 FIXME("snooping works only on i386 for now.\n");
1188 #endif /* __i386__ */