server: Support unbound console input device.
[wine.git] / dlls / ntdll / relay.c
blobe159631249108a306920562d42841cc21cde39c3
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;
170 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
171 'W','i','n','e','\\',
172 'D','e','b','u','g',0};
173 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
174 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
175 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
176 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
177 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
178 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
179 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
180 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
182 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
183 attr.Length = sizeof(attr);
184 attr.RootDirectory = root;
185 attr.ObjectName = &name;
186 attr.Attributes = 0;
187 attr.SecurityDescriptor = NULL;
188 attr.SecurityQualityOfService = NULL;
189 RtlInitUnicodeString( &name, configW );
191 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
192 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
193 NtClose( root );
194 if (!hkey) return TRUE;
196 debug_relay_includelist = load_list( hkey, RelayIncludeW );
197 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
198 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
199 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
200 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
201 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
202 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
203 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
205 NtClose( hkey );
206 return TRUE;
210 /***********************************************************************
211 * check_list
213 * Check if a given module and function is in the list.
215 static BOOL check_list( const WCHAR *module, int ordinal, const char *func, const WCHAR *const *list )
217 char ord_str[10];
219 sprintf( ord_str, "%d", ordinal );
220 for(; *list; list++)
222 const WCHAR *p = wcsrchr( *list, '.' );
223 if (p && p > *list) /* check module and function */
225 int len = p - *list;
226 if (wcsnicmp( module, *list, len - 1 ) || module[len]) continue;
227 if (p[1] == '*' && !p[2]) return TRUE;
228 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
229 if (func && !strcmpAW( func, p + 1 )) return TRUE;
231 else /* function only */
233 if (func && !strcmpAW( func, *list )) return TRUE;
236 return FALSE;
240 /***********************************************************************
241 * check_relay_include
243 * Check if a given function must be included in the relay output.
245 static BOOL check_relay_include( const WCHAR *module, int ordinal, const char *func )
247 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
248 return FALSE;
249 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
250 return FALSE;
251 return TRUE;
254 /***********************************************************************
255 * check_from_module
257 * Check if calls from a given module must be included in the relay/snoop output,
258 * given the exclusion and inclusion lists.
260 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
262 static const WCHAR dllW[] = {'.','d','l','l',0 };
263 const WCHAR **listitem;
264 BOOL show;
266 if (!module) return TRUE;
267 if (!includelist && !excludelist) return TRUE;
268 if (excludelist)
270 show = TRUE;
271 listitem = excludelist;
273 else
275 show = FALSE;
276 listitem = includelist;
278 for(; *listitem; listitem++)
280 int len;
282 if (!wcsicmp( *listitem, module )) return !show;
283 len = wcslen( *listitem );
284 if (!wcsnicmp( *listitem, module, len ) && !wcsicmp( module + len, dllW ))
285 return !show;
287 return show;
291 static BOOL is_ret_val( char type )
293 return type >= 'A' && type <= 'Z';
296 static const char *func_name( struct relay_private_data *data, unsigned int ordinal )
298 struct relay_entry_point *entry_point = data->entry_points + ordinal;
300 if (entry_point->name)
301 return wine_dbg_sprintf( "%s.%s", data->dllname, entry_point->name );
302 else
303 return wine_dbg_sprintf( "%s.%u", data->dllname, data->base + ordinal );
306 static void trace_string_a( INT_PTR ptr )
308 if (!IS_INTARG( ptr )) TRACE( "%08Ix %s", ptr, debugstr_a( (char *)ptr ));
309 else TRACE( "%08Ix", ptr );
312 static void trace_string_w( INT_PTR ptr )
314 if (!IS_INTARG( ptr )) TRACE( "%08Ix %s", ptr, debugstr_w( (WCHAR *)ptr ));
315 else TRACE( "%08Ix", ptr );
318 #ifdef __i386__
320 /***********************************************************************
321 * relay_trace_entry
323 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
324 const DWORD *stack, unsigned int *nb_args )
326 WORD ordinal = LOWORD(idx);
327 const char *arg_types = descr->args_string + HIWORD(idx);
328 struct relay_private_data *data = descr->private;
329 struct relay_entry_point *entry_point = data->entry_points + ordinal;
330 unsigned int i, pos;
332 TRACE( "\1Call %s(", func_name( data, ordinal ));
334 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
336 switch (arg_types[i])
338 case 'j': /* int64 */
339 TRACE( "%x%08x", stack[pos+1], stack[pos] );
340 pos += 2;
341 break;
342 case 'k': /* int128 */
343 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
344 pos += 4;
345 break;
346 case 's': /* str */
347 trace_string_a( stack[pos++] );
348 break;
349 case 'w': /* wstr */
350 trace_string_w( stack[pos++] );
351 break;
352 case 'f': /* float */
353 TRACE( "%g", *(const float *)&stack[pos++] );
354 break;
355 case 'd': /* double */
356 TRACE( "%g", *(const double *)&stack[pos] );
357 pos += 2;
358 break;
359 case 'i': /* long */
360 default:
361 TRACE( "%08x", stack[pos++] );
362 break;
364 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
366 *nb_args = pos;
367 if (arg_types[0] == 't')
369 *nb_args |= 0x80000000; /* thiscall/fastcall */
370 if (arg_types[1] == 't') *nb_args |= 0x40000000; /* fastcall */
372 TRACE( ") ret=%08x\n", stack[-1] );
373 return entry_point->orig_func;
376 /***********************************************************************
377 * relay_trace_exit
379 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
380 void *retaddr, LONGLONG retval )
382 const char *arg_types = descr->args_string + HIWORD(idx);
384 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
386 while (!is_ret_val( *arg_types )) arg_types++;
387 if (*arg_types == 'J') /* int64 return value */
388 TRACE( " retval=%08x%08x ret=%08x\n",
389 (UINT)(retval >> 32), (UINT)retval, (UINT)retaddr );
390 else
391 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, (UINT)retaddr );
394 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx );
395 __ASM_STDCALL_FUNC( relay_call, 8,
396 "pushl %ebp\n\t"
397 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
398 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
399 "movl %esp,%ebp\n\t"
400 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
401 "pushl %esi\n\t"
402 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
403 "pushl %edi\n\t"
404 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
405 "pushl %ecx\n\t"
406 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
407 /* trace the parameters */
408 "pushl %eax\n\t"
409 "pushl %esp\n\t" /* number of args return ptr */
410 "leal 20(%ebp),%esi\n\t" /* stack */
411 "pushl %esi\n\t"
412 "pushl 12(%ebp)\n\t"
413 "pushl 8(%ebp)\n\t"
414 "call " __ASM_STDCALL("relay_trace_entry",16) "\n\t"
415 /* copy the arguments*/
416 "movzwl -16(%ebp),%ecx\n\t" /* number of args */
417 "jecxz 1f\n\t"
418 "leal 0(,%ecx,4),%edx\n\t"
419 "subl %edx,%esp\n\t"
420 "andl $~15,%esp\n\t"
421 "movl %esp,%edi\n\t"
422 "cld\n\t"
423 "rep; movsl\n\t"
424 "testl $0x80000000,-16(%ebp)\n\t" /* thiscall */
425 "jz 1f\n\t"
426 "popl %ecx\n\t"
427 "testl $0x40000000,-16(%ebp)\n\t" /* fastcall */
428 "jz 1f\n\t"
429 "popl %edx\n"
430 /* call the entry point */
431 "1:\tcall *%eax\n\t"
432 "movl %eax,%esi\n\t"
433 "movl %edx,%edi\n\t"
434 /* trace the return value */
435 "leal -20(%ebp),%esp\n\t"
436 "pushl %edx\n\t"
437 "pushl %eax\n\t"
438 "pushl 16(%ebp)\n\t"
439 "pushl 12(%ebp)\n\t"
440 "pushl 8(%ebp)\n\t"
441 "call " __ASM_STDCALL("relay_trace_exit",20) "\n\t"
442 /* restore return value and return */
443 "leal -12(%ebp),%esp\n\t"
444 "movl %esi,%eax\n\t"
445 "movl %edi,%edx\n\t"
446 "popl %ecx\n\t"
447 __ASM_CFI(".cfi_same_value %ecx\n\t")
448 "popl %edi\n\t"
449 __ASM_CFI(".cfi_same_value %edi\n\t")
450 "popl %esi\n\t"
451 __ASM_CFI(".cfi_same_value %esi\n\t")
452 "popl %ebp\n\t"
453 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
454 __ASM_CFI(".cfi_same_value %ebp\n\t")
455 "ret $8" )
457 #elif defined(__arm__)
459 /***********************************************************************
460 * relay_trace_entry
462 DECLSPEC_HIDDEN void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx,
463 const DWORD *stack, unsigned int *nb_args )
465 WORD ordinal = LOWORD(idx);
466 const char *arg_types = descr->args_string + HIWORD(idx);
467 struct relay_private_data *data = descr->private;
468 struct relay_entry_point *entry_point = data->entry_points + ordinal;
469 unsigned int i, pos;
470 #ifndef __SOFTFP__
471 unsigned int float_pos = 0, double_pos = 0;
472 const union fpregs { float s[16]; double d[8]; } *fpstack = (const union fpregs *)stack - 1;
473 #endif
475 TRACE( "\1Call %s(", func_name( data, ordinal ));
477 for (i = pos = 0; !is_ret_val( arg_types[i] ); i++)
479 switch (arg_types[i])
481 case 'j': /* int64 */
482 pos = (pos + 1) & ~1;
483 TRACE( "%x%08x", stack[pos+1], stack[pos] );
484 pos += 2;
485 break;
486 case 'k': /* int128 */
487 TRACE( "{%08x,%08x,%08x,%08x}", stack[pos], stack[pos+1], stack[pos+2], stack[pos+3] );
488 pos += 4;
489 break;
490 case 's': /* str */
491 trace_string_a( stack[pos++] );
492 break;
493 case 'w': /* wstr */
494 trace_string_w( stack[pos++] );
495 break;
496 case 'f': /* float */
497 #ifndef __SOFTFP__
498 if (!(float_pos % 2)) float_pos = max( float_pos, double_pos * 2 );
499 if (float_pos < 16)
501 TRACE( "%g", fpstack->s[float_pos++] );
502 break;
504 #endif
505 TRACE( "%g", *(const float *)&stack[pos++] );
506 break;
507 case 'd': /* double */
508 #ifndef __SOFTFP__
509 double_pos = max( (float_pos + 1) / 2, double_pos );
510 if (double_pos < 8)
512 TRACE( "%g", fpstack->d[double_pos++] );
513 break;
515 #endif
516 pos = (pos + 1) & ~1;
517 TRACE( "%g", *(const double *)&stack[pos] );
518 pos += 2;
519 break;
520 case 'i': /* long */
521 default:
522 TRACE( "%08x", stack[pos++] );
523 break;
525 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
528 #ifndef __SOFTFP__
529 if (float_pos || double_pos)
531 pos |= 0x80000000;
532 stack = (const DWORD *)fpstack; /* retaddr is below the fp regs */
534 #endif
535 *nb_args = pos;
536 TRACE( ") ret=%08x\n", stack[-1] );
537 return entry_point->orig_func;
540 /***********************************************************************
541 * relay_trace_exit
543 DECLSPEC_HIDDEN void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
544 DWORD retaddr, LONGLONG retval )
546 const char *arg_types = descr->args_string + HIWORD(idx);
548 TRACE( "\1Ret %s()", func_name( descr->private, LOWORD(idx) ));
550 while (!is_ret_val( *arg_types )) arg_types++;
551 if (*arg_types == 'J') /* int64 return value */
552 TRACE( " retval=%08x%08x ret=%08x\n",
553 (UINT)(retval >> 32), (UINT)retval, retaddr );
554 else
555 TRACE( " retval=%08x ret=%08x\n", (UINT)retval, retaddr );
558 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const DWORD *stack );
559 __ASM_GLOBAL_FUNC( relay_call,
560 ".arm\n\t"
561 "push {r4-r8,lr}\n\t"
562 "sub sp, #16\n\t"
563 "mov r6, r2\n\t"
564 "add r3, sp, #12\n\t"
565 "mov r7, r0\n\t"
566 "mov r8, r1\n\t"
567 "bl " __ASM_NAME("relay_trace_entry") "\n\t"
568 "mov ip, r0\n\t" /* entry point */
569 "mov r5, sp\n\t"
570 "ldr r1, [sp, #12]\n\t" /* number of args */
571 "lsl r3, r1, #2\n\t"
572 "subs r3, #16\n\t" /* first 4 args are in registers */
573 "ble 2f\n\t"
574 "sub sp, r3\n\t"
575 "and sp, #~7\n"
576 "add r2, r6, #16\n\t" /* skip r0-r3 */
577 "1:\tsubs r3, r3, #4\n\t"
578 "ldr r0, [r2, r3]\n\t"
579 "str r0, [sp, r3]\n\t"
580 "bgt 1b\n"
581 "2:\t"
582 #ifndef __SOFTFP__
583 "tst r1, #0x80000000\n\t"
584 "ldm r6, {r0-r3}\n\t"
585 "vldmdbne r6!, {s0-s15}\n\t"
586 #else
587 "ldm r6, {r0-r3}\n\t"
588 #endif
589 "blx ip\n\t"
590 "mov sp, r5\n\t"
591 "ldr r2, [r6, #-4]\n\t" /* retaddr */
592 "mov r4, r0\n\t"
593 "mov r5, r1\n\t"
594 "mov r0, r7\n\t"
595 "mov r1, r8\n\t"
596 "str r4, [sp]\n\t"
597 "str r5, [sp, #4]\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( "%08zx", stack[i] );
639 break;
641 if (!is_ret_val( arg_types[i + 1] )) TRACE( "," );
643 *nb_args = i;
644 TRACE( ") ret=%08zx\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=%08zx ret=%08zx\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( "%08zx", stack[i] );
745 break;
747 if (!is_ret_val( arg_types[i+1] )) TRACE( "," );
749 *nb_args = i;
750 TRACE( ") ret=%08zx\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=%08zx ret=%08zx\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_SEH(".seh_pushreg %rbp\n\t")
768 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
769 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
770 "movq %rsp,%rbp\n\t"
771 __ASM_SEH(".seh_setframe %rbp,0\n\t")
772 __ASM_SEH(".seh_endprologue\n\t")
773 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
774 "leaq -0x48(%rbp),%rsp\n\t"
775 "andq $~15,%rsp\n\t"
776 "movq %rcx,-32(%rbp)\n\t"
777 __ASM_CFI(".cfi_rel_offset %rcx,-32\n\t")
778 "movq %rdx,-24(%rbp)\n\t"
779 __ASM_CFI(".cfi_rel_offset %rdx,-24\n\t")
780 "movq %rsi,-16(%rbp)\n\t"
781 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
782 "movq %rdi,-8(%rbp)\n\t"
783 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
784 /* trace the parameters */
785 "leaq 24(%rbp),%r8\n\t" /* stack */
786 "leaq -40(%rbp),%r9\n\t"
787 "call " __ASM_NAME("relay_trace_entry") "\n\t"
788 /* copy the arguments */
789 "movl -40(%rbp),%edx\n\t" /* number of args */
790 "movq $4,%rcx\n\t"
791 "cmp %rcx,%rdx\n\t"
792 "cmovgq %rdx,%rcx\n\t"
793 "leaq -16(,%rcx,8),%rdx\n\t"
794 "andq $~15,%rdx\n\t"
795 "subq %rdx,%rsp\n\t"
796 "leaq 24(%rbp),%rsi\n\t" /* original stack */
797 "movq %rsp,%rdi\n\t"
798 "rep; movsq\n\t"
799 /* call the entry point */
800 "movq 0(%rsp),%rcx\n\t"
801 "movq 8(%rsp),%rdx\n\t"
802 "movq 16(%rsp),%r8\n\t"
803 "movq 24(%rsp),%r9\n\t"
804 "movq 0(%rsp),%xmm0\n\t"
805 "movq 8(%rsp),%xmm1\n\t"
806 "movq 16(%rsp),%xmm2\n\t"
807 "movq 24(%rsp),%xmm3\n\t"
808 "callq *%rax\n\t"
809 /* trace the return value */
810 "movq -32(%rbp),%rcx\n\t"
811 "movq -24(%rbp),%rdx\n\t"
812 "movq 16(%rbp),%r8\n\t" /* retaddr */
813 "movq %rax,%rsi\n\t"
814 "movaps %xmm0,32(%rsp)\n\t"
815 "movq %rax,%r9\n\t"
816 "call " __ASM_NAME("relay_trace_exit") "\n\t"
817 /* restore return value and return */
818 "movq %rsi,%rax\n\t"
819 "movaps 32(%rsp),%xmm0\n\t"
820 "movq -16(%rbp),%rsi\n\t"
821 __ASM_CFI(".cfi_same_value %rsi\n\t")
822 "movq -8(%rbp),%rdi\n\t"
823 __ASM_CFI(".cfi_same_value %rdi\n\t")
824 "leaq 0(%rbp),%rsp\n\t"
825 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
826 "popq %rbp\n\t"
827 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
828 __ASM_CFI(".cfi_same_value %rbp\n\t")
829 "ret")
831 #else
832 #error Not supported on this CPU
833 #endif
836 static struct relay_descr *get_relay_descr( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
837 DWORD exp_size )
839 struct relay_descr *descr;
840 struct relay_descr_rva *rva;
841 ULONG_PTR ptr = (ULONG_PTR)module + exports->Name;
843 /* sanity checks */
844 if (ptr <= (ULONG_PTR)(exports + 1)) return NULL;
845 if (ptr > (ULONG_PTR)exports + exp_size) return NULL;
846 if (ptr % sizeof(DWORD)) return NULL;
848 rva = (struct relay_descr_rva *)ptr - 1;
849 if (rva->magic != RELAY_DESCR_MAGIC) return NULL;
850 if (rva->descr) descr = (struct relay_descr *)((char *)module + rva->descr);
851 else descr = (struct relay_descr *)((const char *)exports + exp_size);
852 if (descr->magic != RELAY_DESCR_MAGIC) return NULL;
853 return descr;
856 /***********************************************************************
857 * RELAY_GetProcAddress
859 * Return the proc address to use for a given function.
861 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
862 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
864 struct relay_private_data *data;
865 const struct relay_descr *descr = get_relay_descr( module, exports, exp_size );
867 if (!descr || !(data = descr->private)) return proc; /* no relay data */
868 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
869 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
870 return proc; /* we want to relay it */
871 return data->entry_points[ordinal].orig_func;
875 /***********************************************************************
876 * RELAY_SetupDLL
878 * Setup relay debugging for a built-in dll.
880 void RELAY_SetupDLL( HMODULE module )
882 IMAGE_EXPORT_DIRECTORY *exports;
883 DWORD *funcs;
884 unsigned int i, len;
885 DWORD size, entry_point_rva, old_prot;
886 struct relay_descr *descr;
887 struct relay_private_data *data;
888 WCHAR dllnameW[sizeof(data->dllname)];
889 const WORD *ordptr;
890 void *func_base;
891 SIZE_T func_size;
893 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
895 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
896 if (!exports) return;
898 if (!(descr = get_relay_descr( module, exports, size ))) return;
900 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
901 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
902 return;
904 descr->relay_call = relay_call;
905 descr->private = data;
907 data->module = module;
908 data->base = exports->Base;
909 len = strlen( (char *)module + exports->Name );
910 if (len > 4 && !_stricmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
911 len = min( len, sizeof(data->dllname) - 1 );
912 memcpy( data->dllname, (char *)module + exports->Name, len );
913 data->dllname[len] = 0;
914 ascii_to_unicode( dllnameW, data->dllname, len + 1 );
916 /* fetch name pointer for all entry points and store them in the private structure */
918 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
919 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
921 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
922 data->entry_points[*ordptr].name = (const char *)module + name_rva;
925 /* patch the functions in the export table to point to the relay thunks */
927 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
928 entry_point_rva = descr->entry_point_base - (const char *)module;
930 func_base = funcs;
931 func_size = exports->NumberOfFunctions * sizeof(*funcs);
932 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, PAGE_READWRITE, &old_prot );
933 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
935 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
936 if (!check_relay_include( dllnameW, i + exports->Base, data->entry_points[i].name ))
937 continue; /* don't include this entry point */
939 data->entry_points[i].orig_func = (char *)module + *funcs;
940 *funcs = entry_point_rva + descr->entry_point_offsets[i];
942 if (old_prot != PAGE_READWRITE)
943 NtProtectVirtualMemory( NtCurrentProcess(), &func_base, &func_size, old_prot, &old_prot );
946 #else /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
948 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
949 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
951 return proc;
954 void RELAY_SetupDLL( HMODULE module )
958 #endif /* __i386__ || __x86_64__ || __arm__ || __aarch64__ */
961 /***********************************************************************/
962 /* snoop support */
963 /***********************************************************************/
965 #ifdef __i386__
967 WINE_DECLARE_DEBUG_CHANNEL(seh);
968 WINE_DECLARE_DEBUG_CHANNEL(snoop);
970 #include "pshpack1.h"
972 typedef struct
974 /* code part */
975 BYTE lcall; /* 0xe8 call snoopentry (relative) */
976 DWORD snoopentry; /* SNOOP_Entry relative */
977 /* unreached */
978 int nrofargs;
979 FARPROC origfun;
980 const char *name;
981 } SNOOP_FUN;
983 typedef struct tagSNOOP_DLL {
984 HMODULE hmod;
985 SNOOP_FUN *funs;
986 DWORD ordbase;
987 DWORD nrofordinals;
988 struct tagSNOOP_DLL *next;
989 char name[1];
990 } SNOOP_DLL;
992 typedef struct
994 /* code part */
995 BYTE lcall; /* 0xe8 call snoopret relative*/
996 DWORD snoopret; /* SNOOP_Ret relative */
997 /* unreached */
998 FARPROC origreturn;
999 SNOOP_DLL *dll;
1000 DWORD ordinal;
1001 void **origESP;
1002 DWORD *args; /* saved args across a stdcall */
1003 } SNOOP_RETURNENTRY;
1005 typedef struct tagSNOOP_RETURNENTRIES {
1006 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
1007 struct tagSNOOP_RETURNENTRIES *next;
1008 } SNOOP_RETURNENTRIES;
1010 #include "poppack.h"
1012 extern void WINAPI SNOOP_Entry(void);
1013 extern void WINAPI SNOOP_Return(void);
1015 static SNOOP_DLL *firstdll;
1016 static SNOOP_RETURNENTRIES *firstrets;
1019 /***********************************************************************
1020 * SNOOP_ShowDebugmsgSnoop
1022 * Simple function to decide if a particular debugging message is
1023 * wanted.
1025 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
1027 WCHAR moduleW[40];
1028 int len = strlen(module);
1030 if (len >= ARRAY_SIZE( moduleW )) return FALSE;
1031 ascii_to_unicode( moduleW, module, len + 1 );
1032 if (debug_snoop_excludelist && check_list( moduleW, ordinal, func, debug_snoop_excludelist ))
1033 return FALSE;
1034 if (debug_snoop_includelist && !check_list( moduleW, ordinal, func, debug_snoop_includelist ))
1035 return FALSE;
1036 return TRUE;
1040 /***********************************************************************
1041 * SNOOP_SetupDLL
1043 * Setup snoop debugging for a native dll.
1045 void SNOOP_SetupDLL(HMODULE hmod)
1047 SNOOP_DLL **dll = &firstdll;
1048 char *p, *name;
1049 void *addr;
1050 SIZE_T size;
1051 ULONG size32;
1052 IMAGE_EXPORT_DIRECTORY *exports;
1054 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
1056 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
1057 if (!exports || !exports->NumberOfFunctions) return;
1058 name = (char *)hmod + exports->Name;
1059 size = size32;
1061 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
1063 while (*dll) {
1064 if ((*dll)->hmod == hmod)
1066 /* another dll, loaded at the same address */
1067 addr = (*dll)->funs;
1068 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
1069 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
1070 break;
1072 dll = &((*dll)->next);
1074 if (*dll)
1075 *dll = RtlReAllocateHeap(GetProcessHeap(),
1076 HEAP_ZERO_MEMORY, *dll,
1077 sizeof(SNOOP_DLL) + strlen(name));
1078 else
1079 *dll = RtlAllocateHeap(GetProcessHeap(),
1080 HEAP_ZERO_MEMORY,
1081 sizeof(SNOOP_DLL) + strlen(name));
1082 (*dll)->hmod = hmod;
1083 (*dll)->ordbase = exports->Base;
1084 (*dll)->nrofordinals = exports->NumberOfFunctions;
1085 strcpy( (*dll)->name, name );
1086 p = (*dll)->name + strlen((*dll)->name) - 4;
1087 if (p > (*dll)->name && !_stricmp( p, ".dll" )) *p = 0;
1089 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
1090 addr = NULL;
1091 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1092 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
1093 if (!addr) {
1094 RtlFreeHeap(GetProcessHeap(),0,*dll);
1095 FIXME("out of memory\n");
1096 return;
1098 (*dll)->funs = addr;
1099 memset((*dll)->funs,0,size);
1103 /***********************************************************************
1104 * SNOOP_GetProcAddress
1106 * Return the proc address to use for a given function.
1108 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
1109 DWORD exp_size, FARPROC origfun, DWORD ordinal,
1110 const WCHAR *user)
1112 unsigned int i;
1113 const char *ename;
1114 const WORD *ordinals;
1115 const DWORD *names;
1116 SNOOP_DLL *dll = firstdll;
1117 SNOOP_FUN *fun;
1118 const IMAGE_SECTION_HEADER *sec;
1120 if (!TRACE_ON(snoop)) return origfun;
1121 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
1122 return origfun; /* the calling module was explicitly excluded */
1124 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
1125 return origfun;
1127 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
1129 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
1130 return origfun; /* most likely a data reference */
1132 while (dll) {
1133 if (hmod == dll->hmod)
1134 break;
1135 dll = dll->next;
1137 if (!dll) /* probably internal */
1138 return origfun;
1140 /* try to find a name for it */
1141 ename = NULL;
1142 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
1143 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
1144 if (names) for (i = 0; i < exports->NumberOfNames; i++)
1146 if (ordinals[i] == ordinal)
1148 ename = (const char *)hmod + names[i];
1149 break;
1152 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
1153 return origfun;
1154 assert(ordinal < dll->nrofordinals);
1155 fun = dll->funs + ordinal;
1156 if (!fun->name)
1158 fun->name = ename;
1159 fun->lcall = 0xe8;
1160 fun->snoopentry = (char *)SNOOP_Entry - (char *)(&fun->snoopentry + 1);
1161 fun->origfun = origfun;
1162 fun->nrofargs = -1;
1164 return (FARPROC)&(fun->lcall);
1167 static void SNOOP_PrintArg(DWORD x)
1169 int i,nostring;
1171 TRACE_(snoop)("%08x",x);
1172 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
1173 __TRY
1175 LPBYTE s=(LPBYTE)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]>=0x80) {nostring=1;break;}
1181 i++;
1183 if (!nostring && i > 5)
1184 TRACE_(snoop)(" %s",debugstr_an((LPSTR)x,i));
1185 else /* try unicode */
1187 LPWSTR s=(LPWSTR)x;
1188 i=0;nostring=0;
1189 while (i<80) {
1190 if (s[i]==0) break;
1191 if (s[i]<0x20) {nostring=1;break;}
1192 if (s[i]>0x100) {nostring=1;break;}
1193 i++;
1195 if (!nostring && i > 5) TRACE_(snoop)(" %s",debugstr_wn((LPWSTR)x,i));
1198 __EXCEPT_PAGE_FAULT
1201 __ENDTRY
1204 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Entry( void **stack )
1206 SNOOP_DLL *dll;
1207 SNOOP_FUN *fun = (SNOOP_FUN *)((char *)stack[0] - 5);
1208 SNOOP_RETURNENTRIES **rets = &firstrets;
1209 SNOOP_RETURNENTRY *ret;
1210 int i=0, max;
1212 for (dll = firstdll; dll; dll = dll->next )
1213 if (fun >= dll->funs && fun < dll->funs + dll->nrofordinals) break;
1215 if (!dll) {
1216 FIXME("entrypoint %p not found\n", fun);
1217 return; /* oops */
1219 /* guess cdecl ... */
1220 if (fun->nrofargs<0) {
1221 /* Typical cdecl return frame is:
1222 * add esp, xxxxxxxx
1223 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1224 * (after that 81 C2 xx xx xx xx)
1226 LPBYTE reteip = stack[1];
1228 if (reteip) {
1229 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1230 fun->nrofargs=reteip[2]/4;
1235 while (*rets) {
1236 for (i=0;i<ARRAY_SIZE( (*rets)->entry );i++)
1237 if (!(*rets)->entry[i].origreturn)
1238 break;
1239 if (i!=ARRAY_SIZE( (*rets)->entry ))
1240 break;
1241 rets = &((*rets)->next);
1243 if (!*rets) {
1244 SIZE_T size = 4096;
1245 VOID* addr = NULL;
1247 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1248 MEM_COMMIT | MEM_RESERVE,
1249 PAGE_EXECUTE_READWRITE);
1250 if (!addr) return;
1251 *rets = addr;
1252 i = 0; /* entry 0 is free */
1254 ret = &((*rets)->entry[i]);
1255 ret->lcall = 0xe8;
1256 ret->snoopret = (char *)SNOOP_Return - (char *)(&ret->snoopret + 1);
1257 ret->origreturn = stack[1];
1258 stack[1] = &ret->lcall;
1259 ret->dll = dll;
1260 ret->args = NULL;
1261 ret->ordinal = fun - dll->funs;
1262 ret->origESP = stack;
1264 stack[0] = fun->origfun;
1266 if (!TRACE_ON(snoop)) return;
1268 if (fun->name) TRACE_(snoop)("\1CALL %s.%s(", dll->name, fun->name);
1269 else TRACE_(snoop)("\1CALL %s.%d(", dll->name, dll->ordbase+ret->ordinal);
1270 if (fun->nrofargs>0) {
1271 max = fun->nrofargs; if (max>16) max=16;
1272 for (i=0;i<max;i++)
1274 SNOOP_PrintArg( (DWORD)stack[i + 2] );
1275 if (i<fun->nrofargs-1) TRACE_(snoop)(",");
1277 if (max!=fun->nrofargs)
1278 TRACE_(snoop)(" ...");
1279 } else if (fun->nrofargs<0) {
1280 TRACE_(snoop)("<unknown, check return>");
1281 ret->args = RtlAllocateHeap(GetProcessHeap(),
1282 0,16*sizeof(DWORD));
1283 memcpy(ret->args, stack + 2, sizeof(DWORD)*16);
1285 TRACE_(snoop)(") ret=%08x\n",(DWORD)ret->origreturn);
1288 void WINAPI DECLSPEC_HIDDEN __regs_SNOOP_Return( void **stack )
1290 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)((char *)stack[0] - 5);
1291 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1292 DWORD retval = (DWORD)stack[-1]; /* get saved %eax from the stack */
1294 /* We haven't found out the nrofargs yet. If we called a cdecl
1295 * function it is too late anyway and we can just set '0' (which
1296 * will be the difference between orig and current ESP
1297 * If stdcall -> everything ok.
1299 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1300 ret->dll->funs[ret->ordinal].nrofargs = stack - ret->origESP - 1;
1301 stack[0] = ret->origreturn;
1303 if (!TRACE_ON(snoop)) {
1304 ret->origreturn = NULL; /* mark as empty */
1305 return;
1308 if (ret->args) {
1309 int i,max;
1311 if (fun->name)
1312 TRACE_(snoop)("\1RET %s.%s(", ret->dll->name, fun->name);
1313 else
1314 TRACE_(snoop)("\1RET %s.%d(", ret->dll->name, ret->dll->ordbase+ret->ordinal);
1316 max = fun->nrofargs;
1317 if (max>16) max=16;
1319 for (i=0;i<max;i++)
1321 SNOOP_PrintArg(ret->args[i]);
1322 if (i<max-1) TRACE_(snoop)(",");
1324 TRACE_(snoop)(") retval=%08x ret=%08x\n", retval, (DWORD)ret->origreturn );
1325 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1326 ret->args = NULL;
1328 else
1330 if (fun->name)
1331 TRACE_(snoop)("\1RET %s.%s() retval=%08x ret=%08x\n",
1332 ret->dll->name, fun->name, retval, (DWORD)ret->origreturn);
1333 else
1334 TRACE_(snoop)("\1RET %s.%d() retval=%08x ret=%08x\n",
1335 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1336 retval, (DWORD)ret->origreturn);
1338 ret->origreturn = NULL; /* mark as empty */
1341 /* small wrappers that save registers and get the stack pointer */
1342 #define SNOOP_WRAPPER(name) \
1343 __ASM_STDCALL_FUNC( name, 0, \
1344 "pushl %eax\n\t" \
1345 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1346 "pushl %ecx\n\t" \
1347 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1348 "pushl %edx\n\t" \
1349 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1350 "leal 12(%esp),%eax\n\t" \
1351 "pushl %eax\n\t" \
1352 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1353 "call " __ASM_STDCALL("__regs_" #name,4) "\n\t" \
1354 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1355 "popl %edx\n\t" \
1356 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1357 "popl %ecx\n\t" \
1358 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1359 "popl %eax\n\t" \
1360 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
1361 "ret" )
1363 SNOOP_WRAPPER( SNOOP_Entry )
1364 SNOOP_WRAPPER( SNOOP_Return )
1366 #else /* __i386__ */
1368 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1369 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1371 return origfun;
1374 void SNOOP_SetupDLL( HMODULE hmod )
1376 FIXME("snooping works only on i386 for now.\n");
1379 #endif /* __i386__ */