l3codeca.acm: Avoid mpg123 functions with suffix.
[wine.git] / dlls / ntdll / relay.c
blobbe2dc8333777b9daf380918cca2e685187ff686f
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 <assert.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winternl.h"
31 #include "wine/exception.h"
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(relay);
37 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
39 struct relay_descr /* descriptor for a module */
41 ULONG_PTR magic; /* signature */
42 void *relay_call; /* functions to call from relay thunks */
43 void *private; /* reserved for the relay code private data */
44 const char *entry_point_base; /* base address of entry point thunks */
45 const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
46 const char *args_string; /* string describing the arguments */
49 struct relay_descr_rva /* RVA to the descriptor for PE dlls */
51 DWORD magic;
52 DWORD descr;
55 #define RELAY_DESCR_MAGIC 0xdeb90002
56 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
58 /* private data built at dll load time */
60 struct relay_entry_point
62 void *orig_func; /* original entry point function */
63 const char *name; /* function name (if any) */
66 struct relay_private_data
68 HMODULE module; /* module handle of this dll */
69 unsigned int base; /* ordinal base */
70 char dllname[40]; /* dll name (without .dll extension) */
71 struct relay_entry_point entry_points[1]; /* list of dll entry points */
74 static const WCHAR **debug_relay_excludelist;
75 static const WCHAR **debug_relay_includelist;
76 static const WCHAR **debug_snoop_excludelist;
77 static const WCHAR **debug_snoop_includelist;
78 static const WCHAR **debug_from_relay_excludelist;
79 static const WCHAR **debug_from_relay_includelist;
80 static const WCHAR **debug_from_snoop_excludelist;
81 static const WCHAR **debug_from_snoop_includelist;
83 static RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
85 /* compare an ASCII and a Unicode string without depending on the current codepage */
86 static inline int strcmpAW( const char *strA, const WCHAR *strW )
88 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
89 return (unsigned char)*strA - *strW;
92 /***********************************************************************
93 * build_list
95 * Build a function list from a ';'-separated string.
97 static const WCHAR **build_list( const WCHAR *buffer )
99 int count = 1;
100 const WCHAR *p = buffer;
101 const WCHAR **ret;
103 while ((p = wcschr( p, ';' )))
105 count++;
106 p++;
108 /* allocate count+1 pointers, plus the space for a copy of the string */
109 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
110 (count+1) * sizeof(WCHAR*) + (wcslen(buffer)+1) * sizeof(WCHAR) )))
112 WCHAR *str = (WCHAR *)(ret + count + 1);
113 WCHAR *q = str;
115 wcscpy( str, buffer );
116 count = 0;
117 for (;;)
119 ret[count++] = q;
120 if (!(q = wcschr( q, ';' ))) break;
121 *q++ = 0;
123 ret[count++] = NULL;
125 return ret;
128 /***********************************************************************
129 * load_list_value
131 * Load a function list from a registry value.
133 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
135 char initial_buffer[4096];
136 char *buffer = initial_buffer;
137 DWORD count;
138 NTSTATUS status;
139 UNICODE_STRING name;
140 const WCHAR **list = NULL;
142 RtlInitUnicodeString( &name, value );
143 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(initial_buffer), &count );
144 if (status == STATUS_BUFFER_OVERFLOW)
146 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
147 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
149 if (status == STATUS_SUCCESS)
151 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
152 list = build_list( str );
153 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
156 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
157 return list;
160 /***********************************************************************
161 * init_debug_lists
163 * Build the relay include/exclude function lists.
165 static DWORD WINAPI init_debug_lists( RTL_RUN_ONCE *once, void *param, void **context )
167 OBJECT_ATTRIBUTES attr;
168 UNICODE_STRING name;
169 HANDLE root, hkey;
171 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
172 attr.Length = sizeof(attr);
173 attr.RootDirectory = root;
174 attr.ObjectName = &name;
175 attr.Attributes = 0;
176 attr.SecurityDescriptor = NULL;
177 attr.SecurityQualityOfService = NULL;
178 RtlInitUnicodeString( &name, L"Software\\Wine\\Debug" );
180 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
181 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
182 NtClose( root );
183 if (!hkey) return TRUE;
185 debug_relay_includelist = load_list( hkey, L"RelayInclude" );
186 debug_relay_excludelist = load_list( hkey, L"RelayExclude" );
187 debug_snoop_includelist = load_list( hkey, L"SnoopInclude" );
188 debug_snoop_excludelist = load_list( hkey, L"SnoopExclude" );
189 debug_from_relay_includelist = load_list( hkey, L"RelayFromInclude" );
190 debug_from_relay_excludelist = load_list( hkey, L"RelayFromExclude" );
191 debug_from_snoop_includelist = load_list( hkey, L"SnoopFromInclude" );
192 debug_from_snoop_excludelist = load_list( hkey, L"SnoopFromExclude" );
194 NtClose( hkey );
195 return TRUE;
199 /***********************************************************************
200 * check_list
202 * Check if a given module and function is in the list.
204 static BOOL check_list( const WCHAR *module, int ordinal, const char *func, const WCHAR *const *list )
206 char ord_str[10];
208 sprintf( ord_str, "%d", ordinal );
209 for(; *list; list++)
211 const WCHAR *p = wcsrchr( *list, '.' );
212 if (p && p > *list) /* check module and function */
214 int len = p - *list;
215 if (wcsnicmp( module, *list, len - 1 ) || module[len]) continue;
216 if (p[1] == '*' && !p[2]) return TRUE;
217 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
218 if (func && !strcmpAW( func, p + 1 )) return TRUE;
220 else /* function only */
222 if (func && !strcmpAW( func, *list )) return TRUE;
225 return FALSE;
229 /***********************************************************************
230 * check_relay_include
232 * Check if a given function must be included in the relay output.
234 static BOOL check_relay_include( const WCHAR *module, int ordinal, const char *func )
236 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
237 return FALSE;
238 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
239 return FALSE;
240 return TRUE;
243 /***********************************************************************
244 * check_from_module
246 * Check if calls from a given module must be included in the relay/snoop output,
247 * given the exclusion and inclusion lists.
249 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
251 const WCHAR **listitem;
252 BOOL show;
254 if (!module) return TRUE;
255 if (!includelist && !excludelist) return TRUE;
256 if (excludelist)
258 show = TRUE;
259 listitem = excludelist;
261 else
263 show = FALSE;
264 listitem = includelist;
266 for(; *listitem; listitem++)
268 int len;
270 if (!wcsicmp( *listitem, module )) return !show;
271 len = wcslen( *listitem );
272 if (!wcsnicmp( *listitem, module, len ) && !wcsicmp( module + len, L".dll" ))
273 return !show;
275 return show;
279 static BOOL is_ret_val( char type )
281 return type >= 'A' && type <= 'Z';
284 static const char *func_name( struct relay_private_data *data, unsigned int ordinal )
286 struct relay_entry_point *entry_point = data->entry_points + ordinal;
288 if (entry_point->name)
289 return wine_dbg_sprintf( "%s.%s", data->dllname, entry_point->name );
290 else
291 return wine_dbg_sprintf( "%s.%u", data->dllname, data->base + ordinal );
294 static void trace_string_a( INT_PTR ptr )
296 if (!IS_INTARG( ptr )) TRACE( "%08Ix %s", ptr, debugstr_a( (char *)ptr ));
297 else TRACE( "%08Ix", ptr );
300 static void trace_string_w( INT_PTR ptr )
302 if (!IS_INTARG( ptr )) TRACE( "%08Ix %s", ptr, debugstr_w( (WCHAR *)ptr ));
303 else TRACE( "%08Ix", ptr );
306 #ifdef __i386__
308 /***********************************************************************
309 * relay_trace_entry
311 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
312 const DWORD *stack, unsigned int *nb_args )
314 WORD ordinal = LOWORD(idx);
315 const char *arg_types = descr->args_string + HIWORD(idx);
316 struct relay_private_data *data = descr->private;
317 struct relay_entry_point *entry_point = data->entry_points + ordinal;
318 unsigned int i, pos;
320 TRACE( "\1Call %s(", func_name( data, ordinal ));
322 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
324 switch (arg_types[i])
326 case 'j': /* int64 */
327 TRACE( "%x%08x", stack[pos+1], stack[pos] );
328 pos += 2;
329 break;
330 case 'k': /* int128 */
331 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
332 pos += 4;
333 break;
334 case 's': /* str */
335 trace_string_a( stack[pos++] );
336 break;
337 case 'w': /* wstr */
338 trace_string_w( stack[pos++] );
339 break;
340 case 'f': /* float */
341 TRACE( "%g", *(const float *)&stack[pos++] );
342 break;
343 case 'd': /* double */
344 TRACE( "%g", *(const double *)&stack[pos] );
345 pos += 2;
346 break;
347 case 'i': /* long */
348 default:
349 TRACE( "%08x", stack[pos++] );
350 break;
352 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
354 *nb_args = pos;
355 if (arg_types[0] == 't')
357 *nb_args |= 0x80000000; /* thiscall/fastcall */
358 if (arg_types[1] == 't') *nb_args |= 0x40000000; /* fastcall */
360 TRACE( ") ret=%08x\n", stack[-1] );
361 return entry_point->orig_func;
364 /***********************************************************************
365 * relay_trace_exit
367 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
368 void *retaddr, LONGLONG retval )
370 const char *arg_types = descr->args_string + HIWORD(idx);
372 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
374 while (!is_ret_val( *arg_types )) arg_types++;
375 if (*arg_types == 'J') /* int64 return value */
376 TRACE( " retval=%08x%08x ret=%08x\n",
377 (UINT)(retval >> 32), (UINT)retval, (UINT)retaddr );
378 else
379 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, (UINT)retaddr );
382 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx );
383 __ASM_STDCALL_FUNC( relay_call, 8,
384 "pushl %ebp\n\t"
385 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
386 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
387 "movl %esp,%ebp\n\t"
388 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
389 "pushl %esi\n\t"
390 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
391 "pushl %edi\n\t"
392 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
393 "pushl %ecx\n\t"
394 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
395 /* trace the parameters */
396 "pushl %eax\n\t"
397 "pushl %esp\n\t" /* number of args return ptr */
398 "leal 20(%ebp),%esi\n\t" /* stack */
399 "pushl %esi\n\t"
400 "pushl 12(%ebp)\n\t"
401 "pushl 8(%ebp)\n\t"
402 "call " __ASM_STDCALL("relay_trace_entry",16) "\n\t"
403 /* copy the arguments*/
404 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
405 "jecxz 1f\n\t"
406 "leal 0(,%ecx,4),%edx\n\t"
407 "subl %edx,%esp\n\t"
408 "andl $~15,%esp\n\t"
409 "movl %esp,%edi\n\t"
410 "cld\n\t"
411 "rep; movsl\n\t"
412 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
413 "jz 1f\n\t"
414 "popl %ecx\n\t"
415 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
416 "jz 1f\n\t"
417 "popl %edx\n"
418 /* call the entry point */
419 "1:\tcall *%eax\n\t"
420 "movl %eax,%esi\n\t"
421 "movl %edx,%edi\n\t"
422 /* trace the return value */
423 "leal -20(%ebp),%esp\n\t"
424 "pushl %edx\n\t"
425 "pushl %eax\n\t"
426 "pushl 16(%ebp)\n\t"
427 "pushl 12(%ebp)\n\t"
428 "pushl 8(%ebp)\n\t"
429 "call " __ASM_STDCALL("relay_trace_exit",20) "\n\t"
430 /* restore return value and return */
431 "leal -12(%ebp),%esp\n\t"
432 "movl %esi,%eax\n\t"
433 "movl %edi,%edx\n\t"
434 "popl %ecx\n\t"
435 __ASM_CFI(".cfi_same_value %ecx\n\t")
436 "popl %edi\n\t"
437 __ASM_CFI(".cfi_same_value %edi\n\t")
438 "popl %esi\n\t"
439 __ASM_CFI(".cfi_same_value %esi\n\t")
440 "popl %ebp\n\t"
441 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
442 __ASM_CFI(".cfi_same_value %ebp\n\t")
443 "ret $8" )
445 #elif defined(__arm__)
447 /***********************************************************************
448 * relay_trace_entry
450 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
451 const DWORD *stack, unsigned int *nb_args )
453 WORD ordinal = LOWORD(idx);
454 const char *arg_types = descr->args_string + HIWORD(idx);
455 struct relay_private_data *data = descr->private;
456 struct relay_entry_point *entry_point = data->entry_points + ordinal;
457 unsigned int i, pos;
458 #ifndef __SOFTFP__
459 unsigned int float_pos = 0, double_pos = 0;
460 const union fpregs { float s[16]; double d[8]; } *fpstack = (const union fpregs *)stack - 1;
461 #endif
463 TRACE( "\1Call %s(", func_name( data, ordinal ));
465 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
467 switch (arg_types[i])
469 case 'j': /* int64 */
470 pos = (pos + 1) & ~1;
471 TRACE( "%x%08x", stack[pos+1], stack[pos] );
472 pos += 2;
473 break;
474 case 'k': /* int128 */
475 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
476 pos += 4;
477 break;
478 case 's': /* str */
479 trace_string_a( stack[pos++] );
480 break;
481 case 'w': /* wstr */
482 trace_string_w( stack[pos++] );
483 break;
484 case 'f': /* float */
485 #ifndef __SOFTFP__
486 if (!(float_pos % 2)) float_pos = max( float_pos, double_pos * 2 );
487 if (float_pos < 16)
489 TRACE( "%g", fpstack->s[float_pos++] );
490 break;
492 #endif
493 TRACE( "%g", *(const float *)&stack[pos++] );
494 break;
495 case 'd': /* double */
496 #ifndef __SOFTFP__
497 double_pos = max( (float_pos + 1) / 2, double_pos );
498 if (double_pos < 8)
500 TRACE( "%g", fpstack->d[double_pos++] );
501 break;
503 #endif
504 pos = (pos + 1) & ~1;
505 TRACE( "%g", *(const double *)&stack[pos] );
506 pos += 2;
507 break;
508 case 'i': /* long */
509 default:
510 TRACE( "%08x", stack[pos++] );
511 break;
513 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
516 #ifndef __SOFTFP__
517 if (float_pos || double_pos)
519 pos |= 0x80000000;
520 stack = (const DWORD *)fpstack; /* retaddr is below the fp regs */
522 #endif
523 *nb_args = pos;
524 TRACE( ") ret=%08x\n", stack[-1] );
525 return entry_point->orig_func;
528 /***********************************************************************
529 * relay_trace_exit
531 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
532 DWORD retaddr, LONGLONG retval )
534 const char *arg_types = descr->args_string + HIWORD(idx);
536 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
538 while (!is_ret_val( *arg_types )) arg_types++;
539 if (*arg_types == 'J') /* int64 return value */
540 TRACE( " retval=%08x%08x ret=%08x\n",
541 (UINT)(retval >> 32), (UINT)retval, retaddr );
542 else
543 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, retaddr );
546 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const DWORD *stack );
547 __ASM_GLOBAL_FUNC( relay_call,
548 "push {r4-r8,lr}\n\t"
549 "sub sp, #16\n\t"
550 "mov r6, r2\n\t"
551 "add r3, sp, #12\n\t"
552 "mov r7, r0\n\t"
553 "mov r8, r1\n\t"
554 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
555 "mov ip, r0\n\t" /* entry point */
556 "mov r5, sp\n\t"
557 "ldr r1, [sp, #12]\n\t" /* number of args */
558 "lsl r3, r1, #2\n\t"
559 "subs r3, #16\n\t" /* first 4 args are in registers */
560 "ble 2f\n\t"
561 "add r3, #7\n\t"
562 "and r3, #~7\n"
563 "sub sp, r3\n\t"
564 "add r2, r6, #16\n\t" /* skip r0-r3 */
565 "1:\tsubs r3, r3, #4\n\t"
566 "ldr r0, [r2, r3]\n\t"
567 "str r0, [sp, r3]\n\t"
568 "bgt 1b\n"
569 "2:\t"
570 #ifndef __SOFTFP__
571 "tst r1, #0x80000000\n\t"
572 "ldm r6, {r0-r3}\n\t"
573 "it ne\n\t"
574 "vldmdbne r6!, {s0-s15}\n\t"
575 #else
576 "ldm r6, {r0-r3}\n\t"
577 #endif
578 "blx ip\n\t"
579 "mov sp, r5\n\t"
580 "ldr r2, [r6, #-4]\n\t" /* retaddr */
581 "mov r4, r0\n\t"
582 "mov r5, r1\n\t"
583 "mov r0, r7\n\t"
584 "mov r1, r8\n\t"
585 "str r4, [sp]\n\t"
586 "str r5, [sp, #4]\n\t"
587 #ifndef __SOFTFP__
588 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
589 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
590 "vldr d0, [sp, #8]\n\t"
591 #else
592 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
593 #endif
594 "mov r0, r4\n\t"
595 "mov r1, r5\n\t"
596 "add sp, #16\n\t"
597 "pop {r4-r8,pc}" )
599 #elif defined(__aarch64__)
601 /***********************************************************************
602 * relay_trace_entry
604 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
605 const INT_PTR *stack, unsigned int *nb_args )
607 WORD ordinal = LOWORD(idx);
608 const char *arg_types = descr->args_string + HIWORD(idx);
609 struct relay_private_data *data = descr->private;
610 struct relay_entry_point *entry_point = data->entry_points + ordinal;
611 unsigned int i;
613 TRACE( "\1Call %s(", func_name( data, ordinal ));
615 for (i = 0; !is_ret_val( arg_types[i] ); i++)
617 switch (arg_types[i])
619 case 's': /* str */
620 trace_string_a( stack[i] );
621 break;
622 case 'w': /* wstr */
623 trace_string_w( stack[i] );
624 break;
625 case 'i': /* long */
626 default:
627 TRACE( "%08zx", stack[i] );
628 break;
630 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
632 *nb_args = i;
633 TRACE( ") ret=%08zx\n", stack[-1] );
634 return entry_point->orig_func;
637 /***********************************************************************
638 * relay_trace_exit
640 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
641 INT_PTR retaddr, INT_PTR retval )
643 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
644 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
647 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
648 __ASM_GLOBAL_FUNC( call_entry_point,
649 "stp x29, x30, [SP,#-16]!\n\t"
650 "stp x19, x20, [SP,#-16]!\n\t"
651 "mov x29, SP\n\t"
652 "mov x19, x2\n\t"
653 "ldp x8, x9, [x19, #-32]\n\t"
654 "mov x9, x0\n\t"
655 "cbz w1, 2f\n\t"
656 "mov w10, w1\n\t"
657 "mov x11, x2\n\t"
658 "mov x12, #0\n\t"
659 "ldp x0, x1, [x11], #16\n\t"
660 "add w12, w12, #2\n\t"
661 "cmp w12, w10\n\t"
662 "b.hs 2f\n\t"
663 "ldp x2, x3, [x11], #16\n\t"
664 "add w12, w12, #2\n\t"
665 "cmp w12, w10\n\t"
666 "b.hs 2f\n\t"
667 "ldp x4, x5, [x11], #16\n\t"
668 "add w12, w12, #2\n\t"
669 "cmp w12, w10\n\t"
670 "b.hs 2f\n\t"
671 "ldp x6, x7, [x11], #16\n\t"
672 "add w12, w12, #2\n\t"
673 "cmp w12, w10\n\t"
674 "b.hs 2f\n\t"
675 "sub w12, w10, #8\n\t"
676 "lsl w12, w12, #3\n\t"
677 "sub SP, SP, w12, uxtw\n\t"
678 "tbz w12, #3, 1f\n\t"
679 "sub SP, SP, #8\n\t"
680 "1: sub w12, w12, #8\n\t"
681 "ldr x13, [x11, x12]\n\t"
682 "str x13, [SP, x12]\n\t"
683 "cbnz w12, 1b\n\t"
684 "2: blr x9\n\t"
685 "mov SP, x29\n\t"
686 "ldp x19, x20, [SP], #16\n\t"
687 "ldp x29, x30, [SP], #16\n\t"
688 "ret\n" )
690 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
692 unsigned int nb_args;
693 void *func = relay_trace_entry( descr, idx, stack, &nb_args );
694 LONGLONG ret = call_entry_point( func, nb_args, stack );
695 relay_trace_exit( descr, idx, stack[-1], ret );
696 return ret;
699 #elif defined(__x86_64__)
701 /***********************************************************************
702 * relay_trace_entry
704 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
705 const INT_PTR *stack, unsigned int *nb_args )
707 WORD ordinal = LOWORD(idx);
708 const char *arg_types = descr->args_string + HIWORD(idx);
709 struct relay_private_data *data = descr->private;
710 struct relay_entry_point *entry_point = data->entry_points + ordinal;
711 unsigned int i;
713 TRACE( "\1Call %s(", func_name( data, ordinal ));
715 for (i = 0; !is_ret_val( arg_types[i] ); i++)
717 switch (arg_types[i])
719 case 's': /* str */
720 trace_string_a( stack[i] );
721 break;
722 case 'w': /* wstr */
723 trace_string_w( stack[i] );
724 break;
725 case 'f': /* float */
726 TRACE( "%g", *(const float *)&stack[i] );
727 break;
728 case 'd': /* double */
729 TRACE( "%g", *(const double *)&stack[i] );
730 break;
731 case 'i': /* long */
732 default:
733 TRACE( "%08zx", stack[i] );
734 break;
736 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
738 *nb_args = i;
739 TRACE( ") ret=%08zx\n", stack[-1] );
740 return entry_point->orig_func;
743 /***********************************************************************
744 * relay_trace_exit
746 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
747 INT_PTR retaddr, INT_PTR retval )
749 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
750 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
753 extern INT_PTR WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
754 __ASM_GLOBAL_FUNC( relay_call,
755 "pushq %rbp\n\t"
756 __ASM_SEH(".seh_pushreg %rbp\n\t")
757 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
758 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
759 "movq %rsp,%rbp\n\t"
760 __ASM_SEH(".seh_setframe %rbp,0\n\t")
761 __ASM_SEH(".seh_endprologue\n\t")
762 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
763 "leaq -0x48(%rbp),%rsp\n\t"
764 "andq $~15,%rsp\n\t"
765 "movq %rcx,-32(%rbp)\n\t"
766 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
767 "movq %rdx,-24(%rbp)\n\t"
768 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
769 "movq %rsi,-16(%rbp)\n\t"
770 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
771 "movq %rdi,-8(%rbp)\n\t"
772 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
773 /* trace the parameters */
774 "leaq 24(%rbp),%r8\n\t" /* stack */
775 "leaq -40(%rbp),%r9\n\t"
776 "call " __ASM_NAME("relay_trace_entry") "\n\t"
777 /* copy the arguments */
778 "movl -40(%rbp),%edx\n\t" /* number of args */
779 "movq $4,%rcx\n\t"
780 "cmp %rcx,%rdx\n\t"
781 "cmovgq %rdx,%rcx\n\t"
782 "leaq -16(,%rcx,8),%rdx\n\t"
783 "andq $~15,%rdx\n\t"
784 "subq %rdx,%rsp\n\t"
785 "leaq 24(%rbp),%rsi\n\t" /* original stack */
786 "movq %rsp,%rdi\n\t"
787 "rep; movsq\n\t"
788 /* call the entry point */
789 "movq 0(%rsp),%rcx\n\t"
790 "movq 8(%rsp),%rdx\n\t"
791 "movq 16(%rsp),%r8\n\t"
792 "movq 24(%rsp),%r9\n\t"
793 "movq 0(%rsp),%xmm0\n\t"
794 "movq 8(%rsp),%xmm1\n\t"
795 "movq 16(%rsp),%xmm2\n\t"
796 "movq 24(%rsp),%xmm3\n\t"
797 "callq *%rax\n\t"
798 /* trace the return value */
799 "movq -32(%rbp),%rcx\n\t"
800 "movq -24(%rbp),%rdx\n\t"
801 "movq 16(%rbp),%r8\n\t" /* retaddr */
802 "movq %rax,%rsi\n\t"
803 "movaps %xmm0,32(%rsp)\n\t"
804 "movq %rax,%r9\n\t"
805 "call " __ASM_NAME("relay_trace_exit") "\n\t"
806 /* restore return value and return */
807 "movq %rsi,%rax\n\t"
808 "movaps 32(%rsp),%xmm0\n\t"
809 "movq -16(%rbp),%rsi\n\t"
810 __ASM_CFI(".cfi_same_value %rsi\n\t")
811 "movq -8(%rbp),%rdi\n\t"
812 __ASM_CFI(".cfi_same_value %rdi\n\t")
813 "leaq 0(%rbp),%rsp\n\t"
814 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
815 "popq %rbp\n\t"
816 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
817 __ASM_CFI(".cfi_same_value %rbp\n\t")
818 "ret")
820 #else
821 #error Not supported on this CPU
822 #endif
825 static struct relay_descr *get_relay_descr( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
826 DWORD exp_size )
828 struct relay_descr *descr;
829 struct relay_descr_rva *rva;
830 ULONG_PTR ptr = (ULONG_PTR)module + exports->Name;
832 /* sanity checks */
833 if (ptr <= (ULONG_PTR)(exports + 1)) return NULL;
834 if (ptr > (ULONG_PTR)exports + exp_size) return NULL;
835 if (ptr % sizeof(DWORD)) return NULL;
837 rva = (struct relay_descr_rva *)ptr - 1;
838 if (rva->magic != RELAY_DESCR_MAGIC) return NULL;
839 if (rva->descr) descr = (struct relay_descr *)((char *)module + rva->descr);
840 else descr = (struct relay_descr *)((const char *)exports + exp_size);
841 if (descr->magic != RELAY_DESCR_MAGIC) return NULL;
842 return descr;
845 /***********************************************************************
846 * RELAY_GetProcAddress
848 * Return the proc address to use for a given function.
850 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
851 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
853 struct relay_private_data *data;
854 const struct relay_descr *descr = get_relay_descr( module, exports, exp_size );
856 if (!descr || !(data = descr->private)) return proc; /* no relay data */
857 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
858 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
859 return proc; /* we want to relay it */
860 return data->entry_points[ordinal].orig_func;
864 /***********************************************************************
865 * RELAY_SetupDLL
867 * Setup relay debugging for a built-in dll.
869 void RELAY_SetupDLL( HMODULE module )
871 IMAGE_EXPORT_DIRECTORY *exports;
872 DWORD *funcs;
873 unsigned int i, len;
874 DWORD size, entry_point_rva, old_prot;
875 struct relay_descr *descr;
876 struct relay_private_data *data;
877 WCHAR dllnameW[sizeof(data->dllname)];
878 const WORD *ordptr;
879 void *func_base;
880 SIZE_T func_size;
882 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
884 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
885 if (!exports) return;
887 if (!(descr = get_relay_descr( module, exports, size ))) return;
889 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
890 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
891 return;
893 descr->relay_call = relay_call;
894 descr->private = data;
896 data->module = module;
897 data->base = exports->Base;
898 len = strlen( (char *)module + exports->Name );
899 if (len > 4 && !_stricmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
900 len = min( len, sizeof(data->dllname) - 1 );
901 memcpy( data->dllname, (char *)module + exports->Name, len );
902 data->dllname[len] = 0;
903 ascii_to_unicode( dllnameW, data->dllname, len + 1 );
905 /* fetch name pointer for all entry points and store them in the private structure */
907 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
908 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
910 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
911 data->entry_points[*ordptr].name = (const char *)module + name_rva;
914 /* patch the functions in the export table to point to the relay thunks */
916 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
917 entry_point_rva = descr->entry_point_base - (const char *)module;
919 func_base = funcs;
920 func_size = exports->NumberOfFunctions * sizeof(*funcs);
921 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, PAGE_READWRITE, &old_prot );
922 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
924 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
925 if (!check_relay_include( dllnameW, i + exports->Base, data->entry_points[i].name ))
926 continue; /* don't include this entry point */
928 data->entry_points[i].orig_func = (char *)module + *funcs;
929 *funcs = entry_point_rva + descr->entry_point_offsets[i];
931 if (old_prot != PAGE_READWRITE)
932 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, old_prot, &old_prot );
935 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
937 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
938 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
940 return proc;
943 void RELAY_SetupDLL( HMODULE module )
947 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
950 /***********************************************************************/
951 /* snoop support */
952 /***********************************************************************/
954 #ifdef __i386__
956 WINE_DECLARE_DEBUG_CHANNEL(seh);
957 WINE_DECLARE_DEBUG_CHANNEL(snoop);
959 #include "pshpack1.h"
961 typedef struct
963 /* code part */
964 BYTE lcall; /* 0xe8 call snoopentry (relative) */
965 DWORD snoopentry; /* SNOOP_Entry relative */
966 /* unreached */
967 int nrofargs;
968 FARPROC origfun;
969 const char *name;
970 } SNOOP_FUN;
972 typedef struct tagSNOOP_DLL {
973 HMODULE hmod;
974 SNOOP_FUN *funs;
975 DWORD ordbase;
976 DWORD nrofordinals;
977 struct tagSNOOP_DLL *next;
978 char name[1];
979 } SNOOP_DLL;
981 typedef struct
983 /* code part */
984 BYTE lcall; /* 0xe8 call snoopret relative*/
985 DWORD snoopret; /* SNOOP_Ret relative */
986 /* unreached */
987 FARPROC origreturn;
988 SNOOP_DLL *dll;
989 DWORD ordinal;
990 void **origESP;
991 DWORD *args; /* saved args across a stdcall */
992 } SNOOP_RETURNENTRY;
994 typedef struct tagSNOOP_RETURNENTRIES {
995 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
996 struct tagSNOOP_RETURNENTRIES *next;
997 } SNOOP_RETURNENTRIES;
999 #include "poppack.h"
1001 extern void WINAPI SNOOP_Entry(void);
1002 extern void WINAPI SNOOP_Return(void);
1004 static SNOOP_DLL *firstdll;
1005 static SNOOP_RETURNENTRIES *firstrets;
1008 /***********************************************************************
1009 * SNOOP_ShowDebugmsgSnoop
1011 * Simple function to decide if a particular debugging message is
1012 * wanted.
1014 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
1016 WCHAR moduleW[40];
1017 int len = strlen(module);
1019 if (len >= ARRAY_SIZE( moduleW )) return FALSE;
1020 ascii_to_unicode( moduleW, module, len + 1 );
1021 if (debug_snoop_excludelist && check_list( moduleW, ordinal, func, debug_snoop_excludelist ))
1022 return FALSE;
1023 if (debug_snoop_includelist && !check_list( moduleW, ordinal, func, debug_snoop_includelist ))
1024 return FALSE;
1025 return TRUE;
1029 /***********************************************************************
1030 * SNOOP_SetupDLL
1032 * Setup snoop debugging for a native dll.
1034 void SNOOP_SetupDLL(HMODULE hmod)
1036 SNOOP_DLL **dll = &firstdll;
1037 char *p, *name;
1038 void *addr;
1039 SIZE_T size;
1040 ULONG size32;
1041 IMAGE_EXPORT_DIRECTORY *exports;
1043 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1045 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1046 if (!exports || !exports->NumberOfFunctions) return;
1047 name = (char *)hmod + exports->Name;
1048 size = size32;
1050 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1052 while (*dll) {
1053 if ((*dll)->hmod == hmod)
1055 /* another dll, loaded at the same address */
1056 addr = (*dll)->funs;
1057 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1058 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1059 break;
1061 dll = &((*dll)->next);
1063 if (*dll)
1064 *dll = RtlReAllocateHeap(GetProcessHeap(),
1065 HEAP_ZERO_MEMORY, *dll,
1066 sizeof(SNOOP_DLL) + strlen(name));
1067 else
1068 *dll = RtlAllocateHeap(GetProcessHeap(),
1069 HEAP_ZERO_MEMORY,
1070 sizeof(SNOOP_DLL) + strlen(name));
1071 (*dll)->hmod = hmod;
1072 (*dll)->ordbase = exports->Base;
1073 (*dll)->nrofordinals = exports->NumberOfFunctions;
1074 strcpy( (*dll)->name, name );
1075 p = (*dll)->name + strlen((*dll)->name) - 4;
1076 if (p > (*dll)->name && !_stricmp( p, ".dll" )) *p = 0;
1078 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1079 addr = NULL;
1080 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1081 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1082 if (!addr) {
1083 RtlFreeHeap(GetProcessHeap(),0,*dll);
1084 FIXME("out of memory\n");
1085 return;
1087 (*dll)->funs = addr;
1088 memset((*dll)->funs,0,size);
1092 /***********************************************************************
1093 * SNOOP_GetProcAddress
1095 * Return the proc address to use for a given function.
1097 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1098 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1099 const WCHAR *user)
1101 unsigned int i;
1102 const char *ename;
1103 const WORD *ordinals;
1104 const DWORD *names;
1105 SNOOP_DLL *dll = firstdll;
1106 SNOOP_FUN *fun;
1107 const IMAGE_SECTION_HEADER *sec;
1109 if (!TRACE_ON(snoop)) return origfun;
1110 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1111 return origfun; /* the calling module was explicitly excluded */
1113 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1114 return origfun;
1116 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1118 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1119 return origfun; /* most likely a data reference */
1121 while (dll) {
1122 if (hmod == dll->hmod)
1123 break;
1124 dll = dll->next;
1126 if (!dll) /* probably internal */
1127 return origfun;
1129 /* try to find a name for it */
1130 ename = NULL;
1131 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1132 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1133 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1135 if (ordinals[i] == ordinal)
1137 ename = (const char *)hmod + names[i];
1138 break;
1141 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1142 return origfun;
1143 assert(ordinal < dll->nrofordinals);
1144 fun = dll->funs + ordinal;
1145 if (!fun->name)
1147 fun->name = ename;
1148 fun->lcall = 0xe8;
1149 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1150 fun->origfun = origfun;
1151 fun->nrofargs = -1;
1153 return (FARPROC)&(fun->lcall);
1156 static void SNOOP_PrintArg(DWORD x)
1158 int i,nostring;
1160 TRACE_(snoop)("%08x",x);
1161 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1162 __TRY
1164 LPBYTE s=(LPBYTE)x;
1165 i=0;nostring=0;
1166 while (i<80) {
1167 if (s[i]==0) break;
1168 if (s[i]<0x20) {nostring=1;break;}
1169 if (s[i]>=0x80) {nostring=1;break;}
1170 i++;
1172 if (!nostring && i > 5)
1173 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1174 else /* try unicode */
1176 LPWSTR s=(LPWSTR)x;
1177 i=0;nostring=0;
1178 while (i<80) {
1179 if (s[i]==0) break;
1180 if (s[i]<0x20) {nostring=1;break;}
1181 if (s[i]>0x100) {nostring=1;break;}
1182 i++;
1184 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1187 __EXCEPT_PAGE_FAULT
1190 __ENDTRY
1193 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1195 SNOOP_DLL *dll;
1196 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1197 SNOOP_RETURNENTRIES **rets = &firstrets;
1198 SNOOP_RETURNENTRY *ret;
1199 int i=0, max;
1201 for (dll = firstdll; dll; dll = dll->next )
1202 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1204 if (!dll) {
1205 FIXME("entrypoint %p not found\n", fun);
1206 return; /* oops */
1208 /* guess cdecl ... */
1209 if (fun->nrofargs<0) {
1210 /* Typical cdecl return frame is:
1211 * add esp, xxxxxxxx
1212 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1213 * (after that 81 C2 xx xx xx xx)
1215 LPBYTE reteip = stack[1];
1217 if (reteip) {
1218 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1219 fun->nrofargs=reteip[2]/4;
1224 while (*rets) {
1225 for (i=0;i<ARRAY_SIZE( (*rets)->entry );i++)
1226 if (!(*rets)->entry[i].origreturn)
1227 break;
1228 if (i!=ARRAY_SIZE( (*rets)->entry ))
1229 break;
1230 rets = &((*rets)->next);
1232 if (!*rets) {
1233 SIZE_T size = 4096;
1234 VOID* addr = NULL;
1236 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1237 MEM_COMMIT | MEM_RESERVE,
1238 PAGE_EXECUTE_READWRITE);
1239 if (!addr) return;
1240 *rets = addr;
1241 i = 0; /* entry 0 is free */
1243 ret = &((*rets)->entry[i]);
1244 ret->lcall = 0xe8;
1245 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1246 ret->origreturn = stack[1];
1247 stack[1] = &ret->lcall;
1248 ret->dll = dll;
1249 ret->args = NULL;
1250 ret->ordinal = fun - dll->funs;
1251 ret->origESP = stack;
1253 stack[0] = fun->origfun;
1255 if (!TRACE_ON(snoop)) return;
1257 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1258 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1259 if (fun->nrofargs>0) {
1260 max = fun->nrofargs; if (max>16) max=16;
1261 for (i=0;i<max;i++)
1263 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1264 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1266 if (max!=fun->nrofargs)
1267 TRACE_(snoop)(" ...");
1268 } else if (fun->nrofargs<0) {
1269 TRACE_(snoop)("<unknown, check return>");
1270 ret->args = RtlAllocateHeap(GetProcessHeap(),
1271 0,16*sizeof(DWORD));
1272 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1274 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1277 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1279 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1280 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1281 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1283 /* We haven't found out the nrofargs yet. If we called a cdecl
1284 * function it is too late anyway and we can just set '0' (which
1285 * will be the difference between orig and current ESP
1286 * If stdcall -> everything ok.
1288 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1289 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1290 stack[0] = ret->origreturn;
1292 if (!TRACE_ON(snoop)) {
1293 ret->origreturn = NULL; /* mark as empty */
1294 return;
1297 if (ret->args) {
1298 int i,max;
1300 if (fun->name)
1301 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1302 else
1303 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1305 max = fun->nrofargs;
1306 if (max>16) max=16;
1308 for (i=0;i<max;i++)
1310 SNOOP_PrintArg(ret->args[i]);
1311 if (i<max-1) TRACE_(snoop)(",");
1313 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1314 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1315 ret->args = NULL;
1317 else
1319 if (fun->name)
1320 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1321 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1322 else
1323 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1324 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1325 retval, (DWORD)ret->origreturn);
1327 ret->origreturn = NULL; /* mark as empty */
1330 /* small wrappers that save registers and get the stack pointer */
1331 #define SNOOP_WRAPPER(name) \
1332 __ASM_STDCALL_FUNC( name, 0, \
1333 "pushl %eax\n\t" \
1334 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1335 "pushl %ecx\n\t" \
1336 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1337 "pushl %edx\n\t" \
1338 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1339 "leal 12(%esp),%eax\n\t" \
1340 "pushl %eax\n\t" \
1341 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1342 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1343 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1344 "popl %edx\n\t" \
1345 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1346 "popl %ecx\n\t" \
1347 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1348 "popl %eax\n\t" \
1349 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1350 "ret" )
1352 SNOOP_WRAPPER( SNOOP_Entry )
1353 SNOOP_WRAPPER( SNOOP_Return )
1355 #else /* __i386__ */
1357 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1358 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1360 return origfun;
1363 void SNOOP_SetupDLL( HMODULE hmod )
1365 FIXME("snooping works only on i386 for now.\n");
1368 #endif /* __i386__ */