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__)
43 struct relay_descr /* descriptor for a module */
45 void *magic; /* signature */
46 void *relay_call; /* functions to call from relay thunks */
47 void *relay_call_regs;
48 void *private; /* reserved for the relay code private data */
49 const char *entry_point_base; /* base address of entry point thunks */
50 const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
51 const unsigned int *arg_types; /* table of argument types for all entry points */
54 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
56 /* private data built at dll load time */
58 struct relay_entry_point
60 void *orig_func; /* original entry point function */
61 const char *name; /* function name (if any) */
64 struct relay_private_data
66 HMODULE module; /* module handle of this dll */
67 unsigned int base; /* ordinal base */
68 char dllname[40]; /* dll name (without .dll extension) */
69 struct relay_entry_point entry_points[1]; /* list of dll entry points */
72 static const WCHAR **debug_relay_excludelist;
73 static const WCHAR **debug_relay_includelist;
74 static const WCHAR **debug_snoop_excludelist;
75 static const WCHAR **debug_snoop_includelist;
76 static const WCHAR **debug_from_relay_excludelist;
77 static const WCHAR **debug_from_relay_includelist;
78 static const WCHAR **debug_from_snoop_excludelist;
79 static const WCHAR **debug_from_snoop_includelist;
81 static BOOL init_done;
83 /* compare an ASCII and a Unicode string without depending on the current codepage */
84 static inline int strcmpAW( const char *strA, const WCHAR *strW )
86 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
87 return (unsigned char)*strA - *strW;
90 /* compare an ASCII and a Unicode string without depending on the current codepage */
91 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
94 for ( ; n > 0; n--, strA++, strW++)
95 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
99 /***********************************************************************
102 * Build a function list from a ';'-separated string.
104 static const WCHAR **build_list( const WCHAR *buffer )
107 const WCHAR *p = buffer;
110 while ((p = strchrW( p, ';' )))
115 /* allocate count+1 pointers, plus the space for a copy of the string */
116 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
117 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
119 WCHAR *str = (WCHAR *)(ret + count + 1);
122 strcpyW( str, buffer );
127 if (!(p = strchrW( p, ';' ))) break;
135 /***********************************************************************
138 * Load a function list from a registry value.
140 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
142 char initial_buffer[4096];
143 char *buffer = initial_buffer;
147 const WCHAR **list = NULL;
149 RtlInitUnicodeString( &name, value );
150 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count );
151 if (status == STATUS_BUFFER_OVERFLOW)
153 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
154 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
156 if (status == STATUS_SUCCESS)
158 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
159 list = build_list( str );
160 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
163 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
167 /***********************************************************************
170 * Build the relay include/exclude function lists.
172 static void init_debug_lists(void)
174 OBJECT_ATTRIBUTES attr;
177 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
178 'W','i','n','e','\\',
179 'D','e','b','u','g',0};
180 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
181 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
182 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
183 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
184 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
185 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
186 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
187 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
189 if (init_done) return;
192 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
193 attr.Length = sizeof(attr);
194 attr.RootDirectory = root;
195 attr.ObjectName = &name;
197 attr.SecurityDescriptor = NULL;
198 attr.SecurityQualityOfService = NULL;
199 RtlInitUnicodeString( &name, configW );
201 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
202 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
206 debug_relay_includelist = load_list( hkey, RelayIncludeW );
207 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
208 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
209 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
210 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
211 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
212 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
213 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
219 /***********************************************************************
222 * Check if a given module and function is in the list.
224 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
228 sprintf( ord_str, "%d", ordinal );
231 const WCHAR *p = strrchrW( *list, '.' );
232 if (p && p > *list) /* check module and function */
235 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
236 if (p[1] == '*' && !p[2]) return TRUE;
237 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
238 if (func && !strcmpAW( func, p + 1 )) return TRUE;
240 else /* function only */
242 if (func && !strcmpAW( func, *list )) return TRUE;
249 /***********************************************************************
250 * check_relay_include
252 * Check if a given function must be included in the relay output.
254 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
256 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
258 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
263 /***********************************************************************
266 * Check if calls from a given module must be included in the relay/snoop output,
267 * given the exclusion and inclusion lists.
269 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
271 static const WCHAR dllW[] = {'.','d','l','l',0 };
272 const WCHAR **listitem;
275 if (!module) return TRUE;
276 if (!includelist && !excludelist) return TRUE;
280 listitem = excludelist;
285 listitem = includelist;
287 for(; *listitem; listitem++)
291 if (!strcmpiW( *listitem, module )) return !show;
292 len = strlenW( *listitem );
293 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
299 /***********************************************************************
302 static inline void RELAY_PrintArgs( const INT_PTR *args, int nb_args, unsigned int typemask )
306 if ((typemask & 3) && HIWORD(*args))
309 DPRINTF( "%08lx %s", *args, debugstr_w((LPCWSTR)*args) );
311 DPRINTF( "%08lx %s", *args, debugstr_a((LPCSTR)*args) );
313 else DPRINTF( "%08lx", *args );
314 if (nb_args) DPRINTF( "," );
320 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
322 __ASM_GLOBAL_FUNC( call_entry_point,
327 "\tmovl 12(%ebp),%edx\n"
332 "\tmovl 12(%ebp),%ecx\n"
333 "\tmovl 16(%ebp),%esi\n"
337 "1:\tcall *8(%ebp)\n"
338 "\tleal -8(%ebp),%esp\n"
344 __ASM_GLOBAL_FUNC( call_entry_point,
352 "\tcmovgq %rdx,%rcx\n"
353 "\tleaq 0(,%rcx,8),%rdx\n"
359 "\tmovq 0(%rsp),%rcx\n"
360 "\tmovq 8(%rsp),%rdx\n"
361 "\tmovq 16(%rsp),%r8\n"
362 "\tmovq 24(%rsp),%r9\n"
364 "\tleaq -16(%rbp),%rsp\n"
372 /***********************************************************************
375 * stack points to the return address, i.e. the first argument is stack[1].
377 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
380 WORD ordinal = LOWORD(idx);
381 BYTE nb_args = LOBYTE(HIWORD(idx));
382 BYTE flags = HIBYTE(HIWORD(idx));
383 struct relay_private_data *data = descr->private;
384 struct relay_entry_point *entry_point = data->entry_points + ordinal;
386 if (!TRACE_ON(relay))
387 ret = call_entry_point( entry_point->orig_func, nb_args, stack + 1 );
390 if (entry_point->name)
391 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
393 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
394 RELAY_PrintArgs( stack + 1, nb_args, descr->arg_types[ordinal] );
395 DPRINTF( ") ret=%08lx\n", stack[0] );
397 ret = call_entry_point( entry_point->orig_func, nb_args, stack + 1 );
399 if (entry_point->name)
400 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data->dllname, entry_point->name );
402 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data->dllname, data->base + ordinal );
404 if (flags & 1) /* 64-bit return value */
405 DPRINTF( " retval=%08x%08x ret=%08lx\n",
406 (UINT)(ret >> 32), (UINT)ret, stack[0] );
408 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR)ret, stack[0] );
414 /***********************************************************************
418 void WINAPI __regs_relay_call_regs( struct relay_descr *descr, unsigned int idx,
419 unsigned int orig_eax, unsigned int ret_addr,
422 WORD ordinal = LOWORD(idx);
423 BYTE nb_args = LOBYTE(HIWORD(idx));
424 struct relay_private_data *data = descr->private;
425 struct relay_entry_point *entry_point = data->entry_points + ordinal;
426 BYTE *orig_func = entry_point->orig_func;
427 INT_PTR *args = (INT_PTR *)context->Esp;
428 INT_PTR args_copy[32];
430 /* restore the context to what it was before the relay thunk */
431 context->Eax = orig_eax;
432 context->Eip = ret_addr;
433 context->Esp += nb_args * sizeof(int);
437 if (entry_point->name)
438 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
440 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
441 RELAY_PrintArgs( args, nb_args, descr->arg_types[ordinal] );
442 DPRINTF( ") ret=%08x\n", ret_addr );
444 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
445 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
446 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
447 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
448 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
450 assert( orig_func[0] == 0x68 /* pushl func */ );
451 assert( orig_func[5] == 0x6a /* pushl args */ );
452 assert( orig_func[7] == 0xe8 /* call */ );
455 /* now call the real function */
457 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
458 args_copy[nb_args++] = (INT_PTR)context; /* append context argument */
460 call_entry_point( orig_func + 12 + *(int *)(orig_func + 1), nb_args, args_copy );
465 if (entry_point->name)
466 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
467 GetCurrentThreadId(), data->dllname, entry_point->name,
468 context->Eax, context->Eip );
470 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
471 GetCurrentThreadId(), data->dllname, data->base + ordinal,
472 context->Eax, context->Eip );
473 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
474 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
475 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
476 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
477 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
480 extern void WINAPI relay_call_regs(void);
481 DEFINE_REGS_ENTRYPOINT( relay_call_regs, 4 )
485 void WINAPI __regs_relay_call_regs( struct relay_descr *descr, INT_PTR idx,
486 INT_PTR *stack, CONTEXT *context )
488 WORD ordinal = LOWORD(idx);
489 BYTE nb_args = LOBYTE(HIWORD(idx));
490 struct relay_private_data *data = descr->private;
491 struct relay_entry_point *entry_point = data->entry_points + ordinal;
492 BYTE *orig_func = entry_point->orig_func;
493 INT_PTR *args = stack + 1;
494 INT_PTR args_copy[32];
496 /* restore the context to what it was before the relay thunk */
497 context->Rip = stack[0];
498 context->Rsp = (INT_PTR)args;
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=%08lx\n", context->Rip );
509 DPRINTF( "%04x: rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
510 GetCurrentThreadId(), context->Rax, context->Rbx, context->Rcx, context->Rdx,
511 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
512 DPRINTF( "%04x: r8=%016lx r9=%016lx r10=%016lx r11=%016lx r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
513 GetCurrentThreadId(), context->R8, context->R9, context->R10, context->R11,
514 context->R12, context->R13, context->R14, context->R15 );
516 assert( orig_func[17] == 0x48 /* leaq */ );
517 assert( orig_func[18] == 0x8d );
518 assert( orig_func[19] == 0x15 );
519 assert( orig_func[24] == 0xe8 /* call */ );
522 /* now call the real function */
524 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
525 args_copy[nb_args++] = (INT_PTR)context; /* append context argument */
527 call_entry_point( orig_func + 24 + *(int *)(orig_func + 20), nb_args, args_copy );
532 if (entry_point->name)
533 DPRINTF( "%04x:Ret %s.%s() retval=%08lx ret=%08lx\n",
534 GetCurrentThreadId(), data->dllname, entry_point->name,
535 context->Rax, context->Rip );
537 DPRINTF( "%04x:Ret %s.%u() retval=%08lx ret=%08lx\n",
538 GetCurrentThreadId(), data->dllname, data->base + ordinal,
539 context->Rax, context->Rip );
540 DPRINTF( "%04x: rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
541 GetCurrentThreadId(), context->Rax, context->Rbx, context->Rcx, context->Rdx,
542 context->Rsi, context->Rdi, context->Rbp, context->Rsp );
543 DPRINTF( "%04x: r8=%016lx r9=%016lx r10=%016lx r11=%016lx r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
544 GetCurrentThreadId(), context->R8, context->R9, context->R10, context->R11,
545 context->R12, context->R13, context->R14, context->R15 );
548 extern void WINAPI relay_call_regs(void);
549 DEFINE_REGS_ENTRYPOINT( relay_call_regs, 3 )
551 #endif /* __i386__ */
554 /***********************************************************************
555 * RELAY_GetProcAddress
557 * Return the proc address to use for a given function.
559 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
560 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
562 struct relay_private_data *data;
563 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
565 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
566 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
567 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
568 return proc; /* we want to relay it */
569 return data->entry_points[ordinal].orig_func;
573 /***********************************************************************
576 * Setup relay debugging for a built-in dll.
578 void RELAY_SetupDLL( HMODULE module )
580 IMAGE_EXPORT_DIRECTORY *exports;
583 DWORD size, entry_point_rva;
584 struct relay_descr *descr;
585 struct relay_private_data *data;
588 if (!init_done) init_debug_lists();
590 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
591 if (!exports) return;
593 descr = (struct relay_descr *)((char *)exports + size);
594 if (descr->magic != RELAY_DESCR_MAGIC) return;
596 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
597 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
600 descr->relay_call = relay_call;
601 descr->relay_call_regs = relay_call_regs;
602 descr->private = data;
604 data->module = module;
605 data->base = exports->Base;
606 len = strlen( (char *)module + exports->Name );
607 if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
608 len = min( len, sizeof(data->dllname) - 1 );
609 memcpy( data->dllname, (char *)module + exports->Name, len );
610 data->dllname[len] = 0;
612 /* fetch name pointer for all entry points and store them in the private structure */
614 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
615 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
617 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
618 data->entry_points[*ordptr].name = (const char *)module + name_rva;
621 /* patch the functions in the export table to point to the relay thunks */
623 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
624 entry_point_rva = descr->entry_point_base - (const char *)module;
625 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
627 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
628 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
629 continue; /* don't include this entry point */
631 data->entry_points[i].orig_func = (char *)module + *funcs;
632 *funcs = entry_point_rva + descr->entry_point_offsets[i];
636 #else /* __i386__ || __x86_64__ */
638 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
639 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
644 void RELAY_SetupDLL( HMODULE module )
648 #endif /* __i386__ || __x86_64__ */
651 /***********************************************************************/
653 /***********************************************************************/
657 WINE_DECLARE_DEBUG_CHANNEL(seh);
658 WINE_DECLARE_DEBUG_CHANNEL(snoop);
660 #include "pshpack1.h"
665 BYTE lcall; /* 0xe8 call snoopentry (relative) */
666 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
669 DWORD snoopentry; /* SNOOP_Entry relative */
676 typedef struct tagSNOOP_DLL {
681 struct tagSNOOP_DLL *next;
688 BYTE lcall; /* 0xe8 call snoopret relative*/
689 /* NOTE: If you move snoopret OR origreturn fix the relative offset
692 DWORD snoopret; /* SNOOP_Ret relative */
698 DWORD *args; /* saved args across a stdcall */
701 typedef struct tagSNOOP_RETURNENTRIES {
702 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
703 struct tagSNOOP_RETURNENTRIES *next;
704 } SNOOP_RETURNENTRIES;
708 extern void WINAPI SNOOP_Entry(void);
709 extern void WINAPI SNOOP_Return(void);
711 static SNOOP_DLL *firstdll;
712 static SNOOP_RETURNENTRIES *firstrets;
715 /***********************************************************************
716 * SNOOP_ShowDebugmsgSnoop
718 * Simple function to decide if a particular debugging message is
721 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
723 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
725 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
731 /***********************************************************************
734 * Setup snoop debugging for a native dll.
736 void SNOOP_SetupDLL(HMODULE hmod)
738 SNOOP_DLL **dll = &firstdll;
743 IMAGE_EXPORT_DIRECTORY *exports;
745 if (!init_done) init_debug_lists();
747 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
748 if (!exports || !exports->NumberOfFunctions) return;
749 name = (char *)hmod + exports->Name;
752 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
755 if ((*dll)->hmod == hmod)
757 /* another dll, loaded at the same address */
759 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
760 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
763 dll = &((*dll)->next);
766 *dll = RtlReAllocateHeap(GetProcessHeap(),
767 HEAP_ZERO_MEMORY, *dll,
768 sizeof(SNOOP_DLL) + strlen(name));
770 *dll = RtlAllocateHeap(GetProcessHeap(),
772 sizeof(SNOOP_DLL) + strlen(name));
774 (*dll)->ordbase = exports->Base;
775 (*dll)->nrofordinals = exports->NumberOfFunctions;
776 strcpy( (*dll)->name, name );
777 p = (*dll)->name + strlen((*dll)->name) - 4;
778 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
780 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
782 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
783 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
785 RtlFreeHeap(GetProcessHeap(),0,*dll);
786 FIXME("out of memory\n");
790 memset((*dll)->funs,0,size);
794 /***********************************************************************
795 * SNOOP_GetProcAddress
797 * Return the proc address to use for a given function.
799 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
800 DWORD exp_size, FARPROC origfun, DWORD ordinal,
805 const WORD *ordinals;
807 SNOOP_DLL *dll = firstdll;
809 const IMAGE_SECTION_HEADER *sec;
811 if (!TRACE_ON(snoop)) return origfun;
812 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
813 return origfun; /* the calling module was explicitly excluded */
815 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
818 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
820 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
821 return origfun; /* most likely a data reference */
824 if (hmod == dll->hmod)
828 if (!dll) /* probably internal */
831 /* try to find a name for it */
833 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
834 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
835 if (names) for (i = 0; i < exports->NumberOfNames; i++)
837 if (ordinals[i] == ordinal)
839 ename = (const char *)hmod + names[i];
843 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
845 assert(ordinal < dll->nrofordinals);
846 fun = dll->funs + ordinal;
851 /* NOTE: origreturn struct member MUST come directly after snoopentry */
852 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
853 fun->origfun = origfun;
856 return (FARPROC)&(fun->lcall);
859 static void SNOOP_PrintArg(DWORD x)
864 if (!HIWORD(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
871 if (s[i]<0x20) {nostring=1;break;}
872 if (s[i]>=0x80) {nostring=1;break;}
875 if (!nostring && i > 5)
876 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
877 else /* try unicode */
883 if (s[i]<0x20) {nostring=1;break;}
884 if (s[i]>0x100) {nostring=1;break;}
887 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
896 #define CALLER1REF (*(DWORD*)context->Esp)
898 void WINAPI __regs_SNOOP_Entry( CONTEXT86 *context )
900 DWORD ordinal=0,entry = context->Eip - 5;
901 SNOOP_DLL *dll = firstdll;
902 SNOOP_FUN *fun = NULL;
903 SNOOP_RETURNENTRIES **rets = &firstrets;
904 SNOOP_RETURNENTRY *ret;
908 if ( ((char*)entry>=(char*)dll->funs) &&
909 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
911 fun = (SNOOP_FUN*)entry;
912 ordinal = fun-dll->funs;
918 FIXME("entrypoint 0x%08x not found\n",entry);
921 /* guess cdecl ... */
922 if (fun->nrofargs<0) {
923 /* Typical cdecl return frame is:
925 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
926 * (after that 81 C2 xx xx xx xx)
928 LPBYTE reteip = (LPBYTE)CALLER1REF;
931 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
932 fun->nrofargs=reteip[2]/4;
938 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
939 if (!(*rets)->entry[i].origreturn)
941 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
943 rets = &((*rets)->next);
949 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
950 MEM_COMMIT | MEM_RESERVE,
951 PAGE_EXECUTE_READWRITE);
954 memset(*rets,0,4096);
955 i = 0; /* entry 0 is free */
957 ret = &((*rets)->entry[i]);
959 /* NOTE: origreturn struct member MUST come directly after snoopret */
960 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
961 ret->origreturn = (FARPROC)CALLER1REF;
962 CALLER1REF = (DWORD)&ret->lcall;
965 ret->ordinal = ordinal;
966 ret->origESP = context->Esp;
968 context->Eip = (DWORD)fun->origfun;
970 if (fun->name) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
971 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
972 if (fun->nrofargs>0) {
973 max = fun->nrofargs; if (max>16) max=16;
976 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
977 if (i<fun->nrofargs-1) DPRINTF(",");
979 if (max!=fun->nrofargs)
981 } else if (fun->nrofargs<0) {
982 DPRINTF("<unknown, check return>");
983 ret->args = RtlAllocateHeap(GetProcessHeap(),
985 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
987 DPRINTF(") ret=%08x\n",(DWORD)ret->origreturn);
991 void WINAPI __regs_SNOOP_Return( CONTEXT86 *context )
993 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
994 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
996 /* We haven't found out the nrofargs yet. If we called a cdecl
997 * function it is too late anyway and we can just set '0' (which
998 * will be the difference between orig and current ESP
999 * If stdcall -> everything ok.
1001 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1002 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
1003 context->Eip = (DWORD)ret->origreturn;
1008 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1010 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1011 ret->dll->name,ret->dll->ordbase+ret->ordinal);
1013 max = fun->nrofargs;
1018 SNOOP_PrintArg(ret->args[i]);
1019 if (i<max-1) DPRINTF(",");
1021 DPRINTF(") retval=%08x ret=%08x\n",
1022 context->Eax,(DWORD)ret->origreturn );
1023 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1029 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1030 GetCurrentThreadId(),
1031 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
1033 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1034 GetCurrentThreadId(),
1035 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1036 context->Eax, (DWORD)ret->origreturn);
1038 ret->origreturn = NULL; /* mark as empty */
1041 /* assembly wrappers that save the context */
1042 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry, 0 )
1043 DEFINE_REGS_ENTRYPOINT( SNOOP_Return, 0 )
1045 #else /* __i386__ */
1047 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1048 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1053 void SNOOP_SetupDLL( HMODULE hmod )
1055 FIXME("snooping works only on i386 for now.\n");
1058 #endif /* __i386__ */