usp10/tests: A spelling fix in an ok() message.
[wine.git] / dlls / ntdll / relay.c
blob1deb2b7bf284007e4e301966979bb0fc9d6b1c28
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 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
43 struct relay_descr /* descriptor for a module */
45 ULONG_PTR magic; /* signature */
46 void *relay_call; /* functions to call from relay thunks */
47 void *private; /* reserved for the relay code private data */
48 const char *entry_point_base; /* base address of entry point thunks */
49 const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
50 const char *args_string; /* string describing the arguments */
53 struct relay_descr_rva /* RVA to the descriptor for PE dlls */
55 DWORD magic;
56 DWORD descr;
59 #define RELAY_DESCR_MAGIC 0xdeb90002
60 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
62 /* private data built at dll load time */
64 struct relay_entry_point
66 void *orig_func; /* original entry point function */
67 const char *name; /* function name (if any) */
70 struct relay_private_data
72 HMODULE module; /* module handle of this dll */
73 unsigned int base; /* ordinal base */
74 char dllname[40]; /* dll name (without .dll extension) */
75 struct relay_entry_point entry_points[1]; /* list of dll entry points */
78 static const WCHAR **debug_relay_excludelist;
79 static const WCHAR **debug_relay_includelist;
80 static const WCHAR **debug_snoop_excludelist;
81 static const WCHAR **debug_snoop_includelist;
82 static const WCHAR **debug_from_relay_excludelist;
83 static const WCHAR **debug_from_relay_includelist;
84 static const WCHAR **debug_from_snoop_excludelist;
85 static const WCHAR **debug_from_snoop_includelist;
87 static RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
89 /* compare an ASCII and a Unicode string without depending on the current codepage */
90 static inline int strcmpAW( const char *strA, const WCHAR *strW )
92 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
93 return (unsigned char)*strA - *strW;
96 /* compare an ASCII and a Unicode string without depending on the current codepage */
97 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
99 int ret = 0;
100 for ( ; n > 0; n--, strA++, strW++)
101 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
102 return ret;
105 /***********************************************************************
106 * build_list
108 * Build a function list from a ';'-separated string.
110 static const WCHAR **build_list( const WCHAR *buffer )
112 int count = 1;
113 const WCHAR *p = buffer;
114 const WCHAR **ret;
116 while ((p = strchrW( p, ';' )))
118 count++;
119 p++;
121 /* allocate count+1 pointers, plus the space for a copy of the string */
122 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
123 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
125 WCHAR *str = (WCHAR *)(ret + count + 1);
126 WCHAR *q = str;
128 strcpyW( str, buffer );
129 count = 0;
130 for (;;)
132 ret[count++] = q;
133 if (!(q = strchrW( q, ';' ))) break;
134 *q++ = 0;
136 ret[count++] = NULL;
138 return ret;
141 /***********************************************************************
142 * load_list_value
144 * Load a function list from a registry value.
146 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
148 char initial_buffer[4096];
149 char *buffer = initial_buffer;
150 DWORD count;
151 NTSTATUS status;
152 UNICODE_STRING name;
153 const WCHAR **list = NULL;
155 RtlInitUnicodeString( &name, value );
156 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(initial_buffer), &count );
157 if (status == STATUS_BUFFER_OVERFLOW)
159 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
160 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
162 if (status == STATUS_SUCCESS)
164 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
165 list = build_list( str );
166 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
169 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
170 return list;
173 /***********************************************************************
174 * init_debug_lists
176 * Build the relay include/exclude function lists.
178 static DWORD WINAPI init_debug_lists( RTL_RUN_ONCE *once, void *param, void **context )
180 OBJECT_ATTRIBUTES attr;
181 UNICODE_STRING name;
182 HANDLE root, hkey;
183 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
184 'W','i','n','e','\\',
185 'D','e','b','u','g',0};
186 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
187 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
188 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
189 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
190 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
191 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
192 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
193 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
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 TRUE;
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 );
219 return TRUE;
223 /***********************************************************************
224 * check_list
226 * Check if a given module and function is in the list.
228 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
230 char ord_str[10];
232 sprintf( ord_str, "%d", ordinal );
233 for(; *list; list++)
235 const WCHAR *p = strrchrW( *list, '.' );
236 if (p && p > *list) /* check module and function */
238 int len = p - *list;
239 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
240 if (p[1] == '*' && !p[2]) return TRUE;
241 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
242 if (func && !strcmpAW( func, p + 1 )) return TRUE;
244 else /* function only */
246 if (func && !strcmpAW( func, *list )) return TRUE;
249 return FALSE;
253 /***********************************************************************
254 * check_relay_include
256 * Check if a given function must be included in the relay output.
258 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
260 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
261 return FALSE;
262 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
263 return FALSE;
264 return TRUE;
267 /***********************************************************************
268 * check_from_module
270 * Check if calls from a given module must be included in the relay/snoop output,
271 * given the exclusion and inclusion lists.
273 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
275 static const WCHAR dllW[] = {'.','d','l','l',0 };
276 const WCHAR **listitem;
277 BOOL show;
279 if (!module) return TRUE;
280 if (!includelist && !excludelist) return TRUE;
281 if (excludelist)
283 show = TRUE;
284 listitem = excludelist;
286 else
288 show = FALSE;
289 listitem = includelist;
291 for(; *listitem; listitem++)
293 int len;
295 if (!strcmpiW( *listitem, module )) return !show;
296 len = strlenW( *listitem );
297 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
298 return !show;
300 return show;
304 static BOOL is_ret_val( char type )
306 return type >= 'A' && type <= 'Z';
309 static const char *func_name( struct relay_private_data *data, unsigned int ordinal )
311 struct relay_entry_point *entry_point = data->entry_points + ordinal;
313 if (entry_point->name)
314 return wine_dbg_sprintf( "%s.%s", data->dllname, entry_point->name );
315 else
316 return wine_dbg_sprintf( "%s.%u", data->dllname, data->base + ordinal );
319 static void trace_string_a( INT_PTR ptr )
321 if (!IS_INTARG( ptr )) TRACE( "%08lx %s", ptr, debugstr_a( (char *)ptr ));
322 else TRACE( "%08lx", ptr );
325 static void trace_string_w( INT_PTR ptr )
327 if (!IS_INTARG( ptr )) TRACE( "%08lx %s", ptr, debugstr_w( (WCHAR *)ptr ));
328 else TRACE( "%08lx", ptr );
331 #ifdef __i386__
333 /***********************************************************************
334 * relay_trace_entry
336 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
337 const DWORD *stack, unsigned int *nb_args )
339 WORD ordinal = LOWORD(idx);
340 const char *arg_types = descr->args_string + HIWORD(idx);
341 struct relay_private_data *data = descr->private;
342 struct relay_entry_point *entry_point = data->entry_points + ordinal;
343 unsigned int i, pos;
345 TRACE( "\1Call %s(", func_name( data, ordinal ));
347 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
349 switch (arg_types[i])
351 case 'j': /* int64 */
352 TRACE( "%x%08x", stack[pos+1], stack[pos] );
353 pos += 2;
354 break;
355 case 'k': /* int128 */
356 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
357 pos += 4;
358 break;
359 case 's': /* str */
360 trace_string_a( stack[pos++] );
361 break;
362 case 'w': /* wstr */
363 trace_string_w( stack[pos++] );
364 break;
365 case 'f': /* float */
366 TRACE( "%g", *(const float *)&stack[pos++] );
367 break;
368 case 'd': /* double */
369 TRACE( "%g", *(const double *)&stack[pos] );
370 pos += 2;
371 break;
372 case 'i': /* long */
373 default:
374 TRACE( "%08x", stack[pos++] );
375 break;
377 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
379 *nb_args = pos;
380 if (arg_types[0] == 't')
382 *nb_args |= 0x80000000; /* thiscall/fastcall */
383 if (arg_types[1] == 't') *nb_args |= 0x40000000; /* fastcall */
385 TRACE( ") ret=%08x\n", stack[-1] );
386 return entry_point->orig_func;
389 /***********************************************************************
390 * relay_trace_exit
392 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
393 void *retaddr, LONGLONG retval )
395 const char *arg_types = descr->args_string + HIWORD(idx);
397 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
399 while (!is_ret_val( *arg_types )) arg_types++;
400 if (*arg_types == 'J') /* int64 return value */
401 TRACE( " retval=%08x%08x ret=%08x\n",
402 (UINT)(retval >> 32), (UINT)retval, (UINT)retaddr );
403 else
404 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, (UINT)retaddr );
407 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx );
408 __ASM_GLOBAL_FUNC( relay_call,
409 "pushl %ebp\n\t"
410 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
411 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
412 "movl %esp,%ebp\n\t"
413 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
414 "pushl %esi\n\t"
415 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
416 "pushl %edi\n\t"
417 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
418 "pushl %ecx\n\t"
419 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
420 /* trace the parameters */
421 "pushl %eax\n\t"
422 "pushl %esp\n\t" /* number of args return ptr */
423 "leal 20(%ebp),%esi\n\t" /* stack */
424 "pushl %esi\n\t"
425 "pushl 12(%ebp)\n\t"
426 "pushl 8(%ebp)\n\t"
427 "call " __ASM_NAME("relay_trace_entry") "\n\t"
428 /* copy the arguments*/
429 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
430 "jecxz 1f\n\t"
431 "leal 0(,%ecx,4),%edx\n\t"
432 "subl %edx,%esp\n\t"
433 "andl $~15,%esp\n\t"
434 "movl %esp,%edi\n\t"
435 "cld\n\t"
436 "rep; movsl\n\t"
437 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
438 "jz 1f\n\t"
439 "popl %ecx\n\t"
440 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
441 "jz 1f\n\t"
442 "popl %edx\n"
443 /* call the entry point */
444 "1:\tcall *%eax\n\t"
445 "movl %eax,%esi\n\t"
446 "movl %edx,%edi\n\t"
447 /* trace the return value */
448 "leal -20(%ebp),%esp\n\t"
449 "pushl %edx\n\t"
450 "pushl %eax\n\t"
451 "pushl 16(%ebp)\n\t"
452 "pushl 12(%ebp)\n\t"
453 "pushl 8(%ebp)\n\t"
454 "call " __ASM_NAME("relay_trace_exit") "\n\t"
455 /* restore return value and return */
456 "leal -12(%ebp),%esp\n\t"
457 "movl %esi,%eax\n\t"
458 "movl %edi,%edx\n\t"
459 "popl %ecx\n\t"
460 __ASM_CFI(".cfi_same_value %ecx\n\t")
461 "popl %edi\n\t"
462 __ASM_CFI(".cfi_same_value %edi\n\t")
463 "popl %esi\n\t"
464 __ASM_CFI(".cfi_same_value %esi\n\t")
465 "popl %ebp\n\t"
466 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
467 __ASM_CFI(".cfi_same_value %ebp\n\t")
468 "ret $8" )
470 #elif defined(__arm__)
472 /***********************************************************************
473 * relay_trace_entry
475 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
476 const DWORD *stack, unsigned int *nb_args )
478 WORD ordinal = LOWORD(idx);
479 const char *arg_types = descr->args_string + HIWORD(idx);
480 struct relay_private_data *data = descr->private;
481 struct relay_entry_point *entry_point = data->entry_points + ordinal;
482 unsigned int i, pos;
483 #ifndef __SOFTFP__
484 unsigned int float_pos = 0, double_pos = 0;
485 const union fpregs { float s[16]; double d[8]; } *fpstack = (const union fpregs *)stack - 1;
486 #endif
488 TRACE( "\1Call %s(", func_name( data, ordinal ));
490 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
492 switch (arg_types[i])
494 case 'j': /* int64 */
495 pos = (pos + 1) & ~1;
496 TRACE( "%x%08x", stack[pos+1], stack[pos] );
497 pos += 2;
498 break;
499 case 'k': /* int128 */
500 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
501 pos += 4;
502 break;
503 case 's': /* str */
504 trace_string_a( stack[pos++] );
505 break;
506 case 'w': /* wstr */
507 trace_string_w( stack[pos++] );
508 break;
509 case 'f': /* float */
510 #ifndef __SOFTFP__
511 if (!(float_pos % 2)) float_pos = max( float_pos, double_pos * 2 );
512 if (float_pos < 16)
514 TRACE( "%g", fpstack->s[float_pos++] );
515 break;
517 #endif
518 TRACE( "%g", *(const float *)&stack[pos++] );
519 break;
520 case 'd': /* double */
521 #ifndef __SOFTFP__
522 double_pos = max( (float_pos + 1) / 2, double_pos );
523 if (double_pos < 8)
525 TRACE( "%g", fpstack->d[double_pos++] );
526 break;
528 #endif
529 pos = (pos + 1) & ~1;
530 TRACE( "%g", *(const double *)&stack[pos] );
531 pos += 2;
532 break;
533 case 'i': /* long */
534 default:
535 TRACE( "%08x", stack[pos++] );
536 break;
538 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
541 #ifndef __SOFTFP__
542 if (float_pos || double_pos)
544 pos |= 0x80000000;
545 stack = (const DWORD *)fpstack; /* retaddr is below the fp regs */
547 #endif
548 *nb_args = pos;
549 TRACE( ") ret=%08x\n", stack[-1] );
550 return entry_point->orig_func;
553 /***********************************************************************
554 * relay_trace_exit
556 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
557 DWORD retaddr, LONGLONG retval )
559 const char *arg_types = descr->args_string + HIWORD(idx);
561 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
563 while (!is_ret_val( *arg_types )) arg_types++;
564 if (*arg_types == 'J') /* int64 return value */
565 TRACE( " retval=%08x%08x ret=%08x\n",
566 (UINT)(retval >> 32), (UINT)retval, retaddr );
567 else
568 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, retaddr );
571 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const DWORD *stack );
572 __ASM_GLOBAL_FUNC( relay_call,
573 ".arm\n\t"
574 "push {r4-r8,lr}\n\t"
575 "sub sp, #16\n\t"
576 "mov r6, r2\n\t"
577 "add r3, sp, #12\n\t"
578 "mov r7, r0\n\t"
579 "mov r8, r1\n\t"
580 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
581 "mov ip, r0\n\t" /* entry point */
582 "mov r5, sp\n\t"
583 "ldr r1, [sp, #12]\n\t" /* number of args */
584 "lsl r3, r1, #2\n\t"
585 "subs r3, #16\n\t" /* first 4 args are in registers */
586 "ble 2f\n\t"
587 "sub sp, r3\n\t"
588 "and sp, #~7\n"
589 "add r2, r6, #16\n\t" /* skip r0-r3 */
590 "1:\tsubs r3, r3, #4\n\t"
591 "ldr r0, [r2, r3]\n\t"
592 "str r0, [sp, r3]\n\t"
593 "bgt 1b\n"
594 "2:\t"
595 #ifndef __SOFTFP__
596 "tst r1, #0x80000000\n\t"
597 "ldm r6, {r0-r3}\n\t"
598 "vldmdbne r6!, {s0-s15}\n\t"
599 #else
600 "ldm r6, {r0-r3}\n\t"
601 #endif
602 "blx ip\n\t"
603 "mov sp, r5\n\t"
604 "ldr r2, [r6, #-4]\n\t" /* retaddr */
605 "mov r4, r0\n\t"
606 "mov r5, r1\n\t"
607 "mov r0, r7\n\t"
608 "mov r1, r8\n\t"
609 "str r4, [sp]\n\t"
610 "str r5, [sp, #4]\n\t"
611 #ifndef __SOFTFP__
612 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
613 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
614 "vldr d0, [sp, #8]\n\t"
615 #else
616 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
617 #endif
618 "mov r0, r4\n\t"
619 "mov r1, r5\n\t"
620 "add sp, #16\n\t"
621 "pop {r4-r8,pc}" )
623 #elif defined(__aarch64__)
625 /***********************************************************************
626 * relay_trace_entry
628 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
629 const INT_PTR *stack, unsigned int *nb_args )
631 WORD ordinal = LOWORD(idx);
632 const char *arg_types = descr->args_string + HIWORD(idx);
633 struct relay_private_data *data = descr->private;
634 struct relay_entry_point *entry_point = data->entry_points + ordinal;
635 unsigned int i;
637 TRACE( "\1Call %s(", func_name( data, ordinal ));
639 for (i = 0; !is_ret_val( arg_types[i] ); i++)
641 switch (arg_types[i])
643 case 's': /* str */
644 trace_string_a( stack[i] );
645 break;
646 case 'w': /* wstr */
647 trace_string_w( stack[i] );
648 break;
649 case 'i': /* long */
650 default:
651 TRACE( "%08lx", stack[i] );
652 break;
654 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
656 *nb_args = i;
657 TRACE( ") ret=%08lx\n", stack[-1] );
658 return entry_point->orig_func;
661 /***********************************************************************
662 * relay_trace_exit
664 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
665 INT_PTR retaddr, INT_PTR retval )
667 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
668 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
671 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
672 __ASM_GLOBAL_FUNC( call_entry_point,
673 "stp x29, x30, [SP,#-16]!\n\t"
674 "stp x19, x20, [SP,#-16]!\n\t"
675 "mov x29, SP\n\t"
676 "mov x19, x2\n\t"
677 "ldp x8, x9, [x19, #-32]\n\t"
678 "mov x9, x0\n\t"
679 "cbz w1, 2f\n\t"
680 "mov w10, w1\n\t"
681 "mov x11, x2\n\t"
682 "mov x12, #0\n\t"
683 "ldp x0, x1, [x11], #16\n\t"
684 "add w12, w12, #2\n\t"
685 "cmp w12, w10\n\t"
686 "b.hs 2f\n\t"
687 "ldp x2, x3, [x11], #16\n\t"
688 "add w12, w12, #2\n\t"
689 "cmp w12, w10\n\t"
690 "b.hs 2f\n\t"
691 "ldp x4, x5, [x11], #16\n\t"
692 "add w12, w12, #2\n\t"
693 "cmp w12, w10\n\t"
694 "b.hs 2f\n\t"
695 "ldp x6, x7, [x11], #16\n\t"
696 "add w12, w12, #2\n\t"
697 "cmp w12, w10\n\t"
698 "b.hs 2f\n\t"
699 "sub w12, w10, #8\n\t"
700 "lsl w12, w12, #3\n\t"
701 "sub SP, SP, w12, uxtw\n\t"
702 "tbz w12, #3, 1f\n\t"
703 "sub SP, SP, #8\n\t"
704 "1: sub w12, w12, #8\n\t"
705 "ldr x13, [x11, x12]\n\t"
706 "str x13, [SP, x12]\n\t"
707 "cbnz w12, 1b\n\t"
708 "2: blr x9\n\t"
709 "mov SP, x29\n\t"
710 "ldp x19, x20, [SP], #16\n\t"
711 "ldp x29, x30, [SP], #16\n\t"
712 "ret\n" )
714 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
716 unsigned int nb_args;
717 void *func = relay_trace_entry( descr, idx, stack, &nb_args );
718 LONGLONG ret = call_entry_point( func, nb_args, stack );
719 relay_trace_exit( descr, idx, stack[-1], ret );
720 return ret;
723 #elif defined(__x86_64__)
725 /***********************************************************************
726 * relay_trace_entry
728 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
729 const INT_PTR *stack, unsigned int *nb_args )
731 WORD ordinal = LOWORD(idx);
732 const char *arg_types = descr->args_string + HIWORD(idx);
733 struct relay_private_data *data = descr->private;
734 struct relay_entry_point *entry_point = data->entry_points + ordinal;
735 unsigned int i;
737 TRACE( "\1Call %s(", func_name( data, ordinal ));
739 for (i = 0; !is_ret_val( arg_types[i] ); i++)
741 switch (arg_types[i])
743 case 's': /* str */
744 trace_string_a( stack[i] );
745 break;
746 case 'w': /* wstr */
747 trace_string_w( stack[i] );
748 break;
749 case 'f': /* float */
750 TRACE( "%g", *(const float *)&stack[i] );
751 break;
752 case 'd': /* double */
753 TRACE( "%g", *(const double *)&stack[i] );
754 break;
755 case 'i': /* long */
756 default:
757 TRACE( "%08lx", stack[i] );
758 break;
760 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
762 *nb_args = i;
763 TRACE( ") ret=%08lx\n", stack[-1] );
764 return entry_point->orig_func;
767 /***********************************************************************
768 * relay_trace_exit
770 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
771 INT_PTR retaddr, INT_PTR retval )
773 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
774 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
777 extern INT_PTR WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
778 __ASM_GLOBAL_FUNC( relay_call,
779 "pushq %rbp\n\t"
780 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
781 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
782 "movq %rsp,%rbp\n\t"
783 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
784 "leaq -0x48(%rbp),%rsp\n\t"
785 "andq $~15,%rsp\n\t"
786 "movq %rcx,-32(%rbp)\n\t"
787 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
788 "movq %rdx,-24(%rbp)\n\t"
789 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
790 "movq %rsi,-16(%rbp)\n\t"
791 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
792 "movq %rdi,-8(%rbp)\n\t"
793 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
794 /* trace the parameters */
795 "leaq 24(%rbp),%r8\n\t" /* stack */
796 "leaq -40(%rbp),%r9\n\t"
797 "call " __ASM_NAME("relay_trace_entry") "\n\t"
798 /* copy the arguments */
799 "movl -40(%rbp),%edx\n\t" /* number of args */
800 "movq $4,%rcx\n\t"
801 "cmp %rcx,%rdx\n\t"
802 "cmovgq %rdx,%rcx\n\t"
803 "leaq -16(,%rcx,8),%rdx\n\t"
804 "andq $~15,%rdx\n\t"
805 "subq %rdx,%rsp\n\t"
806 "leaq 24(%rbp),%rsi\n\t" /* original stack */
807 "movq %rsp,%rdi\n\t"
808 "rep; movsq\n\t"
809 /* call the entry point */
810 "movq 0(%rsp),%rcx\n\t"
811 "movq 8(%rsp),%rdx\n\t"
812 "movq 16(%rsp),%r8\n\t"
813 "movq 24(%rsp),%r9\n\t"
814 "movq 0(%rsp),%xmm0\n\t"
815 "movq 8(%rsp),%xmm1\n\t"
816 "movq 16(%rsp),%xmm2\n\t"
817 "movq 24(%rsp),%xmm3\n\t"
818 "callq *%rax\n\t"
819 /* trace the return value */
820 "movq -32(%rbp),%rcx\n\t"
821 "movq -24(%rbp),%rdx\n\t"
822 "movq 16(%rbp),%r8\n\t" /* retaddr */
823 "movq %rax,%rsi\n\t"
824 "movaps %xmm0,32(%rsp)\n\t"
825 "movq %rax,%r9\n\t"
826 "call " __ASM_NAME("relay_trace_exit") "\n\t"
827 /* restore return value and return */
828 "movq %rsi,%rax\n\t"
829 "movaps 32(%rsp),%xmm0\n\t"
830 "movq -16(%rbp),%rsi\n\t"
831 __ASM_CFI(".cfi_same_value %rsi\n\t")
832 "movq -8(%rbp),%rdi\n\t"
833 __ASM_CFI(".cfi_same_value %rdi\n\t")
834 "movq %rbp,%rsp\n\t"
835 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
836 "popq %rbp\n\t"
837 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
838 __ASM_CFI(".cfi_same_value %rbp\n\t")
839 "ret")
841 #else
842 #error Not supported on this CPU
843 #endif
846 static struct relay_descr *get_relay_descr( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
847 DWORD exp_size )
849 struct relay_descr *descr;
850 struct relay_descr_rva *rva;
851 ULONG_PTR ptr = (ULONG_PTR)module + exports->Name;
853 /* sanity checks */
854 if (ptr <= (ULONG_PTR)(exports + 1)) return NULL;
855 if (ptr > (ULONG_PTR)exports + exp_size) return NULL;
856 if (ptr % sizeof(DWORD)) return NULL;
858 rva = (struct relay_descr_rva *)ptr - 1;
859 if (rva->magic != RELAY_DESCR_MAGIC) return NULL;
860 if (rva->descr) descr = (struct relay_descr *)((char *)module + rva->descr);
861 else descr = (struct relay_descr *)((const char *)exports + exp_size);
862 if (descr->magic != RELAY_DESCR_MAGIC) return NULL;
863 return descr;
866 /***********************************************************************
867 * RELAY_GetProcAddress
869 * Return the proc address to use for a given function.
871 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
872 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
874 struct relay_private_data *data;
875 const struct relay_descr *descr = get_relay_descr( module, exports, exp_size );
877 if (!descr || !(data = descr->private)) return proc; /* no relay data */
878 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
879 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
880 return proc; /* we want to relay it */
881 return data->entry_points[ordinal].orig_func;
885 /***********************************************************************
886 * RELAY_SetupDLL
888 * Setup relay debugging for a built-in dll.
890 void RELAY_SetupDLL( HMODULE module )
892 IMAGE_EXPORT_DIRECTORY *exports;
893 DWORD *funcs;
894 unsigned int i, len;
895 DWORD size, entry_point_rva, old_prot;
896 struct relay_descr *descr;
897 struct relay_private_data *data;
898 const WORD *ordptr;
899 void *func_base;
900 SIZE_T func_size;
902 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
904 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
905 if (!exports) return;
907 if (!(descr = get_relay_descr( module, exports, size ))) return;
909 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
910 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
911 return;
913 descr->relay_call = relay_call;
914 descr->private = data;
916 data->module = module;
917 data->base = exports->Base;
918 len = strlen( (char *)module + exports->Name );
919 if (len > 4 && !_stricmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
920 len = min( len, sizeof(data->dllname) - 1 );
921 memcpy( data->dllname, (char *)module + exports->Name, len );
922 data->dllname[len] = 0;
924 /* fetch name pointer for all entry points and store them in the private structure */
926 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
927 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
929 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
930 data->entry_points[*ordptr].name = (const char *)module + name_rva;
933 /* patch the functions in the export table to point to the relay thunks */
935 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
936 entry_point_rva = descr->entry_point_base - (const char *)module;
938 func_base = funcs;
939 func_size = exports->NumberOfFunctions * sizeof(*funcs);
940 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, PAGE_READWRITE, &old_prot );
941 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
943 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
944 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
945 continue; /* don't include this entry point */
947 data->entry_points[i].orig_func = (char *)module + *funcs;
948 *funcs = entry_point_rva + descr->entry_point_offsets[i];
950 if (old_prot != PAGE_READWRITE)
951 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, old_prot, &old_prot );
954 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
956 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
957 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
959 return proc;
962 void RELAY_SetupDLL( HMODULE module )
966 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
969 /***********************************************************************/
970 /* snoop support */
971 /***********************************************************************/
973 #ifdef __i386__
975 WINE_DECLARE_DEBUG_CHANNEL(seh);
976 WINE_DECLARE_DEBUG_CHANNEL(snoop);
978 #include "pshpack1.h"
980 typedef struct
982 /* code part */
983 BYTE lcall; /* 0xe8 call snoopentry (relative) */
984 DWORD snoopentry; /* SNOOP_Entry relative */
985 /* unreached */
986 int nrofargs;
987 FARPROC origfun;
988 const char *name;
989 } SNOOP_FUN;
991 typedef struct tagSNOOP_DLL {
992 HMODULE hmod;
993 SNOOP_FUN *funs;
994 DWORD ordbase;
995 DWORD nrofordinals;
996 struct tagSNOOP_DLL *next;
997 char name[1];
998 } SNOOP_DLL;
1000 typedef struct
1002 /* code part */
1003 BYTE lcall; /* 0xe8 call snoopret relative*/
1004 DWORD snoopret; /* SNOOP_Ret relative */
1005 /* unreached */
1006 FARPROC origreturn;
1007 SNOOP_DLL *dll;
1008 DWORD ordinal;
1009 void **origESP;
1010 DWORD *args; /* saved args across a stdcall */
1011 } SNOOP_RETURNENTRY;
1013 typedef struct tagSNOOP_RETURNENTRIES {
1014 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
1015 struct tagSNOOP_RETURNENTRIES *next;
1016 } SNOOP_RETURNENTRIES;
1018 #include "poppack.h"
1020 extern void WINAPI SNOOP_Entry(void);
1021 extern void WINAPI SNOOP_Return(void);
1023 static SNOOP_DLL *firstdll;
1024 static SNOOP_RETURNENTRIES *firstrets;
1027 /***********************************************************************
1028 * SNOOP_ShowDebugmsgSnoop
1030 * Simple function to decide if a particular debugging message is
1031 * wanted.
1033 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
1035 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
1036 return FALSE;
1037 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
1038 return FALSE;
1039 return TRUE;
1043 /***********************************************************************
1044 * SNOOP_SetupDLL
1046 * Setup snoop debugging for a native dll.
1048 void SNOOP_SetupDLL(HMODULE hmod)
1050 SNOOP_DLL **dll = &firstdll;
1051 char *p, *name;
1052 void *addr;
1053 SIZE_T size;
1054 ULONG size32;
1055 IMAGE_EXPORT_DIRECTORY *exports;
1057 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1059 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1060 if (!exports || !exports->NumberOfFunctions) return;
1061 name = (char *)hmod + exports->Name;
1062 size = size32;
1064 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1066 while (*dll) {
1067 if ((*dll)->hmod == hmod)
1069 /* another dll, loaded at the same address */
1070 addr = (*dll)->funs;
1071 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1072 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1073 break;
1075 dll = &((*dll)->next);
1077 if (*dll)
1078 *dll = RtlReAllocateHeap(GetProcessHeap(),
1079 HEAP_ZERO_MEMORY, *dll,
1080 sizeof(SNOOP_DLL) + strlen(name));
1081 else
1082 *dll = RtlAllocateHeap(GetProcessHeap(),
1083 HEAP_ZERO_MEMORY,
1084 sizeof(SNOOP_DLL) + strlen(name));
1085 (*dll)->hmod = hmod;
1086 (*dll)->ordbase = exports->Base;
1087 (*dll)->nrofordinals = exports->NumberOfFunctions;
1088 strcpy( (*dll)->name, name );
1089 p = (*dll)->name + strlen((*dll)->name) - 4;
1090 if (p > (*dll)->name && !_stricmp( p, ".dll" )) *p = 0;
1092 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1093 addr = NULL;
1094 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1095 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1096 if (!addr) {
1097 RtlFreeHeap(GetProcessHeap(),0,*dll);
1098 FIXME("out of memory\n");
1099 return;
1101 (*dll)->funs = addr;
1102 memset((*dll)->funs,0,size);
1106 /***********************************************************************
1107 * SNOOP_GetProcAddress
1109 * Return the proc address to use for a given function.
1111 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1112 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1113 const WCHAR *user)
1115 unsigned int i;
1116 const char *ename;
1117 const WORD *ordinals;
1118 const DWORD *names;
1119 SNOOP_DLL *dll = firstdll;
1120 SNOOP_FUN *fun;
1121 const IMAGE_SECTION_HEADER *sec;
1123 if (!TRACE_ON(snoop)) return origfun;
1124 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1125 return origfun; /* the calling module was explicitly excluded */
1127 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1128 return origfun;
1130 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1132 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1133 return origfun; /* most likely a data reference */
1135 while (dll) {
1136 if (hmod == dll->hmod)
1137 break;
1138 dll = dll->next;
1140 if (!dll) /* probably internal */
1141 return origfun;
1143 /* try to find a name for it */
1144 ename = NULL;
1145 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1146 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1147 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1149 if (ordinals[i] == ordinal)
1151 ename = (const char *)hmod + names[i];
1152 break;
1155 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1156 return origfun;
1157 assert(ordinal < dll->nrofordinals);
1158 fun = dll->funs + ordinal;
1159 if (!fun->name)
1161 fun->name = ename;
1162 fun->lcall = 0xe8;
1163 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1164 fun->origfun = origfun;
1165 fun->nrofargs = -1;
1167 return (FARPROC)&(fun->lcall);
1170 static void SNOOP_PrintArg(DWORD x)
1172 int i,nostring;
1174 TRACE_(snoop)("%08x",x);
1175 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1176 __TRY
1178 LPBYTE s=(LPBYTE)x;
1179 i=0;nostring=0;
1180 while (i<80) {
1181 if (s[i]==0) break;
1182 if (s[i]<0x20) {nostring=1;break;}
1183 if (s[i]>=0x80) {nostring=1;break;}
1184 i++;
1186 if (!nostring && i > 5)
1187 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1188 else /* try unicode */
1190 LPWSTR s=(LPWSTR)x;
1191 i=0;nostring=0;
1192 while (i<80) {
1193 if (s[i]==0) break;
1194 if (s[i]<0x20) {nostring=1;break;}
1195 if (s[i]>0x100) {nostring=1;break;}
1196 i++;
1198 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1201 __EXCEPT_PAGE_FAULT
1204 __ENDTRY
1207 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1209 SNOOP_DLL *dll;
1210 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1211 SNOOP_RETURNENTRIES **rets = &firstrets;
1212 SNOOP_RETURNENTRY *ret;
1213 int i=0, max;
1215 for (dll = firstdll; dll; dll = dll->next )
1216 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1218 if (!dll) {
1219 FIXME("entrypoint %p not found\n", fun);
1220 return; /* oops */
1222 /* guess cdecl ... */
1223 if (fun->nrofargs<0) {
1224 /* Typical cdecl return frame is:
1225 * add esp, xxxxxxxx
1226 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1227 * (after that 81 C2 xx xx xx xx)
1229 LPBYTE reteip = stack[1];
1231 if (reteip) {
1232 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1233 fun->nrofargs=reteip[2]/4;
1238 while (*rets) {
1239 for (i=0;i<ARRAY_SIZE( (*rets)->entry );i++)
1240 if (!(*rets)->entry[i].origreturn)
1241 break;
1242 if (i!=ARRAY_SIZE( (*rets)->entry ))
1243 break;
1244 rets = &((*rets)->next);
1246 if (!*rets) {
1247 SIZE_T size = 4096;
1248 VOID* addr = NULL;
1250 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1251 MEM_COMMIT | MEM_RESERVE,
1252 PAGE_EXECUTE_READWRITE);
1253 if (!addr) return;
1254 *rets = addr;
1255 i = 0; /* entry 0 is free */
1257 ret = &((*rets)->entry[i]);
1258 ret->lcall = 0xe8;
1259 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1260 ret->origreturn = stack[1];
1261 stack[1] = &ret->lcall;
1262 ret->dll = dll;
1263 ret->args = NULL;
1264 ret->ordinal = fun - dll->funs;
1265 ret->origESP = stack;
1267 stack[0] = fun->origfun;
1269 if (!TRACE_ON(snoop)) return;
1271 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1272 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1273 if (fun->nrofargs>0) {
1274 max = fun->nrofargs; if (max>16) max=16;
1275 for (i=0;i<max;i++)
1277 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1278 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1280 if (max!=fun->nrofargs)
1281 TRACE_(snoop)(" ...");
1282 } else if (fun->nrofargs<0) {
1283 TRACE_(snoop)("<unknown, check return>");
1284 ret->args = RtlAllocateHeap(GetProcessHeap(),
1285 0,16*sizeof(DWORD));
1286 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1288 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1291 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1293 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1294 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1295 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1297 /* We haven't found out the nrofargs yet. If we called a cdecl
1298 * function it is too late anyway and we can just set '0' (which
1299 * will be the difference between orig and current ESP
1300 * If stdcall -> everything ok.
1302 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1303 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1304 stack[0] = ret->origreturn;
1306 if (!TRACE_ON(snoop)) {
1307 ret->origreturn = NULL; /* mark as empty */
1308 return;
1311 if (ret->args) {
1312 int i,max;
1314 if (fun->name)
1315 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1316 else
1317 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1319 max = fun->nrofargs;
1320 if (max>16) max=16;
1322 for (i=0;i<max;i++)
1324 SNOOP_PrintArg(ret->args[i]);
1325 if (i<max-1) TRACE_(snoop)(",");
1327 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1328 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1329 ret->args = NULL;
1331 else
1333 if (fun->name)
1334 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1335 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1336 else
1337 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1338 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1339 retval, (DWORD)ret->origreturn);
1341 ret->origreturn = NULL; /* mark as empty */
1344 /* small wrappers that save registers and get the stack pointer */
1345 #define SNOOP_WRAPPER(name) \
1346 __ASM_STDCALL_FUNC( name, 0, \
1347 "pushl %eax\n\t" \
1348 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1349 "pushl %ecx\n\t" \
1350 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1351 "pushl %edx\n\t" \
1352 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1353 "leal 12(%esp),%eax\n\t" \
1354 "pushl %eax\n\t" \
1355 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1356 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1357 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1358 "popl %edx\n\t" \
1359 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1360 "popl %ecx\n\t" \
1361 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1362 "popl %eax\n\t" \
1363 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1364 "ret" )
1366 SNOOP_WRAPPER( SNOOP_Entry )
1367 SNOOP_WRAPPER( SNOOP_Return )
1369 #else /* __i386__ */
1371 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1372 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1374 return origfun;
1377 void SNOOP_SetupDLL( HMODULE hmod )
1379 FIXME("snooping works only on i386 for now.\n");
1382 #endif /* __i386__ */