wined3d: Use glFinish() for synchronisation when cleaning up a destroyed context...
[wine.git] / dlls / ntdll / relay.c
blobee7d9951c59f875ade8aae087fb5a0bf9be056c3
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 ".arm\n\t"
549 "push {r4-r8,lr}\n\t"
550 "sub sp, #16\n\t"
551 "mov r6, r2\n\t"
552 "add r3, sp, #12\n\t"
553 "mov r7, r0\n\t"
554 "mov r8, r1\n\t"
555 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
556 "mov ip, r0\n\t" /* entry point */
557 "mov r5, sp\n\t"
558 "ldr r1, [sp, #12]\n\t" /* number of args */
559 "lsl r3, r1, #2\n\t"
560 "subs r3, #16\n\t" /* first 4 args are in registers */
561 "ble 2f\n\t"
562 "sub sp, r3\n\t"
563 "and sp, #~7\n"
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 "vldmdbne r6!, {s0-s15}\n\t"
574 #else
575 "ldm r6, {r0-r3}\n\t"
576 #endif
577 "blx ip\n\t"
578 "mov sp, r5\n\t"
579 "ldr r2, [r6, #-4]\n\t" /* retaddr */
580 "mov r4, r0\n\t"
581 "mov r5, r1\n\t"
582 "mov r0, r7\n\t"
583 "mov r1, r8\n\t"
584 "str r4, [sp]\n\t"
585 "str r5, [sp, #4]\n\t"
586 #ifndef __SOFTFP__
587 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
588 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
589 "vldr d0, [sp, #8]\n\t"
590 #else
591 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
592 #endif
593 "mov r0, r4\n\t"
594 "mov r1, r5\n\t"
595 "add sp, #16\n\t"
596 "pop {r4-r8,pc}" )
598 #elif defined(__aarch64__)
600 /***********************************************************************
601 * relay_trace_entry
603 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
604 const INT_PTR *stack, unsigned int *nb_args )
606 WORD ordinal = LOWORD(idx);
607 const char *arg_types = descr->args_string + HIWORD(idx);
608 struct relay_private_data *data = descr->private;
609 struct relay_entry_point *entry_point = data->entry_points + ordinal;
610 unsigned int i;
612 TRACE( "\1Call %s(", func_name( data, ordinal ));
614 for (i = 0; !is_ret_val( arg_types[i] ); i++)
616 switch (arg_types[i])
618 case 's': /* str */
619 trace_string_a( stack[i] );
620 break;
621 case 'w': /* wstr */
622 trace_string_w( stack[i] );
623 break;
624 case 'i': /* long */
625 default:
626 TRACE( "%08zx", stack[i] );
627 break;
629 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
631 *nb_args = i;
632 TRACE( ") ret=%08zx\n", stack[-1] );
633 return entry_point->orig_func;
636 /***********************************************************************
637 * relay_trace_exit
639 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
640 INT_PTR retaddr, INT_PTR retval )
642 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
643 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
646 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
647 __ASM_GLOBAL_FUNC( call_entry_point,
648 "stp x29, x30, [SP,#-16]!\n\t"
649 "stp x19, x20, [SP,#-16]!\n\t"
650 "mov x29, SP\n\t"
651 "mov x19, x2\n\t"
652 "ldp x8, x9, [x19, #-32]\n\t"
653 "mov x9, x0\n\t"
654 "cbz w1, 2f\n\t"
655 "mov w10, w1\n\t"
656 "mov x11, x2\n\t"
657 "mov x12, #0\n\t"
658 "ldp x0, x1, [x11], #16\n\t"
659 "add w12, w12, #2\n\t"
660 "cmp w12, w10\n\t"
661 "b.hs 2f\n\t"
662 "ldp x2, x3, [x11], #16\n\t"
663 "add w12, w12, #2\n\t"
664 "cmp w12, w10\n\t"
665 "b.hs 2f\n\t"
666 "ldp x4, x5, [x11], #16\n\t"
667 "add w12, w12, #2\n\t"
668 "cmp w12, w10\n\t"
669 "b.hs 2f\n\t"
670 "ldp x6, x7, [x11], #16\n\t"
671 "add w12, w12, #2\n\t"
672 "cmp w12, w10\n\t"
673 "b.hs 2f\n\t"
674 "sub w12, w10, #8\n\t"
675 "lsl w12, w12, #3\n\t"
676 "sub SP, SP, w12, uxtw\n\t"
677 "tbz w12, #3, 1f\n\t"
678 "sub SP, SP, #8\n\t"
679 "1: sub w12, w12, #8\n\t"
680 "ldr x13, [x11, x12]\n\t"
681 "str x13, [SP, x12]\n\t"
682 "cbnz w12, 1b\n\t"
683 "2: blr x9\n\t"
684 "mov SP, x29\n\t"
685 "ldp x19, x20, [SP], #16\n\t"
686 "ldp x29, x30, [SP], #16\n\t"
687 "ret\n" )
689 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
691 unsigned int nb_args;
692 void *func = relay_trace_entry( descr, idx, stack, &nb_args );
693 LONGLONG ret = call_entry_point( func, nb_args, stack );
694 relay_trace_exit( descr, idx, stack[-1], ret );
695 return ret;
698 #elif defined(__x86_64__)
700 /***********************************************************************
701 * relay_trace_entry
703 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
704 const INT_PTR *stack, unsigned int *nb_args )
706 WORD ordinal = LOWORD(idx);
707 const char *arg_types = descr->args_string + HIWORD(idx);
708 struct relay_private_data *data = descr->private;
709 struct relay_entry_point *entry_point = data->entry_points + ordinal;
710 unsigned int i;
712 TRACE( "\1Call %s(", func_name( data, ordinal ));
714 for (i = 0; !is_ret_val( arg_types[i] ); i++)
716 switch (arg_types[i])
718 case 's': /* str */
719 trace_string_a( stack[i] );
720 break;
721 case 'w': /* wstr */
722 trace_string_w( stack[i] );
723 break;
724 case 'f': /* float */
725 TRACE( "%g", *(const float *)&stack[i] );
726 break;
727 case 'd': /* double */
728 TRACE( "%g", *(const double *)&stack[i] );
729 break;
730 case 'i': /* long */
731 default:
732 TRACE( "%08zx", stack[i] );
733 break;
735 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
737 *nb_args = i;
738 TRACE( ") ret=%08zx\n", stack[-1] );
739 return entry_point->orig_func;
742 /***********************************************************************
743 * relay_trace_exit
745 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
746 INT_PTR retaddr, INT_PTR retval )
748 TRACE( "\1Ret %s() retval=%08zx ret=%08zx\n",
749 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
752 extern INT_PTR WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
753 __ASM_GLOBAL_FUNC( relay_call,
754 "pushq %rbp\n\t"
755 __ASM_SEH(".seh_pushreg %rbp\n\t")
756 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
757 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
758 "movq %rsp,%rbp\n\t"
759 __ASM_SEH(".seh_setframe %rbp,0\n\t")
760 __ASM_SEH(".seh_endprologue\n\t")
761 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
762 "leaq -0x48(%rbp),%rsp\n\t"
763 "andq $~15,%rsp\n\t"
764 "movq %rcx,-32(%rbp)\n\t"
765 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
766 "movq %rdx,-24(%rbp)\n\t"
767 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
768 "movq %rsi,-16(%rbp)\n\t"
769 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
770 "movq %rdi,-8(%rbp)\n\t"
771 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
772 /* trace the parameters */
773 "leaq 24(%rbp),%r8\n\t" /* stack */
774 "leaq -40(%rbp),%r9\n\t"
775 "call " __ASM_NAME("relay_trace_entry") "\n\t"
776 /* copy the arguments */
777 "movl -40(%rbp),%edx\n\t" /* number of args */
778 "movq $4,%rcx\n\t"
779 "cmp %rcx,%rdx\n\t"
780 "cmovgq %rdx,%rcx\n\t"
781 "leaq -16(,%rcx,8),%rdx\n\t"
782 "andq $~15,%rdx\n\t"
783 "subq %rdx,%rsp\n\t"
784 "leaq 24(%rbp),%rsi\n\t" /* original stack */
785 "movq %rsp,%rdi\n\t"
786 "rep; movsq\n\t"
787 /* call the entry point */
788 "movq 0(%rsp),%rcx\n\t"
789 "movq 8(%rsp),%rdx\n\t"
790 "movq 16(%rsp),%r8\n\t"
791 "movq 24(%rsp),%r9\n\t"
792 "movq 0(%rsp),%xmm0\n\t"
793 "movq 8(%rsp),%xmm1\n\t"
794 "movq 16(%rsp),%xmm2\n\t"
795 "movq 24(%rsp),%xmm3\n\t"
796 "callq *%rax\n\t"
797 /* trace the return value */
798 "movq -32(%rbp),%rcx\n\t"
799 "movq -24(%rbp),%rdx\n\t"
800 "movq 16(%rbp),%r8\n\t" /* retaddr */
801 "movq %rax,%rsi\n\t"
802 "movaps %xmm0,32(%rsp)\n\t"
803 "movq %rax,%r9\n\t"
804 "call " __ASM_NAME("relay_trace_exit") "\n\t"
805 /* restore return value and return */
806 "movq %rsi,%rax\n\t"
807 "movaps 32(%rsp),%xmm0\n\t"
808 "movq -16(%rbp),%rsi\n\t"
809 __ASM_CFI(".cfi_same_value %rsi\n\t")
810 "movq -8(%rbp),%rdi\n\t"
811 __ASM_CFI(".cfi_same_value %rdi\n\t")
812 "leaq 0(%rbp),%rsp\n\t"
813 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
814 "popq %rbp\n\t"
815 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
816 __ASM_CFI(".cfi_same_value %rbp\n\t")
817 "ret")
819 #else
820 #error Not supported on this CPU
821 #endif
824 static struct relay_descr *get_relay_descr( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
825 DWORD exp_size )
827 struct relay_descr *descr;
828 struct relay_descr_rva *rva;
829 ULONG_PTR ptr = (ULONG_PTR)module + exports->Name;
831 /* sanity checks */
832 if (ptr <= (ULONG_PTR)(exports + 1)) return NULL;
833 if (ptr > (ULONG_PTR)exports + exp_size) return NULL;
834 if (ptr % sizeof(DWORD)) return NULL;
836 rva = (struct relay_descr_rva *)ptr - 1;
837 if (rva->magic != RELAY_DESCR_MAGIC) return NULL;
838 if (rva->descr) descr = (struct relay_descr *)((char *)module + rva->descr);
839 else descr = (struct relay_descr *)((const char *)exports + exp_size);
840 if (descr->magic != RELAY_DESCR_MAGIC) return NULL;
841 return descr;
844 /***********************************************************************
845 * RELAY_GetProcAddress
847 * Return the proc address to use for a given function.
849 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
850 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
852 struct relay_private_data *data;
853 const struct relay_descr *descr = get_relay_descr( module, exports, exp_size );
855 if (!descr || !(data = descr->private)) return proc; /* no relay data */
856 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
857 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
858 return proc; /* we want to relay it */
859 return data->entry_points[ordinal].orig_func;
863 /***********************************************************************
864 * RELAY_SetupDLL
866 * Setup relay debugging for a built-in dll.
868 void RELAY_SetupDLL( HMODULE module )
870 IMAGE_EXPORT_DIRECTORY *exports;
871 DWORD *funcs;
872 unsigned int i, len;
873 DWORD size, entry_point_rva, old_prot;
874 struct relay_descr *descr;
875 struct relay_private_data *data;
876 WCHAR dllnameW[sizeof(data->dllname)];
877 const WORD *ordptr;
878 void *func_base;
879 SIZE_T func_size;
881 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
883 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
884 if (!exports) return;
886 if (!(descr = get_relay_descr( module, exports, size ))) return;
888 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
889 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
890 return;
892 descr->relay_call = relay_call;
893 descr->private = data;
895 data->module = module;
896 data->base = exports->Base;
897 len = strlen( (char *)module + exports->Name );
898 if (len > 4 && !_stricmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
899 len = min( len, sizeof(data->dllname) - 1 );
900 memcpy( data->dllname, (char *)module + exports->Name, len );
901 data->dllname[len] = 0;
902 ascii_to_unicode( dllnameW, data->dllname, len + 1 );
904 /* fetch name pointer for all entry points and store them in the private structure */
906 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
907 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
909 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
910 data->entry_points[*ordptr].name = (const char *)module + name_rva;
913 /* patch the functions in the export table to point to the relay thunks */
915 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
916 entry_point_rva = descr->entry_point_base - (const char *)module;
918 func_base = funcs;
919 func_size = exports->NumberOfFunctions * sizeof(*funcs);
920 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, PAGE_READWRITE, &old_prot );
921 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
923 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
924 if (!check_relay_include( dllnameW, i + exports->Base, data->entry_points[i].name ))
925 continue; /* don't include this entry point */
927 data->entry_points[i].orig_func = (char *)module + *funcs;
928 *funcs = entry_point_rva + descr->entry_point_offsets[i];
930 if (old_prot != PAGE_READWRITE)
931 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, old_prot, &old_prot );
934 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
936 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
937 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
939 return proc;
942 void RELAY_SetupDLL( HMODULE module )
946 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
949 /***********************************************************************/
950 /* snoop support */
951 /***********************************************************************/
953 #ifdef __i386__
955 WINE_DECLARE_DEBUG_CHANNEL(seh);
956 WINE_DECLARE_DEBUG_CHANNEL(snoop);
958 #include "pshpack1.h"
960 typedef struct
962 /* code part */
963 BYTE lcall; /* 0xe8 call snoopentry (relative) */
964 DWORD snoopentry; /* SNOOP_Entry relative */
965 /* unreached */
966 int nrofargs;
967 FARPROC origfun;
968 const char *name;
969 } SNOOP_FUN;
971 typedef struct tagSNOOP_DLL {
972 HMODULE hmod;
973 SNOOP_FUN *funs;
974 DWORD ordbase;
975 DWORD nrofordinals;
976 struct tagSNOOP_DLL *next;
977 char name[1];
978 } SNOOP_DLL;
980 typedef struct
982 /* code part */
983 BYTE lcall; /* 0xe8 call snoopret relative*/
984 DWORD snoopret; /* SNOOP_Ret relative */
985 /* unreached */
986 FARPROC origreturn;
987 SNOOP_DLL *dll;
988 DWORD ordinal;
989 void **origESP;
990 DWORD *args; /* saved args across a stdcall */
991 } SNOOP_RETURNENTRY;
993 typedef struct tagSNOOP_RETURNENTRIES {
994 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
995 struct tagSNOOP_RETURNENTRIES *next;
996 } SNOOP_RETURNENTRIES;
998 #include "poppack.h"
1000 extern void WINAPI SNOOP_Entry(void);
1001 extern void WINAPI SNOOP_Return(void);
1003 static SNOOP_DLL *firstdll;
1004 static SNOOP_RETURNENTRIES *firstrets;
1007 /***********************************************************************
1008 * SNOOP_ShowDebugmsgSnoop
1010 * Simple function to decide if a particular debugging message is
1011 * wanted.
1013 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
1015 WCHAR moduleW[40];
1016 int len = strlen(module);
1018 if (len >= ARRAY_SIZE( moduleW )) return FALSE;
1019 ascii_to_unicode( moduleW, module, len + 1 );
1020 if (debug_snoop_excludelist && check_list( moduleW, ordinal, func, debug_snoop_excludelist ))
1021 return FALSE;
1022 if (debug_snoop_includelist && !check_list( moduleW, ordinal, func, debug_snoop_includelist ))
1023 return FALSE;
1024 return TRUE;
1028 /***********************************************************************
1029 * SNOOP_SetupDLL
1031 * Setup snoop debugging for a native dll.
1033 void SNOOP_SetupDLL(HMODULE hmod)
1035 SNOOP_DLL **dll = &firstdll;
1036 char *p, *name;
1037 void *addr;
1038 SIZE_T size;
1039 ULONG size32;
1040 IMAGE_EXPORT_DIRECTORY *exports;
1042 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1044 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1045 if (!exports || !exports->NumberOfFunctions) return;
1046 name = (char *)hmod + exports->Name;
1047 size = size32;
1049 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1051 while (*dll) {
1052 if ((*dll)->hmod == hmod)
1054 /* another dll, loaded at the same address */
1055 addr = (*dll)->funs;
1056 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1057 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1058 break;
1060 dll = &((*dll)->next);
1062 if (*dll)
1063 *dll = RtlReAllocateHeap(GetProcessHeap(),
1064 HEAP_ZERO_MEMORY, *dll,
1065 sizeof(SNOOP_DLL) + strlen(name));
1066 else
1067 *dll = RtlAllocateHeap(GetProcessHeap(),
1068 HEAP_ZERO_MEMORY,
1069 sizeof(SNOOP_DLL) + strlen(name));
1070 (*dll)->hmod = hmod;
1071 (*dll)->ordbase = exports->Base;
1072 (*dll)->nrofordinals = exports->NumberOfFunctions;
1073 strcpy( (*dll)->name, name );
1074 p = (*dll)->name + strlen((*dll)->name) - 4;
1075 if (p > (*dll)->name && !_stricmp( p, ".dll" )) *p = 0;
1077 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1078 addr = NULL;
1079 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1080 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1081 if (!addr) {
1082 RtlFreeHeap(GetProcessHeap(),0,*dll);
1083 FIXME("out of memory\n");
1084 return;
1086 (*dll)->funs = addr;
1087 memset((*dll)->funs,0,size);
1091 /***********************************************************************
1092 * SNOOP_GetProcAddress
1094 * Return the proc address to use for a given function.
1096 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1097 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1098 const WCHAR *user)
1100 unsigned int i;
1101 const char *ename;
1102 const WORD *ordinals;
1103 const DWORD *names;
1104 SNOOP_DLL *dll = firstdll;
1105 SNOOP_FUN *fun;
1106 const IMAGE_SECTION_HEADER *sec;
1108 if (!TRACE_ON(snoop)) return origfun;
1109 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1110 return origfun; /* the calling module was explicitly excluded */
1112 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1113 return origfun;
1115 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1117 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1118 return origfun; /* most likely a data reference */
1120 while (dll) {
1121 if (hmod == dll->hmod)
1122 break;
1123 dll = dll->next;
1125 if (!dll) /* probably internal */
1126 return origfun;
1128 /* try to find a name for it */
1129 ename = NULL;
1130 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1131 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1132 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1134 if (ordinals[i] == ordinal)
1136 ename = (const char *)hmod + names[i];
1137 break;
1140 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1141 return origfun;
1142 assert(ordinal < dll->nrofordinals);
1143 fun = dll->funs + ordinal;
1144 if (!fun->name)
1146 fun->name = ename;
1147 fun->lcall = 0xe8;
1148 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1149 fun->origfun = origfun;
1150 fun->nrofargs = -1;
1152 return (FARPROC)&(fun->lcall);
1155 static void SNOOP_PrintArg(DWORD x)
1157 int i,nostring;
1159 TRACE_(snoop)("%08x",x);
1160 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1161 __TRY
1163 LPBYTE s=(LPBYTE)x;
1164 i=0;nostring=0;
1165 while (i<80) {
1166 if (s[i]==0) break;
1167 if (s[i]<0x20) {nostring=1;break;}
1168 if (s[i]>=0x80) {nostring=1;break;}
1169 i++;
1171 if (!nostring && i > 5)
1172 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1173 else /* try unicode */
1175 LPWSTR s=(LPWSTR)x;
1176 i=0;nostring=0;
1177 while (i<80) {
1178 if (s[i]==0) break;
1179 if (s[i]<0x20) {nostring=1;break;}
1180 if (s[i]>0x100) {nostring=1;break;}
1181 i++;
1183 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1186 __EXCEPT_PAGE_FAULT
1189 __ENDTRY
1192 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1194 SNOOP_DLL *dll;
1195 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1196 SNOOP_RETURNENTRIES **rets = &firstrets;
1197 SNOOP_RETURNENTRY *ret;
1198 int i=0, max;
1200 for (dll = firstdll; dll; dll = dll->next )
1201 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1203 if (!dll) {
1204 FIXME("entrypoint %p not found\n", fun);
1205 return; /* oops */
1207 /* guess cdecl ... */
1208 if (fun->nrofargs<0) {
1209 /* Typical cdecl return frame is:
1210 * add esp, xxxxxxxx
1211 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1212 * (after that 81 C2 xx xx xx xx)
1214 LPBYTE reteip = stack[1];
1216 if (reteip) {
1217 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1218 fun->nrofargs=reteip[2]/4;
1223 while (*rets) {
1224 for (i=0;i<ARRAY_SIZE( (*rets)->entry );i++)
1225 if (!(*rets)->entry[i].origreturn)
1226 break;
1227 if (i!=ARRAY_SIZE( (*rets)->entry ))
1228 break;
1229 rets = &((*rets)->next);
1231 if (!*rets) {
1232 SIZE_T size = 4096;
1233 VOID* addr = NULL;
1235 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1236 MEM_COMMIT | MEM_RESERVE,
1237 PAGE_EXECUTE_READWRITE);
1238 if (!addr) return;
1239 *rets = addr;
1240 i = 0; /* entry 0 is free */
1242 ret = &((*rets)->entry[i]);
1243 ret->lcall = 0xe8;
1244 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1245 ret->origreturn = stack[1];
1246 stack[1] = &ret->lcall;
1247 ret->dll = dll;
1248 ret->args = NULL;
1249 ret->ordinal = fun - dll->funs;
1250 ret->origESP = stack;
1252 stack[0] = fun->origfun;
1254 if (!TRACE_ON(snoop)) return;
1256 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1257 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1258 if (fun->nrofargs>0) {
1259 max = fun->nrofargs; if (max>16) max=16;
1260 for (i=0;i<max;i++)
1262 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1263 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1265 if (max!=fun->nrofargs)
1266 TRACE_(snoop)(" ...");
1267 } else if (fun->nrofargs<0) {
1268 TRACE_(snoop)("<unknown, check return>");
1269 ret->args = RtlAllocateHeap(GetProcessHeap(),
1270 0,16*sizeof(DWORD));
1271 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1273 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1276 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1278 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1279 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1280 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1282 /* We haven't found out the nrofargs yet. If we called a cdecl
1283 * function it is too late anyway and we can just set '0' (which
1284 * will be the difference between orig and current ESP
1285 * If stdcall -> everything ok.
1287 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1288 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1289 stack[0] = ret->origreturn;
1291 if (!TRACE_ON(snoop)) {
1292 ret->origreturn = NULL; /* mark as empty */
1293 return;
1296 if (ret->args) {
1297 int i,max;
1299 if (fun->name)
1300 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1301 else
1302 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1304 max = fun->nrofargs;
1305 if (max>16) max=16;
1307 for (i=0;i<max;i++)
1309 SNOOP_PrintArg(ret->args[i]);
1310 if (i<max-1) TRACE_(snoop)(",");
1312 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1313 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1314 ret->args = NULL;
1316 else
1318 if (fun->name)
1319 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1320 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1321 else
1322 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1323 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1324 retval, (DWORD)ret->origreturn);
1326 ret->origreturn = NULL; /* mark as empty */
1329 /* small wrappers that save registers and get the stack pointer */
1330 #define SNOOP_WRAPPER(name) \
1331 __ASM_STDCALL_FUNC( name, 0, \
1332 "pushl %eax\n\t" \
1333 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1334 "pushl %ecx\n\t" \
1335 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1336 "pushl %edx\n\t" \
1337 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1338 "leal 12(%esp),%eax\n\t" \
1339 "pushl %eax\n\t" \
1340 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1341 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1342 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1343 "popl %edx\n\t" \
1344 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1345 "popl %ecx\n\t" \
1346 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1347 "popl %eax\n\t" \
1348 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1349 "ret" )
1351 SNOOP_WRAPPER( SNOOP_Entry )
1352 SNOOP_WRAPPER( SNOOP_Return )
1354 #else /* __i386__ */
1356 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1357 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1359 return origfun;
1362 void SNOOP_SetupDLL( HMODULE hmod )
1364 FIXME("snooping works only on i386 for now.\n");
1367 #endif /* __i386__ */