wined3d: Pass a wined3d_resource_desc structure to wined3d_texture_create_3d().
[wine/multimedia.git] / dlls / ntdll / relay.c
blobb6790da2b9415de6d5f1550efa10cf0d3b04fd6f
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__)
43 WINE_DECLARE_DEBUG_CHANNEL(timestamp);
45 struct relay_descr /* descriptor for a module */
47 void *magic; /* signature */
48 void *relay_call; /* functions to call from relay thunks */
49 void *relay_call_regs;
50 void *private; /* reserved for the relay code private data */
51 const char *entry_point_base; /* base address of entry point thunks */
52 const unsigned int *entry_point_offsets; /* offsets of entry points thunks */
53 const unsigned int *arg_types; /* table of argument types for all entry points */
56 #define RELAY_DESCR_MAGIC ((void *)0xdeb90001)
57 #define IS_INTARG(x) (((ULONG_PTR)(x) >> 16) == 0)
59 /* private data built at dll load time */
61 struct relay_entry_point
63 void *orig_func; /* original entry point function */
64 const char *name; /* function name (if any) */
67 struct relay_private_data
69 HMODULE module; /* module handle of this dll */
70 unsigned int base; /* ordinal base */
71 char dllname[40]; /* dll name (without .dll extension) */
72 struct relay_entry_point entry_points[1]; /* list of dll entry points */
75 static const WCHAR **debug_relay_excludelist;
76 static const WCHAR **debug_relay_includelist;
77 static const WCHAR **debug_snoop_excludelist;
78 static const WCHAR **debug_snoop_includelist;
79 static const WCHAR **debug_from_relay_excludelist;
80 static const WCHAR **debug_from_relay_includelist;
81 static const WCHAR **debug_from_snoop_excludelist;
82 static const WCHAR **debug_from_snoop_includelist;
84 static BOOL init_done;
86 /* compare an ASCII and a Unicode string without depending on the current codepage */
87 static inline int strcmpAW( const char *strA, const WCHAR *strW )
89 while (*strA && ((unsigned char)*strA == *strW)) { strA++; strW++; }
90 return (unsigned char)*strA - *strW;
93 /* compare an ASCII and a Unicode string without depending on the current codepage */
94 static inline int strncmpiAW( const char *strA, const WCHAR *strW, int n )
96 int ret = 0;
97 for ( ; n > 0; n--, strA++, strW++)
98 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
99 return ret;
102 /***********************************************************************
103 * build_list
105 * Build a function list from a ';'-separated string.
107 static const WCHAR **build_list( const WCHAR *buffer )
109 int count = 1;
110 const WCHAR *p = buffer;
111 const WCHAR **ret;
113 while ((p = strchrW( p, ';' )))
115 count++;
116 p++;
118 /* allocate count+1 pointers, plus the space for a copy of the string */
119 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
120 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
122 WCHAR *str = (WCHAR *)(ret + count + 1);
123 WCHAR *q = str;
125 strcpyW( str, buffer );
126 count = 0;
127 for (;;)
129 ret[count++] = q;
130 if (!(q = strchrW( q, ';' ))) break;
131 *q++ = 0;
133 ret[count++] = NULL;
135 return ret;
138 /***********************************************************************
139 * load_list_value
141 * Load a function list from a registry value.
143 static const WCHAR **load_list( HKEY hkey, const WCHAR *value )
145 char initial_buffer[4096];
146 char *buffer = initial_buffer;
147 DWORD count;
148 NTSTATUS status;
149 UNICODE_STRING name;
150 const WCHAR **list = NULL;
152 RtlInitUnicodeString( &name, value );
153 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(initial_buffer), &count );
154 if (status == STATUS_BUFFER_OVERFLOW)
156 buffer = RtlAllocateHeap( GetProcessHeap(), 0, count );
157 status = NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, count, &count );
159 if (status == STATUS_SUCCESS)
161 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
162 list = build_list( str );
163 if (list) TRACE( "%s = %s\n", debugstr_w(value), debugstr_w(str) );
166 if (buffer != initial_buffer) RtlFreeHeap( GetProcessHeap(), 0, buffer );
167 return list;
170 /***********************************************************************
171 * init_debug_lists
173 * Build the relay include/exclude function lists.
175 static void init_debug_lists(void)
177 OBJECT_ATTRIBUTES attr;
178 UNICODE_STRING name;
179 HANDLE root, hkey;
180 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
181 'W','i','n','e','\\',
182 'D','e','b','u','g',0};
183 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
184 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
185 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
186 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
187 static const WCHAR RelayFromIncludeW[] = {'R','e','l','a','y','F','r','o','m','I','n','c','l','u','d','e',0};
188 static const WCHAR RelayFromExcludeW[] = {'R','e','l','a','y','F','r','o','m','E','x','c','l','u','d','e',0};
189 static const WCHAR SnoopFromIncludeW[] = {'S','n','o','o','p','F','r','o','m','I','n','c','l','u','d','e',0};
190 static const WCHAR SnoopFromExcludeW[] = {'S','n','o','o','p','F','r','o','m','E','x','c','l','u','d','e',0};
192 if (init_done) return;
193 init_done = TRUE;
195 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
196 attr.Length = sizeof(attr);
197 attr.RootDirectory = root;
198 attr.ObjectName = &name;
199 attr.Attributes = 0;
200 attr.SecurityDescriptor = NULL;
201 attr.SecurityQualityOfService = NULL;
202 RtlInitUnicodeString( &name, configW );
204 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
205 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
206 NtClose( root );
207 if (!hkey) return;
209 debug_relay_includelist = load_list( hkey, RelayIncludeW );
210 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
211 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
212 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
213 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
214 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
215 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
216 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
218 NtClose( hkey );
222 /***********************************************************************
223 * check_list
225 * Check if a given module and function is in the list.
227 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
229 char ord_str[10];
231 sprintf( ord_str, "%d", ordinal );
232 for(; *list; list++)
234 const WCHAR *p = strrchrW( *list, '.' );
235 if (p && p > *list) /* check module and function */
237 int len = p - *list;
238 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
239 if (p[1] == '*' && !p[2]) return TRUE;
240 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
241 if (func && !strcmpAW( func, p + 1 )) return TRUE;
243 else /* function only */
245 if (func && !strcmpAW( func, *list )) return TRUE;
248 return FALSE;
252 /***********************************************************************
253 * check_relay_include
255 * Check if a given function must be included in the relay output.
257 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
259 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
260 return FALSE;
261 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
262 return FALSE;
263 return TRUE;
266 /***********************************************************************
267 * check_from_module
269 * Check if calls from a given module must be included in the relay/snoop output,
270 * given the exclusion and inclusion lists.
272 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
274 static const WCHAR dllW[] = {'.','d','l','l',0 };
275 const WCHAR **listitem;
276 BOOL show;
278 if (!module) return TRUE;
279 if (!includelist && !excludelist) return TRUE;
280 if (excludelist)
282 show = TRUE;
283 listitem = excludelist;
285 else
287 show = FALSE;
288 listitem = includelist;
290 for(; *listitem; listitem++)
292 int len;
294 if (!strcmpiW( *listitem, module )) return !show;
295 len = strlenW( *listitem );
296 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
297 return !show;
299 return show;
302 /***********************************************************************
303 * RELAY_PrintArgs
305 static inline void RELAY_PrintArgs( const INT_PTR *args, int nb_args, unsigned int typemask )
307 while (nb_args--)
309 if ((typemask & 3) && !IS_INTARG(*args))
311 if (typemask & 2)
312 DPRINTF( "%08lx %s", *args, debugstr_w((LPCWSTR)*args) );
313 else
314 DPRINTF( "%08lx %s", *args, debugstr_a((LPCSTR)*args) );
316 else DPRINTF( "%08lx", *args );
317 if (nb_args) DPRINTF( "," );
318 args++;
319 typemask >>= 2;
323 static void print_timestamp(void)
325 ULONG ticks = NtGetTickCount();
326 DPRINTF( "%3u.%03u:", ticks / 1000, ticks % 1000 );
329 /***********************************************************************
330 * relay_trace_entry
332 * stack points to the return address, i.e. the first argument is stack[1].
334 void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
336 WORD ordinal = LOWORD(idx);
337 BYTE nb_args = LOBYTE(HIWORD(idx));
338 struct relay_private_data *data = descr->private;
339 struct relay_entry_point *entry_point = data->entry_points + ordinal;
341 if (TRACE_ON(relay))
343 if (TRACE_ON(timestamp)) print_timestamp();
345 if (entry_point->name)
346 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
347 else
348 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
349 RELAY_PrintArgs( stack + 1, nb_args, descr->arg_types[ordinal] );
350 DPRINTF( ") ret=%08lx\n", stack[0] );
352 return entry_point->orig_func;
355 /***********************************************************************
356 * relay_trace_exit
358 void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
359 const INT_PTR *stack, LONGLONG retval )
361 WORD ordinal = LOWORD(idx);
362 BYTE flags = HIBYTE(HIWORD(idx));
363 struct relay_private_data *data = descr->private;
364 struct relay_entry_point *entry_point = data->entry_points + ordinal;
366 if (!TRACE_ON(relay)) return;
368 if (TRACE_ON(timestamp)) print_timestamp();
370 if (entry_point->name)
371 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data->dllname, entry_point->name );
372 else
373 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data->dllname, data->base + ordinal );
375 if (flags & 1) /* 64-bit return value */
376 DPRINTF( " retval=%08x%08x ret=%08lx\n",
377 (UINT)(retval >> 32), (UINT)retval, stack[0] );
378 else
379 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR)retval, stack[0] );
382 #ifdef __i386__
384 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
385 __ASM_GLOBAL_FUNC( call_entry_point,
386 "pushl %ebp\n\t"
387 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
388 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
389 "movl %esp,%ebp\n\t"
390 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
391 "pushl %esi\n\t"
392 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
393 "pushl %edi\n\t"
394 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
395 "movl 12(%ebp),%edx\n\t"
396 "shll $2,%edx\n\t"
397 "jz 1f\n\t"
398 "subl %edx,%esp\n\t"
399 "andl $~15,%esp\n\t"
400 "movl 12(%ebp),%ecx\n\t"
401 "movl 16(%ebp),%esi\n\t"
402 "movl %esp,%edi\n\t"
403 "cld\n\t"
404 "rep; movsl\n"
405 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
406 "jz 1f\n\t"
407 "popl %ecx\n\t"
408 "1:\tcall *8(%ebp)\n\t"
409 "leal -8(%ebp),%esp\n\t"
410 "popl %edi\n\t"
411 __ASM_CFI(".cfi_same_value %edi\n\t")
412 "popl %esi\n\t"
413 __ASM_CFI(".cfi_same_value %esi\n\t")
414 "popl %ebp\n\t"
415 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
416 __ASM_CFI(".cfi_same_value %ebp\n\t")
417 "ret" )
419 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
420 __ASM_GLOBAL_FUNC( relay_call,
421 "pushl %ebp\n\t"
422 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
423 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
424 "movl %esp,%ebp\n\t"
425 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
426 "pushl %esi\n\t"
427 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
428 "pushl %edi\n\t"
429 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
430 "pushl %ecx\n\t"
431 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
432 /* trace the parameters */
433 "pushl 16(%ebp)\n\t"
434 "pushl 12(%ebp)\n\t"
435 "pushl 8(%ebp)\n\t"
436 "call " __ASM_NAME("relay_trace_entry") "\n\t"
437 /* copy the arguments*/
438 "movzbl 14(%ebp),%ecx\n\t" /* number of args */
439 "jecxz 1f\n\t"
440 "leal 0(,%ecx,4),%edx\n\t"
441 "subl %edx,%esp\n\t"
442 "andl $~15,%esp\n\t"
443 "movl 16(%ebp),%esi\n\t"
444 "addl $4,%esi\n\t"
445 "movl %esp,%edi\n\t"
446 "cld\n\t"
447 "rep; movsl\n\t"
448 "testb $2,15(%ebp)\n\t" /* (flags & 2) -> thiscall */
449 "jz 1f\n\t"
450 "popl %ecx\n"
451 /* call the entry point */
452 "1:\tcall *%eax\n\t"
453 "movl %eax,%esi\n\t"
454 "movl %edx,%edi\n\t"
455 /* trace the return value */
456 "leal -20(%ebp),%esp\n\t"
457 "pushl %edx\n\t"
458 "pushl %eax\n\t"
459 "pushl 16(%ebp)\n\t"
460 "pushl 12(%ebp)\n\t"
461 "pushl 8(%ebp)\n\t"
462 "call " __ASM_NAME("relay_trace_exit") "\n\t"
463 /* restore return value and return */
464 "leal -12(%ebp),%esp\n\t"
465 "movl %esi,%eax\n\t"
466 "movl %edi,%edx\n\t"
467 "popl %ecx\n\t"
468 __ASM_CFI(".cfi_same_value %ecx\n\t")
469 "popl %edi\n\t"
470 __ASM_CFI(".cfi_same_value %edi\n\t")
471 "popl %esi\n\t"
472 __ASM_CFI(".cfi_same_value %esi\n\t")
473 "popl %ebp\n\t"
474 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
475 __ASM_CFI(".cfi_same_value %ebp\n\t")
476 "ret $12" )
478 void WINAPI __regs_relay_call_regs( struct relay_descr *descr, unsigned int idx,
479 unsigned int orig_eax, unsigned int ret_addr,
480 CONTEXT *context )
482 WORD ordinal = LOWORD(idx);
483 BYTE nb_args = LOBYTE(HIWORD(idx));
484 struct relay_private_data *data = descr->private;
485 struct relay_entry_point *entry_point = data->entry_points + ordinal;
486 BYTE *orig_func = entry_point->orig_func;
487 INT_PTR *args = (INT_PTR *)context->Esp;
488 INT_PTR args_copy[32];
490 /* restore the context to what it was before the relay thunk */
491 context->Eax = orig_eax;
492 context->Eip = ret_addr;
493 context->Esp += nb_args * sizeof(int);
495 if (TRACE_ON(relay))
497 if (entry_point->name)
498 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
499 else
500 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
501 RELAY_PrintArgs( args, nb_args, descr->arg_types[ordinal] );
502 DPRINTF( ") ret=%08x\n", ret_addr );
504 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
505 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
506 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
507 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
508 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
510 assert( orig_func[0] == 0x68 /* pushl func */ );
511 assert( orig_func[5] == 0x6a /* pushl args */ );
512 assert( orig_func[7] == 0xe8 /* call */ );
515 /* now call the real function */
517 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
518 args_copy[nb_args++] = (INT_PTR)context; /* append context argument */
520 call_entry_point( orig_func + 12 + *(int *)(orig_func + 1), nb_args, args_copy, 0 );
522 if (TRACE_ON(relay))
524 if (entry_point->name)
525 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
526 GetCurrentThreadId(), data->dllname, entry_point->name,
527 context->Eax, context->Eip );
528 else
529 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
530 GetCurrentThreadId(), data->dllname, data->base + ordinal,
531 context->Eax, context->Eip );
532 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
533 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
534 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
535 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
536 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
539 extern void WINAPI relay_call_regs(void);
540 DEFINE_REGS_ENTRYPOINT( relay_call_regs, 4 )
542 #elif defined(__arm__)
544 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
545 __ASM_GLOBAL_FUNC( call_entry_point,
546 ".arm\n\t"
547 "push {r4, r5, LR}\n\t"
548 "mov r4, r0\n\t"
549 "mov r5, SP\n\t"
550 "lsl r3, r1, #2\n\t"
551 "cmp r3, #0\n\t"
552 "beq 5f\n\t"
553 "sub SP, SP, r3\n\t"
554 "tst r1, #1\n\t"
555 "subeq SP, SP, #4\n\t"
556 "1:\tsub r3, r3, #4\n\t"
557 "ldr r0, [r2, r3]\n\t"
558 "str r0, [SP, r3]\n\t"
559 "cmp r3, #0\n\t"
560 "bgt 1b\n\t"
561 "cmp r1, #1\n\t"
562 "bgt 2f\n\t"
563 "pop {r0}\n\t"
564 "b 5f\n\t"
565 "2:\tcmp r1, #2\n\t"
566 "bgt 3f\n\t"
567 "pop {r0-r1}\n\t"
568 "b 5f\n\t"
569 "3:\tcmp r1, #3\n\t"
570 "bgt 4f\n\t"
571 "pop {r0-r2}\n\t"
572 "b 5f\n\t"
573 "4:\tpop {r0-r3}\n\t"
574 "5:\tblx r4\n\t"
575 "mov SP, r5\n\t"
576 "pop {r4, r5, PC}" )
578 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
580 BYTE nb_args = LOBYTE(HIWORD(idx));
581 BYTE flags = HIBYTE(HIWORD(idx));
582 void *func = relay_trace_entry( descr, idx, stack );
583 LONGLONG ret = call_entry_point( func, nb_args, stack + 1, flags );
584 relay_trace_exit( descr, idx, stack, ret );
585 return ret;
588 static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
590 assert(0); /* should never be called */
593 #elif defined(__x86_64__)
595 extern void * WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
596 __ASM_GLOBAL_FUNC( relay_call,
597 "pushq %rbp\n\t"
598 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
599 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
600 "movq %rsp,%rbp\n\t"
601 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
602 "subq $0x30,%rsp\n\t"
603 "movq %rsi,0x20(%rsp)\n\t"
604 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
605 "movq %rdi,0x28(%rsp)\n\t"
606 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
607 /* trace the parameters */
608 "movq %rcx,0x10(%rbp)\n\t"
609 "movq %rdx,0x18(%rbp)\n\t"
610 "movq %r8,0x20(%rbp)\n\t"
611 "call " __ASM_NAME("relay_trace_entry") "\n\t"
612 /* copy the arguments */
613 "movzbq 0x1a(%rbp),%rdx\n\t" /* number of args */
614 "movq $4,%rcx\n\t"
615 "cmp %rcx,%rdx\n\t"
616 "cmovgq %rdx,%rcx\n\t"
617 "leaq -16(,%rcx,8),%rdx\n\t"
618 "andq $~15,%rdx\n\t"
619 "subq %rdx,%rsp\n\t"
620 "movq 0x20(%rbp),%r8\n\t" /* original stack */
621 "leaq 8(%r8),%rsi\n\t"
622 "movq %rsp,%rdi\n\t"
623 "rep; movsq\n\t"
624 /* call the entry point */
625 "movq 0(%rsp),%rcx\n\t"
626 "movq 8(%rsp),%rdx\n\t"
627 "movq 16(%rsp),%r8\n\t"
628 "movq 24(%rsp),%r9\n\t"
629 "movq %rcx,%xmm0\n\t"
630 "movq %rdx,%xmm1\n\t"
631 "movq %r8,%xmm2\n\t"
632 "movq %r9,%xmm3\n\t"
633 "callq *%rax\n\t"
634 /* trace the return value */
635 "leaq -0x30(%rbp),%rsp\n\t"
636 "movq 0x10(%rbp),%rcx\n\t"
637 "movq 0x18(%rbp),%rdx\n\t"
638 "movq 0x20(%rbp),%r8\n\t"
639 "movq %rax,%rsi\n\t"
640 "movaps %xmm0,0x10(%rbp)\n\t"
641 "movq %rax,%r9\n\t"
642 "call " __ASM_NAME("relay_trace_exit") "\n\t"
643 /* restore return value and return */
644 "movq %rsi,%rax\n\t"
645 "movaps 0x10(%rbp),%xmm0\n\t"
646 "movq 0x20(%rsp),%rsi\n\t"
647 __ASM_CFI(".cfi_same_value %rsi\n\t")
648 "movq 0x28(%rsp),%rdi\n\t"
649 __ASM_CFI(".cfi_same_value %rdi\n\t")
650 "movq %rbp,%rsp\n\t"
651 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
652 "popq %rbp\n\t"
653 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
654 __ASM_CFI(".cfi_same_value %rbp\n\t")
655 "ret")
657 static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
659 assert(0); /* should never be called */
662 #else
663 #error Not supported on this CPU
664 #endif
667 /***********************************************************************
668 * RELAY_GetProcAddress
670 * Return the proc address to use for a given function.
672 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
673 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
675 struct relay_private_data *data;
676 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
678 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
679 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
680 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
681 return proc; /* we want to relay it */
682 return data->entry_points[ordinal].orig_func;
686 /***********************************************************************
687 * RELAY_SetupDLL
689 * Setup relay debugging for a built-in dll.
691 void RELAY_SetupDLL( HMODULE module )
693 IMAGE_EXPORT_DIRECTORY *exports;
694 DWORD *funcs;
695 unsigned int i, len;
696 DWORD size, entry_point_rva;
697 struct relay_descr *descr;
698 struct relay_private_data *data;
699 const WORD *ordptr;
701 if (!init_done) init_debug_lists();
703 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
704 if (!exports) return;
706 descr = (struct relay_descr *)((char *)exports + size);
707 if (descr->magic != RELAY_DESCR_MAGIC) return;
709 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
710 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
711 return;
713 descr->relay_call = relay_call;
714 descr->relay_call_regs = relay_call_regs;
715 descr->private = data;
717 data->module = module;
718 data->base = exports->Base;
719 len = strlen( (char *)module + exports->Name );
720 if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
721 len = min( len, sizeof(data->dllname) - 1 );
722 memcpy( data->dllname, (char *)module + exports->Name, len );
723 data->dllname[len] = 0;
725 /* fetch name pointer for all entry points and store them in the private structure */
727 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
728 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
730 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
731 data->entry_points[*ordptr].name = (const char *)module + name_rva;
734 /* patch the functions in the export table to point to the relay thunks */
736 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
737 entry_point_rva = descr->entry_point_base - (const char *)module;
738 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
740 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
741 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
742 continue; /* don't include this entry point */
744 data->entry_points[i].orig_func = (char *)module + *funcs;
745 *funcs = entry_point_rva + descr->entry_point_offsets[i];
749 #else /* __i386__ || __x86_64__ || __arm__ */
751 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
752 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
754 return proc;
757 void RELAY_SetupDLL( HMODULE module )
761 #endif /* __i386__ || __x86_64__ || __arm__ */
764 /***********************************************************************/
765 /* snoop support */
766 /***********************************************************************/
768 #ifdef __i386__
770 WINE_DECLARE_DEBUG_CHANNEL(seh);
771 WINE_DECLARE_DEBUG_CHANNEL(snoop);
773 #include "pshpack1.h"
775 typedef struct
777 /* code part */
778 BYTE lcall; /* 0xe8 call snoopentry (relative) */
779 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
780 * calculation!
782 DWORD snoopentry; /* SNOOP_Entry relative */
783 /* unreached */
784 int nrofargs;
785 FARPROC origfun;
786 const char *name;
787 } SNOOP_FUN;
789 typedef struct tagSNOOP_DLL {
790 HMODULE hmod;
791 SNOOP_FUN *funs;
792 DWORD ordbase;
793 DWORD nrofordinals;
794 struct tagSNOOP_DLL *next;
795 char name[1];
796 } SNOOP_DLL;
798 typedef struct
800 /* code part */
801 BYTE lcall; /* 0xe8 call snoopret relative*/
802 /* NOTE: If you move snoopret OR origreturn fix the relative offset
803 * calculation!
805 DWORD snoopret; /* SNOOP_Ret relative */
806 /* unreached */
807 FARPROC origreturn;
808 SNOOP_DLL *dll;
809 DWORD ordinal;
810 DWORD origESP;
811 DWORD *args; /* saved args across a stdcall */
812 } SNOOP_RETURNENTRY;
814 typedef struct tagSNOOP_RETURNENTRIES {
815 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
816 struct tagSNOOP_RETURNENTRIES *next;
817 } SNOOP_RETURNENTRIES;
819 #include "poppack.h"
821 extern void WINAPI SNOOP_Entry(void);
822 extern void WINAPI SNOOP_Return(void);
824 static SNOOP_DLL *firstdll;
825 static SNOOP_RETURNENTRIES *firstrets;
828 /***********************************************************************
829 * SNOOP_ShowDebugmsgSnoop
831 * Simple function to decide if a particular debugging message is
832 * wanted.
834 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
836 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
837 return FALSE;
838 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
839 return FALSE;
840 return TRUE;
844 /***********************************************************************
845 * SNOOP_SetupDLL
847 * Setup snoop debugging for a native dll.
849 void SNOOP_SetupDLL(HMODULE hmod)
851 SNOOP_DLL **dll = &firstdll;
852 char *p, *name;
853 void *addr;
854 SIZE_T size;
855 ULONG size32;
856 IMAGE_EXPORT_DIRECTORY *exports;
858 if (!init_done) init_debug_lists();
860 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
861 if (!exports || !exports->NumberOfFunctions) return;
862 name = (char *)hmod + exports->Name;
863 size = size32;
865 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
867 while (*dll) {
868 if ((*dll)->hmod == hmod)
870 /* another dll, loaded at the same address */
871 addr = (*dll)->funs;
872 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
873 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
874 break;
876 dll = &((*dll)->next);
878 if (*dll)
879 *dll = RtlReAllocateHeap(GetProcessHeap(),
880 HEAP_ZERO_MEMORY, *dll,
881 sizeof(SNOOP_DLL) + strlen(name));
882 else
883 *dll = RtlAllocateHeap(GetProcessHeap(),
884 HEAP_ZERO_MEMORY,
885 sizeof(SNOOP_DLL) + strlen(name));
886 (*dll)->hmod = hmod;
887 (*dll)->ordbase = exports->Base;
888 (*dll)->nrofordinals = exports->NumberOfFunctions;
889 strcpy( (*dll)->name, name );
890 p = (*dll)->name + strlen((*dll)->name) - 4;
891 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
893 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
894 addr = NULL;
895 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
896 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
897 if (!addr) {
898 RtlFreeHeap(GetProcessHeap(),0,*dll);
899 FIXME("out of memory\n");
900 return;
902 (*dll)->funs = addr;
903 memset((*dll)->funs,0,size);
907 /***********************************************************************
908 * SNOOP_GetProcAddress
910 * Return the proc address to use for a given function.
912 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
913 DWORD exp_size, FARPROC origfun, DWORD ordinal,
914 const WCHAR *user)
916 unsigned int i;
917 const char *ename;
918 const WORD *ordinals;
919 const DWORD *names;
920 SNOOP_DLL *dll = firstdll;
921 SNOOP_FUN *fun;
922 const IMAGE_SECTION_HEADER *sec;
924 if (!TRACE_ON(snoop)) return origfun;
925 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
926 return origfun; /* the calling module was explicitly excluded */
928 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
929 return origfun;
931 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
933 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
934 return origfun; /* most likely a data reference */
936 while (dll) {
937 if (hmod == dll->hmod)
938 break;
939 dll = dll->next;
941 if (!dll) /* probably internal */
942 return origfun;
944 /* try to find a name for it */
945 ename = NULL;
946 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
947 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
948 if (names) for (i = 0; i < exports->NumberOfNames; i++)
950 if (ordinals[i] == ordinal)
952 ename = (const char *)hmod + names[i];
953 break;
956 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
957 return origfun;
958 assert(ordinal < dll->nrofordinals);
959 fun = dll->funs + ordinal;
960 if (!fun->name)
962 fun->name = ename;
963 fun->lcall = 0xe8;
964 /* NOTE: origreturn struct member MUST come directly after snoopentry */
965 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
966 fun->origfun = origfun;
967 fun->nrofargs = -1;
969 return (FARPROC)&(fun->lcall);
972 static void SNOOP_PrintArg(DWORD x)
974 int i,nostring;
976 DPRINTF("%08x",x);
977 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
978 __TRY
980 LPBYTE s=(LPBYTE)x;
981 i=0;nostring=0;
982 while (i<80) {
983 if (s[i]==0) break;
984 if (s[i]<0x20) {nostring=1;break;}
985 if (s[i]>=0x80) {nostring=1;break;}
986 i++;
988 if (!nostring && i > 5)
989 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
990 else /* try unicode */
992 LPWSTR s=(LPWSTR)x;
993 i=0;nostring=0;
994 while (i<80) {
995 if (s[i]==0) break;
996 if (s[i]<0x20) {nostring=1;break;}
997 if (s[i]>0x100) {nostring=1;break;}
998 i++;
1000 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
1003 __EXCEPT_PAGE_FAULT
1006 __ENDTRY
1009 #define CALLER1REF (*(DWORD*)context->Esp)
1011 void WINAPI __regs_SNOOP_Entry( CONTEXT *context )
1013 DWORD ordinal=0,entry = context->Eip - 5;
1014 SNOOP_DLL *dll = firstdll;
1015 SNOOP_FUN *fun = NULL;
1016 SNOOP_RETURNENTRIES **rets = &firstrets;
1017 SNOOP_RETURNENTRY *ret;
1018 int i=0, max;
1020 while (dll) {
1021 if ( ((char*)entry>=(char*)dll->funs) &&
1022 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
1024 fun = (SNOOP_FUN*)entry;
1025 ordinal = fun-dll->funs;
1026 break;
1028 dll=dll->next;
1030 if (!dll) {
1031 FIXME("entrypoint 0x%08x not found\n",entry);
1032 return; /* oops */
1034 /* guess cdecl ... */
1035 if (fun->nrofargs<0) {
1036 /* Typical cdecl return frame is:
1037 * add esp, xxxxxxxx
1038 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1039 * (after that 81 C2 xx xx xx xx)
1041 LPBYTE reteip = (LPBYTE)CALLER1REF;
1043 if (reteip) {
1044 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1045 fun->nrofargs=reteip[2]/4;
1050 while (*rets) {
1051 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
1052 if (!(*rets)->entry[i].origreturn)
1053 break;
1054 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
1055 break;
1056 rets = &((*rets)->next);
1058 if (!*rets) {
1059 SIZE_T size = 4096;
1060 VOID* addr = NULL;
1062 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1063 MEM_COMMIT | MEM_RESERVE,
1064 PAGE_EXECUTE_READWRITE);
1065 if (!addr) return;
1066 *rets = addr;
1067 memset(*rets,0,4096);
1068 i = 0; /* entry 0 is free */
1070 ret = &((*rets)->entry[i]);
1071 ret->lcall = 0xe8;
1072 /* NOTE: origreturn struct member MUST come directly after snoopret */
1073 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
1074 ret->origreturn = (FARPROC)CALLER1REF;
1075 CALLER1REF = (DWORD)&ret->lcall;
1076 ret->dll = dll;
1077 ret->args = NULL;
1078 ret->ordinal = ordinal;
1079 ret->origESP = context->Esp;
1081 context->Eip = (DWORD)fun->origfun;
1083 if (TRACE_ON(timestamp))
1084 print_timestamp();
1085 if (fun->name) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
1086 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
1087 if (fun->nrofargs>0) {
1088 max = fun->nrofargs; if (max>16) max=16;
1089 for (i=0;i<max;i++)
1091 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
1092 if (i<fun->nrofargs-1) DPRINTF(",");
1094 if (max!=fun->nrofargs)
1095 DPRINTF(" ...");
1096 } else if (fun->nrofargs<0) {
1097 DPRINTF("<unknown, check return>");
1098 ret->args = RtlAllocateHeap(GetProcessHeap(),
1099 0,16*sizeof(DWORD));
1100 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
1102 DPRINTF(") ret=%08x\n",(DWORD)ret->origreturn);
1106 void WINAPI __regs_SNOOP_Return( CONTEXT *context )
1108 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
1109 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1111 /* We haven't found out the nrofargs yet. If we called a cdecl
1112 * function it is too late anyway and we can just set '0' (which
1113 * will be the difference between orig and current ESP
1114 * If stdcall -> everything ok.
1116 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1117 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
1118 context->Eip = (DWORD)ret->origreturn;
1119 if (TRACE_ON(timestamp))
1120 print_timestamp();
1121 if (ret->args) {
1122 int i,max;
1124 if (fun->name)
1125 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1126 else
1127 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1128 ret->dll->name,ret->dll->ordbase+ret->ordinal);
1130 max = fun->nrofargs;
1131 if (max>16) max=16;
1133 for (i=0;i<max;i++)
1135 SNOOP_PrintArg(ret->args[i]);
1136 if (i<max-1) DPRINTF(",");
1138 DPRINTF(") retval=%08x ret=%08x\n",
1139 context->Eax,(DWORD)ret->origreturn );
1140 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1141 ret->args = NULL;
1143 else
1145 if (fun->name)
1146 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1147 GetCurrentThreadId(),
1148 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
1149 else
1150 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1151 GetCurrentThreadId(),
1152 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1153 context->Eax, (DWORD)ret->origreturn);
1155 ret->origreturn = NULL; /* mark as empty */
1158 /* assembly wrappers that save the context */
1159 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry, 0 )
1160 DEFINE_REGS_ENTRYPOINT( SNOOP_Return, 0 )
1162 #else /* __i386__ */
1164 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1165 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1167 return origfun;
1170 void SNOOP_SetupDLL( HMODULE hmod )
1172 FIXME("snooping works only on i386 for now.\n");
1175 #endif /* __i386__ */