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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
35 #include "wine/exception.h"
36 #include "ntdll_misc.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(relay);
41 WINE_DECLARE_DEBUG_CHANNEL(snoop);
42 WINE_DECLARE_DEBUG_CHANNEL(seh);
44 static const WCHAR **debug_relay_excludelist;
45 static const WCHAR **debug_relay_includelist;
46 static const WCHAR **debug_snoop_excludelist;
47 static const WCHAR **debug_snoop_includelist;
48 static const WCHAR **debug_from_relay_excludelist;
49 static const WCHAR **debug_from_relay_includelist;
50 static const WCHAR **debug_from_snoop_excludelist;
51 static const WCHAR **debug_from_snoop_includelist;
53 /* compare an ASCII and a Unicode string without depending on the current codepage */
54 inline static int strcmpAW( const char *strA, const WCHAR *strW )
56 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
57 return (unsigned char)*strA - *strW;
60 /* compare an ASCII and a Unicode string without depending on the current codepage */
61 inline static int strncmpiAW( const char *strA, const WCHAR *strW, int n )
64 for ( ; n > 0; n--, strA++, strW++)
65 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
69 /***********************************************************************
72 * Build a function list from a ';'-separated string.
74 static const WCHAR **build_list( const WCHAR *buffer )
77 const WCHAR *p = buffer;
80 while ((p = strchrW( p, ';' )))
85 /* allocate count+1 pointers, plus the space for a copy of the string */
86 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
87 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
89 WCHAR *str = (WCHAR *)(ret + count + 1);
92 strcpyW( str, buffer );
97 if (!(p = strchrW( p, ';' ))) break;
106 /***********************************************************************
107 * RELAY_InitDebugLists
109 * Build the relay include/exclude function lists.
111 void RELAY_InitDebugLists(void)
113 OBJECT_ATTRIBUTES attr;
119 static const WCHAR configW[] = {'M','a','c','h','i','n','e','\\',
120 'S','o','f','t','w','a','r','e','\\',
121 'W','i','n','e','\\',
122 'W','i','n','e','\\',
123 'C','o','n','f','i','g','\\',
124 'D','e','b','u','g',0};
125 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
126 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
127 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
128 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
129 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
130 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
131 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
132 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
134 attr.Length = sizeof(attr);
135 attr.RootDirectory = 0;
136 attr.ObjectName = &name;
138 attr.SecurityDescriptor = NULL;
139 attr.SecurityQualityOfService = NULL;
140 RtlInitUnicodeString( &name, configW );
142 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\Debug */
143 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return;
145 str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
146 RtlInitUnicodeString( &name, RelayIncludeW );
147 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
149 TRACE("RelayInclude = %s\n", debugstr_w(str) );
150 debug_relay_includelist = build_list( str );
153 RtlInitUnicodeString( &name, RelayExcludeW );
154 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
156 TRACE( "RelayExclude = %s\n", debugstr_w(str) );
157 debug_relay_excludelist = build_list( str );
160 RtlInitUnicodeString( &name, SnoopIncludeW );
161 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
163 TRACE_(snoop)( "SnoopInclude = %s\n", debugstr_w(str) );
164 debug_snoop_includelist = build_list( str );
167 RtlInitUnicodeString( &name, SnoopExcludeW );
168 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
170 TRACE_(snoop)( "SnoopExclude = %s\n", debugstr_w(str) );
171 debug_snoop_excludelist = build_list( str );
174 RtlInitUnicodeString( &name, RelayFromIncludeW );
175 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
177 TRACE("RelayFromInclude = %s\n", debugstr_w(str) );
178 debug_from_relay_includelist = build_list( str );
181 RtlInitUnicodeString( &name, RelayFromExcludeW );
182 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
184 TRACE( "RelayFromExclude = %s\n", debugstr_w(str) );
185 debug_from_relay_excludelist = build_list( str );
188 RtlInitUnicodeString( &name, SnoopFromIncludeW );
189 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
191 TRACE_(snoop)("SnoopFromInclude = %s\n", debugstr_w(str) );
192 debug_from_snoop_includelist = build_list( str );
195 RtlInitUnicodeString( &name, SnoopFromExcludeW );
196 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
198 TRACE_(snoop)( "SnoopFromExclude = %s\n", debugstr_w(str) );
199 debug_from_snoop_excludelist = build_list( str );
208 #include "pshpack1.h"
212 BYTE call; /* 0xe8 call callfrom32 (relative) */
213 DWORD callfrom32; /* RELAY_CallFrom32 relative addr */
214 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
215 WORD args; /* nb of args to remove from the stack */
216 void *orig; /* original entry point */
217 DWORD argtypes; /* argument types */
223 BYTE lcall; /* 0xe8 call snoopentry (relative) */
224 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
227 DWORD snoopentry; /* SNOOP_Entry relative */
234 typedef struct tagSNOOP_DLL {
239 struct tagSNOOP_DLL *next;
246 BYTE lcall; /* 0xe8 call snoopret relative*/
247 /* NOTE: If you move snoopret OR origreturn fix the relative offset
250 DWORD snoopret; /* SNOOP_Ret relative */
256 DWORD *args; /* saved args across a stdcall */
259 typedef struct tagSNOOP_RETURNENTRIES {
260 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
261 struct tagSNOOP_RETURNENTRIES *next;
262 } SNOOP_RETURNENTRIES;
266 extern void WINAPI SNOOP_Entry();
267 extern void WINAPI SNOOP_Return();
269 static SNOOP_DLL *firstdll;
270 static SNOOP_RETURNENTRIES *firstrets;
272 static WINE_EXCEPTION_FILTER(page_fault)
274 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
275 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
276 return EXCEPTION_EXECUTE_HANDLER;
277 return EXCEPTION_CONTINUE_SEARCH;
280 /***********************************************************************
283 * Check if a given module and function is in the list.
285 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
289 sprintf( ord_str, "%d", ordinal );
292 const WCHAR *p = strrchrW( *list, '.' );
293 if (p && p > *list) /* check module and function */
296 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
297 if (p[1] == '*' && !p[2]) return TRUE;
298 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
299 if (func && !strcmpAW( func, p + 1 )) return TRUE;
301 else /* function only */
303 if (func && !strcmpAW( func, *list )) return TRUE;
310 /***********************************************************************
311 * check_relay_include
313 * Check if a given function must be included in the relay output.
315 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
317 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
319 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
324 /***********************************************************************
327 * Check if calls from a given module must be included in the relay/snoop output,
328 * given the exclusion and inclusion lists.
330 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
332 static const WCHAR dllW[] = {'.','d','l','l',0 };
333 const WCHAR **listitem;
336 if (!module) return TRUE;
337 if (!includelist && !excludelist) return TRUE;
341 listitem = excludelist;
346 listitem = includelist;
348 for(; *listitem; listitem++)
352 if (!strcmpiW( *listitem, module )) return !show;
353 len = strlenW( *listitem );
354 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
360 /***********************************************************************
363 * Find the name of an exported function.
365 static const char *find_exported_name( HMODULE module,
366 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
369 const char *ret = NULL;
371 WORD *ordptr = (WORD *)((char *)module + exp->AddressOfNameOrdinals);
372 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
373 if (*ordptr + exp->Base == ordinal) break;
374 if (i < exp->NumberOfNames)
375 ret = (char *)module + ((DWORD*)((char *)module + exp->AddressOfNames))[i];
380 /***********************************************************************
383 * Get the name of the DLL entry point corresponding to a relay address.
385 static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
387 IMAGE_EXPORT_DIRECTORY *exp = NULL;
388 DEBUG_ENTRY_POINT *debug;
392 PLIST_ENTRY mark, entry;
393 PLDR_MODULE mod = NULL;
396 /* First find the module */
398 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
399 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
401 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
402 if (!(mod->Flags & LDR_WINE_INTERNAL)) continue;
403 exp = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
405 debug = (DEBUG_ENTRY_POINT *)((char *)exp + size);
406 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
408 ordinal = relay - debug;
413 /* Now find the function */
415 strcpy( buffer, (char *)mod->BaseAddress + exp->Name );
416 p = buffer + strlen(buffer);
417 if (p > buffer + 4 && !strcasecmp( p - 4, ".dll" )) p -= 4;
419 if ((name = find_exported_name( mod->BaseAddress, exp, ordinal + exp->Base )))
420 sprintf( p, ".%s", name );
422 sprintf( p, ".%ld", ordinal + exp->Base );
426 /***********************************************************************
429 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
433 if ((typemask & 3) && HIWORD(*args))
436 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
438 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
440 else DPRINTF( "%08x", *args );
441 if (nb_args) DPRINTF( "," );
448 typedef LONGLONG (*LONGLONG_CPROC)();
449 typedef LONGLONG (WINAPI *LONGLONG_FARPROC)();
452 /***********************************************************************
453 * call_cdecl_function
455 static LONGLONG call_cdecl_function( LONGLONG_CPROC func, int nb_args, const int *args )
460 case 0: ret = func(); break;
461 case 1: ret = func(args[0]); break;
462 case 2: ret = func(args[0],args[1]); break;
463 case 3: ret = func(args[0],args[1],args[2]); break;
464 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
465 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
466 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
468 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
470 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
471 args[6],args[7]); break;
472 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
473 args[6],args[7],args[8]); break;
474 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
475 args[6],args[7],args[8],args[9]); break;
476 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
477 args[6],args[7],args[8],args[9],args[10]); break;
478 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
479 args[6],args[7],args[8],args[9],args[10],
481 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
482 args[6],args[7],args[8],args[9],args[10],args[11],
484 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
485 args[6],args[7],args[8],args[9],args[10],args[11],
486 args[12],args[13]); break;
487 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
488 args[6],args[7],args[8],args[9],args[10],args[11],
489 args[12],args[13],args[14]); break;
490 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
491 args[6],args[7],args[8],args[9],args[10],args[11],
492 args[12],args[13],args[14],args[15]); break;
493 case 17: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
494 args[6],args[7],args[8],args[9],args[10],args[11],
495 args[12],args[13],args[14],args[15],args[16]); break;
496 case 18: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
497 args[6],args[7],args[8],args[9],args[10],args[11],
498 args[12],args[13],args[14],args[15],args[16],
501 ERR( "Unsupported nb of args %d\n", nb_args );
510 /***********************************************************************
511 * call_stdcall_function
513 static LONGLONG call_stdcall_function( LONGLONG_FARPROC func, int nb_args, const int *args )
518 case 0: ret = func(); break;
519 case 1: ret = func(args[0]); break;
520 case 2: ret = func(args[0],args[1]); break;
521 case 3: ret = func(args[0],args[1],args[2]); break;
522 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
523 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
524 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
526 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
528 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
529 args[6],args[7]); break;
530 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
531 args[6],args[7],args[8]); break;
532 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
533 args[6],args[7],args[8],args[9]); break;
534 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
535 args[6],args[7],args[8],args[9],args[10]); break;
536 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
537 args[6],args[7],args[8],args[9],args[10],
539 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
540 args[6],args[7],args[8],args[9],args[10],args[11],
542 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
543 args[6],args[7],args[8],args[9],args[10],args[11],
544 args[12],args[13]); break;
545 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
546 args[6],args[7],args[8],args[9],args[10],args[11],
547 args[12],args[13],args[14]); break;
548 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
549 args[6],args[7],args[8],args[9],args[10],args[11],
550 args[12],args[13],args[14],args[15]); break;
551 case 17: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
552 args[6],args[7],args[8],args[9],args[10],args[11],
553 args[12],args[13],args[14],args[15],args[16]); break;
554 case 18: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
555 args[6],args[7],args[8],args[9],args[10],args[11],
556 args[12],args[13],args[14],args[15],args[16],
559 ERR( "Unsupported nb of args %d\n", nb_args );
568 /***********************************************************************
571 * Stack layout on entry to this function:
576 * (esp) return addr to relay code
578 static LONGLONG RELAY_CallFrom32( int ret_addr, ... )
583 int *args = &ret_addr + 1;
584 /* Relay addr is the return address for this function */
585 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
586 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
587 WORD nb_args = relay->args / sizeof(int);
591 get_entry_point( buffer, relay );
593 DPRINTF( "%04lx:Call %s(", GetCurrentThreadId(), buffer );
594 RELAY_PrintArgs( args, nb_args, relay->argtypes );
595 DPRINTF( ") ret=%08x\n", ret_addr );
598 if (relay->ret == 0xc3) /* cdecl */
600 ret = call_cdecl_function( (LONGLONG_CPROC)relay->orig, nb_args, args );
604 ret = call_stdcall_function( (LONGLONG_FARPROC)relay->orig, nb_args, args );
609 BOOL ret64 = (relay->argtypes & 0x80000000) && (nb_args < 16);
611 DPRINTF( "%04lx:Ret %s() retval=%08x%08x ret=%08x\n",
612 GetCurrentThreadId(),
613 buffer, (UINT)(ret >> 32), (UINT)ret, ret_addr );
615 DPRINTF( "%04lx:Ret %s() retval=%08x ret=%08x\n",
616 GetCurrentThreadId(),
617 buffer, (UINT)ret, ret_addr );
623 /***********************************************************************
624 * RELAY_CallFrom32Regs
626 * Stack layout (esp is context->Esp, not the current %esp):
630 * (esp) return addr to caller
631 * (esp-4) return addr to DEBUG_ENTRY_POINT
632 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
633 * ... >128 bytes space free to be modified (ensured by the assembly glue)
635 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
642 BYTE *relay_addr = *((BYTE **)context->Esp - 1);
643 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
644 WORD nb_args = relay->args / sizeof(int);
646 /* remove extra stuff from the stack */
647 context->Eip = *(DWORD *)context->Esp;
648 context->Esp += sizeof(DWORD);
649 args = (int *)context->Esp;
650 if (relay->ret == 0xc2) /* stdcall */
651 context->Esp += nb_args * sizeof(int);
653 entry_point = (BYTE *)relay->orig;
654 assert( *entry_point == 0xe8 /* lcall */ );
658 get_entry_point( buffer, relay );
660 DPRINTF( "%04lx:Call %s(", GetCurrentThreadId(), buffer );
661 RELAY_PrintArgs( args, nb_args, relay->argtypes );
662 DPRINTF( ") ret=%08lx fs=%04lx\n", context->Eip, context->SegFs );
664 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
665 context->Eax, context->Ebx, context->Ecx,
666 context->Edx, context->Esi, context->Edi );
667 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
668 context->Ebp, context->Esp, context->SegDs,
669 context->SegEs, context->SegGs, context->EFlags );
672 /* Now call the real function */
674 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
675 args_copy[nb_args] = (int)context; /* append context argument */
676 if (relay->ret == 0xc3) /* cdecl */
678 call_cdecl_function( *(LONGLONG_CPROC *)(entry_point + 5), nb_args+1, args_copy );
682 call_stdcall_function( *(LONGLONG_FARPROC *)(entry_point + 5), nb_args+1, args_copy );
687 DPRINTF( "%04lx:Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
688 GetCurrentThreadId(),
689 buffer, context->Eax, context->Eip, context->SegFs );
691 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
692 context->Eax, context->Ebx, context->Ecx,
693 context->Edx, context->Esi, context->Edi );
694 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
695 context->Ebp, context->Esp, context->SegDs,
696 context->SegEs, context->SegGs, context->EFlags );
700 void WINAPI RELAY_CallFrom32Regs(void);
701 __ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs,
702 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
703 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
706 /* check whether the function at addr starts with a call to __wine_call_from_32_regs */
707 static BOOL is_register_entry_point( const BYTE *addr )
709 extern void __wine_call_from_32_regs();
713 if (*addr != 0xe8) return FALSE; /* not a call */
714 /* check if call target is __wine_call_from_32_regs */
715 offset = (const int *)(addr + 1);
716 if (*offset == (const char *)__wine_call_from_32_regs - (const char *)(offset + 1)) return TRUE;
717 /* now check if call target is an import table jump to __wine_call_from_32_regs */
718 addr = (const BYTE *)(offset + 1) + *offset;
719 if (addr[0] != 0xff || addr[1] != 0x25) return FALSE; /* not an indirect jmp */
720 ptr = *(const void * const*)(addr + 2); /* get indirect jmp target address */
721 return (*(const char * const*)ptr == (char *)__wine_call_from_32_regs);
725 /***********************************************************************
726 * RELAY_GetProcAddress
728 * Return the proc address to use for a given function.
730 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
731 DWORD exp_size, FARPROC proc, const WCHAR *user )
733 const DEBUG_ENTRY_POINT *debug = (DEBUG_ENTRY_POINT *)proc;
734 const DEBUG_ENTRY_POINT *list = (const DEBUG_ENTRY_POINT *)((const char *)exports + exp_size);
736 if (debug < list || debug >= list + exports->NumberOfFunctions) return proc;
737 if (list + (debug - list) != debug) return proc; /* not a valid address */
738 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
739 return proc; /* we want to relay it */
740 if (!debug->call) return proc; /* not a normal function */
741 if (debug->call != 0xe8 && debug->call != 0xe9) return proc; /* not a debug thunk at all */
746 /***********************************************************************
749 * Setup relay debugging for a built-in dll.
751 void RELAY_SetupDLL( HMODULE module )
753 IMAGE_EXPORT_DIRECTORY *exports;
754 DEBUG_ENTRY_POINT *debug;
758 char *p, dllname[80];
761 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
762 if (!exports) return;
763 debug = (DEBUG_ENTRY_POINT *)((char *)exports + size);
764 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
765 strcpy( dllname, (char *)module + exports->Name );
766 p = dllname + strlen(dllname) - 4;
767 if (p > dllname && !strcasecmp( p, ".dll" )) *p = 0;
769 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
773 if (!debug->call) continue; /* not a normal function */
774 if (debug->call != 0xe8 && debug->call != 0xe9) break; /* not a debug thunk at all */
776 name = find_exported_name( module, exports, i + exports->Base );
777 on = check_relay_include( dllname, i + exports->Base, name );
781 debug->call = 0xe8; /* call relative */
782 if (is_register_entry_point( debug->orig ))
783 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
785 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
789 debug->call = 0xe9; /* jmp relative */
790 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
792 *funcs = (char *)debug - (char *)module;
797 /***********************************************************************
798 * SNOOP_ShowDebugmsgSnoop
800 * Simple function to decide if a particular debugging message is
803 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
805 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
807 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
813 /***********************************************************************
816 * Setup snoop debugging for a native dll.
818 void SNOOP_SetupDLL(HMODULE hmod)
820 SNOOP_DLL **dll = &firstdll;
824 IMAGE_EXPORT_DIRECTORY *exports;
826 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
827 if (!exports) return;
828 name = (char *)hmod + exports->Name;
830 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
833 if ((*dll)->hmod == hmod)
835 /* another dll, loaded at the same address */
837 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
838 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
841 dll = &((*dll)->next);
844 *dll = RtlReAllocateHeap(GetProcessHeap(),
845 HEAP_ZERO_MEMORY, *dll,
846 sizeof(SNOOP_DLL) + strlen(name));
848 *dll = RtlAllocateHeap(GetProcessHeap(),
850 sizeof(SNOOP_DLL) + strlen(name));
852 (*dll)->ordbase = exports->Base;
853 (*dll)->nrofordinals = exports->NumberOfFunctions;
854 strcpy( (*dll)->name, name );
855 p = (*dll)->name + strlen((*dll)->name) - 4;
856 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
858 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
860 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
861 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
863 RtlFreeHeap(GetProcessHeap(),0,*dll);
864 FIXME("out of memory\n");
868 memset((*dll)->funs,0,size);
872 /***********************************************************************
873 * SNOOP_GetProcAddress
875 * Return the proc address to use for a given function.
877 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
878 DWORD exp_size, FARPROC origfun, DWORD ordinal,
883 const WORD *ordinals;
885 SNOOP_DLL *dll = firstdll;
887 const IMAGE_SECTION_HEADER *sec;
889 if (!TRACE_ON(snoop)) return origfun;
890 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
891 return origfun; /* the calling module was explicitly excluded */
893 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
896 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
898 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
899 return origfun; /* most likely a data reference */
902 if (hmod == dll->hmod)
906 if (!dll) /* probably internal */
909 /* try to find a name for it */
911 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
912 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
913 if (names) for (i = 0; i < exports->NumberOfNames; i++)
915 if (ordinals[i] == ordinal)
917 ename = (const char *)hmod + names[i];
921 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
923 assert(ordinal < dll->nrofordinals);
924 fun = dll->funs + ordinal;
929 /* NOTE: origreturn struct member MUST come directly after snoopentry */
930 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
931 fun->origfun = origfun;
934 return (FARPROC)&(fun->lcall);
937 static void SNOOP_PrintArg(DWORD x)
942 if (!HIWORD(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
949 if (s[i]<0x20) {nostring=1;break;}
950 if (s[i]>=0x80) {nostring=1;break;}
953 if (!nostring && i > 5)
954 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
955 else /* try unicode */
961 if (s[i]<0x20) {nostring=1;break;}
962 if (s[i]>0x100) {nostring=1;break;}
965 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
974 #define CALLER1REF (*(DWORD*)context->Esp)
976 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
978 DWORD ordinal=0,entry = context->Eip - 5;
979 SNOOP_DLL *dll = firstdll;
980 SNOOP_FUN *fun = NULL;
981 SNOOP_RETURNENTRIES **rets = &firstrets;
982 SNOOP_RETURNENTRY *ret;
986 if ( ((char*)entry>=(char*)dll->funs) &&
987 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
989 fun = (SNOOP_FUN*)entry;
990 ordinal = fun-dll->funs;
996 FIXME("entrypoint 0x%08lx not found\n",entry);
999 /* guess cdecl ... */
1000 if (fun->nrofargs<0) {
1001 /* Typical cdecl return frame is:
1003 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1004 * (after that 81 C2 xx xx xx xx)
1006 LPBYTE reteip = (LPBYTE)CALLER1REF;
1009 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1010 fun->nrofargs=reteip[2]/4;
1016 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
1017 if (!(*rets)->entry[i].origreturn)
1019 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
1021 rets = &((*rets)->next);
1027 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1028 MEM_COMMIT | MEM_RESERVE,
1029 PAGE_EXECUTE_READWRITE);
1032 memset(*rets,0,4096);
1033 i = 0; /* entry 0 is free */
1035 ret = &((*rets)->entry[i]);
1037 /* NOTE: origreturn struct member MUST come directly after snoopret */
1038 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
1039 ret->origreturn = (FARPROC)CALLER1REF;
1040 CALLER1REF = (DWORD)&ret->lcall;
1043 ret->ordinal = ordinal;
1044 ret->origESP = context->Esp;
1046 context->Eip = (DWORD)fun->origfun;
1048 if (fun->name) DPRINTF("%04lx:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
1049 else DPRINTF("%04lx:CALL %s.%ld(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
1050 if (fun->nrofargs>0) {
1051 max = fun->nrofargs; if (max>16) max=16;
1054 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
1055 if (i<fun->nrofargs-1) DPRINTF(",");
1057 if (max!=fun->nrofargs)
1059 } else if (fun->nrofargs<0) {
1060 DPRINTF("<unknown, check return>");
1061 ret->args = RtlAllocateHeap(GetProcessHeap(),
1062 0,16*sizeof(DWORD));
1063 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
1065 DPRINTF(") ret=%08lx\n",(DWORD)ret->origreturn);
1069 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
1071 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
1072 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1074 /* We haven't found out the nrofargs yet. If we called a cdecl
1075 * function it is too late anyway and we can just set '0' (which
1076 * will be the difference between orig and current ESP
1077 * If stdcall -> everything ok.
1079 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1080 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
1081 context->Eip = (DWORD)ret->origreturn;
1086 DPRINTF("%04lx:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1088 DPRINTF("%04lx:RET %s.%ld(", GetCurrentThreadId(),
1089 ret->dll->name,ret->dll->ordbase+ret->ordinal);
1091 max = fun->nrofargs;
1096 SNOOP_PrintArg(ret->args[i]);
1097 if (i<max-1) DPRINTF(",");
1099 DPRINTF(") retval=%08lx ret=%08lx\n",
1100 context->Eax,(DWORD)ret->origreturn );
1101 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1107 DPRINTF("%04lx:RET %s.%s() retval=%08lx ret=%08lx\n",
1108 GetCurrentThreadId(),
1109 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
1111 DPRINTF("%04lx:RET %s.%ld() retval=%08lx ret=%08lx\n",
1112 GetCurrentThreadId(),
1113 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1114 context->Eax, (DWORD)ret->origreturn);
1116 ret->origreturn = NULL; /* mark as empty */
1119 /* assembly wrappers that save the context */
1120 __ASM_GLOBAL_FUNC( SNOOP_Entry,
1121 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
1122 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
1123 __ASM_GLOBAL_FUNC( SNOOP_Return,
1124 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
1125 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
1127 #else /* __i386__ */
1129 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
1130 DWORD exp_size, FARPROC proc, const WCHAR *user )
1135 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1136 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1141 void RELAY_SetupDLL( HMODULE module )
1145 void SNOOP_SetupDLL( HMODULE hmod )
1147 FIXME("snooping works only on i386 for now.\n");
1150 #endif /* __i386__ */