ntoskrnl.exe: Implement ExAcquireFastMutex and ExReleaseFastMutex.
[wine.git] / dlls / ntdll / relay.c
blob255d0e12cd0ee85f07d64e93b332d6d3f2bef0e2
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 void *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 #define RELAY_DESCR_MAGIC ((void *)0xdeb90002)
54 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
56 /* private data built at dll load time */
58 struct relay_entry_point
60 void *orig_func; /* original entry point function */
61 const char *name; /* function name (if any) */
64 struct relay_private_data
66 HMODULE module; /* module handle of this dll */
67 unsigned int base; /* ordinal base */
68 char dllname[40]; /* dll name (without .dll extension) */
69 struct relay_entry_point entry_points[1]; /* list of dll entry points */
72 static const WCHAR **debug_relay_excludelist;
73 static const WCHAR **debug_relay_includelist;
74 static const WCHAR **debug_snoop_excludelist;
75 static const WCHAR **debug_snoop_includelist;
76 static const WCHAR **debug_from_relay_excludelist;
77 static const WCHAR **debug_from_relay_includelist;
78 static const WCHAR **debug_from_snoop_excludelist;
79 static const WCHAR **debug_from_snoop_includelist;
81 static RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
83 /* compare an ASCII and a Unicode string without depending on the current codepage */
84 static inline int strcmpAW( const char *strA, const WCHAR *strW )
86 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
87 return (unsigned char)*strA - *strW;
90 /* compare an ASCII and a Unicode string without depending on the current codepage */
91 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
93 int ret = 0;
94 for ( ; n > 0; n--, strA++, strW++)
95 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
96 return ret;
99 /***********************************************************************
100 * build_list
102 * Build a function list from a ';'-separated string.
104 static const WCHAR **build_list( const WCHAR *buffer )
106 int count = 1;
107 const WCHAR *p = buffer;
108 const WCHAR **ret;
110 while ((p = strchrW( p, ';' )))
112 count++;
113 p++;
115 /* allocate count+1 pointers, plus the space for a copy of the string */
116 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
117 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
119 WCHAR *str = (WCHAR *)(ret + count + 1);
120 WCHAR *q = str;
122 strcpyW( str, buffer );
123 count = 0;
124 for (;;)
126 ret[count++] = q;
127 if (!(q = strchrW( q, ';' ))) break;
128 *q++ = 0;
130 ret[count++] = NULL;
132 return ret;
135 /***********************************************************************
136 * load_list_value
138 * Load a function list from a registry value.
140 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
142 char initial_buffer[4096];
143 char *buffer = initial_buffer;
144 DWORD count;
145 NTSTATUS status;
146 UNICODE_STRING name;
147 const WCHAR **list = NULL;
149 RtlInitUnicodeString( &name, value );
150 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(initial_buffer), &count );
151 if (status == STATUS_BUFFER_OVERFLOW)
153 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
154 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
156 if (status == STATUS_SUCCESS)
158 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
159 list = build_list( str );
160 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
163 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
164 return list;
167 /***********************************************************************
168 * init_debug_lists
170 * Build the relay include/exclude function lists.
172 static DWORD WINAPI init_debug_lists( RTL_RUN_ONCE *once, void *param, void **context )
174 OBJECT_ATTRIBUTES attr;
175 UNICODE_STRING name;
176 HANDLE root, hkey;
177 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
178 'W','i','n','e','\\',
179 'D','e','b','u','g',0};
180 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
181 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
182 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
183 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
184 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
185 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
186 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
187 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
189 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
190 attr.Length = sizeof(attr);
191 attr.RootDirectory = root;
192 attr.ObjectName = &name;
193 attr.Attributes = 0;
194 attr.SecurityDescriptor = NULL;
195 attr.SecurityQualityOfService = NULL;
196 RtlInitUnicodeString( &name, configW );
198 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
199 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
200 NtClose( root );
201 if (!hkey) return TRUE;
203 debug_relay_includelist = load_list( hkey, RelayIncludeW );
204 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
205 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
206 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
207 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
208 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
209 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
210 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
212 NtClose( hkey );
213 return TRUE;
217 /***********************************************************************
218 * check_list
220 * Check if a given module and function is in the list.
222 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
224 char ord_str[10];
226 sprintf( ord_str, "%d", ordinal );
227 for(; *list; list++)
229 const WCHAR *p = strrchrW( *list, '.' );
230 if (p && p > *list) /* check module and function */
232 int len = p - *list;
233 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
234 if (p[1] == '*' && !p[2]) return TRUE;
235 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
236 if (func && !strcmpAW( func, p + 1 )) return TRUE;
238 else /* function only */
240 if (func && !strcmpAW( func, *list )) return TRUE;
243 return FALSE;
247 /***********************************************************************
248 * check_relay_include
250 * Check if a given function must be included in the relay output.
252 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
254 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
255 return FALSE;
256 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
257 return FALSE;
258 return TRUE;
261 /***********************************************************************
262 * check_from_module
264 * Check if calls from a given module must be included in the relay/snoop output,
265 * given the exclusion and inclusion lists.
267 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
269 static const WCHAR dllW[] = {'.','d','l','l',0 };
270 const WCHAR **listitem;
271 BOOL show;
273 if (!module) return TRUE;
274 if (!includelist && !excludelist) return TRUE;
275 if (excludelist)
277 show = TRUE;
278 listitem = excludelist;
280 else
282 show = FALSE;
283 listitem = includelist;
285 for(; *listitem; listitem++)
287 int len;
289 if (!strcmpiW( *listitem, module )) return !show;
290 len = strlenW( *listitem );
291 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
292 return !show;
294 return show;
298 static BOOL is_ret_val( char type )
300 return type >= 'A' && type <= 'Z';
303 static const char *func_name( struct relay_private_data *data, unsigned int ordinal )
305 struct relay_entry_point *entry_point = data->entry_points + ordinal;
307 if (entry_point->name)
308 return wine_dbg_sprintf( "%s.%s", data->dllname, entry_point->name );
309 else
310 return wine_dbg_sprintf( "%s.%u", data->dllname, data->base + ordinal );
313 static void trace_string_a( INT_PTR ptr )
315 if (!IS_INTARG( ptr )) TRACE( "%08lx %s", ptr, debugstr_a( (char *)ptr ));
316 else TRACE( "%08lx", ptr );
319 static void trace_string_w( INT_PTR ptr )
321 if (!IS_INTARG( ptr )) TRACE( "%08lx %s", ptr, debugstr_w( (WCHAR *)ptr ));
322 else TRACE( "%08lx", ptr );
325 #ifdef __i386__
327 /***********************************************************************
328 * relay_trace_entry
330 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
331 const DWORD *stack, unsigned int *nb_args )
333 WORD ordinal = LOWORD(idx);
334 const char *arg_types = descr->args_string + HIWORD(idx);
335 struct relay_private_data *data = descr->private;
336 struct relay_entry_point *entry_point = data->entry_points + ordinal;
337 unsigned int i, pos;
339 TRACE( "\1Call %s(", func_name( data, ordinal ));
341 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
343 switch (arg_types[i])
345 case 'j': /* int64 */
346 TRACE( "%x%08x", stack[pos+1], stack[pos] );
347 pos += 2;
348 break;
349 case 'k': /* int128 */
350 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
351 pos += 4;
352 break;
353 case 's': /* str */
354 trace_string_a( stack[pos++] );
355 break;
356 case 'w': /* wstr */
357 trace_string_w( stack[pos++] );
358 break;
359 case 'f': /* float */
360 TRACE( "%g", *(const float *)&stack[pos++] );
361 break;
362 case 'd': /* double */
363 TRACE( "%g", *(const double *)&stack[pos] );
364 pos += 2;
365 break;
366 case 'i': /* long */
367 default:
368 TRACE( "%08x", stack[pos++] );
369 break;
371 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
373 *nb_args = pos;
374 if (arg_types[0] == 't')
376 *nb_args |= 0x80000000; /* thiscall/fastcall */
377 if (arg_types[1] == 't') *nb_args |= 0x40000000; /* fastcall */
379 TRACE( ") ret=%08x\n", stack[-1] );
380 return entry_point->orig_func;
383 /***********************************************************************
384 * relay_trace_exit
386 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
387 void *retaddr, LONGLONG retval )
389 const char *arg_types = descr->args_string + HIWORD(idx);
391 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
393 while (!is_ret_val( *arg_types )) arg_types++;
394 if (*arg_types == 'J') /* int64 return value */
395 TRACE( " retval=%08x%08x ret=%08x\n",
396 (UINT)(retval >> 32), (UINT)retval, (UINT)retaddr );
397 else
398 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, (UINT)retaddr );
401 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx );
402 __ASM_GLOBAL_FUNC( relay_call,
403 "pushl %ebp\n\t"
404 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
405 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
406 "movl %esp,%ebp\n\t"
407 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
408 "pushl %esi\n\t"
409 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
410 "pushl %edi\n\t"
411 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
412 "pushl %ecx\n\t"
413 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
414 /* trace the parameters */
415 "pushl %eax\n\t"
416 "pushl %esp\n\t" /* number of args return ptr */
417 "leal 20(%ebp),%esi\n\t" /* stack */
418 "pushl %esi\n\t"
419 "pushl 12(%ebp)\n\t"
420 "pushl 8(%ebp)\n\t"
421 "call " __ASM_NAME("relay_trace_entry") "\n\t"
422 /* copy the arguments*/
423 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
424 "jecxz 1f\n\t"
425 "leal 0(,%ecx,4),%edx\n\t"
426 "subl %edx,%esp\n\t"
427 "andl $~15,%esp\n\t"
428 "movl %esp,%edi\n\t"
429 "cld\n\t"
430 "rep; movsl\n\t"
431 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
432 "jz 1f\n\t"
433 "popl %ecx\n\t"
434 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
435 "jz 1f\n\t"
436 "popl %edx\n"
437 /* call the entry point */
438 "1:\tcall *%eax\n\t"
439 "movl %eax,%esi\n\t"
440 "movl %edx,%edi\n\t"
441 /* trace the return value */
442 "leal -20(%ebp),%esp\n\t"
443 "pushl %edx\n\t"
444 "pushl %eax\n\t"
445 "pushl 16(%ebp)\n\t"
446 "pushl 12(%ebp)\n\t"
447 "pushl 8(%ebp)\n\t"
448 "call " __ASM_NAME("relay_trace_exit") "\n\t"
449 /* restore return value and return */
450 "leal -12(%ebp),%esp\n\t"
451 "movl %esi,%eax\n\t"
452 "movl %edi,%edx\n\t"
453 "popl %ecx\n\t"
454 __ASM_CFI(".cfi_same_value %ecx\n\t")
455 "popl %edi\n\t"
456 __ASM_CFI(".cfi_same_value %edi\n\t")
457 "popl %esi\n\t"
458 __ASM_CFI(".cfi_same_value %esi\n\t")
459 "popl %ebp\n\t"
460 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
461 __ASM_CFI(".cfi_same_value %ebp\n\t")
462 "ret $8" )
464 #elif defined(__arm__)
466 /***********************************************************************
467 * relay_trace_entry
469 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
470 const DWORD *stack, unsigned int *nb_args )
472 WORD ordinal = LOWORD(idx);
473 const char *arg_types = descr->args_string + HIWORD(idx);
474 struct relay_private_data *data = descr->private;
475 struct relay_entry_point *entry_point = data->entry_points + ordinal;
476 unsigned int i, pos;
477 #ifndef __SOFTFP__
478 unsigned int float_pos = 0, double_pos = 0;
479 const union fpregs { float s[16]; double d[8]; } *fpstack = (const union fpregs *)stack - 1;
480 #endif
482 TRACE( "\1Call %s(", func_name( data, ordinal ));
484 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
486 switch (arg_types[i])
488 case 'j': /* int64 */
489 pos = (pos + 1) & ~1;
490 TRACE( "%x%08x", stack[pos+1], stack[pos] );
491 pos += 2;
492 break;
493 case 'k': /* int128 */
494 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
495 pos += 4;
496 break;
497 case 's': /* str */
498 trace_string_a( stack[pos++] );
499 break;
500 case 'w': /* wstr */
501 trace_string_w( stack[pos++] );
502 break;
503 case 'f': /* float */
504 #ifndef __SOFTFP__
505 if (!(float_pos % 2)) float_pos = max( float_pos, double_pos * 2 );
506 if (float_pos < 16)
508 TRACE( "%g", fpstack->s[float_pos++] );
509 break;
511 #endif
512 TRACE( "%g", *(const float *)&stack[pos++] );
513 break;
514 case 'd': /* double */
515 #ifndef __SOFTFP__
516 double_pos = max( (float_pos + 1) / 2, double_pos );
517 if (double_pos < 8)
519 TRACE( "%g", fpstack->d[double_pos++] );
520 break;
522 #endif
523 pos = (pos + 1) & ~1;
524 TRACE( "%g", *(const double *)&stack[pos] );
525 pos += 2;
526 break;
527 case 'i': /* long */
528 default:
529 TRACE( "%08x", stack[pos++] );
530 break;
532 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
535 #ifndef __SOFTFP__
536 if (float_pos || double_pos)
538 pos |= 0x80000000;
539 stack = (const DWORD *)fpstack; /* retaddr is below the fp regs */
541 #endif
542 *nb_args = pos;
543 TRACE( ") ret=%08x\n", stack[-1] );
544 return entry_point->orig_func;
547 /***********************************************************************
548 * relay_trace_exit
550 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
551 DWORD retaddr, LONGLONG retval )
553 const char *arg_types = descr->args_string + HIWORD(idx);
555 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
557 while (!is_ret_val( *arg_types )) arg_types++;
558 if (*arg_types == 'J') /* int64 return value */
559 TRACE( " retval=%08x%08x ret=%08x\n",
560 (UINT)(retval >> 32), (UINT)retval, retaddr );
561 else
562 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, retaddr );
565 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const DWORD *stack );
566 __ASM_GLOBAL_FUNC( relay_call,
567 ".arm\n\t"
568 "push {r4-r8,lr}\n\t"
569 "sub sp, #16\n\t"
570 "mov r6, r2\n\t"
571 "add r3, sp, #12\n\t"
572 "mov r7, r0\n\t"
573 "mov r8, r1\n\t"
574 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
575 "mov ip, r0\n\t" /* entry point */
576 "mov r5, sp\n\t"
577 "ldr r1, [sp, #12]\n\t" /* number of args */
578 "lsl r3, r1, #2\n\t"
579 "subs r3, #16\n\t" /* first 4 args are in registers */
580 "ble 2f\n\t"
581 "sub sp, r3\n\t"
582 "and sp, #~7\n"
583 "add r2, r6, #16\n\t" /* skip r0-r3 */
584 "1:\tsubs r3, r3, #4\n\t"
585 "ldr r0, [r2, r3]\n\t"
586 "str r0, [sp, r3]\n\t"
587 "bgt 1b\n"
588 "2:\t"
589 #ifndef __SOFTFP__
590 "tst r1, #0x80000000\n\t"
591 "ldm r6, {r0-r3}\n\t"
592 "vldmdbne r6!, {s0-s15}\n\t"
593 #else
594 "ldm r6, {r0-r3}\n\t"
595 #endif
596 "blx ip\n\t"
597 "mov sp, r5\n\t"
598 "ldr r2, [r6, #-4]\n\t" /* retaddr */
599 "mov r4, r0\n\t"
600 "mov r5, r1\n\t"
601 "mov r0, r7\n\t"
602 "mov r1, r8\n\t"
603 "str r4, [sp]\n\t"
604 "str r5, [sp, #4]\n\t"
605 #ifndef __SOFTFP__
606 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
607 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
608 "vldr d0, [sp, #8]\n\t"
609 #else
610 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
611 #endif
612 "mov r0, r4\n\t"
613 "mov r1, r5\n\t"
614 "add sp, #16\n\t"
615 "pop {r4-r8,pc}" )
617 #elif defined(__aarch64__)
619 /***********************************************************************
620 * relay_trace_entry
622 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
623 const INT_PTR *stack, unsigned int *nb_args )
625 WORD ordinal = LOWORD(idx);
626 const char *arg_types = descr->args_string + HIWORD(idx);
627 struct relay_private_data *data = descr->private;
628 struct relay_entry_point *entry_point = data->entry_points + ordinal;
629 unsigned int i;
631 TRACE( "\1Call %s(", func_name( data, ordinal ));
633 for (i = 0; !is_ret_val( arg_types[i] ); i++)
635 switch (arg_types[i])
637 case 's': /* str */
638 trace_string_a( stack[i] );
639 break;
640 case 'w': /* wstr */
641 trace_string_w( stack[i] );
642 break;
643 case 'i': /* long */
644 default:
645 TRACE( "%08lx", stack[i] );
646 break;
648 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
650 *nb_args = i;
651 TRACE( ") ret=%08lx\n", stack[-1] );
652 return entry_point->orig_func;
655 /***********************************************************************
656 * relay_trace_exit
658 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
659 INT_PTR retaddr, INT_PTR retval )
661 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
662 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
665 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
666 __ASM_GLOBAL_FUNC( call_entry_point,
667 "stp x29, x30, [SP,#-16]!\n\t"
668 "stp x19, x20, [SP,#-16]!\n\t"
669 "mov x29, SP\n\t"
670 "mov x19, x2\n\t"
671 "ldp x8, x9, [x19, #-32]\n\t"
672 "mov x9, x0\n\t"
673 "cbz w1, 2f\n\t"
674 "mov w10, w1\n\t"
675 "mov x11, x2\n\t"
676 "mov x12, #0\n\t"
677 "ldp x0, x1, [x11], #16\n\t"
678 "add w12, w12, #2\n\t"
679 "cmp w12, w10\n\t"
680 "b.hs 2f\n\t"
681 "ldp x2, x3, [x11], #16\n\t"
682 "add w12, w12, #2\n\t"
683 "cmp w12, w10\n\t"
684 "b.hs 2f\n\t"
685 "ldp x4, x5, [x11], #16\n\t"
686 "add w12, w12, #2\n\t"
687 "cmp w12, w10\n\t"
688 "b.hs 2f\n\t"
689 "ldp x6, x7, [x11], #16\n\t"
690 "add w12, w12, #2\n\t"
691 "cmp w12, w10\n\t"
692 "b.hs 2f\n\t"
693 "sub w12, w10, #8\n\t"
694 "lsl w12, w12, #3\n\t"
695 "sub SP, SP, w12, uxtw\n\t"
696 "tbz w12, #3, 1f\n\t"
697 "sub SP, SP, #8\n\t"
698 "1: sub w12, w12, #8\n\t"
699 "ldr x13, [x11, x12]\n\t"
700 "str x13, [SP, x12]\n\t"
701 "cbnz w12, 1b\n\t"
702 "2: blr x9\n\t"
703 "mov SP, x29\n\t"
704 "ldp x19, x20, [SP], #16\n\t"
705 "ldp x29, x30, [SP], #16\n\t"
706 "ret\n" )
708 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
710 unsigned int nb_args;
711 void *func = relay_trace_entry( descr, idx, stack, &nb_args );
712 LONGLONG ret = call_entry_point( func, nb_args, stack );
713 relay_trace_exit( descr, idx, stack[-1], ret );
714 return ret;
717 #elif defined(__x86_64__)
719 /***********************************************************************
720 * relay_trace_entry
722 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
723 const INT_PTR *stack, unsigned int *nb_args )
725 WORD ordinal = LOWORD(idx);
726 const char *arg_types = descr->args_string + HIWORD(idx);
727 struct relay_private_data *data = descr->private;
728 struct relay_entry_point *entry_point = data->entry_points + ordinal;
729 unsigned int i;
731 TRACE( "\1Call %s(", func_name( data, ordinal ));
733 for (i = 0; !is_ret_val( arg_types[i] ); i++)
735 switch (arg_types[i])
737 case 's': /* str */
738 trace_string_a( stack[i] );
739 break;
740 case 'w': /* wstr */
741 trace_string_w( stack[i] );
742 break;
743 case 'f': /* float */
744 TRACE( "%g", *(const float *)&stack[i] );
745 break;
746 case 'd': /* double */
747 TRACE( "%g", *(const double *)&stack[i] );
748 break;
749 case 'i': /* long */
750 default:
751 TRACE( "%08lx", stack[i] );
752 break;
754 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
756 *nb_args = i;
757 TRACE( ") ret=%08lx\n", stack[-1] );
758 return entry_point->orig_func;
761 /***********************************************************************
762 * relay_trace_exit
764 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
765 INT_PTR retaddr, INT_PTR retval )
767 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
768 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
771 extern INT_PTR WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
772 __ASM_GLOBAL_FUNC( relay_call,
773 "pushq %rbp\n\t"
774 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
775 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
776 "movq %rsp,%rbp\n\t"
777 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
778 "leaq -0x48(%rbp),%rsp\n\t"
779 "andq $~15,%rsp\n\t"
780 "movq %rcx,-32(%rbp)\n\t"
781 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
782 "movq %rdx,-24(%rbp)\n\t"
783 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
784 "movq %rsi,-16(%rbp)\n\t"
785 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
786 "movq %rdi,-8(%rbp)\n\t"
787 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
788 /* trace the parameters */
789 "leaq 24(%rbp),%r8\n\t" /* stack */
790 "leaq -40(%rbp),%r9\n\t"
791 "call " __ASM_NAME("relay_trace_entry") "\n\t"
792 /* copy the arguments */
793 "movl -40(%rbp),%edx\n\t" /* number of args */
794 "movq $4,%rcx\n\t"
795 "cmp %rcx,%rdx\n\t"
796 "cmovgq %rdx,%rcx\n\t"
797 "leaq -16(,%rcx,8),%rdx\n\t"
798 "andq $~15,%rdx\n\t"
799 "subq %rdx,%rsp\n\t"
800 "leaq 24(%rbp),%rsi\n\t" /* original stack */
801 "movq %rsp,%rdi\n\t"
802 "rep; movsq\n\t"
803 /* call the entry point */
804 "movq 0(%rsp),%rcx\n\t"
805 "movq 8(%rsp),%rdx\n\t"
806 "movq 16(%rsp),%r8\n\t"
807 "movq 24(%rsp),%r9\n\t"
808 "movq 0(%rsp),%xmm0\n\t"
809 "movq 8(%rsp),%xmm1\n\t"
810 "movq 16(%rsp),%xmm2\n\t"
811 "movq 24(%rsp),%xmm3\n\t"
812 "callq *%rax\n\t"
813 /* trace the return value */
814 "movq -32(%rbp),%rcx\n\t"
815 "movq -24(%rbp),%rdx\n\t"
816 "movq 16(%rbp),%r8\n\t" /* retaddr */
817 "movq %rax,%rsi\n\t"
818 "movaps %xmm0,32(%rsp)\n\t"
819 "movq %rax,%r9\n\t"
820 "call " __ASM_NAME("relay_trace_exit") "\n\t"
821 /* restore return value and return */
822 "movq %rsi,%rax\n\t"
823 "movaps 32(%rsp),%xmm0\n\t"
824 "movq -16(%rbp),%rsi\n\t"
825 __ASM_CFI(".cfi_same_value %rsi\n\t")
826 "movq -8(%rbp),%rdi\n\t"
827 __ASM_CFI(".cfi_same_value %rdi\n\t")
828 "movq %rbp,%rsp\n\t"
829 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
830 "popq %rbp\n\t"
831 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
832 __ASM_CFI(".cfi_same_value %rbp\n\t")
833 "ret")
835 #else
836 #error Not supported on this CPU
837 #endif
840 /***********************************************************************
841 * RELAY_GetProcAddress
843 * Return the proc address to use for a given function.
845 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
846 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
848 struct relay_private_data *data;
849 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
851 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
852 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
853 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
854 return proc; /* we want to relay it */
855 return data->entry_points[ordinal].orig_func;
859 /***********************************************************************
860 * RELAY_SetupDLL
862 * Setup relay debugging for a built-in dll.
864 void RELAY_SetupDLL( HMODULE module )
866 IMAGE_EXPORT_DIRECTORY *exports;
867 DWORD *funcs;
868 unsigned int i, len;
869 DWORD size, entry_point_rva;
870 struct relay_descr *descr;
871 struct relay_private_data *data;
872 const WORD *ordptr;
874 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
876 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
877 if (!exports) return;
879 descr = (struct relay_descr *)((char *)exports + size);
880 if (descr->magic != RELAY_DESCR_MAGIC) return;
882 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
883 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
884 return;
886 descr->relay_call = relay_call;
887 descr->private = data;
889 data->module = module;
890 data->base = exports->Base;
891 len = strlen( (char *)module + exports->Name );
892 if (len > 4 && !_stricmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
893 len = min( len, sizeof(data->dllname) - 1 );
894 memcpy( data->dllname, (char *)module + exports->Name, len );
895 data->dllname[len] = 0;
897 /* fetch name pointer for all entry points and store them in the private structure */
899 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
900 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
902 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
903 data->entry_points[*ordptr].name = (const char *)module + name_rva;
906 /* patch the functions in the export table to point to the relay thunks */
908 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
909 entry_point_rva = descr->entry_point_base - (const char *)module;
910 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
912 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
913 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
914 continue; /* don't include this entry point */
916 data->entry_points[i].orig_func = (char *)module + *funcs;
917 *funcs = entry_point_rva + descr->entry_point_offsets[i];
921 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
923 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
924 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
926 return proc;
929 void RELAY_SetupDLL( HMODULE module )
933 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
936 /***********************************************************************/
937 /* snoop support */
938 /***********************************************************************/
940 #ifdef __i386__
942 WINE_DECLARE_DEBUG_CHANNEL(seh);
943 WINE_DECLARE_DEBUG_CHANNEL(snoop);
945 #include "pshpack1.h"
947 typedef struct
949 /* code part */
950 BYTE lcall; /* 0xe8 call snoopentry (relative) */
951 DWORD snoopentry; /* SNOOP_Entry relative */
952 /* unreached */
953 int nrofargs;
954 FARPROC origfun;
955 const char *name;
956 } SNOOP_FUN;
958 typedef struct tagSNOOP_DLL {
959 HMODULE hmod;
960 SNOOP_FUN *funs;
961 DWORD ordbase;
962 DWORD nrofordinals;
963 struct tagSNOOP_DLL *next;
964 char name[1];
965 } SNOOP_DLL;
967 typedef struct
969 /* code part */
970 BYTE lcall; /* 0xe8 call snoopret relative*/
971 DWORD snoopret; /* SNOOP_Ret relative */
972 /* unreached */
973 FARPROC origreturn;
974 SNOOP_DLL *dll;
975 DWORD ordinal;
976 void **origESP;
977 DWORD *args; /* saved args across a stdcall */
978 } SNOOP_RETURNENTRY;
980 typedef struct tagSNOOP_RETURNENTRIES {
981 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
982 struct tagSNOOP_RETURNENTRIES *next;
983 } SNOOP_RETURNENTRIES;
985 #include "poppack.h"
987 extern void WINAPI SNOOP_Entry(void);
988 extern void WINAPI SNOOP_Return(void);
990 static SNOOP_DLL *firstdll;
991 static SNOOP_RETURNENTRIES *firstrets;
994 /***********************************************************************
995 * SNOOP_ShowDebugmsgSnoop
997 * Simple function to decide if a particular debugging message is
998 * wanted.
1000 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
1002 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
1003 return FALSE;
1004 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
1005 return FALSE;
1006 return TRUE;
1010 /***********************************************************************
1011 * SNOOP_SetupDLL
1013 * Setup snoop debugging for a native dll.
1015 void SNOOP_SetupDLL(HMODULE hmod)
1017 SNOOP_DLL **dll = &firstdll;
1018 char *p, *name;
1019 void *addr;
1020 SIZE_T size;
1021 ULONG size32;
1022 IMAGE_EXPORT_DIRECTORY *exports;
1024 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1026 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1027 if (!exports || !exports->NumberOfFunctions) return;
1028 name = (char *)hmod + exports->Name;
1029 size = size32;
1031 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1033 while (*dll) {
1034 if ((*dll)->hmod == hmod)
1036 /* another dll, loaded at the same address */
1037 addr = (*dll)->funs;
1038 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1039 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1040 break;
1042 dll = &((*dll)->next);
1044 if (*dll)
1045 *dll = RtlReAllocateHeap(GetProcessHeap(),
1046 HEAP_ZERO_MEMORY, *dll,
1047 sizeof(SNOOP_DLL) + strlen(name));
1048 else
1049 *dll = RtlAllocateHeap(GetProcessHeap(),
1050 HEAP_ZERO_MEMORY,
1051 sizeof(SNOOP_DLL) + strlen(name));
1052 (*dll)->hmod = hmod;
1053 (*dll)->ordbase = exports->Base;
1054 (*dll)->nrofordinals = exports->NumberOfFunctions;
1055 strcpy( (*dll)->name, name );
1056 p = (*dll)->name + strlen((*dll)->name) - 4;
1057 if (p > (*dll)->name && !_stricmp( p, ".dll" )) *p = 0;
1059 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1060 addr = NULL;
1061 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1062 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1063 if (!addr) {
1064 RtlFreeHeap(GetProcessHeap(),0,*dll);
1065 FIXME("out of memory\n");
1066 return;
1068 (*dll)->funs = addr;
1069 memset((*dll)->funs,0,size);
1073 /***********************************************************************
1074 * SNOOP_GetProcAddress
1076 * Return the proc address to use for a given function.
1078 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1079 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1080 const WCHAR *user)
1082 unsigned int i;
1083 const char *ename;
1084 const WORD *ordinals;
1085 const DWORD *names;
1086 SNOOP_DLL *dll = firstdll;
1087 SNOOP_FUN *fun;
1088 const IMAGE_SECTION_HEADER *sec;
1090 if (!TRACE_ON(snoop)) return origfun;
1091 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1092 return origfun; /* the calling module was explicitly excluded */
1094 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1095 return origfun;
1097 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1099 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1100 return origfun; /* most likely a data reference */
1102 while (dll) {
1103 if (hmod == dll->hmod)
1104 break;
1105 dll = dll->next;
1107 if (!dll) /* probably internal */
1108 return origfun;
1110 /* try to find a name for it */
1111 ename = NULL;
1112 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1113 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1114 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1116 if (ordinals[i] == ordinal)
1118 ename = (const char *)hmod + names[i];
1119 break;
1122 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1123 return origfun;
1124 assert(ordinal < dll->nrofordinals);
1125 fun = dll->funs + ordinal;
1126 if (!fun->name)
1128 fun->name = ename;
1129 fun->lcall = 0xe8;
1130 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1131 fun->origfun = origfun;
1132 fun->nrofargs = -1;
1134 return (FARPROC)&(fun->lcall);
1137 static void SNOOP_PrintArg(DWORD x)
1139 int i,nostring;
1141 TRACE_(snoop)("%08x",x);
1142 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1143 __TRY
1145 LPBYTE s=(LPBYTE)x;
1146 i=0;nostring=0;
1147 while (i<80) {
1148 if (s[i]==0) break;
1149 if (s[i]<0x20) {nostring=1;break;}
1150 if (s[i]>=0x80) {nostring=1;break;}
1151 i++;
1153 if (!nostring && i > 5)
1154 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1155 else /* try unicode */
1157 LPWSTR s=(LPWSTR)x;
1158 i=0;nostring=0;
1159 while (i<80) {
1160 if (s[i]==0) break;
1161 if (s[i]<0x20) {nostring=1;break;}
1162 if (s[i]>0x100) {nostring=1;break;}
1163 i++;
1165 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1168 __EXCEPT_PAGE_FAULT
1171 __ENDTRY
1174 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1176 SNOOP_DLL *dll;
1177 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1178 SNOOP_RETURNENTRIES **rets = &firstrets;
1179 SNOOP_RETURNENTRY *ret;
1180 int i=0, max;
1182 for (dll = firstdll; dll; dll = dll->next )
1183 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1185 if (!dll) {
1186 FIXME("entrypoint %p not found\n", fun);
1187 return; /* oops */
1189 /* guess cdecl ... */
1190 if (fun->nrofargs<0) {
1191 /* Typical cdecl return frame is:
1192 * add esp, xxxxxxxx
1193 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1194 * (after that 81 C2 xx xx xx xx)
1196 LPBYTE reteip = stack[1];
1198 if (reteip) {
1199 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1200 fun->nrofargs=reteip[2]/4;
1205 while (*rets) {
1206 for (i=0;i<ARRAY_SIZE( (*rets)->entry );i++)
1207 if (!(*rets)->entry[i].origreturn)
1208 break;
1209 if (i!=ARRAY_SIZE( (*rets)->entry ))
1210 break;
1211 rets = &((*rets)->next);
1213 if (!*rets) {
1214 SIZE_T size = 4096;
1215 VOID* addr = NULL;
1217 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1218 MEM_COMMIT | MEM_RESERVE,
1219 PAGE_EXECUTE_READWRITE);
1220 if (!addr) return;
1221 *rets = addr;
1222 i = 0; /* entry 0 is free */
1224 ret = &((*rets)->entry[i]);
1225 ret->lcall = 0xe8;
1226 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1227 ret->origreturn = stack[1];
1228 stack[1] = &ret->lcall;
1229 ret->dll = dll;
1230 ret->args = NULL;
1231 ret->ordinal = fun - dll->funs;
1232 ret->origESP = stack;
1234 stack[0] = fun->origfun;
1236 if (!TRACE_ON(snoop)) return;
1238 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1239 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1240 if (fun->nrofargs>0) {
1241 max = fun->nrofargs; if (max>16) max=16;
1242 for (i=0;i<max;i++)
1244 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1245 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1247 if (max!=fun->nrofargs)
1248 TRACE_(snoop)(" ...");
1249 } else if (fun->nrofargs<0) {
1250 TRACE_(snoop)("<unknown, check return>");
1251 ret->args = RtlAllocateHeap(GetProcessHeap(),
1252 0,16*sizeof(DWORD));
1253 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1255 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1258 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1260 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1261 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1262 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1264 /* We haven't found out the nrofargs yet. If we called a cdecl
1265 * function it is too late anyway and we can just set '0' (which
1266 * will be the difference between orig and current ESP
1267 * If stdcall -> everything ok.
1269 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1270 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1271 stack[0] = ret->origreturn;
1273 if (!TRACE_ON(snoop)) {
1274 ret->origreturn = NULL; /* mark as empty */
1275 return;
1278 if (ret->args) {
1279 int i,max;
1281 if (fun->name)
1282 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1283 else
1284 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1286 max = fun->nrofargs;
1287 if (max>16) max=16;
1289 for (i=0;i<max;i++)
1291 SNOOP_PrintArg(ret->args[i]);
1292 if (i<max-1) TRACE_(snoop)(",");
1294 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1295 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1296 ret->args = NULL;
1298 else
1300 if (fun->name)
1301 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1302 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1303 else
1304 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1305 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1306 retval, (DWORD)ret->origreturn);
1308 ret->origreturn = NULL; /* mark as empty */
1311 /* small wrappers that save registers and get the stack pointer */
1312 #define SNOOP_WRAPPER(name) \
1313 __ASM_STDCALL_FUNC( name, 0, \
1314 "pushl %eax\n\t" \
1315 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1316 "pushl %ecx\n\t" \
1317 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1318 "pushl %edx\n\t" \
1319 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1320 "leal 12(%esp),%eax\n\t" \
1321 "pushl %eax\n\t" \
1322 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1323 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1324 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1325 "popl %edx\n\t" \
1326 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1327 "popl %ecx\n\t" \
1328 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1329 "popl %eax\n\t" \
1330 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1331 "ret" )
1333 SNOOP_WRAPPER( SNOOP_Entry )
1334 SNOOP_WRAPPER( SNOOP_Return )
1336 #else /* __i386__ */
1338 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1339 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1341 return origfun;
1344 void SNOOP_SetupDLL( HMODULE hmod )
1346 FIXME("snooping works only on i386 for now.\n");
1349 #endif /* __i386__ */