user32: Fix SPI_SETMOUSESPEED handling, the parameter is not a pointer.
[wine/wine64.git] / dlls / ntdll / relay.c
bloba3031f28e3c0c37e2a2c768fbe57f50e4aee8a43
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
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 #ifdef __i386__
43 WINE_DECLARE_DEBUG_CHANNEL(snoop);
44 WINE_DECLARE_DEBUG_CHANNEL(seh);
46 struct relay_descr /* descriptor for a module */
48 void *magic; /* signature */
49 void *relay_from_32; /* functions to call from relay thunks */
50 void *relay_from_32_regs;
51 void *private; /* reserved for the relay code private data */
52 const char *entry_point_base; /* base address of entry point thunks */
53 const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
54 const unsigned int *arg_types; /* table of argument types for all entry points */
57 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
59 /* private data built at dll load time */
61 struct relay_entry_point
63 void *orig_func; /* original entry point function */
64 const char *name; /* function name (if any) */
67 struct relay_private_data
69 HMODULE module; /* module handle of this dll */
70 unsigned int base; /* ordinal base */
71 char dllname[40]; /* dll name (without .dll extension) */
72 struct relay_entry_point entry_points[1]; /* list of dll entry points */
75 static const WCHAR **debug_relay_excludelist;
76 static const WCHAR **debug_relay_includelist;
77 static const WCHAR **debug_snoop_excludelist;
78 static const WCHAR **debug_snoop_includelist;
79 static const WCHAR **debug_from_relay_excludelist;
80 static const WCHAR **debug_from_relay_includelist;
81 static const WCHAR **debug_from_snoop_excludelist;
82 static const WCHAR **debug_from_snoop_includelist;
84 static BOOL init_done;
86 /* compare an ASCII and a Unicode string without depending on the current codepage */
87 static inline int strcmpAW( const char *strA, const WCHAR *strW )
89 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
90 return (unsigned char)*strA - *strW;
93 /* compare an ASCII and a Unicode string without depending on the current codepage */
94 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
96 int ret = 0;
97 for ( ; n > 0; n--, strA++, strW++)
98 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
99 return ret;
102 /***********************************************************************
103 * build_list
105 * Build a function list from a ';'-separated string.
107 static const WCHAR **build_list( const WCHAR *buffer )
109 int count = 1;
110 const WCHAR *p = buffer;
111 const WCHAR **ret;
113 while ((p = strchrW( p, ';' )))
115 count++;
116 p++;
118 /* allocate count+1 pointers, plus the space for a copy of the string */
119 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
120 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
122 WCHAR *str = (WCHAR *)(ret + count + 1);
123 WCHAR *p = str;
125 strcpyW( str, buffer );
126 count = 0;
127 for (;;)
129 ret[count++] = p;
130 if (!(p = strchrW( p, ';' ))) break;
131 *p++ = 0;
133 ret[count++] = NULL;
135 return ret;
138 /***********************************************************************
139 * load_list_value
141 * Load a function list from a registry value.
143 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
145 char initial_buffer[4096];
146 char *buffer = initial_buffer;
147 DWORD count;
148 NTSTATUS status;
149 UNICODE_STRING name;
150 const WCHAR **list = NULL;
152 RtlInitUnicodeString( &name, value );
153 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count );
154 if (status == STATUS_BUFFER_OVERFLOW)
156 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
157 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
159 if (status == STATUS_SUCCESS)
161 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
162 list = build_list( str );
163 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
166 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
167 return list;
170 /***********************************************************************
171 * init_debug_lists
173 * Build the relay include/exclude function lists.
175 static void init_debug_lists(void)
177 OBJECT_ATTRIBUTES attr;
178 UNICODE_STRING name;
179 HANDLE root, hkey;
180 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
181 'W','i','n','e','\\',
182 'D','e','b','u','g',0};
183 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
184 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
185 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
186 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
187 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
188 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
189 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
190 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
192 if (init_done) return;
193 init_done = TRUE;
195 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
196 attr.Length = sizeof(attr);
197 attr.RootDirectory = root;
198 attr.ObjectName = &name;
199 attr.Attributes = 0;
200 attr.SecurityDescriptor = NULL;
201 attr.SecurityQualityOfService = NULL;
202 RtlInitUnicodeString( &name, configW );
204 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
205 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
206 NtClose( root );
207 if (!hkey) return;
209 debug_relay_includelist = load_list( hkey, RelayIncludeW );
210 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
211 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
212 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
213 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
214 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
215 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
216 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
218 NtClose( hkey );
222 /***********************************************************************
223 * check_list
225 * Check if a given module and function is in the list.
227 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
229 char ord_str[10];
231 sprintf( ord_str, "%d", ordinal );
232 for(; *list; list++)
234 const WCHAR *p = strrchrW( *list, '.' );
235 if (p && p > *list) /* check module and function */
237 int len = p - *list;
238 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
239 if (p[1] == '*' && !p[2]) return TRUE;
240 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
241 if (func && !strcmpAW( func, p + 1 )) return TRUE;
243 else /* function only */
245 if (func && !strcmpAW( func, *list )) return TRUE;
248 return FALSE;
252 /***********************************************************************
253 * check_relay_include
255 * Check if a given function must be included in the relay output.
257 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
259 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
260 return FALSE;
261 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
262 return FALSE;
263 return TRUE;
266 /***********************************************************************
267 * check_from_module
269 * Check if calls from a given module must be included in the relay/snoop output,
270 * given the exclusion and inclusion lists.
272 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
274 static const WCHAR dllW[] = {'.','d','l','l',0 };
275 const WCHAR **listitem;
276 BOOL show;
278 if (!module) return TRUE;
279 if (!includelist && !excludelist) return TRUE;
280 if (excludelist)
282 show = TRUE;
283 listitem = excludelist;
285 else
287 show = FALSE;
288 listitem = includelist;
290 for(; *listitem; listitem++)
292 int len;
294 if (!strcmpiW( *listitem, module )) return !show;
295 len = strlenW( *listitem );
296 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
297 return !show;
299 return show;
302 /***********************************************************************
303 * RELAY_PrintArgs
305 static inline void RELAY_PrintArgs( const int *args, int nb_args, unsigned int typemask )
307 while (nb_args--)
309 if ((typemask & 3) && HIWORD(*args))
311 if (typemask & 2)
312 DPRINTF( "%08x %s", *args, debugstr_w((LPCWSTR)*args) );
313 else
314 DPRINTF( "%08x %s", *args, debugstr_a((LPCSTR)*args) );
316 else DPRINTF( "%08x", *args );
317 if (nb_args) DPRINTF( "," );
318 args++;
319 typemask >>= 2;
323 extern LONGLONG call_entry_point( void *func, int nb_args, const int *args );
324 __ASM_GLOBAL_FUNC( call_entry_point,
325 "\tpushl %ebp\n"
326 "\tmovl %esp,%ebp\n"
327 "\tpushl %esi\n"
328 "\tpushl %edi\n"
329 "\tmovl 12(%ebp),%edx\n"
330 "\tshll $2,%edx\n"
331 "\tjz 1f\n"
332 "\tsubl %edx,%esp\n"
333 "\tandl $~15,%esp\n"
334 "\tmovl 12(%ebp),%ecx\n"
335 "\tmovl 16(%ebp),%esi\n"
336 "\tmovl %esp,%edi\n"
337 "\tcld\n"
338 "\trep; movsl\n"
339 "1:\tcall *8(%ebp)\n"
340 "\tleal -8(%ebp),%esp\n"
341 "\tpopl %edi\n"
342 "\tpopl %esi\n"
343 "\tpopl %ebp\n"
344 "\tret" )
347 /***********************************************************************
348 * relay_call_from_32
350 * stack points to the return address, i.e. the first argument is stack[1].
352 static LONGLONG WINAPI relay_call_from_32( struct relay_descr *descr, unsigned int idx, const int *stack )
354 LONGLONG ret;
355 WORD ordinal = LOWORD(idx);
356 BYTE nb_args = LOBYTE(HIWORD(idx));
357 BYTE flags = HIBYTE(HIWORD(idx));
358 struct relay_private_data *data = descr->private;
359 struct relay_entry_point *entry_point = data->entry_points + ordinal;
361 if (!TRACE_ON(relay))
362 ret = call_entry_point( entry_point->orig_func, nb_args, stack + 1 );
363 else
365 if (entry_point->name)
366 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
367 else
368 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
369 RELAY_PrintArgs( stack + 1, nb_args, descr->arg_types[ordinal] );
370 DPRINTF( ") ret=%08x\n", stack[0] );
372 ret = call_entry_point( entry_point->orig_func, nb_args, stack + 1 );
374 if (entry_point->name)
375 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data->dllname, entry_point->name );
376 else
377 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data->dllname, data->base + ordinal );
379 if (flags & 1) /* 64-bit return value */
380 DPRINTF( " retval=%08x%08x ret=%08x\n",
381 (UINT)(ret >> 32), (UINT)ret, stack[0] );
382 else
383 DPRINTF( " retval=%08x ret=%08x\n", (UINT)ret, stack[0] );
385 return ret;
389 /***********************************************************************
390 * relay_call_from_32_regs
392 void WINAPI __regs_relay_call_from_32_regs( struct relay_descr *descr, unsigned int idx,
393 unsigned int orig_eax, unsigned int ret_addr,
394 CONTEXT86 *context )
396 WORD ordinal = LOWORD(idx);
397 BYTE nb_args = LOBYTE(HIWORD(idx));
398 BYTE flags = HIBYTE(HIWORD(idx));
399 struct relay_private_data *data = descr->private;
400 struct relay_entry_point *entry_point = data->entry_points + ordinal;
401 BYTE *orig_func = entry_point->orig_func;
402 int *args = (int *)context->Esp;
403 int args_copy[32];
405 /* restore the context to what it was before the relay thunk */
406 context->Eax = orig_eax;
407 context->Eip = ret_addr;
408 if (flags & 2) /* stdcall */
409 context->Esp += nb_args * sizeof(int);
411 if (TRACE_ON(relay))
413 if (entry_point->name)
414 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
415 else
416 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
417 RELAY_PrintArgs( args, nb_args, descr->arg_types[ordinal] );
418 DPRINTF( ") ret=%08x\n", ret_addr );
420 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
421 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
422 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
423 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
424 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
426 assert( orig_func[0] == 0x50 /* pushl %eax */ );
427 assert( orig_func[1] == 0xe8 /* call */ );
430 /* now call the real function */
432 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
433 args_copy[nb_args++] = (int)context; /* append context argument */
435 call_entry_point( orig_func + 6 + *(int *)(orig_func + 6), nb_args, args_copy );
438 if (TRACE_ON(relay))
440 if (entry_point->name)
441 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
442 GetCurrentThreadId(), data->dllname, entry_point->name,
443 context->Eax, context->Eip );
444 else
445 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
446 GetCurrentThreadId(), data->dllname, data->base + ordinal,
447 context->Eax, context->Eip );
448 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
449 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
450 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
451 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
452 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
455 extern void WINAPI relay_call_from_32_regs(void);
456 DEFINE_REGS_ENTRYPOINT( relay_call_from_32_regs, 16, 16 )
459 /***********************************************************************
460 * RELAY_GetProcAddress
462 * Return the proc address to use for a given function.
464 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
465 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
467 struct relay_private_data *data;
468 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
470 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
471 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
472 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
473 return proc; /* we want to relay it */
474 return data->entry_points[ordinal].orig_func;
478 /***********************************************************************
479 * RELAY_SetupDLL
481 * Setup relay debugging for a built-in dll.
483 void RELAY_SetupDLL( HMODULE module )
485 IMAGE_EXPORT_DIRECTORY *exports;
486 DWORD *funcs;
487 unsigned int i, len;
488 DWORD size, entry_point_rva;
489 struct relay_descr *descr;
490 struct relay_private_data *data;
491 const WORD *ordptr;
493 if (!init_done) init_debug_lists();
495 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
496 if (!exports) return;
498 descr = (struct relay_descr *)((char *)exports + size);
499 if (descr->magic != RELAY_DESCR_MAGIC) return;
501 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
502 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
503 return;
505 descr->relay_from_32 = relay_call_from_32;
506 descr->relay_from_32_regs = relay_call_from_32_regs;
507 descr->private = data;
509 data->module = module;
510 data->base = exports->Base;
511 len = strlen( (char *)module + exports->Name );
512 if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
513 len = min( len, sizeof(data->dllname) - 1 );
514 memcpy( data->dllname, (char *)module + exports->Name, len );
515 data->dllname[len] = 0;
517 /* fetch name pointer for all entry points and store them in the private structure */
519 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
520 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
522 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
523 data->entry_points[*ordptr].name = (const char *)module + name_rva;
526 /* patch the functions in the export table to point to the relay thunks */
528 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
529 entry_point_rva = descr->entry_point_base - (const char *)module;
530 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
532 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
533 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
534 continue; /* don't include this entry point */
536 data->entry_points[i].orig_func = (char *)module + *funcs;
537 *funcs = entry_point_rva + descr->entry_point_offsets[i];
543 /***********************************************************************/
544 /* snoop support */
545 /***********************************************************************/
547 #include "pshpack1.h"
549 typedef struct
551 /* code part */
552 BYTE lcall; /* 0xe8 call snoopentry (relative) */
553 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
554 * calculation!
556 DWORD snoopentry; /* SNOOP_Entry relative */
557 /* unreached */
558 int nrofargs;
559 FARPROC origfun;
560 const char *name;
561 } SNOOP_FUN;
563 typedef struct tagSNOOP_DLL {
564 HMODULE hmod;
565 SNOOP_FUN *funs;
566 DWORD ordbase;
567 DWORD nrofordinals;
568 struct tagSNOOP_DLL *next;
569 char name[1];
570 } SNOOP_DLL;
572 typedef struct
574 /* code part */
575 BYTE lcall; /* 0xe8 call snoopret relative*/
576 /* NOTE: If you move snoopret OR origreturn fix the relative offset
577 * calculation!
579 DWORD snoopret; /* SNOOP_Ret relative */
580 /* unreached */
581 FARPROC origreturn;
582 SNOOP_DLL *dll;
583 DWORD ordinal;
584 DWORD origESP;
585 DWORD *args; /* saved args across a stdcall */
586 } SNOOP_RETURNENTRY;
588 typedef struct tagSNOOP_RETURNENTRIES {
589 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
590 struct tagSNOOP_RETURNENTRIES *next;
591 } SNOOP_RETURNENTRIES;
593 #include "poppack.h"
595 extern void WINAPI SNOOP_Entry(void);
596 extern void WINAPI SNOOP_Return(void);
598 static SNOOP_DLL *firstdll;
599 static SNOOP_RETURNENTRIES *firstrets;
602 /***********************************************************************
603 * SNOOP_ShowDebugmsgSnoop
605 * Simple function to decide if a particular debugging message is
606 * wanted.
608 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
610 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
611 return FALSE;
612 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
613 return FALSE;
614 return TRUE;
618 /***********************************************************************
619 * SNOOP_SetupDLL
621 * Setup snoop debugging for a native dll.
623 void SNOOP_SetupDLL(HMODULE hmod)
625 SNOOP_DLL **dll = &firstdll;
626 char *p, *name;
627 void *addr;
628 SIZE_T size;
629 ULONG size32;
630 IMAGE_EXPORT_DIRECTORY *exports;
632 if (!init_done) init_debug_lists();
634 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
635 if (!exports || !exports->NumberOfFunctions) return;
636 name = (char *)hmod + exports->Name;
637 size = size32;
639 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
641 while (*dll) {
642 if ((*dll)->hmod == hmod)
644 /* another dll, loaded at the same address */
645 addr = (*dll)->funs;
646 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
647 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
648 break;
650 dll = &((*dll)->next);
652 if (*dll)
653 *dll = RtlReAllocateHeap(GetProcessHeap(),
654 HEAP_ZERO_MEMORY, *dll,
655 sizeof(SNOOP_DLL) + strlen(name));
656 else
657 *dll = RtlAllocateHeap(GetProcessHeap(),
658 HEAP_ZERO_MEMORY,
659 sizeof(SNOOP_DLL) + strlen(name));
660 (*dll)->hmod = hmod;
661 (*dll)->ordbase = exports->Base;
662 (*dll)->nrofordinals = exports->NumberOfFunctions;
663 strcpy( (*dll)->name, name );
664 p = (*dll)->name + strlen((*dll)->name) - 4;
665 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
667 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
668 addr = NULL;
669 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
670 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
671 if (!addr) {
672 RtlFreeHeap(GetProcessHeap(),0,*dll);
673 FIXME("out of memory\n");
674 return;
676 (*dll)->funs = addr;
677 memset((*dll)->funs,0,size);
681 /***********************************************************************
682 * SNOOP_GetProcAddress
684 * Return the proc address to use for a given function.
686 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
687 DWORD exp_size, FARPROC origfun, DWORD ordinal,
688 const WCHAR *user)
690 unsigned int i;
691 const char *ename;
692 const WORD *ordinals;
693 const DWORD *names;
694 SNOOP_DLL *dll = firstdll;
695 SNOOP_FUN *fun;
696 const IMAGE_SECTION_HEADER *sec;
698 if (!TRACE_ON(snoop)) return origfun;
699 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
700 return origfun; /* the calling module was explicitly excluded */
702 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
703 return origfun;
705 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
707 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
708 return origfun; /* most likely a data reference */
710 while (dll) {
711 if (hmod == dll->hmod)
712 break;
713 dll = dll->next;
715 if (!dll) /* probably internal */
716 return origfun;
718 /* try to find a name for it */
719 ename = NULL;
720 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
721 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
722 if (names) for (i = 0; i < exports->NumberOfNames; i++)
724 if (ordinals[i] == ordinal)
726 ename = (const char *)hmod + names[i];
727 break;
730 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
731 return origfun;
732 assert(ordinal < dll->nrofordinals);
733 fun = dll->funs + ordinal;
734 if (!fun->name)
736 fun->name = ename;
737 fun->lcall = 0xe8;
738 /* NOTE: origreturn struct member MUST come directly after snoopentry */
739 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
740 fun->origfun = origfun;
741 fun->nrofargs = -1;
743 return (FARPROC)&(fun->lcall);
746 static void SNOOP_PrintArg(DWORD x)
748 int i,nostring;
750 DPRINTF("%08x",x);
751 if (!HIWORD(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
752 __TRY
754 LPBYTE s=(LPBYTE)x;
755 i=0;nostring=0;
756 while (i<80) {
757 if (s[i]==0) break;
758 if (s[i]<0x20) {nostring=1;break;}
759 if (s[i]>=0x80) {nostring=1;break;}
760 i++;
762 if (!nostring && i > 5)
763 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
764 else /* try unicode */
766 LPWSTR s=(LPWSTR)x;
767 i=0;nostring=0;
768 while (i<80) {
769 if (s[i]==0) break;
770 if (s[i]<0x20) {nostring=1;break;}
771 if (s[i]>0x100) {nostring=1;break;}
772 i++;
774 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
777 __EXCEPT_PAGE_FAULT
780 __ENDTRY
783 #define CALLER1REF (*(DWORD*)context->Esp)
785 void WINAPI __regs_SNOOP_Entry( CONTEXT86 *context )
787 DWORD ordinal=0,entry = context->Eip - 5;
788 SNOOP_DLL *dll = firstdll;
789 SNOOP_FUN *fun = NULL;
790 SNOOP_RETURNENTRIES **rets = &firstrets;
791 SNOOP_RETURNENTRY *ret;
792 int i=0, max;
794 while (dll) {
795 if ( ((char*)entry>=(char*)dll->funs) &&
796 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
798 fun = (SNOOP_FUN*)entry;
799 ordinal = fun-dll->funs;
800 break;
802 dll=dll->next;
804 if (!dll) {
805 FIXME("entrypoint 0x%08x not found\n",entry);
806 return; /* oops */
808 /* guess cdecl ... */
809 if (fun->nrofargs<0) {
810 /* Typical cdecl return frame is:
811 * add esp, xxxxxxxx
812 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
813 * (after that 81 C2 xx xx xx xx)
815 LPBYTE reteip = (LPBYTE)CALLER1REF;
817 if (reteip) {
818 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
819 fun->nrofargs=reteip[2]/4;
824 while (*rets) {
825 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
826 if (!(*rets)->entry[i].origreturn)
827 break;
828 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
829 break;
830 rets = &((*rets)->next);
832 if (!*rets) {
833 SIZE_T size = 4096;
834 VOID* addr = NULL;
836 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
837 MEM_COMMIT | MEM_RESERVE,
838 PAGE_EXECUTE_READWRITE);
839 if (!addr) return;
840 *rets = addr;
841 memset(*rets,0,4096);
842 i = 0; /* entry 0 is free */
844 ret = &((*rets)->entry[i]);
845 ret->lcall = 0xe8;
846 /* NOTE: origreturn struct member MUST come directly after snoopret */
847 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
848 ret->origreturn = (FARPROC)CALLER1REF;
849 CALLER1REF = (DWORD)&ret->lcall;
850 ret->dll = dll;
851 ret->args = NULL;
852 ret->ordinal = ordinal;
853 ret->origESP = context->Esp;
855 context->Eip = (DWORD)fun->origfun;
857 if (fun->name) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
858 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
859 if (fun->nrofargs>0) {
860 max = fun->nrofargs; if (max>16) max=16;
861 for (i=0;i<max;i++)
863 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
864 if (i<fun->nrofargs-1) DPRINTF(",");
866 if (max!=fun->nrofargs)
867 DPRINTF(" ...");
868 } else if (fun->nrofargs<0) {
869 DPRINTF("<unknown, check return>");
870 ret->args = RtlAllocateHeap(GetProcessHeap(),
871 0,16*sizeof(DWORD));
872 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
874 DPRINTF(") ret=%08x\n",(DWORD)ret->origreturn);
878 void WINAPI __regs_SNOOP_Return( CONTEXT86 *context )
880 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
881 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
883 /* We haven't found out the nrofargs yet. If we called a cdecl
884 * function it is too late anyway and we can just set '0' (which
885 * will be the difference between orig and current ESP
886 * If stdcall -> everything ok.
888 if (ret->dll->funs[ret->ordinal].nrofargs<0)
889 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
890 context->Eip = (DWORD)ret->origreturn;
891 if (ret->args) {
892 int i,max;
894 if (fun->name)
895 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
896 else
897 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
898 ret->dll->name,ret->dll->ordbase+ret->ordinal);
900 max = fun->nrofargs;
901 if (max>16) max=16;
903 for (i=0;i<max;i++)
905 SNOOP_PrintArg(ret->args[i]);
906 if (i<max-1) DPRINTF(",");
908 DPRINTF(") retval=%08x ret=%08x\n",
909 context->Eax,(DWORD)ret->origreturn );
910 RtlFreeHeap(GetProcessHeap(),0,ret->args);
911 ret->args = NULL;
913 else
915 if (fun->name)
916 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
917 GetCurrentThreadId(),
918 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
919 else
920 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
921 GetCurrentThreadId(),
922 ret->dll->name,ret->dll->ordbase+ret->ordinal,
923 context->Eax, (DWORD)ret->origreturn);
925 ret->origreturn = NULL; /* mark as empty */
928 /* assembly wrappers that save the context */
929 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry, 0, 0 )
930 DEFINE_REGS_ENTRYPOINT( SNOOP_Return, 0, 0 )
932 #else /* __i386__ */
934 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
935 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
937 return proc;
940 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
941 FARPROC origfun, DWORD ordinal, const WCHAR *user )
943 return origfun;
946 void RELAY_SetupDLL( HMODULE module )
950 void SNOOP_SetupDLL( HMODULE hmod )
952 FIXME("snooping works only on i386 for now.\n");
955 #endif /* __i386__ */