Clean up devenum and properly implement DllCanUnloadNow ref counting.
[wine/multimedia.git] / dlls / ntdll / relay.c
blob27edbc42fdc953da1def83b0b540a2791808f2bd
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "excpt.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 )
63 int ret = 0;
64 for ( ; n > 0; n--, strA++, strW++)
65 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
66 return ret;
69 /***********************************************************************
70 * build_list
72 * Build a function list from a ';'-separated string.
74 static const WCHAR **build_list( const WCHAR *buffer )
76 int count = 1;
77 const WCHAR *p = buffer;
78 const WCHAR **ret;
80 while ((p = strchrW( p, ';' )))
82 count++;
83 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);
90 WCHAR *p = str;
92 strcpyW( str, buffer );
93 count = 0;
94 for (;;)
96 ret[count++] = p;
97 if (!(p = strchrW( p, ';' ))) break;
98 *p++ = 0;
100 ret[count++] = NULL;
102 return ret;
106 /***********************************************************************
107 * RELAY_InitDebugLists
109 * Build the relay include/exclude function lists.
111 void RELAY_InitDebugLists(void)
113 OBJECT_ATTRIBUTES attr;
114 UNICODE_STRING name;
115 char buffer[1024];
116 HKEY hkey;
117 DWORD count;
118 WCHAR *str;
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;
137 attr.Attributes = 0;
138 attr.SecurityDescriptor = NULL;
139 attr.SecurityQualityOfService = NULL;
140 RtlInitUnicodeString( &name, configW );
142 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) return;
144 str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
145 RtlInitUnicodeString( &name, RelayIncludeW );
146 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
148 TRACE("RelayInclude = %s\n", debugstr_w(str) );
149 debug_relay_includelist = build_list( str );
152 RtlInitUnicodeString( &name, RelayExcludeW );
153 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
155 TRACE( "RelayExclude = %s\n", debugstr_w(str) );
156 debug_relay_excludelist = build_list( str );
159 RtlInitUnicodeString( &name, SnoopIncludeW );
160 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
162 TRACE_(snoop)( "SnoopInclude = %s\n", debugstr_w(str) );
163 debug_snoop_includelist = build_list( str );
166 RtlInitUnicodeString( &name, SnoopExcludeW );
167 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
169 TRACE_(snoop)( "SnoopExclude = %s\n", debugstr_w(str) );
170 debug_snoop_excludelist = build_list( str );
173 RtlInitUnicodeString( &name, RelayFromIncludeW );
174 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
176 TRACE("RelayFromInclude = %s\n", debugstr_w(str) );
177 debug_from_relay_includelist = build_list( str );
180 RtlInitUnicodeString( &name, RelayFromExcludeW );
181 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
183 TRACE( "RelayFromExclude = %s\n", debugstr_w(str) );
184 debug_from_relay_excludelist = build_list( str );
187 RtlInitUnicodeString( &name, SnoopFromIncludeW );
188 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
190 TRACE_(snoop)("SnoopFromInclude = %s\n", debugstr_w(str) );
191 debug_from_snoop_includelist = build_list( str );
194 RtlInitUnicodeString( &name, SnoopFromExcludeW );
195 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
197 TRACE_(snoop)( "SnoopFromExclude = %s\n", debugstr_w(str) );
198 debug_from_snoop_excludelist = build_list( str );
201 NtClose( hkey );
205 #ifdef __i386__
207 #include "pshpack1.h"
209 typedef struct
211 BYTE call; /* 0xe8 call callfrom32 (relative) */
212 DWORD callfrom32; /* RELAY_CallFrom32 relative addr */
213 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
214 WORD args; /* nb of args to remove from the stack */
215 void *orig; /* original entry point */
216 DWORD argtypes; /* argument types */
217 } DEBUG_ENTRY_POINT;
219 typedef struct
221 /* code part */
222 BYTE lcall; /* 0xe8 call snoopentry (relative) */
223 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
224 * calculation!
226 DWORD snoopentry; /* SNOOP_Entry relative */
227 /* unreached */
228 int nrofargs;
229 FARPROC origfun;
230 const char *name;
231 } SNOOP_FUN;
233 typedef struct tagSNOOP_DLL {
234 HMODULE hmod;
235 SNOOP_FUN *funs;
236 DWORD ordbase;
237 DWORD nrofordinals;
238 struct tagSNOOP_DLL *next;
239 char name[1];
240 } SNOOP_DLL;
242 typedef struct
244 /* code part */
245 BYTE lcall; /* 0xe8 call snoopret relative*/
246 /* NOTE: If you move snoopret OR origreturn fix the relative offset
247 * calculation!
249 DWORD snoopret; /* SNOOP_Ret relative */
250 /* unreached */
251 FARPROC origreturn;
252 SNOOP_DLL *dll;
253 DWORD ordinal;
254 DWORD origESP;
255 DWORD *args; /* saved args across a stdcall */
256 } SNOOP_RETURNENTRY;
258 typedef struct tagSNOOP_RETURNENTRIES {
259 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
260 struct tagSNOOP_RETURNENTRIES *next;
261 } SNOOP_RETURNENTRIES;
263 #include "poppack.h"
265 extern void WINAPI SNOOP_Entry();
266 extern void WINAPI SNOOP_Return();
268 static SNOOP_DLL *firstdll;
269 static SNOOP_RETURNENTRIES *firstrets;
271 static WINE_EXCEPTION_FILTER(page_fault)
273 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
274 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
275 return EXCEPTION_EXECUTE_HANDLER;
276 return EXCEPTION_CONTINUE_SEARCH;
279 /***********************************************************************
280 * check_list
282 * Check if a given module and function is in the list.
284 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
286 char ord_str[10];
288 sprintf( ord_str, "%d", ordinal );
289 for(; *list; list++)
291 const WCHAR *p = strrchrW( *list, '.' );
292 if (p && p > *list) /* check module and function */
294 int len = p - *list;
295 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
296 if (p[1] == '*' && !p[2]) return TRUE;
297 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
298 if (func && !strcmpAW( func, p + 1 )) return TRUE;
300 else /* function only */
302 if (func && !strcmpAW( func, *list )) return TRUE;
305 return FALSE;
309 /***********************************************************************
310 * check_relay_include
312 * Check if a given function must be included in the relay output.
314 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
316 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
317 return FALSE;
318 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
319 return FALSE;
320 return TRUE;
323 /***********************************************************************
324 * check_from_module
326 * Check if calls from a given module must be included in the relay/snoop output,
327 * given the exclusion and inclusion lists.
329 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
331 static const WCHAR dllW[] = {'.','d','l','l',0 };
332 const WCHAR **listitem;
333 BOOL show;
335 if (!module) return TRUE;
336 if (!includelist && !excludelist) return TRUE;
337 if (excludelist)
339 show = TRUE;
340 listitem = excludelist;
342 else
344 show = FALSE;
345 listitem = includelist;
347 for(; *listitem; listitem++)
349 int len;
351 if (!strcmpiW( *listitem, module )) return !show;
352 len = strlenW( *listitem );
353 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
354 return !show;
356 return show;
359 /***********************************************************************
360 * find_exported_name
362 * Find the name of an exported function.
364 static const char *find_exported_name( HMODULE module,
365 IMAGE_EXPORT_DIRECTORY *exp, int ordinal )
367 unsigned int i;
368 const char *ret = NULL;
370 WORD *ordptr = (WORD *)((char *)module + exp->AddressOfNameOrdinals);
371 for (i = 0; i < exp->NumberOfNames; i++, ordptr++)
372 if (*ordptr + exp->Base == ordinal) break;
373 if (i < exp->NumberOfNames)
374 ret = (char *)module + ((DWORD*)((char *)module + exp->AddressOfNames))[i];
375 return ret;
379 /***********************************************************************
380 * get_entry_point
382 * Get the name of the DLL entry point corresponding to a relay address.
384 static void get_entry_point( char *buffer, DEBUG_ENTRY_POINT *relay )
386 IMAGE_EXPORT_DIRECTORY *exp = NULL;
387 DEBUG_ENTRY_POINT *debug;
388 char *p;
389 const char *name;
390 int ordinal = 0;
391 PLIST_ENTRY mark, entry;
392 PLDR_MODULE mod = NULL;
393 DWORD size;
395 /* First find the module */
397 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
398 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
400 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
401 if (!(mod->Flags & LDR_WINE_INTERNAL)) continue;
402 exp = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
403 if (!exp) continue;
404 debug = (DEBUG_ENTRY_POINT *)((char *)exp + size);
405 if (debug <= relay && relay < debug + exp->NumberOfFunctions)
407 ordinal = relay - debug;
408 break;
412 /* Now find the function */
414 strcpy( buffer, (char *)mod->BaseAddress + exp->Name );
415 p = buffer + strlen(buffer);
416 if (p > buffer + 4 && !strcasecmp( p - 4, ".dll" )) p -= 4;
418 if ((name = find_exported_name( mod->BaseAddress, exp, ordinal + exp->Base )))
419 sprintf( p, ".%s", name );
420 else
421 sprintf( p, ".%ld", ordinal + exp->Base );
425 /***********************************************************************
426 * RELAY_PrintArgs
428 static inline void RELAY_PrintArgs( int *args, int nb_args, unsigned int typemask )
430 while (nb_args--)
432 if ((typemask & 3) && HIWORD(*args))
434 if (typemask & 2)
435 DPRINTF( "%08x %s", *args, debugstr_w((LPWSTR)*args) );
436 else
437 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
439 else DPRINTF( "%08x", *args );
440 if (nb_args) DPRINTF( "," );
441 args++;
442 typemask >>= 2;
447 typedef LONGLONG (*LONGLONG_CPROC)();
448 typedef LONGLONG (WINAPI *LONGLONG_FARPROC)();
451 /***********************************************************************
452 * call_cdecl_function
454 static LONGLONG call_cdecl_function( LONGLONG_CPROC func, int nb_args, const int *args )
456 LONGLONG ret;
457 switch(nb_args)
459 case 0: ret = func(); break;
460 case 1: ret = func(args[0]); break;
461 case 2: ret = func(args[0],args[1]); break;
462 case 3: ret = func(args[0],args[1],args[2]); break;
463 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
464 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
465 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
466 args[5]); break;
467 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
468 args[6]); break;
469 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
470 args[6],args[7]); break;
471 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
472 args[6],args[7],args[8]); break;
473 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
474 args[6],args[7],args[8],args[9]); break;
475 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
476 args[6],args[7],args[8],args[9],args[10]); break;
477 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
478 args[6],args[7],args[8],args[9],args[10],
479 args[11]); break;
480 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
481 args[6],args[7],args[8],args[9],args[10],args[11],
482 args[12]); break;
483 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
484 args[6],args[7],args[8],args[9],args[10],args[11],
485 args[12],args[13]); break;
486 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
487 args[6],args[7],args[8],args[9],args[10],args[11],
488 args[12],args[13],args[14]); break;
489 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
490 args[6],args[7],args[8],args[9],args[10],args[11],
491 args[12],args[13],args[14],args[15]); break;
492 default:
493 ERR( "Unsupported nb of args %d\n", nb_args );
494 assert(FALSE);
495 ret = 0;
496 break;
498 return ret;
502 /***********************************************************************
503 * call_stdcall_function
505 static LONGLONG call_stdcall_function( LONGLONG_FARPROC func, int nb_args, const int *args )
507 LONGLONG ret;
508 switch(nb_args)
510 case 0: ret = func(); break;
511 case 1: ret = func(args[0]); break;
512 case 2: ret = func(args[0],args[1]); break;
513 case 3: ret = func(args[0],args[1],args[2]); break;
514 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
515 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
516 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
517 args[5]); break;
518 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
519 args[6]); break;
520 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
521 args[6],args[7]); break;
522 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
523 args[6],args[7],args[8]); break;
524 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
525 args[6],args[7],args[8],args[9]); break;
526 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
527 args[6],args[7],args[8],args[9],args[10]); break;
528 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
529 args[6],args[7],args[8],args[9],args[10],
530 args[11]); break;
531 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
532 args[6],args[7],args[8],args[9],args[10],args[11],
533 args[12]); break;
534 case 14: 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],args[11],
536 args[12],args[13]); break;
537 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
538 args[6],args[7],args[8],args[9],args[10],args[11],
539 args[12],args[13],args[14]); break;
540 case 16: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
541 args[6],args[7],args[8],args[9],args[10],args[11],
542 args[12],args[13],args[14],args[15]); break;
543 default:
544 ERR( "Unsupported nb of args %d\n", nb_args );
545 assert(FALSE);
546 ret = 0;
547 break;
549 return ret;
553 /***********************************************************************
554 * RELAY_CallFrom32
556 * Stack layout on entry to this function:
557 * ... ...
558 * (esp+12) arg2
559 * (esp+8) arg1
560 * (esp+4) ret_addr
561 * (esp) return addr to relay code
563 static LONGLONG RELAY_CallFrom32( int ret_addr, ... )
565 LONGLONG ret;
566 char buffer[80];
568 int *args = &ret_addr + 1;
569 /* Relay addr is the return address for this function */
570 BYTE *relay_addr = (BYTE *)__builtin_return_address(0);
571 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
572 WORD nb_args = relay->args / sizeof(int);
574 if (TRACE_ON(relay))
576 get_entry_point( buffer, relay );
578 DPRINTF( "%04lx:Call %s(", GetCurrentThreadId(), buffer );
579 RELAY_PrintArgs( args, nb_args, relay->argtypes );
580 DPRINTF( ") ret=%08x\n", ret_addr );
583 if (relay->ret == 0xc3) /* cdecl */
585 ret = call_cdecl_function( (LONGLONG_CPROC)relay->orig, nb_args, args );
587 else /* stdcall */
589 ret = call_stdcall_function( (LONGLONG_FARPROC)relay->orig, nb_args, args );
592 if (TRACE_ON(relay))
594 BOOL ret64 = (relay->argtypes & 0x80000000) && (nb_args < 16);
595 if (ret64)
596 DPRINTF( "%04lx:Ret %s() retval=%08x%08x ret=%08x\n",
597 GetCurrentThreadId(),
598 buffer, (UINT)(ret >> 32), (UINT)ret, ret_addr );
599 else
600 DPRINTF( "%04lx:Ret %s() retval=%08x ret=%08x\n",
601 GetCurrentThreadId(),
602 buffer, (UINT)ret, ret_addr );
604 return ret;
608 /***********************************************************************
609 * RELAY_CallFrom32Regs
611 * Stack layout (esp is context->Esp, not the current %esp):
613 * ...
614 * (esp+4) first arg
615 * (esp) return addr to caller
616 * (esp-4) return addr to DEBUG_ENTRY_POINT
617 * (esp-8) ptr to relay entry code for RELAY_CallFrom32Regs
618 * ... >128 bytes space free to be modified (ensured by the assembly glue)
620 void WINAPI RELAY_DoCallFrom32Regs( CONTEXT86 *context )
622 char buffer[80];
623 int* args;
624 int args_copy[17];
625 BYTE *entry_point;
627 BYTE *relay_addr = *((BYTE **)context->Esp - 1);
628 DEBUG_ENTRY_POINT *relay = (DEBUG_ENTRY_POINT *)(relay_addr - 5);
629 WORD nb_args = relay->args / sizeof(int);
631 /* remove extra stuff from the stack */
632 context->Eip = *(DWORD *)context->Esp;
633 context->Esp += sizeof(DWORD);
634 args = (int *)context->Esp;
635 if (relay->ret == 0xc2) /* stdcall */
636 context->Esp += nb_args * sizeof(int);
638 entry_point = (BYTE *)relay->orig;
639 assert( *entry_point == 0xe8 /* lcall */ );
641 if (TRACE_ON(relay))
643 get_entry_point( buffer, relay );
645 DPRINTF( "%04lx:Call %s(", GetCurrentThreadId(), buffer );
646 RELAY_PrintArgs( args, nb_args, relay->argtypes );
647 DPRINTF( ") ret=%08lx fs=%04lx\n", context->Eip, context->SegFs );
649 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
650 context->Eax, context->Ebx, context->Ecx,
651 context->Edx, context->Esi, context->Edi );
652 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
653 context->Ebp, context->Esp, context->SegDs,
654 context->SegEs, context->SegGs, context->EFlags );
657 /* Now call the real function */
659 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
660 args_copy[nb_args] = (int)context; /* append context argument */
661 if (relay->ret == 0xc3) /* cdecl */
663 call_cdecl_function( *(LONGLONG_CPROC *)(entry_point + 5), nb_args+1, args_copy );
665 else /* stdcall */
667 call_stdcall_function( *(LONGLONG_FARPROC *)(entry_point + 5), nb_args+1, args_copy );
670 if (TRACE_ON(relay))
672 DPRINTF( "%04lx:Ret %s() retval=%08lx ret=%08lx fs=%04lx\n",
673 GetCurrentThreadId(),
674 buffer, context->Eax, context->Eip, context->SegFs );
676 DPRINTF(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
677 context->Eax, context->Ebx, context->Ecx,
678 context->Edx, context->Esi, context->Edi );
679 DPRINTF(" ebp=%08lx esp=%08lx ds=%04lx es=%04lx gs=%04lx flags=%08lx\n",
680 context->Ebp, context->Esp, context->SegDs,
681 context->SegEs, context->SegGs, context->EFlags );
685 void WINAPI RELAY_CallFrom32Regs(void);
686 __ASM_GLOBAL_FUNC( RELAY_CallFrom32Regs,
687 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
688 ".long " __ASM_NAME("RELAY_DoCallFrom32Regs") ",0" );
691 /* check whether the function at addr starts with a call to __wine_call_from_32_regs */
692 static BOOL is_register_entry_point( const BYTE *addr )
694 extern void __wine_call_from_32_regs();
695 const int *offset;
696 const void *ptr;
698 if (*addr != 0xe8) return FALSE; /* not a call */
699 /* check if call target is __wine_call_from_32_regs */
700 offset = (const int *)(addr + 1);
701 if (*offset == (const char *)__wine_call_from_32_regs - (const char *)(offset + 1)) return TRUE;
702 /* now check if call target is an import table jump to __wine_call_from_32_regs */
703 addr = (const BYTE *)(offset + 1) + *offset;
704 if (addr[0] != 0xff || addr[1] != 0x25) return FALSE; /* not an indirect jmp */
705 ptr = *(const void * const*)(addr + 2); /* get indirect jmp target address */
706 return (*(const char * const*)ptr == (char *)__wine_call_from_32_regs);
710 /***********************************************************************
711 * RELAY_GetProcAddress
713 * Return the proc address to use for a given function.
715 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
716 DWORD exp_size, FARPROC proc, const WCHAR *user )
718 const DEBUG_ENTRY_POINT *debug = (DEBUG_ENTRY_POINT *)proc;
719 const DEBUG_ENTRY_POINT *list = (const DEBUG_ENTRY_POINT *)((const char *)exports + exp_size);
721 if (debug < list || debug >= list + exports->NumberOfFunctions) return proc;
722 if (list + (debug - list) != debug) return proc; /* not a valid address */
723 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
724 return proc; /* we want to relay it */
725 if (!debug->call) return proc; /* not a normal function */
726 if (debug->call != 0xe8 && debug->call != 0xe9) return proc; /* not a debug thunk at all */
727 return debug->orig;
731 /***********************************************************************
732 * RELAY_SetupDLL
734 * Setup relay debugging for a built-in dll.
736 void RELAY_SetupDLL( HMODULE module )
738 IMAGE_EXPORT_DIRECTORY *exports;
739 DEBUG_ENTRY_POINT *debug;
740 DWORD *funcs;
741 unsigned int i;
742 const char *name;
743 char *p, dllname[80];
744 DWORD size;
746 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
747 if (!exports) return;
748 debug = (DEBUG_ENTRY_POINT *)((char *)exports + size);
749 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
750 strcpy( dllname, (char *)module + exports->Name );
751 p = dllname + strlen(dllname) - 4;
752 if (p > dllname && !strcasecmp( p, ".dll" )) *p = 0;
754 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++, debug++)
756 int on = 1;
758 if (!debug->call) continue; /* not a normal function */
759 if (debug->call != 0xe8 && debug->call != 0xe9) break; /* not a debug thunk at all */
761 name = find_exported_name( module, exports, i + exports->Base );
762 on = check_relay_include( dllname, i + exports->Base, name );
764 if (on)
766 debug->call = 0xe8; /* call relative */
767 if (is_register_entry_point( debug->orig ))
768 debug->callfrom32 = (char *)RELAY_CallFrom32Regs - (char *)&debug->ret;
769 else
770 debug->callfrom32 = (char *)RELAY_CallFrom32 - (char *)&debug->ret;
772 else
774 debug->call = 0xe9; /* jmp relative */
775 debug->callfrom32 = (char *)debug->orig - (char *)&debug->ret;
777 *funcs = (char *)debug - (char *)module;
782 /***********************************************************************
783 * SNOOP_ShowDebugmsgSnoop
785 * Simple function to decide if a particular debugging message is
786 * wanted.
788 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
790 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
791 return FALSE;
792 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
793 return FALSE;
794 return TRUE;
798 /***********************************************************************
799 * SNOOP_SetupDLL
801 * Setup snoop debugging for a native dll.
803 void SNOOP_SetupDLL(HMODULE hmod)
805 SNOOP_DLL **dll = &firstdll;
806 char *p, *name;
807 void *addr;
808 SIZE_T size;
809 IMAGE_EXPORT_DIRECTORY *exports;
811 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
812 if (!exports) return;
813 name = (char *)hmod + exports->Name;
815 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
817 while (*dll) {
818 if ((*dll)->hmod == hmod)
820 /* another dll, loaded at the same address */
821 addr = (*dll)->funs;
822 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
823 NtFreeVirtualMemory(GetCurrentProcess(), &addr, &size, MEM_RELEASE);
824 break;
826 dll = &((*dll)->next);
828 if (*dll)
829 *dll = RtlReAllocateHeap(GetProcessHeap(),
830 HEAP_ZERO_MEMORY, *dll,
831 sizeof(SNOOP_DLL) + strlen(name));
832 else
833 *dll = RtlAllocateHeap(GetProcessHeap(),
834 HEAP_ZERO_MEMORY,
835 sizeof(SNOOP_DLL) + strlen(name));
836 (*dll)->hmod = hmod;
837 (*dll)->ordbase = exports->Base;
838 (*dll)->nrofordinals = exports->NumberOfFunctions;
839 strcpy( (*dll)->name, name );
840 p = (*dll)->name + strlen((*dll)->name) - 4;
841 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
843 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
844 addr = NULL;
845 NtAllocateVirtualMemory(GetCurrentProcess(), &addr, 0, &size,
846 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
847 if (!addr) {
848 RtlFreeHeap(GetProcessHeap(),0,*dll);
849 FIXME("out of memory\n");
850 return;
852 (*dll)->funs = addr;
853 memset((*dll)->funs,0,size);
857 /***********************************************************************
858 * SNOOP_GetProcAddress
860 * Return the proc address to use for a given function.
862 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
863 DWORD exp_size, FARPROC origfun, DWORD ordinal,
864 const WCHAR *user)
866 unsigned int i;
867 const char *ename;
868 const WORD *ordinals;
869 const DWORD *names;
870 SNOOP_DLL *dll = firstdll;
871 SNOOP_FUN *fun;
872 const IMAGE_SECTION_HEADER *sec;
874 if (!TRACE_ON(snoop)) return origfun;
875 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
876 return origfun; /* the calling module was explicitly excluded */
878 if (!*(LPBYTE)origfun) /* 0x00 is an imposs. opcode, poss. dataref. */
879 return origfun;
881 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
883 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
884 return origfun; /* most likely a data reference */
886 while (dll) {
887 if (hmod == dll->hmod)
888 break;
889 dll = dll->next;
891 if (!dll) /* probably internal */
892 return origfun;
894 /* try to find a name for it */
895 ename = NULL;
896 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
897 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
898 if (names) for (i = 0; i < exports->NumberOfNames; i++)
900 if (ordinals[i] == ordinal)
902 ename = (const char *)hmod + names[i];
903 break;
906 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
907 return origfun;
908 assert(ordinal < dll->nrofordinals);
909 fun = dll->funs + ordinal;
910 if (!fun->name)
912 fun->name = ename;
913 fun->lcall = 0xe8;
914 /* NOTE: origreturn struct member MUST come directly after snoopentry */
915 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
916 fun->origfun = origfun;
917 fun->nrofargs = -1;
919 return (FARPROC)&(fun->lcall);
922 static void SNOOP_PrintArg(DWORD x)
924 int i,nostring;
926 DPRINTF("%08lx",x);
927 if (!HIWORD(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
928 __TRY
930 LPBYTE s=(LPBYTE)x;
931 i=0;nostring=0;
932 while (i<80) {
933 if (s[i]==0) break;
934 if (s[i]<0x20) {nostring=1;break;}
935 if (s[i]>=0x80) {nostring=1;break;}
936 i++;
938 if (!nostring && i > 5)
939 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
940 else /* try unicode */
942 LPWSTR s=(LPWSTR)x;
943 i=0;nostring=0;
944 while (i<80) {
945 if (s[i]==0) break;
946 if (s[i]<0x20) {nostring=1;break;}
947 if (s[i]>0x100) {nostring=1;break;}
948 i++;
950 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
953 __EXCEPT(page_fault)
956 __ENDTRY
959 #define CALLER1REF (*(DWORD*)context->Esp)
961 void WINAPI SNOOP_DoEntry( CONTEXT86 *context )
963 DWORD ordinal=0,entry = context->Eip - 5;
964 SNOOP_DLL *dll = firstdll;
965 SNOOP_FUN *fun = NULL;
966 SNOOP_RETURNENTRIES **rets = &firstrets;
967 SNOOP_RETURNENTRY *ret;
968 int i=0, max;
970 while (dll) {
971 if ( ((char*)entry>=(char*)dll->funs) &&
972 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
974 fun = (SNOOP_FUN*)entry;
975 ordinal = fun-dll->funs;
976 break;
978 dll=dll->next;
980 if (!dll) {
981 FIXME("entrypoint 0x%08lx not found\n",entry);
982 return; /* oops */
984 /* guess cdecl ... */
985 if (fun->nrofargs<0) {
986 /* Typical cdecl return frame is:
987 * add esp, xxxxxxxx
988 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
989 * (after that 81 C2 xx xx xx xx)
991 LPBYTE reteip = (LPBYTE)CALLER1REF;
993 if (reteip) {
994 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
995 fun->nrofargs=reteip[2]/4;
1000 while (*rets) {
1001 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
1002 if (!(*rets)->entry[i].origreturn)
1003 break;
1004 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
1005 break;
1006 rets = &((*rets)->next);
1008 if (!*rets) {
1009 SIZE_T size = 4096;
1010 VOID* addr = NULL;
1012 NtAllocateVirtualMemory(GetCurrentProcess(), &addr, 0, &size,
1013 MEM_COMMIT | MEM_RESERVE,
1014 PAGE_EXECUTE_READWRITE);
1015 if (!addr) return;
1016 *rets = addr;
1017 memset(*rets,0,4096);
1018 i = 0; /* entry 0 is free */
1020 ret = &((*rets)->entry[i]);
1021 ret->lcall = 0xe8;
1022 /* NOTE: origreturn struct member MUST come directly after snoopret */
1023 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
1024 ret->origreturn = (FARPROC)CALLER1REF;
1025 CALLER1REF = (DWORD)&ret->lcall;
1026 ret->dll = dll;
1027 ret->args = NULL;
1028 ret->ordinal = ordinal;
1029 ret->origESP = context->Esp;
1031 context->Eip = (DWORD)fun->origfun;
1033 if (fun->name) DPRINTF("%04lx:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
1034 else DPRINTF("%04lx:CALL %s.%ld(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
1035 if (fun->nrofargs>0) {
1036 max = fun->nrofargs; if (max>16) max=16;
1037 for (i=0;i<max;i++)
1039 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
1040 if (i<fun->nrofargs-1) DPRINTF(",");
1042 if (max!=fun->nrofargs)
1043 DPRINTF(" ...");
1044 } else if (fun->nrofargs<0) {
1045 DPRINTF("<unknown, check return>");
1046 ret->args = RtlAllocateHeap(GetProcessHeap(),
1047 0,16*sizeof(DWORD));
1048 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
1050 DPRINTF(") ret=%08lx\n",(DWORD)ret->origreturn);
1054 void WINAPI SNOOP_DoReturn( CONTEXT86 *context )
1056 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
1057 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1059 /* We haven't found out the nrofargs yet. If we called a cdecl
1060 * function it is too late anyway and we can just set '0' (which
1061 * will be the difference between orig and current ESP
1062 * If stdcall -> everything ok.
1064 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1065 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
1066 context->Eip = (DWORD)ret->origreturn;
1067 if (ret->args) {
1068 int i,max;
1070 if (fun->name)
1071 DPRINTF("%04lx:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1072 else
1073 DPRINTF("%04lx:RET %s.%ld(", GetCurrentThreadId(),
1074 ret->dll->name,ret->dll->ordbase+ret->ordinal);
1076 max = fun->nrofargs;
1077 if (max>16) max=16;
1079 for (i=0;i<max;i++)
1081 SNOOP_PrintArg(ret->args[i]);
1082 if (i<max-1) DPRINTF(",");
1084 DPRINTF(") retval=%08lx ret=%08lx\n",
1085 context->Eax,(DWORD)ret->origreturn );
1086 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1087 ret->args = NULL;
1089 else
1091 if (fun->name)
1092 DPRINTF("%04lx:RET %s.%s() retval=%08lx ret=%08lx\n",
1093 GetCurrentThreadId(),
1094 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
1095 else
1096 DPRINTF("%04lx:RET %s.%ld() retval=%08lx ret=%08lx\n",
1097 GetCurrentThreadId(),
1098 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1099 context->Eax, (DWORD)ret->origreturn);
1101 ret->origreturn = NULL; /* mark as empty */
1104 /* assembly wrappers that save the context */
1105 __ASM_GLOBAL_FUNC( SNOOP_Entry,
1106 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
1107 ".long " __ASM_NAME("SNOOP_DoEntry") ",0" );
1108 __ASM_GLOBAL_FUNC( SNOOP_Return,
1109 "call " __ASM_NAME("__wine_call_from_32_regs") "\n\t"
1110 ".long " __ASM_NAME("SNOOP_DoReturn") ",0" );
1112 #else /* __i386__ */
1114 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
1115 DWORD exp_size, FARPROC proc, const WCHAR *user )
1117 return proc;
1120 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1121 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1123 return origfun;
1126 void RELAY_SetupDLL( HMODULE module )
1130 void SNOOP_SetupDLL( HMODULE hmod )
1132 FIXME("snooping works only on i386 for now.\n");
1135 #endif /* __i386__ */