ntdll: Fix handling of floating point arguments in relay debugging on ARM.
[wine.git] / dlls / ntdll / relay.c
bloba0c3cb1190f8ead1005119b4b9889345bfa8b756
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') *nb_args |= 0x80000000; /* thiscall */
375 TRACE( ") ret=%08x\n", stack[-1] );
376 return entry_point->orig_func;
379 /***********************************************************************
380 * relay_trace_exit
382 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
383 void *retaddr, LONGLONG retval )
385 const char *arg_types = descr->args_string + HIWORD(idx);
387 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
389 while (!is_ret_val( *arg_types )) arg_types++;
390 if (*arg_types == 'J') /* int64 return value */
391 TRACE( " retval=%08x%08x ret=%08x\n",
392 (UINT)(retval >> 32), (UINT)retval, (UINT)retaddr );
393 else
394 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, (UINT)retaddr );
397 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx );
398 __ASM_GLOBAL_FUNC( relay_call,
399 "pushl %ebp\n\t"
400 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
401 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
402 "movl %esp,%ebp\n\t"
403 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
404 "pushl %esi\n\t"
405 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
406 "pushl %edi\n\t"
407 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
408 "pushl %ecx\n\t"
409 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
410 /* trace the parameters */
411 "pushl %eax\n\t"
412 "pushl %esp\n\t" /* number of args return ptr */
413 "leal 20(%ebp),%esi\n\t" /* stack */
414 "pushl %esi\n\t"
415 "pushl 12(%ebp)\n\t"
416 "pushl 8(%ebp)\n\t"
417 "call " __ASM_NAME("relay_trace_entry") "\n\t"
418 /* copy the arguments*/
419 "movl -16(%ebp),%ecx\n\t" /* number of args */
420 "jecxz 1f\n\t"
421 "andl $0x7fffffff,%ecx\n\t"
422 "leal 0(,%ecx,4),%edx\n\t"
423 "subl %edx,%esp\n\t"
424 "andl $~15,%esp\n\t"
425 "movl %esp,%edi\n\t"
426 "cld\n\t"
427 "rep; movsl\n\t"
428 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
429 "jz 1f\n\t"
430 "popl %ecx\n"
431 /* call the entry point */
432 "1:\tcall *%eax\n\t"
433 "movl %eax,%esi\n\t"
434 "movl %edx,%edi\n\t"
435 /* trace the return value */
436 "leal -20(%ebp),%esp\n\t"
437 "pushl %edx\n\t"
438 "pushl %eax\n\t"
439 "pushl 16(%ebp)\n\t"
440 "pushl 12(%ebp)\n\t"
441 "pushl 8(%ebp)\n\t"
442 "call " __ASM_NAME("relay_trace_exit") "\n\t"
443 /* restore return value and return */
444 "leal -12(%ebp),%esp\n\t"
445 "movl %esi,%eax\n\t"
446 "movl %edi,%edx\n\t"
447 "popl %ecx\n\t"
448 __ASM_CFI(".cfi_same_value %ecx\n\t")
449 "popl %edi\n\t"
450 __ASM_CFI(".cfi_same_value %edi\n\t")
451 "popl %esi\n\t"
452 __ASM_CFI(".cfi_same_value %esi\n\t")
453 "popl %ebp\n\t"
454 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
455 __ASM_CFI(".cfi_same_value %ebp\n\t")
456 "ret $8" )
458 #elif defined(__arm__)
460 /***********************************************************************
461 * relay_trace_entry
463 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
464 const DWORD *stack, unsigned int *nb_args )
466 WORD ordinal = LOWORD(idx);
467 const char *arg_types = descr->args_string + HIWORD(idx);
468 struct relay_private_data *data = descr->private;
469 struct relay_entry_point *entry_point = data->entry_points + ordinal;
470 unsigned int i, pos;
471 #ifndef __SOFTFP__
472 unsigned int float_pos = 0, double_pos = 0;
473 const union fpregs { float s[16]; double d[8]; } *fpstack = (const union fpregs *)stack - 1;
474 #endif
476 TRACE( "\1Call %s(", func_name( data, ordinal ));
478 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
480 switch (arg_types[i])
482 case 'j': /* int64 */
483 pos = (pos + 1) & ~1;
484 TRACE( "%x%08x", stack[pos+1], stack[pos] );
485 pos += 2;
486 break;
487 case 'k': /* int128 */
488 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
489 pos += 4;
490 break;
491 case 's': /* str */
492 trace_string_a( stack[pos++] );
493 break;
494 case 'w': /* wstr */
495 trace_string_w( stack[pos++] );
496 break;
497 case 'f': /* float */
498 #ifndef __SOFTFP__
499 if (!(float_pos % 2)) float_pos = max( float_pos, double_pos * 2 );
500 if (float_pos < 16)
502 TRACE( "%g", fpstack->s[float_pos++] );
503 break;
505 #endif
506 TRACE( "%g", *(const float *)&stack[pos++] );
507 break;
508 case 'd': /* double */
509 #ifndef __SOFTFP__
510 double_pos = max( (float_pos + 1) / 2, double_pos );
511 if (double_pos < 8)
513 TRACE( "%g", fpstack->d[double_pos++] );
514 break;
516 #endif
517 pos = (pos + 1) & ~1;
518 TRACE( "%g", *(const double *)&stack[pos] );
519 pos += 2;
520 break;
521 case 'i': /* long */
522 default:
523 TRACE( "%08x", stack[pos++] );
524 break;
526 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
529 #ifndef __SOFTFP__
530 if (float_pos || double_pos)
532 pos |= 0x80000000;
533 stack = (const DWORD *)fpstack; /* retaddr is below the fp regs */
535 #endif
536 *nb_args = pos;
537 TRACE( ") ret=%08x\n", stack[-1] );
538 return entry_point->orig_func;
541 /***********************************************************************
542 * relay_trace_exit
544 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
545 DWORD retaddr, LONGLONG retval )
547 const char *arg_types = descr->args_string + HIWORD(idx);
549 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
551 while (!is_ret_val( *arg_types )) arg_types++;
552 if (*arg_types == 'J') /* int64 return value */
553 TRACE( " retval=%08x%08x ret=%08x\n",
554 (UINT)(retval >> 32), (UINT)retval, retaddr );
555 else
556 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, retaddr );
559 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const DWORD *stack );
560 __ASM_GLOBAL_FUNC( relay_call,
561 ".arm\n\t"
562 "push {r4-r8,lr}\n\t"
563 "sub sp, #16\n\t"
564 "mov r6, r2\n\t"
565 "add r3, sp, #12\n\t"
566 "mov r7, r0\n\t"
567 "mov r8, r1\n\t"
568 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
569 "mov ip, r0\n\t" /* entry point */
570 "mov r5, sp\n\t"
571 "ldr r1, [sp, #12]\n\t" /* number of args */
572 "lsl r3, r1, #2\n\t"
573 "subs r3, #16\n\t" /* first 4 args are in registers */
574 "ble 2f\n\t"
575 "sub sp, r3\n\t"
576 "and sp, #~7\n"
577 "add r2, r6, #16\n\t" /* skip r0-r3 */
578 "1:\tsubs r3, r3, #4\n\t"
579 "ldr r0, [r2, r3]\n\t"
580 "str r0, [sp, r3]\n\t"
581 "bgt 1b\n"
582 "2:\t"
583 #ifndef __SOFTFP__
584 "tst r1, #0x80000000\n\t"
585 "ldm r6, {r0-r3}\n\t"
586 "vldmdbne r6!, {s0-s15}\n\t"
587 #else
588 "ldm r6, {r0-r3}\n\t"
589 #endif
590 "blx ip\n\t"
591 "mov sp, r5\n\t"
592 "ldr r2, [r6, #-4]\n\t" /* retaddr */
593 "mov r4, r0\n\t"
594 "mov r5, r1\n\t"
595 "mov r0, r7\n\t"
596 "mov r1, r8\n\t"
597 "strd r4, [sp]\n\t"
598 #ifndef __SOFTFP__
599 "vstr d0, [sp, #8]\n\t" /* preserve floating point retval */
600 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
601 "vldr d0, [sp, #8]\n\t"
602 #else
603 "bl " __ASM_NAME("relay_trace_exit") "\n\t"
604 #endif
605 "mov r0, r4\n\t"
606 "mov r1, r5\n\t"
607 "add sp, #16\n\t"
608 "pop {r4-r8,pc}" )
610 #elif defined(__aarch64__)
612 /***********************************************************************
613 * relay_trace_entry
615 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
616 const INT_PTR *stack, unsigned int *nb_args )
618 WORD ordinal = LOWORD(idx);
619 const char *arg_types = descr->args_string + HIWORD(idx);
620 struct relay_private_data *data = descr->private;
621 struct relay_entry_point *entry_point = data->entry_points + ordinal;
622 unsigned int i;
624 TRACE( "\1Call %s(", func_name( data, ordinal ));
626 for (i = 0; !is_ret_val( arg_types[i] ); i++)
628 switch (arg_types[i])
630 case 's': /* str */
631 trace_string_a( stack[i] );
632 break;
633 case 'w': /* wstr */
634 trace_string_w( stack[i] );
635 break;
636 case 'i': /* long */
637 default:
638 TRACE( "%08lx", stack[i] );
639 break;
641 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
643 *nb_args = i;
644 TRACE( ") ret=%08lx\n", stack[-1] );
645 return entry_point->orig_func;
648 /***********************************************************************
649 * relay_trace_exit
651 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
652 INT_PTR retaddr, INT_PTR retval )
654 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
655 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
658 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args );
659 __ASM_GLOBAL_FUNC( call_entry_point,
660 "stp x29, x30, [SP,#-16]!\n\t"
661 "stp x19, x20, [SP,#-16]!\n\t"
662 "mov x29, SP\n\t"
663 "mov x19, x2\n\t"
664 "ldp x8, x9, [x19, #-32]\n\t"
665 "mov x9, x0\n\t"
666 "cbz w1, 2f\n\t"
667 "mov w10, w1\n\t"
668 "mov x11, x2\n\t"
669 "mov x12, #0\n\t"
670 "ldp x0, x1, [x11], #16\n\t"
671 "add w12, w12, #2\n\t"
672 "cmp w12, w10\n\t"
673 "b.hs 2f\n\t"
674 "ldp x2, x3, [x11], #16\n\t"
675 "add w12, w12, #2\n\t"
676 "cmp w12, w10\n\t"
677 "b.hs 2f\n\t"
678 "ldp x4, x5, [x11], #16\n\t"
679 "add w12, w12, #2\n\t"
680 "cmp w12, w10\n\t"
681 "b.hs 2f\n\t"
682 "ldp x6, x7, [x11], #16\n\t"
683 "add w12, w12, #2\n\t"
684 "cmp w12, w10\n\t"
685 "b.hs 2f\n\t"
686 "sub w12, w10, #8\n\t"
687 "lsl w12, w12, #3\n\t"
688 "sub SP, SP, w12, uxtw\n\t"
689 "tbz w12, #3, 1f\n\t"
690 "sub SP, SP, #8\n\t"
691 "1: sub w12, w12, #8\n\t"
692 "ldr x13, [x11, x12]\n\t"
693 "str x13, [SP, x12]\n\t"
694 "cbnz w12, 1b\n\t"
695 "2: blr x9\n\t"
696 "mov SP, x29\n\t"
697 "ldp x19, x20, [SP], #16\n\t"
698 "ldp x29, x30, [SP], #16\n\t"
699 "ret\n" )
701 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
703 unsigned int nb_args;
704 void *func = relay_trace_entry( descr, idx, stack, &nb_args );
705 LONGLONG ret = call_entry_point( func, nb_args, stack );
706 relay_trace_exit( descr, idx, stack[-1], ret );
707 return ret;
710 #elif defined(__x86_64__)
712 /***********************************************************************
713 * relay_trace_entry
715 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
716 const INT_PTR *stack, unsigned int *nb_args )
718 WORD ordinal = LOWORD(idx);
719 const char *arg_types = descr->args_string + HIWORD(idx);
720 struct relay_private_data *data = descr->private;
721 struct relay_entry_point *entry_point = data->entry_points + ordinal;
722 unsigned int i;
724 TRACE( "\1Call %s(", func_name( data, ordinal ));
726 for (i = 0; !is_ret_val( arg_types[i] ); i++)
728 switch (arg_types[i])
730 case 's': /* str */
731 trace_string_a( stack[i] );
732 break;
733 case 'w': /* wstr */
734 trace_string_w( stack[i] );
735 break;
736 case 'f': /* float */
737 TRACE( "%g", *(const float *)&stack[i] );
738 break;
739 case 'd': /* double */
740 TRACE( "%g", *(const double *)&stack[i] );
741 break;
742 case 'i': /* long */
743 default:
744 TRACE( "%08lx", stack[i] );
745 break;
747 if (!is_ret_val( arg_types[i] )) TRACE( "," );
749 *nb_args = i;
750 TRACE( ") ret=%08lx\n", stack[-1] );
751 return entry_point->orig_func;
754 /***********************************************************************
755 * relay_trace_exit
757 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
758 INT_PTR retaddr, INT_PTR retval )
760 TRACE( "\1Ret %s() retval=%08lx ret=%08lx\n",
761 func_name( descr->private, LOWORD(idx) ), retval, retaddr );
764 extern INT_PTR WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
765 __ASM_GLOBAL_FUNC( relay_call,
766 "pushq %rbp\n\t"
767 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
768 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
769 "movq %rsp,%rbp\n\t"
770 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
771 "leaq -0x48(%rbp),%rsp\n\t"
772 "andq $~15,%rsp\n\t"
773 "movq %rcx,-32(%rbp)\n\t"
774 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
775 "movq %rdx,-24(%rbp)\n\t"
776 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
777 "movq %rsi,-16(%rbp)\n\t"
778 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
779 "movq %rdi,-8(%rbp)\n\t"
780 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
781 /* trace the parameters */
782 "leaq 24(%rbp),%r8\n\t" /* stack */
783 "leaq -40(%rbp),%r9\n\t"
784 "call " __ASM_NAME("relay_trace_entry") "\n\t"
785 /* copy the arguments */
786 "movl -40(%rbp),%edx\n\t" /* number of args */
787 "movq $4,%rcx\n\t"
788 "cmp %rcx,%rdx\n\t"
789 "cmovgq %rdx,%rcx\n\t"
790 "leaq -16(,%rcx,8),%rdx\n\t"
791 "andq $~15,%rdx\n\t"
792 "subq %rdx,%rsp\n\t"
793 "leaq 24(%rbp),%rsi\n\t" /* original stack */
794 "movq %rsp,%rdi\n\t"
795 "rep; movsq\n\t"
796 /* call the entry point */
797 "movq 0(%rsp),%rcx\n\t"
798 "movq 8(%rsp),%rdx\n\t"
799 "movq 16(%rsp),%r8\n\t"
800 "movq 24(%rsp),%r9\n\t"
801 "movq 0(%rsp),%xmm0\n\t"
802 "movq 8(%rsp),%xmm1\n\t"
803 "movq 16(%rsp),%xmm2\n\t"
804 "movq 24(%rsp),%xmm3\n\t"
805 "callq *%rax\n\t"
806 /* trace the return value */
807 "movq -32(%rbp),%rcx\n\t"
808 "movq -24(%rbp),%rdx\n\t"
809 "movq 16(%rbp),%r8\n\t" /* retaddr */
810 "movq %rax,%rsi\n\t"
811 "movaps %xmm0,32(%rsp)\n\t"
812 "movq %rax,%r9\n\t"
813 "call " __ASM_NAME("relay_trace_exit") "\n\t"
814 /* restore return value and return */
815 "movq %rsi,%rax\n\t"
816 "movaps 32(%rsp),%xmm0\n\t"
817 "movq -16(%rbp),%rsi\n\t"
818 __ASM_CFI(".cfi_same_value %rsi\n\t")
819 "movq -8(%rbp),%rdi\n\t"
820 __ASM_CFI(".cfi_same_value %rdi\n\t")
821 "movq %rbp,%rsp\n\t"
822 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
823 "popq %rbp\n\t"
824 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
825 __ASM_CFI(".cfi_same_value %rbp\n\t")
826 "ret")
828 #else
829 #error Not supported on this CPU
830 #endif
833 /***********************************************************************
834 * RELAY_GetProcAddress
836 * Return the proc address to use for a given function.
838 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
839 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
841 struct relay_private_data *data;
842 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
844 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
845 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
846 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
847 return proc; /* we want to relay it */
848 return data->entry_points[ordinal].orig_func;
852 /***********************************************************************
853 * RELAY_SetupDLL
855 * Setup relay debugging for a built-in dll.
857 void RELAY_SetupDLL( HMODULE module )
859 IMAGE_EXPORT_DIRECTORY *exports;
860 DWORD *funcs;
861 unsigned int i, len;
862 DWORD size, entry_point_rva;
863 struct relay_descr *descr;
864 struct relay_private_data *data;
865 const WORD *ordptr;
867 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
869 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
870 if (!exports) return;
872 descr = (struct relay_descr *)((char *)exports + size);
873 if (descr->magic != RELAY_DESCR_MAGIC) return;
875 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
876 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
877 return;
879 descr->relay_call = relay_call;
880 descr->private = data;
882 data->module = module;
883 data->base = exports->Base;
884 len = strlen( (char *)module + exports->Name );
885 if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
886 len = min( len, sizeof(data->dllname) - 1 );
887 memcpy( data->dllname, (char *)module + exports->Name, len );
888 data->dllname[len] = 0;
890 /* fetch name pointer for all entry points and store them in the private structure */
892 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
893 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
895 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
896 data->entry_points[*ordptr].name = (const char *)module + name_rva;
899 /* patch the functions in the export table to point to the relay thunks */
901 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
902 entry_point_rva = descr->entry_point_base - (const char *)module;
903 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
905 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
906 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
907 continue; /* don't include this entry point */
909 data->entry_points[i].orig_func = (char *)module + *funcs;
910 *funcs = entry_point_rva + descr->entry_point_offsets[i];
914 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
916 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
917 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
919 return proc;
922 void RELAY_SetupDLL( HMODULE module )
926 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
929 /***********************************************************************/
930 /* snoop support */
931 /***********************************************************************/
933 #ifdef __i386__
935 WINE_DECLARE_DEBUG_CHANNEL(seh);
936 WINE_DECLARE_DEBUG_CHANNEL(snoop);
938 #include "pshpack1.h"
940 typedef struct
942 /* code part */
943 BYTE lcall; /* 0xe8 call snoopentry (relative) */
944 DWORD snoopentry; /* SNOOP_Entry relative */
945 /* unreached */
946 int nrofargs;
947 FARPROC origfun;
948 const char *name;
949 } SNOOP_FUN;
951 typedef struct tagSNOOP_DLL {
952 HMODULE hmod;
953 SNOOP_FUN *funs;
954 DWORD ordbase;
955 DWORD nrofordinals;
956 struct tagSNOOP_DLL *next;
957 char name[1];
958 } SNOOP_DLL;
960 typedef struct
962 /* code part */
963 BYTE lcall; /* 0xe8 call snoopret relative*/
964 DWORD snoopret; /* SNOOP_Ret relative */
965 /* unreached */
966 FARPROC origreturn;
967 SNOOP_DLL *dll;
968 DWORD ordinal;
969 void **origESP;
970 DWORD *args; /* saved args across a stdcall */
971 } SNOOP_RETURNENTRY;
973 typedef struct tagSNOOP_RETURNENTRIES {
974 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
975 struct tagSNOOP_RETURNENTRIES *next;
976 } SNOOP_RETURNENTRIES;
978 #include "poppack.h"
980 extern void WINAPI SNOOP_Entry(void);
981 extern void WINAPI SNOOP_Return(void);
983 static SNOOP_DLL *firstdll;
984 static SNOOP_RETURNENTRIES *firstrets;
987 /***********************************************************************
988 * SNOOP_ShowDebugmsgSnoop
990 * Simple function to decide if a particular debugging message is
991 * wanted.
993 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
995 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
996 return FALSE;
997 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
998 return FALSE;
999 return TRUE;
1003 /***********************************************************************
1004 * SNOOP_SetupDLL
1006 * Setup snoop debugging for a native dll.
1008 void SNOOP_SetupDLL(HMODULE hmod)
1010 SNOOP_DLL **dll = &firstdll;
1011 char *p, *name;
1012 void *addr;
1013 SIZE_T size;
1014 ULONG size32;
1015 IMAGE_EXPORT_DIRECTORY *exports;
1017 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1019 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1020 if (!exports || !exports->NumberOfFunctions) return;
1021 name = (char *)hmod + exports->Name;
1022 size = size32;
1024 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1026 while (*dll) {
1027 if ((*dll)->hmod == hmod)
1029 /* another dll, loaded at the same address */
1030 addr = (*dll)->funs;
1031 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1032 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1033 break;
1035 dll = &((*dll)->next);
1037 if (*dll)
1038 *dll = RtlReAllocateHeap(GetProcessHeap(),
1039 HEAP_ZERO_MEMORY, *dll,
1040 sizeof(SNOOP_DLL) + strlen(name));
1041 else
1042 *dll = RtlAllocateHeap(GetProcessHeap(),
1043 HEAP_ZERO_MEMORY,
1044 sizeof(SNOOP_DLL) + strlen(name));
1045 (*dll)->hmod = hmod;
1046 (*dll)->ordbase = exports->Base;
1047 (*dll)->nrofordinals = exports->NumberOfFunctions;
1048 strcpy( (*dll)->name, name );
1049 p = (*dll)->name + strlen((*dll)->name) - 4;
1050 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
1052 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1053 addr = NULL;
1054 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1055 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1056 if (!addr) {
1057 RtlFreeHeap(GetProcessHeap(),0,*dll);
1058 FIXME("out of memory\n");
1059 return;
1061 (*dll)->funs = addr;
1062 memset((*dll)->funs,0,size);
1066 /***********************************************************************
1067 * SNOOP_GetProcAddress
1069 * Return the proc address to use for a given function.
1071 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1072 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1073 const WCHAR *user)
1075 unsigned int i;
1076 const char *ename;
1077 const WORD *ordinals;
1078 const DWORD *names;
1079 SNOOP_DLL *dll = firstdll;
1080 SNOOP_FUN *fun;
1081 const IMAGE_SECTION_HEADER *sec;
1083 if (!TRACE_ON(snoop)) return origfun;
1084 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1085 return origfun; /* the calling module was explicitly excluded */
1087 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1088 return origfun;
1090 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1092 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1093 return origfun; /* most likely a data reference */
1095 while (dll) {
1096 if (hmod == dll->hmod)
1097 break;
1098 dll = dll->next;
1100 if (!dll) /* probably internal */
1101 return origfun;
1103 /* try to find a name for it */
1104 ename = NULL;
1105 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1106 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1107 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1109 if (ordinals[i] == ordinal)
1111 ename = (const char *)hmod + names[i];
1112 break;
1115 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1116 return origfun;
1117 assert(ordinal < dll->nrofordinals);
1118 fun = dll->funs + ordinal;
1119 if (!fun->name)
1121 fun->name = ename;
1122 fun->lcall = 0xe8;
1123 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1124 fun->origfun = origfun;
1125 fun->nrofargs = -1;
1127 return (FARPROC)&(fun->lcall);
1130 static void SNOOP_PrintArg(DWORD x)
1132 int i,nostring;
1134 TRACE_(snoop)("%08x",x);
1135 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1136 __TRY
1138 LPBYTE s=(LPBYTE)x;
1139 i=0;nostring=0;
1140 while (i<80) {
1141 if (s[i]==0) break;
1142 if (s[i]<0x20) {nostring=1;break;}
1143 if (s[i]>=0x80) {nostring=1;break;}
1144 i++;
1146 if (!nostring && i > 5)
1147 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1148 else /* try unicode */
1150 LPWSTR s=(LPWSTR)x;
1151 i=0;nostring=0;
1152 while (i<80) {
1153 if (s[i]==0) break;
1154 if (s[i]<0x20) {nostring=1;break;}
1155 if (s[i]>0x100) {nostring=1;break;}
1156 i++;
1158 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1161 __EXCEPT_PAGE_FAULT
1164 __ENDTRY
1167 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1169 SNOOP_DLL *dll;
1170 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1171 SNOOP_RETURNENTRIES **rets = &firstrets;
1172 SNOOP_RETURNENTRY *ret;
1173 int i=0, max;
1175 for (dll = firstdll; dll; dll = dll->next )
1176 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1178 if (!dll) {
1179 FIXME("entrypoint %p not found\n", fun);
1180 return; /* oops */
1182 /* guess cdecl ... */
1183 if (fun->nrofargs<0) {
1184 /* Typical cdecl return frame is:
1185 * add esp, xxxxxxxx
1186 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1187 * (after that 81 C2 xx xx xx xx)
1189 LPBYTE reteip = stack[1];
1191 if (reteip) {
1192 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1193 fun->nrofargs=reteip[2]/4;
1198 while (*rets) {
1199 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
1200 if (!(*rets)->entry[i].origreturn)
1201 break;
1202 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
1203 break;
1204 rets = &((*rets)->next);
1206 if (!*rets) {
1207 SIZE_T size = 4096;
1208 VOID* addr = NULL;
1210 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1211 MEM_COMMIT | MEM_RESERVE,
1212 PAGE_EXECUTE_READWRITE);
1213 if (!addr) return;
1214 *rets = addr;
1215 i = 0; /* entry 0 is free */
1217 ret = &((*rets)->entry[i]);
1218 ret->lcall = 0xe8;
1219 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1220 ret->origreturn = stack[1];
1221 stack[1] = &ret->lcall;
1222 ret->dll = dll;
1223 ret->args = NULL;
1224 ret->ordinal = fun - dll->funs;
1225 ret->origESP = stack;
1227 stack[0] = fun->origfun;
1229 if (!TRACE_ON(snoop)) return;
1231 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1232 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1233 if (fun->nrofargs>0) {
1234 max = fun->nrofargs; if (max>16) max=16;
1235 for (i=0;i<max;i++)
1237 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1238 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1240 if (max!=fun->nrofargs)
1241 TRACE_(snoop)(" ...");
1242 } else if (fun->nrofargs<0) {
1243 TRACE_(snoop)("<unknown, check return>");
1244 ret->args = RtlAllocateHeap(GetProcessHeap(),
1245 0,16*sizeof(DWORD));
1246 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1248 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1251 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1253 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1254 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1255 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1257 /* We haven't found out the nrofargs yet. If we called a cdecl
1258 * function it is too late anyway and we can just set '0' (which
1259 * will be the difference between orig and current ESP
1260 * If stdcall -> everything ok.
1262 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1263 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1264 stack[0] = ret->origreturn;
1266 if (!TRACE_ON(snoop)) {
1267 ret->origreturn = NULL; /* mark as empty */
1268 return;
1271 if (ret->args) {
1272 int i,max;
1274 if (fun->name)
1275 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1276 else
1277 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1279 max = fun->nrofargs;
1280 if (max>16) max=16;
1282 for (i=0;i<max;i++)
1284 SNOOP_PrintArg(ret->args[i]);
1285 if (i<max-1) TRACE_(snoop)(",");
1287 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1288 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1289 ret->args = NULL;
1291 else
1293 if (fun->name)
1294 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1295 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1296 else
1297 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1298 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1299 retval, (DWORD)ret->origreturn);
1301 ret->origreturn = NULL; /* mark as empty */
1304 /* small wrappers that save registers and get the stack pointer */
1305 #define SNOOP_WRAPPER(name) \
1306 __ASM_STDCALL_FUNC( name, 0, \
1307 "pushl %eax\n\t" \
1308 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1309 "pushl %ecx\n\t" \
1310 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1311 "pushl %edx\n\t" \
1312 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1313 "leal 12(%esp),%eax\n\t" \
1314 "pushl %eax\n\t" \
1315 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1316 "call " __ASM_NAME("__regs_" #name) __ASM_STDCALL(4) "\n\t" \
1317 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1318 "popl %edx\n\t" \
1319 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1320 "popl %ecx\n\t" \
1321 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1322 "popl %eax\n\t" \
1323 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1324 "ret" )
1326 SNOOP_WRAPPER( SNOOP_Entry )
1327 SNOOP_WRAPPER( SNOOP_Return )
1329 #else /* __i386__ */
1331 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1332 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1334 return origfun;
1337 void SNOOP_SetupDLL( HMODULE hmod )
1339 FIXME("snooping works only on i386 for now.\n");
1342 #endif /* __i386__ */