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
);
45 struct relay_descr
/* descriptor for a module */
47 void *magic
; /* signature */
48 void *relay_call
; /* functions to call from relay thunks */
49 void *relay_call_regs
;
50 void *private; /* reserved for the relay code private data */
51 const char *entry_point_base
; /* base address of entry point thunks */
52 const unsigned int *entry_point_offsets
; /* offsets of entry points thunks */
53 const unsigned int *arg_types
; /* table of argument types for all entry points */
56 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
57 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
59 /* private data built at dll load time */
61 struct relay_entry_point
63 void *orig_func
; /* original entry point function */
64 const char *name
; /* function name (if any) */
67 struct relay_private_data
69 HMODULE module
; /* module handle of this dll */
70 unsigned int base
; /* ordinal base */
71 char dllname
[40]; /* dll name (without .dll extension) */
72 struct relay_entry_point entry_points
[1]; /* list of dll entry points */
75 static const WCHAR
**debug_relay_excludelist
;
76 static const WCHAR
**debug_relay_includelist
;
77 static const WCHAR
**debug_snoop_excludelist
;
78 static const WCHAR
**debug_snoop_includelist
;
79 static const WCHAR
**debug_from_relay_excludelist
;
80 static const WCHAR
**debug_from_relay_includelist
;
81 static const WCHAR
**debug_from_snoop_excludelist
;
82 static const WCHAR
**debug_from_snoop_includelist
;
84 static BOOL init_done
;
86 /* compare an ASCII and a Unicode string without depending on the current codepage */
87 static inline int strcmpAW( const char *strA
, const WCHAR
*strW
)
89 while (*strA
&& ((unsigned char)*strA
== *strW
)) { strA
++; strW
++; }
90 return (unsigned char)*strA
- *strW
;
93 /* compare an ASCII and a Unicode string without depending on the current codepage */
94 static inline int strncmpiAW( const char *strA
, const WCHAR
*strW
, int n
)
97 for ( ; n
> 0; n
--, strA
++, strW
++)
98 if ((ret
= toupperW((unsigned char)*strA
) - toupperW(*strW
)) || !*strA
) break;
102 /***********************************************************************
105 * Build a function list from a ';'-separated string.
107 static const WCHAR
**build_list( const WCHAR
*buffer
)
110 const WCHAR
*p
= buffer
;
113 while ((p
= strchrW( p
, ';' )))
118 /* allocate count+1 pointers, plus the space for a copy of the string */
119 if ((ret
= RtlAllocateHeap( GetProcessHeap(), 0,
120 (count
+1) * sizeof(WCHAR
*) + (strlenW(buffer
)+1) * sizeof(WCHAR
) )))
122 WCHAR
*str
= (WCHAR
*)(ret
+ count
+ 1);
125 strcpyW( str
, buffer
);
130 if (!(q
= strchrW( q
, ';' ))) break;
138 /***********************************************************************
141 * Load a function list from a registry value.
143 static const WCHAR
**load_list( HKEY hkey
, const WCHAR
*value
)
145 char initial_buffer
[4096];
146 char *buffer
= initial_buffer
;
150 const WCHAR
**list
= NULL
;
152 RtlInitUnicodeString( &name
, value
);
153 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, sizeof(initial_buffer
), &count
);
154 if (status
== STATUS_BUFFER_OVERFLOW
)
156 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, count
);
157 status
= NtQueryValueKey( hkey
, &name
, KeyValuePartialInformation
, buffer
, count
, &count
);
159 if (status
== STATUS_SUCCESS
)
161 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
162 list
= build_list( str
);
163 if (list
) TRACE( "%s = %s\n", debugstr_w(value
), debugstr_w(str
) );
166 if (buffer
!= initial_buffer
) RtlFreeHeap( GetProcessHeap(), 0, buffer
);
170 /***********************************************************************
173 * Build the relay include/exclude function lists.
175 static void init_debug_lists(void)
177 OBJECT_ATTRIBUTES attr
;
180 static const WCHAR configW
[] = {'S','o','f','t','w','a','r','e','\\',
181 'W','i','n','e','\\',
182 'D','e','b','u','g',0};
183 static const WCHAR RelayIncludeW
[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
184 static const WCHAR RelayExcludeW
[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
185 static const WCHAR SnoopIncludeW
[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
186 static const WCHAR SnoopExcludeW
[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
187 static const WCHAR RelayFromIncludeW
[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
188 static const WCHAR RelayFromExcludeW
[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
189 static const WCHAR SnoopFromIncludeW
[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
190 static const WCHAR SnoopFromExcludeW
[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
192 if (init_done
) return;
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;
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
);
222 /***********************************************************************
225 * Check if a given module and function is in the list.
227 static BOOL
check_list( const char *module
, int ordinal
, const char *func
, const WCHAR
*const *list
)
231 sprintf( ord_str
, "%d", ordinal
);
234 const WCHAR
*p
= strrchrW( *list
, '.' );
235 if (p
&& p
> *list
) /* check module and function */
238 if (strncmpiAW( module
, *list
, len
-1 ) || module
[len
]) continue;
239 if (p
[1] == '*' && !p
[2]) return TRUE
;
240 if (!strcmpAW( ord_str
, p
+ 1 )) return TRUE
;
241 if (func
&& !strcmpAW( func
, p
+ 1 )) return TRUE
;
243 else /* function only */
245 if (func
&& !strcmpAW( func
, *list
)) return TRUE
;
252 /***********************************************************************
253 * check_relay_include
255 * Check if a given function must be included in the relay output.
257 static BOOL
check_relay_include( const char *module
, int ordinal
, const char *func
)
259 if (debug_relay_excludelist
&& check_list( module
, ordinal
, func
, debug_relay_excludelist
))
261 if (debug_relay_includelist
&& !check_list( module
, ordinal
, func
, debug_relay_includelist
))
266 /***********************************************************************
269 * Check if calls from a given module must be included in the relay/snoop output,
270 * given the exclusion and inclusion lists.
272 static BOOL
check_from_module( const WCHAR
**includelist
, const WCHAR
**excludelist
, const WCHAR
*module
)
274 static const WCHAR dllW
[] = {'.','d','l','l',0 };
275 const WCHAR
**listitem
;
278 if (!module
) return TRUE
;
279 if (!includelist
&& !excludelist
) return TRUE
;
283 listitem
= excludelist
;
288 listitem
= includelist
;
290 for(; *listitem
; listitem
++)
294 if (!strcmpiW( *listitem
, module
)) return !show
;
295 len
= strlenW( *listitem
);
296 if (!strncmpiW( *listitem
, module
, len
) && !strcmpiW( module
+ len
, dllW
))
302 /***********************************************************************
305 static inline void RELAY_PrintArgs( const INT_PTR
*args
, int nb_args
, unsigned int typemask
)
309 if ((typemask
& 3) && !IS_INTARG(*args
))
312 DPRINTF( "%08lx %s", *args
, debugstr_w((LPCWSTR
)*args
) );
314 DPRINTF( "%08lx %s", *args
, debugstr_a((LPCSTR
)*args
) );
316 else DPRINTF( "%08lx", *args
);
317 if (nb_args
) DPRINTF( "," );
323 extern LONGLONG CDECL
call_entry_point( void *func
, int nb_args
, const INT_PTR
*args
, int flags
);
325 __ASM_GLOBAL_FUNC( call_entry_point
,
327 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
328 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
330 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
332 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
334 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
335 "movl 12(%ebp),%edx\n\t"
340 "movl 12(%ebp),%ecx\n\t"
341 "movl 16(%ebp),%esi\n\t"
345 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
348 "1:\tcall *8(%ebp)\n\t"
349 "leal -8(%ebp),%esp\n\t"
351 __ASM_CFI(".cfi_same_value %edi\n\t")
353 __ASM_CFI(".cfi_same_value %esi\n\t")
355 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
356 __ASM_CFI(".cfi_same_value %ebp\n\t")
358 #elif defined(__arm__)
359 __ASM_GLOBAL_FUNC( call_entry_point
,
360 "push {r4, r5, LR}\n\t"
367 "bic SP, SP, #15\n\t"
368 "1:\tsub r3, r3, #4\n\t"
369 "ldr r0, [r2, r3]\n\t"
370 "str r0, [SP, r3]\n\t"
385 "4:\tpop {r0-r3}\n\t"
390 __ASM_GLOBAL_FUNC( call_entry_point
,
392 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
393 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
395 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
397 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
399 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
403 "cmovgq %rdx,%rcx\n\t"
404 "leaq 0(,%rcx,8),%rdx\n\t"
410 "movq 0(%rsp),%rcx\n\t"
411 "movq 8(%rsp),%rdx\n\t"
412 "movq 16(%rsp),%r8\n\t"
413 "movq 24(%rsp),%r9\n\t"
414 "movq %rcx,%xmm0\n\t"
415 "movq %rdx,%xmm1\n\t"
419 "leaq -16(%rbp),%rsp\n\t"
421 __ASM_CFI(".cfi_same_value %rdi\n\t")
423 __ASM_CFI(".cfi_same_value %rsi\n\t")
424 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
426 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
427 __ASM_CFI(".cfi_same_value %rbp\n\t")
432 static void print_timestamp(void)
434 ULONG ticks
= NtGetTickCount();
435 DPRINTF( "%3u.%03u:", ticks
/ 1000, ticks
% 1000 );
439 /***********************************************************************
442 * stack points to the return address, i.e. the first argument is stack[1].
444 static LONGLONG WINAPI
relay_call( struct relay_descr
*descr
, unsigned int idx
, const INT_PTR
*stack
)
447 WORD ordinal
= LOWORD(idx
);
448 BYTE nb_args
= LOBYTE(HIWORD(idx
));
449 BYTE flags
= HIBYTE(HIWORD(idx
));
450 struct relay_private_data
*data
= descr
->private;
451 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
453 if (!TRACE_ON(relay
))
454 ret
= call_entry_point( entry_point
->orig_func
, nb_args
, stack
+ 1, flags
);
457 if (TRACE_ON(timestamp
))
459 if (entry_point
->name
)
460 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
462 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
463 RELAY_PrintArgs( stack
+ 1, nb_args
, descr
->arg_types
[ordinal
] );
464 DPRINTF( ") ret=%08lx\n", stack
[0] );
466 ret
= call_entry_point( entry_point
->orig_func
, nb_args
, stack
+ 1, flags
);
468 if (TRACE_ON(timestamp
))
470 if (entry_point
->name
)
471 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
473 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
475 if (flags
& 1) /* 64-bit return value */
476 DPRINTF( " retval=%08x%08x ret=%08lx\n",
477 (UINT
)(ret
>> 32), (UINT
)ret
, stack
[0] );
479 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR
)ret
, stack
[0] );
485 /***********************************************************************
489 void WINAPI
__regs_relay_call_regs( struct relay_descr
*descr
, unsigned int idx
,
490 unsigned int orig_eax
, unsigned int ret_addr
,
493 WORD ordinal
= LOWORD(idx
);
494 BYTE nb_args
= LOBYTE(HIWORD(idx
));
495 struct relay_private_data
*data
= descr
->private;
496 struct relay_entry_point
*entry_point
= data
->entry_points
+ ordinal
;
497 BYTE
*orig_func
= entry_point
->orig_func
;
498 INT_PTR
*args
= (INT_PTR
*)context
->Esp
;
499 INT_PTR args_copy
[32];
501 /* restore the context to what it was before the relay thunk */
502 context
->Eax
= orig_eax
;
503 context
->Eip
= ret_addr
;
504 context
->Esp
+= nb_args
* sizeof(int);
508 if (entry_point
->name
)
509 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data
->dllname
, entry_point
->name
);
511 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
);
512 RELAY_PrintArgs( args
, nb_args
, descr
->arg_types
[ordinal
] );
513 DPRINTF( ") ret=%08x\n", ret_addr
);
515 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
516 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
517 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
518 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
519 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
521 assert( orig_func
[0] == 0x68 /* pushl func */ );
522 assert( orig_func
[5] == 0x6a /* pushl args */ );
523 assert( orig_func
[7] == 0xe8 /* call */ );
526 /* now call the real function */
528 memcpy( args_copy
, args
, nb_args
* sizeof(args
[0]) );
529 args_copy
[nb_args
++] = (INT_PTR
)context
; /* append context argument */
531 call_entry_point( orig_func
+ 12 + *(int *)(orig_func
+ 1), nb_args
, args_copy
, 0 );
536 if (entry_point
->name
)
537 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
538 GetCurrentThreadId(), data
->dllname
, entry_point
->name
,
539 context
->Eax
, context
->Eip
);
541 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
542 GetCurrentThreadId(), data
->dllname
, data
->base
+ ordinal
,
543 context
->Eax
, context
->Eip
);
544 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
545 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
546 GetCurrentThreadId(), context
->Eax
, context
->Ebx
, context
->Ecx
,
547 context
->Edx
, context
->Esi
, context
->Edi
, context
->Ebp
, context
->Esp
,
548 context
->SegDs
, context
->SegEs
, context
->SegFs
, context
->SegGs
, context
->EFlags
);
551 extern void WINAPI
relay_call_regs(void);
552 DEFINE_REGS_ENTRYPOINT( relay_call_regs
, 4 )
556 void WINAPI
relay_call_regs( struct relay_descr
*descr
, INT_PTR idx
, INT_PTR
*stack
)
558 assert(0); /* should never be called */
561 #endif /* __i386__ */
564 /***********************************************************************
565 * RELAY_GetProcAddress
567 * Return the proc address to use for a given function.
569 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
570 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
572 struct relay_private_data
*data
;
573 const struct relay_descr
*descr
= (const struct relay_descr
*)((const char *)exports
+ exp_size
);
575 if (descr
->magic
!= RELAY_DESCR_MAGIC
|| !(data
= descr
->private)) return proc
; /* no relay data */
576 if (!data
->entry_points
[ordinal
].orig_func
) return proc
; /* not a relayed function */
577 if (check_from_module( debug_from_relay_includelist
, debug_from_relay_excludelist
, user
))
578 return proc
; /* we want to relay it */
579 return data
->entry_points
[ordinal
].orig_func
;
583 /***********************************************************************
586 * Setup relay debugging for a built-in dll.
588 void RELAY_SetupDLL( HMODULE module
)
590 IMAGE_EXPORT_DIRECTORY
*exports
;
593 DWORD size
, entry_point_rva
;
594 struct relay_descr
*descr
;
595 struct relay_private_data
*data
;
598 if (!init_done
) init_debug_lists();
600 exports
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
);
601 if (!exports
) return;
603 descr
= (struct relay_descr
*)((char *)exports
+ size
);
604 if (descr
->magic
!= RELAY_DESCR_MAGIC
) return;
606 if (!(data
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
) +
607 (exports
->NumberOfFunctions
-1) * sizeof(data
->entry_points
) )))
610 descr
->relay_call
= relay_call
;
611 descr
->relay_call_regs
= relay_call_regs
;
612 descr
->private = data
;
614 data
->module
= module
;
615 data
->base
= exports
->Base
;
616 len
= strlen( (char *)module
+ exports
->Name
);
617 if (len
> 4 && !strcasecmp( (char *)module
+ exports
->Name
+ len
- 4, ".dll" )) len
-= 4;
618 len
= min( len
, sizeof(data
->dllname
) - 1 );
619 memcpy( data
->dllname
, (char *)module
+ exports
->Name
, len
);
620 data
->dllname
[len
] = 0;
622 /* fetch name pointer for all entry points and store them in the private structure */
624 ordptr
= (const WORD
*)((char *)module
+ exports
->AddressOfNameOrdinals
);
625 for (i
= 0; i
< exports
->NumberOfNames
; i
++, ordptr
++)
627 DWORD name_rva
= ((DWORD
*)((char *)module
+ exports
->AddressOfNames
))[i
];
628 data
->entry_points
[*ordptr
].name
= (const char *)module
+ name_rva
;
631 /* patch the functions in the export table to point to the relay thunks */
633 funcs
= (DWORD
*)((char *)module
+ exports
->AddressOfFunctions
);
634 entry_point_rva
= descr
->entry_point_base
- (const char *)module
;
635 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++, funcs
++)
637 if (!descr
->entry_point_offsets
[i
]) continue; /* not a normal function */
638 if (!check_relay_include( data
->dllname
, i
+ exports
->Base
, data
->entry_points
[i
].name
))
639 continue; /* don't include this entry point */
641 data
->entry_points
[i
].orig_func
= (char *)module
+ *funcs
;
642 *funcs
= entry_point_rva
+ descr
->entry_point_offsets
[i
];
646 #else /* __i386__ || __x86_64__ */
648 FARPROC
RELAY_GetProcAddress( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
649 DWORD exp_size
, FARPROC proc
, DWORD ordinal
, const WCHAR
*user
)
654 void RELAY_SetupDLL( HMODULE module
)
658 #endif /* __i386__ || __x86_64__ */
661 /***********************************************************************/
663 /***********************************************************************/
667 WINE_DECLARE_DEBUG_CHANNEL(seh
);
668 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
670 #include "pshpack1.h"
675 BYTE lcall
; /* 0xe8 call snoopentry (relative) */
676 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
679 DWORD snoopentry
; /* SNOOP_Entry relative */
686 typedef struct tagSNOOP_DLL
{
691 struct tagSNOOP_DLL
*next
;
698 BYTE lcall
; /* 0xe8 call snoopret relative*/
699 /* NOTE: If you move snoopret OR origreturn fix the relative offset
702 DWORD snoopret
; /* SNOOP_Ret relative */
708 DWORD
*args
; /* saved args across a stdcall */
711 typedef struct tagSNOOP_RETURNENTRIES
{
712 SNOOP_RETURNENTRY entry
[4092/sizeof(SNOOP_RETURNENTRY
)];
713 struct tagSNOOP_RETURNENTRIES
*next
;
714 } SNOOP_RETURNENTRIES
;
718 extern void WINAPI
SNOOP_Entry(void);
719 extern void WINAPI
SNOOP_Return(void);
721 static SNOOP_DLL
*firstdll
;
722 static SNOOP_RETURNENTRIES
*firstrets
;
725 /***********************************************************************
726 * SNOOP_ShowDebugmsgSnoop
728 * Simple function to decide if a particular debugging message is
731 static BOOL
SNOOP_ShowDebugmsgSnoop(const char *module
, int ordinal
, const char *func
)
733 if (debug_snoop_excludelist
&& check_list( module
, ordinal
, func
, debug_snoop_excludelist
))
735 if (debug_snoop_includelist
&& !check_list( module
, ordinal
, func
, debug_snoop_includelist
))
741 /***********************************************************************
744 * Setup snoop debugging for a native dll.
746 void SNOOP_SetupDLL(HMODULE hmod
)
748 SNOOP_DLL
**dll
= &firstdll
;
753 IMAGE_EXPORT_DIRECTORY
*exports
;
755 if (!init_done
) init_debug_lists();
757 exports
= RtlImageDirectoryEntryToData( hmod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &size32
);
758 if (!exports
|| !exports
->NumberOfFunctions
) return;
759 name
= (char *)hmod
+ exports
->Name
;
762 TRACE_(snoop
)("hmod=%p, name=%s\n", hmod
, name
);
765 if ((*dll
)->hmod
== hmod
)
767 /* another dll, loaded at the same address */
769 size
= (*dll
)->nrofordinals
* sizeof(SNOOP_FUN
);
770 NtFreeVirtualMemory(NtCurrentProcess(), &addr
, &size
, MEM_RELEASE
);
773 dll
= &((*dll
)->next
);
776 *dll
= RtlReAllocateHeap(GetProcessHeap(),
777 HEAP_ZERO_MEMORY
, *dll
,
778 sizeof(SNOOP_DLL
) + strlen(name
));
780 *dll
= RtlAllocateHeap(GetProcessHeap(),
782 sizeof(SNOOP_DLL
) + strlen(name
));
784 (*dll
)->ordbase
= exports
->Base
;
785 (*dll
)->nrofordinals
= exports
->NumberOfFunctions
;
786 strcpy( (*dll
)->name
, name
);
787 p
= (*dll
)->name
+ strlen((*dll
)->name
) - 4;
788 if (p
> (*dll
)->name
&& !strcasecmp( p
, ".dll" )) *p
= 0;
790 size
= exports
->NumberOfFunctions
* sizeof(SNOOP_FUN
);
792 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
793 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
795 RtlFreeHeap(GetProcessHeap(),0,*dll
);
796 FIXME("out of memory\n");
800 memset((*dll
)->funs
,0,size
);
804 /***********************************************************************
805 * SNOOP_GetProcAddress
807 * Return the proc address to use for a given function.
809 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
,
810 DWORD exp_size
, FARPROC origfun
, DWORD ordinal
,
815 const WORD
*ordinals
;
817 SNOOP_DLL
*dll
= firstdll
;
819 const IMAGE_SECTION_HEADER
*sec
;
821 if (!TRACE_ON(snoop
)) return origfun
;
822 if (!check_from_module( debug_from_snoop_includelist
, debug_from_snoop_excludelist
, user
))
823 return origfun
; /* the calling module was explicitly excluded */
825 if (!*(LPBYTE
)origfun
) /* 0x00 is an impossible opcode, possible dataref. */
828 sec
= RtlImageRvaToSection( RtlImageNtHeader(hmod
), hmod
, (char *)origfun
- (char *)hmod
);
830 if (!sec
|| !(sec
->Characteristics
& IMAGE_SCN_CNT_CODE
))
831 return origfun
; /* most likely a data reference */
834 if (hmod
== dll
->hmod
)
838 if (!dll
) /* probably internal */
841 /* try to find a name for it */
843 names
= (const DWORD
*)((const char *)hmod
+ exports
->AddressOfNames
);
844 ordinals
= (const WORD
*)((const char *)hmod
+ exports
->AddressOfNameOrdinals
);
845 if (names
) for (i
= 0; i
< exports
->NumberOfNames
; i
++)
847 if (ordinals
[i
] == ordinal
)
849 ename
= (const char *)hmod
+ names
[i
];
853 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
,ordinal
,ename
))
855 assert(ordinal
< dll
->nrofordinals
);
856 fun
= dll
->funs
+ ordinal
;
861 /* NOTE: origreturn struct member MUST come directly after snoopentry */
862 fun
->snoopentry
= (char*)SNOOP_Entry
-((char*)(&fun
->nrofargs
));
863 fun
->origfun
= origfun
;
866 return (FARPROC
)&(fun
->lcall
);
869 static void SNOOP_PrintArg(DWORD x
)
874 if (IS_INTARG(x
) || TRACE_ON(seh
)) return; /* trivial reject to avoid faults */
881 if (s
[i
]<0x20) {nostring
=1;break;}
882 if (s
[i
]>=0x80) {nostring
=1;break;}
885 if (!nostring
&& i
> 5)
886 DPRINTF(" %s",debugstr_an((LPSTR
)x
,i
));
887 else /* try unicode */
893 if (s
[i
]<0x20) {nostring
=1;break;}
894 if (s
[i
]>0x100) {nostring
=1;break;}
897 if (!nostring
&& i
> 5) DPRINTF(" %s",debugstr_wn((LPWSTR
)x
,i
));
906 #define CALLER1REF (*(DWORD*)context->Esp)
908 void WINAPI
__regs_SNOOP_Entry( CONTEXT
*context
)
910 DWORD ordinal
=0,entry
= context
->Eip
- 5;
911 SNOOP_DLL
*dll
= firstdll
;
912 SNOOP_FUN
*fun
= NULL
;
913 SNOOP_RETURNENTRIES
**rets
= &firstrets
;
914 SNOOP_RETURNENTRY
*ret
;
918 if ( ((char*)entry
>=(char*)dll
->funs
) &&
919 ((char*)entry
<=(char*)(dll
->funs
+dll
->nrofordinals
))
921 fun
= (SNOOP_FUN
*)entry
;
922 ordinal
= fun
-dll
->funs
;
928 FIXME("entrypoint 0x%08x not found\n",entry
);
931 /* guess cdecl ... */
932 if (fun
->nrofargs
<0) {
933 /* Typical cdecl return frame is:
935 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
936 * (after that 81 C2 xx xx xx xx)
938 LPBYTE reteip
= (LPBYTE
)CALLER1REF
;
941 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
942 fun
->nrofargs
=reteip
[2]/4;
948 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
949 if (!(*rets
)->entry
[i
].origreturn
)
951 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
953 rets
= &((*rets
)->next
);
959 NtAllocateVirtualMemory(NtCurrentProcess(), &addr
, 0, &size
,
960 MEM_COMMIT
| MEM_RESERVE
,
961 PAGE_EXECUTE_READWRITE
);
964 memset(*rets
,0,4096);
965 i
= 0; /* entry 0 is free */
967 ret
= &((*rets
)->entry
[i
]);
969 /* NOTE: origreturn struct member MUST come directly after snoopret */
970 ret
->snoopret
= ((char*)SNOOP_Return
)-(char*)(&ret
->origreturn
);
971 ret
->origreturn
= (FARPROC
)CALLER1REF
;
972 CALLER1REF
= (DWORD
)&ret
->lcall
;
975 ret
->ordinal
= ordinal
;
976 ret
->origESP
= context
->Esp
;
978 context
->Eip
= (DWORD
)fun
->origfun
;
980 if (TRACE_ON(timestamp
))
982 if (fun
->name
) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll
->name
,fun
->name
);
983 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll
->name
,dll
->ordbase
+ordinal
);
984 if (fun
->nrofargs
>0) {
985 max
= fun
->nrofargs
; if (max
>16) max
=16;
988 SNOOP_PrintArg(*(DWORD
*)(context
->Esp
+ 4 + sizeof(DWORD
)*i
));
989 if (i
<fun
->nrofargs
-1) DPRINTF(",");
991 if (max
!=fun
->nrofargs
)
993 } else if (fun
->nrofargs
<0) {
994 DPRINTF("<unknown, check return>");
995 ret
->args
= RtlAllocateHeap(GetProcessHeap(),
997 memcpy(ret
->args
,(LPBYTE
)(context
->Esp
+ 4),sizeof(DWORD
)*16);
999 DPRINTF(") ret=%08x\n",(DWORD
)ret
->origreturn
);
1003 void WINAPI
__regs_SNOOP_Return( CONTEXT
*context
)
1005 SNOOP_RETURNENTRY
*ret
= (SNOOP_RETURNENTRY
*)(context
->Eip
- 5);
1006 SNOOP_FUN
*fun
= &ret
->dll
->funs
[ret
->ordinal
];
1008 /* We haven't found out the nrofargs yet. If we called a cdecl
1009 * function it is too late anyway and we can just set '0' (which
1010 * will be the difference between orig and current ESP
1011 * If stdcall -> everything ok.
1013 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
1014 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(context
->Esp
- ret
->origESP
-4)/4;
1015 context
->Eip
= (DWORD
)ret
->origreturn
;
1016 if (TRACE_ON(timestamp
))
1022 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret
->dll
->name
, fun
->name
);
1024 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1025 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
);
1027 max
= fun
->nrofargs
;
1032 SNOOP_PrintArg(ret
->args
[i
]);
1033 if (i
<max
-1) DPRINTF(",");
1035 DPRINTF(") retval=%08x ret=%08x\n",
1036 context
->Eax
,(DWORD
)ret
->origreturn
);
1037 RtlFreeHeap(GetProcessHeap(),0,ret
->args
);
1043 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1044 GetCurrentThreadId(),
1045 ret
->dll
->name
, fun
->name
, context
->Eax
, (DWORD
)ret
->origreturn
);
1047 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1048 GetCurrentThreadId(),
1049 ret
->dll
->name
,ret
->dll
->ordbase
+ret
->ordinal
,
1050 context
->Eax
, (DWORD
)ret
->origreturn
);
1052 ret
->origreturn
= NULL
; /* mark as empty */
1055 /* assembly wrappers that save the context */
1056 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry
, 0 )
1057 DEFINE_REGS_ENTRYPOINT( SNOOP_Return
, 0 )
1059 #else /* __i386__ */
1061 FARPROC
SNOOP_GetProcAddress( HMODULE hmod
, const IMAGE_EXPORT_DIRECTORY
*exports
, DWORD exp_size
,
1062 FARPROC origfun
, DWORD ordinal
, const WCHAR
*user
)
1067 void SNOOP_SetupDLL( HMODULE hmod
)
1069 FIXME("snooping works only on i386 for now.\n");
1072 #endif /* __i386__ */