wined3d: Track if a context's private hdc has had its pixel format set, so we don...
[wine/multimedia.git] / dlls / ntdll / relay.c
blob57ad2e1b58855ac65e8f44ecdbcf6604bb5570d3
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 RTL_RUN_ONCE init_once = RTL_RUN_ONCE_INIT;
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 DWORD WINAPI init_debug_lists( RTL_RUN_ONCE *once, void *param, void **context )
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 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
193 attr.Length = sizeof(attr);
194 attr.RootDirectory = root;
195 attr.ObjectName = &name;
196 attr.Attributes = 0;
197 attr.SecurityDescriptor = NULL;
198 attr.SecurityQualityOfService = NULL;
199 RtlInitUnicodeString( &name, configW );
201 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
202 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
203 NtClose( root );
204 if (!hkey) return TRUE;
206 debug_relay_includelist = load_list( hkey, RelayIncludeW );
207 debug_relay_excludelist = load_list( hkey, RelayExcludeW );
208 debug_snoop_includelist = load_list( hkey, SnoopIncludeW );
209 debug_snoop_excludelist = load_list( hkey, SnoopExcludeW );
210 debug_from_relay_includelist = load_list( hkey, RelayFromIncludeW );
211 debug_from_relay_excludelist = load_list( hkey, RelayFromExcludeW );
212 debug_from_snoop_includelist = load_list( hkey, SnoopFromIncludeW );
213 debug_from_snoop_excludelist = load_list( hkey, SnoopFromExcludeW );
215 NtClose( hkey );
216 return TRUE;
220 /***********************************************************************
221 * check_list
223 * Check if a given module and function is in the list.
225 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR *const *list )
227 char ord_str[10];
229 sprintf( ord_str, "%d", ordinal );
230 for(; *list; list++)
232 const WCHAR *p = strrchrW( *list, '.' );
233 if (p && p > *list) /* check module and function */
235 int len = p - *list;
236 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
237 if (p[1] == '*' && !p[2]) return TRUE;
238 if (!strcmpAW( ord_str, p + 1 )) return TRUE;
239 if (func && !strcmpAW( func, p + 1 )) return TRUE;
241 else /* function only */
243 if (func && !strcmpAW( func, *list )) return TRUE;
246 return FALSE;
250 /***********************************************************************
251 * check_relay_include
253 * Check if a given function must be included in the relay output.
255 static BOOL check_relay_include( const char *module, int ordinal, const char *func )
257 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
258 return FALSE;
259 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
260 return FALSE;
261 return TRUE;
264 /***********************************************************************
265 * check_from_module
267 * Check if calls from a given module must be included in the relay/snoop output,
268 * given the exclusion and inclusion lists.
270 static BOOL check_from_module( const WCHAR **includelist, const WCHAR **excludelist, const WCHAR *module )
272 static const WCHAR dllW[] = {'.','d','l','l',0 };
273 const WCHAR **listitem;
274 BOOL show;
276 if (!module) return TRUE;
277 if (!includelist && !excludelist) return TRUE;
278 if (excludelist)
280 show = TRUE;
281 listitem = excludelist;
283 else
285 show = FALSE;
286 listitem = includelist;
288 for(; *listitem; listitem++)
290 int len;
292 if (!strcmpiW( *listitem, module )) return !show;
293 len = strlenW( *listitem );
294 if (!strncmpiW( *listitem, module, len ) && !strcmpiW( module + len, dllW ))
295 return !show;
297 return show;
300 /***********************************************************************
301 * RELAY_PrintArgs
303 static inline void RELAY_PrintArgs( const INT_PTR *args, int nb_args, unsigned int typemask )
305 while (nb_args--)
307 if ((typemask & 3) && !IS_INTARG(*args))
309 if (typemask & 2)
310 DPRINTF( "%08lx %s", *args, debugstr_w((LPCWSTR)*args) );
311 else
312 DPRINTF( "%08lx %s", *args, debugstr_a((LPCSTR)*args) );
314 else DPRINTF( "%08lx", *args );
315 if (nb_args) DPRINTF( "," );
316 args++;
317 typemask >>= 2;
321 static void print_timestamp(void)
323 ULONG ticks = NtGetTickCount();
324 DPRINTF( "%3u.%03u:", ticks / 1000, ticks % 1000 );
327 /***********************************************************************
328 * relay_trace_entry
330 * stack points to the return address, i.e. the first argument is stack[1].
332 void * WINAPI relay_trace_entry( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
334 WORD ordinal = LOWORD(idx);
335 BYTE nb_args = LOBYTE(HIWORD(idx));
336 struct relay_private_data *data = descr->private;
337 struct relay_entry_point *entry_point = data->entry_points + ordinal;
339 if (TRACE_ON(relay))
341 if (TRACE_ON(timestamp)) print_timestamp();
343 if (entry_point->name)
344 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
345 else
346 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
347 RELAY_PrintArgs( stack + 1, nb_args, descr->arg_types[ordinal] );
348 DPRINTF( ") ret=%08lx\n", stack[0] );
350 return entry_point->orig_func;
353 /***********************************************************************
354 * relay_trace_exit
356 void WINAPI relay_trace_exit( struct relay_descr *descr, unsigned int idx,
357 const INT_PTR *stack, LONGLONG retval )
359 WORD ordinal = LOWORD(idx);
360 BYTE flags = HIBYTE(HIWORD(idx));
361 struct relay_private_data *data = descr->private;
362 struct relay_entry_point *entry_point = data->entry_points + ordinal;
364 if (!TRACE_ON(relay)) return;
366 if (TRACE_ON(timestamp)) print_timestamp();
368 if (entry_point->name)
369 DPRINTF( "%04x:Ret %s.%s()", GetCurrentThreadId(), data->dllname, entry_point->name );
370 else
371 DPRINTF( "%04x:Ret %s.%u()", GetCurrentThreadId(), data->dllname, data->base + ordinal );
373 if (flags & 1) /* 64-bit return value */
374 DPRINTF( " retval=%08x%08x ret=%08lx\n",
375 (UINT)(retval >> 32), (UINT)retval, stack[0] );
376 else
377 DPRINTF( " retval=%08lx ret=%08lx\n", (UINT_PTR)retval, stack[0] );
380 #ifdef __i386__
382 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
383 __ASM_GLOBAL_FUNC( call_entry_point,
384 "pushl %ebp\n\t"
385 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
386 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
387 "movl %esp,%ebp\n\t"
388 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
389 "pushl %esi\n\t"
390 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
391 "pushl %edi\n\t"
392 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
393 "movl 12(%ebp),%edx\n\t"
394 "shll $2,%edx\n\t"
395 "jz 1f\n\t"
396 "subl %edx,%esp\n\t"
397 "andl $~15,%esp\n\t"
398 "movl 12(%ebp),%ecx\n\t"
399 "movl 16(%ebp),%esi\n\t"
400 "movl %esp,%edi\n\t"
401 "cld\n\t"
402 "rep; movsl\n"
403 "testl $2,20(%ebp)\n\t" /* (flags & 2) -> thiscall */
404 "jz 1f\n\t"
405 "popl %ecx\n\t"
406 "1:\tcall *8(%ebp)\n\t"
407 "leal -8(%ebp),%esp\n\t"
408 "popl %edi\n\t"
409 __ASM_CFI(".cfi_same_value %edi\n\t")
410 "popl %esi\n\t"
411 __ASM_CFI(".cfi_same_value %esi\n\t")
412 "popl %ebp\n\t"
413 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
414 __ASM_CFI(".cfi_same_value %ebp\n\t")
415 "ret" )
417 extern LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
418 __ASM_GLOBAL_FUNC( relay_call,
419 "pushl %ebp\n\t"
420 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
421 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
422 "movl %esp,%ebp\n\t"
423 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
424 "pushl %esi\n\t"
425 __ASM_CFI(".cfi_rel_offset %esi,-4\n\t")
426 "pushl %edi\n\t"
427 __ASM_CFI(".cfi_rel_offset %edi,-8\n\t")
428 "pushl %ecx\n\t"
429 __ASM_CFI(".cfi_rel_offset %ecx,-12\n\t")
430 /* trace the parameters */
431 "pushl 16(%ebp)\n\t"
432 "pushl 12(%ebp)\n\t"
433 "pushl 8(%ebp)\n\t"
434 "call " __ASM_NAME("relay_trace_entry") "\n\t"
435 /* copy the arguments*/
436 "movzbl 14(%ebp),%ecx\n\t" /* number of args */
437 "jecxz 1f\n\t"
438 "leal 0(,%ecx,4),%edx\n\t"
439 "subl %edx,%esp\n\t"
440 "andl $~15,%esp\n\t"
441 "movl 16(%ebp),%esi\n\t"
442 "addl $4,%esi\n\t"
443 "movl %esp,%edi\n\t"
444 "cld\n\t"
445 "rep; movsl\n\t"
446 "testb $2,15(%ebp)\n\t" /* (flags & 2) -> thiscall */
447 "jz 1f\n\t"
448 "popl %ecx\n"
449 /* call the entry point */
450 "1:\tcall *%eax\n\t"
451 "movl %eax,%esi\n\t"
452 "movl %edx,%edi\n\t"
453 /* trace the return value */
454 "leal -20(%ebp),%esp\n\t"
455 "pushl %edx\n\t"
456 "pushl %eax\n\t"
457 "pushl 16(%ebp)\n\t"
458 "pushl 12(%ebp)\n\t"
459 "pushl 8(%ebp)\n\t"
460 "call " __ASM_NAME("relay_trace_exit") "\n\t"
461 /* restore return value and return */
462 "leal -12(%ebp),%esp\n\t"
463 "movl %esi,%eax\n\t"
464 "movl %edi,%edx\n\t"
465 "popl %ecx\n\t"
466 __ASM_CFI(".cfi_same_value %ecx\n\t")
467 "popl %edi\n\t"
468 __ASM_CFI(".cfi_same_value %edi\n\t")
469 "popl %esi\n\t"
470 __ASM_CFI(".cfi_same_value %esi\n\t")
471 "popl %ebp\n\t"
472 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
473 __ASM_CFI(".cfi_same_value %ebp\n\t")
474 "ret $12" )
476 void WINAPI __regs_relay_call_regs( struct relay_descr *descr, unsigned int idx,
477 unsigned int orig_eax, unsigned int ret_addr,
478 CONTEXT *context )
480 WORD ordinal = LOWORD(idx);
481 BYTE nb_args = LOBYTE(HIWORD(idx));
482 struct relay_private_data *data = descr->private;
483 struct relay_entry_point *entry_point = data->entry_points + ordinal;
484 BYTE *orig_func = entry_point->orig_func;
485 INT_PTR *args = (INT_PTR *)context->Esp;
486 INT_PTR args_copy[32];
488 /* restore the context to what it was before the relay thunk */
489 context->Eax = orig_eax;
490 context->Eip = ret_addr;
491 context->Esp += nb_args * sizeof(int);
493 if (TRACE_ON(relay))
495 if (entry_point->name)
496 DPRINTF( "%04x:Call %s.%s(", GetCurrentThreadId(), data->dllname, entry_point->name );
497 else
498 DPRINTF( "%04x:Call %s.%u(", GetCurrentThreadId(), data->dllname, data->base + ordinal );
499 RELAY_PrintArgs( args, nb_args, descr->arg_types[ordinal] );
500 DPRINTF( ") ret=%08x\n", ret_addr );
502 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
503 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
504 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
505 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
506 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
508 assert( orig_func[0] == 0x68 /* pushl func */ );
509 assert( orig_func[5] == 0x6a /* pushl args */ );
510 assert( orig_func[7] == 0xe8 /* call */ );
513 /* now call the real function */
515 memcpy( args_copy, args, nb_args * sizeof(args[0]) );
516 args_copy[nb_args++] = (INT_PTR)context; /* append context argument */
518 call_entry_point( orig_func + 12 + *(int *)(orig_func + 1), nb_args, args_copy, 0 );
520 if (TRACE_ON(relay))
522 if (entry_point->name)
523 DPRINTF( "%04x:Ret %s.%s() retval=%08x ret=%08x\n",
524 GetCurrentThreadId(), data->dllname, entry_point->name,
525 context->Eax, context->Eip );
526 else
527 DPRINTF( "%04x:Ret %s.%u() retval=%08x ret=%08x\n",
528 GetCurrentThreadId(), data->dllname, data->base + ordinal,
529 context->Eax, context->Eip );
530 DPRINTF( "%04x: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x "
531 "ebp=%08x esp=%08x ds=%04x es=%04x fs=%04x gs=%04x flags=%08x\n",
532 GetCurrentThreadId(), context->Eax, context->Ebx, context->Ecx,
533 context->Edx, context->Esi, context->Edi, context->Ebp, context->Esp,
534 context->SegDs, context->SegEs, context->SegFs, context->SegGs, context->EFlags );
537 extern void WINAPI relay_call_regs(void);
538 DEFINE_REGS_ENTRYPOINT( relay_call_regs, 4 )
540 #elif defined(__arm__)
542 extern LONGLONG CDECL call_entry_point( void *func, int nb_args, const INT_PTR *args, int flags );
543 __ASM_GLOBAL_FUNC( call_entry_point,
544 ".arm\n\t"
545 "push {r4, r5, LR}\n\t"
546 "mov r4, r0\n\t"
547 "mov r5, SP\n\t"
548 "lsl r3, r1, #2\n\t"
549 "cmp r3, #0\n\t"
550 "beq 5f\n\t"
551 "sub SP, SP, r3\n\t"
552 "tst r1, #1\n\t"
553 "subeq SP, SP, #4\n\t"
554 "1:\tsub r3, r3, #4\n\t"
555 "ldr r0, [r2, r3]\n\t"
556 "str r0, [SP, r3]\n\t"
557 "cmp r3, #0\n\t"
558 "bgt 1b\n\t"
559 "cmp r1, #1\n\t"
560 "bgt 2f\n\t"
561 "pop {r0}\n\t"
562 "b 5f\n\t"
563 "2:\tcmp r1, #2\n\t"
564 "bgt 3f\n\t"
565 "pop {r0-r1}\n\t"
566 "b 5f\n\t"
567 "3:\tcmp r1, #3\n\t"
568 "bgt 4f\n\t"
569 "pop {r0-r2}\n\t"
570 "b 5f\n\t"
571 "4:\tpop {r0-r3}\n\t"
572 "5:\tblx r4\n\t"
573 "mov SP, r5\n\t"
574 "pop {r4, r5, PC}" )
576 static LONGLONG WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack )
578 BYTE nb_args = LOBYTE(HIWORD(idx));
579 BYTE flags = HIBYTE(HIWORD(idx));
580 void *func = relay_trace_entry( descr, idx, stack );
581 LONGLONG ret = call_entry_point( func, nb_args, stack + 1, flags );
582 relay_trace_exit( descr, idx, stack, ret );
583 return ret;
586 static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
588 assert(0); /* should never be called */
591 #elif defined(__x86_64__)
593 extern void * WINAPI relay_call( struct relay_descr *descr, unsigned int idx, const INT_PTR *stack );
594 __ASM_GLOBAL_FUNC( relay_call,
595 "pushq %rbp\n\t"
596 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
597 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
598 "movq %rsp,%rbp\n\t"
599 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
600 "subq $0x30,%rsp\n\t"
601 "movq %rsi,0x20(%rsp)\n\t"
602 __ASM_CFI(".cfi_rel_offset %rsi,-16\n\t")
603 "movq %rdi,0x28(%rsp)\n\t"
604 __ASM_CFI(".cfi_rel_offset %rdi,-8\n\t")
605 /* trace the parameters */
606 "movq %rcx,0x10(%rbp)\n\t"
607 "movq %rdx,0x18(%rbp)\n\t"
608 "movq %r8,0x20(%rbp)\n\t"
609 "call " __ASM_NAME("relay_trace_entry") "\n\t"
610 /* copy the arguments */
611 "movzbq 0x1a(%rbp),%rdx\n\t" /* number of args */
612 "movq $4,%rcx\n\t"
613 "cmp %rcx,%rdx\n\t"
614 "cmovgq %rdx,%rcx\n\t"
615 "leaq -16(,%rcx,8),%rdx\n\t"
616 "andq $~15,%rdx\n\t"
617 "subq %rdx,%rsp\n\t"
618 "movq 0x20(%rbp),%r8\n\t" /* original stack */
619 "leaq 8(%r8),%rsi\n\t"
620 "movq %rsp,%rdi\n\t"
621 "rep; movsq\n\t"
622 /* call the entry point */
623 "movq 0(%rsp),%rcx\n\t"
624 "movq 8(%rsp),%rdx\n\t"
625 "movq 16(%rsp),%r8\n\t"
626 "movq 24(%rsp),%r9\n\t"
627 "movq %rcx,%xmm0\n\t"
628 "movq %rdx,%xmm1\n\t"
629 "movq %r8,%xmm2\n\t"
630 "movq %r9,%xmm3\n\t"
631 "callq *%rax\n\t"
632 /* trace the return value */
633 "leaq -0x30(%rbp),%rsp\n\t"
634 "movq 0x10(%rbp),%rcx\n\t"
635 "movq 0x18(%rbp),%rdx\n\t"
636 "movq 0x20(%rbp),%r8\n\t"
637 "movq %rax,%rsi\n\t"
638 "movaps %xmm0,0x10(%rbp)\n\t"
639 "movq %rax,%r9\n\t"
640 "call " __ASM_NAME("relay_trace_exit") "\n\t"
641 /* restore return value and return */
642 "movq %rsi,%rax\n\t"
643 "movaps 0x10(%rbp),%xmm0\n\t"
644 "movq 0x20(%rsp),%rsi\n\t"
645 __ASM_CFI(".cfi_same_value %rsi\n\t")
646 "movq 0x28(%rsp),%rdi\n\t"
647 __ASM_CFI(".cfi_same_value %rdi\n\t")
648 "movq %rbp,%rsp\n\t"
649 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
650 "popq %rbp\n\t"
651 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
652 __ASM_CFI(".cfi_same_value %rbp\n\t")
653 "ret")
655 static void WINAPI relay_call_regs( struct relay_descr *descr, INT_PTR idx, INT_PTR *stack )
657 assert(0); /* should never be called */
660 #else
661 #error Not supported on this CPU
662 #endif
665 /***********************************************************************
666 * RELAY_GetProcAddress
668 * Return the proc address to use for a given function.
670 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
671 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
673 struct relay_private_data *data;
674 const struct relay_descr *descr = (const struct relay_descr *)((const char *)exports + exp_size);
676 if (descr->magic != RELAY_DESCR_MAGIC || !(data = descr->private)) return proc; /* no relay data */
677 if (!data->entry_points[ordinal].orig_func) return proc; /* not a relayed function */
678 if (check_from_module( debug_from_relay_includelist, debug_from_relay_excludelist, user ))
679 return proc; /* we want to relay it */
680 return data->entry_points[ordinal].orig_func;
684 /***********************************************************************
685 * RELAY_SetupDLL
687 * Setup relay debugging for a built-in dll.
689 void RELAY_SetupDLL( HMODULE module )
691 IMAGE_EXPORT_DIRECTORY *exports;
692 DWORD *funcs;
693 unsigned int i, len;
694 DWORD size, entry_point_rva;
695 struct relay_descr *descr;
696 struct relay_private_data *data;
697 const WORD *ordptr;
699 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
701 exports = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size );
702 if (!exports) return;
704 descr = (struct relay_descr *)((char *)exports + size);
705 if (descr->magic != RELAY_DESCR_MAGIC) return;
707 if (!(data = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) +
708 (exports->NumberOfFunctions-1) * sizeof(data->entry_points) )))
709 return;
711 descr->relay_call = relay_call;
712 descr->relay_call_regs = relay_call_regs;
713 descr->private = data;
715 data->module = module;
716 data->base = exports->Base;
717 len = strlen( (char *)module + exports->Name );
718 if (len > 4 && !strcasecmp( (char *)module + exports->Name + len - 4, ".dll" )) len -= 4;
719 len = min( len, sizeof(data->dllname) - 1 );
720 memcpy( data->dllname, (char *)module + exports->Name, len );
721 data->dllname[len] = 0;
723 /* fetch name pointer for all entry points and store them in the private structure */
725 ordptr = (const WORD *)((char *)module + exports->AddressOfNameOrdinals);
726 for (i = 0; i < exports->NumberOfNames; i++, ordptr++)
728 DWORD name_rva = ((DWORD*)((char *)module + exports->AddressOfNames))[i];
729 data->entry_points[*ordptr].name = (const char *)module + name_rva;
732 /* patch the functions in the export table to point to the relay thunks */
734 funcs = (DWORD *)((char *)module + exports->AddressOfFunctions);
735 entry_point_rva = descr->entry_point_base - (const char *)module;
736 for (i = 0; i < exports->NumberOfFunctions; i++, funcs++)
738 if (!descr->entry_point_offsets[i]) continue; /* not a normal function */
739 if (!check_relay_include( data->dllname, i + exports->Base, data->entry_points[i].name ))
740 continue; /* don't include this entry point */
742 data->entry_points[i].orig_func = (char *)module + *funcs;
743 *funcs = entry_point_rva + descr->entry_point_offsets[i];
747 #else /* __i386__ || __x86_64__ || __arm__ */
749 FARPROC RELAY_GetProcAddress( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
750 DWORD exp_size, FARPROC proc, DWORD ordinal, const WCHAR *user )
752 return proc;
755 void RELAY_SetupDLL( HMODULE module )
759 #endif /* __i386__ || __x86_64__ || __arm__ */
762 /***********************************************************************/
763 /* snoop support */
764 /***********************************************************************/
766 #ifdef __i386__
768 WINE_DECLARE_DEBUG_CHANNEL(seh);
769 WINE_DECLARE_DEBUG_CHANNEL(snoop);
771 #include "pshpack1.h"
773 typedef struct
775 /* code part */
776 BYTE lcall; /* 0xe8 call snoopentry (relative) */
777 /* NOTE: If you move snoopentry OR nrofargs fix the relative offset
778 * calculation!
780 DWORD snoopentry; /* SNOOP_Entry relative */
781 /* unreached */
782 int nrofargs;
783 FARPROC origfun;
784 const char *name;
785 } SNOOP_FUN;
787 typedef struct tagSNOOP_DLL {
788 HMODULE hmod;
789 SNOOP_FUN *funs;
790 DWORD ordbase;
791 DWORD nrofordinals;
792 struct tagSNOOP_DLL *next;
793 char name[1];
794 } SNOOP_DLL;
796 typedef struct
798 /* code part */
799 BYTE lcall; /* 0xe8 call snoopret relative*/
800 /* NOTE: If you move snoopret OR origreturn fix the relative offset
801 * calculation!
803 DWORD snoopret; /* SNOOP_Ret relative */
804 /* unreached */
805 FARPROC origreturn;
806 SNOOP_DLL *dll;
807 DWORD ordinal;
808 DWORD origESP;
809 DWORD *args; /* saved args across a stdcall */
810 } SNOOP_RETURNENTRY;
812 typedef struct tagSNOOP_RETURNENTRIES {
813 SNOOP_RETURNENTRY entry[4092/sizeof(SNOOP_RETURNENTRY)];
814 struct tagSNOOP_RETURNENTRIES *next;
815 } SNOOP_RETURNENTRIES;
817 #include "poppack.h"
819 extern void WINAPI SNOOP_Entry(void);
820 extern void WINAPI SNOOP_Return(void);
822 static SNOOP_DLL *firstdll;
823 static SNOOP_RETURNENTRIES *firstrets;
826 /***********************************************************************
827 * SNOOP_ShowDebugmsgSnoop
829 * Simple function to decide if a particular debugging message is
830 * wanted.
832 static BOOL SNOOP_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
834 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
835 return FALSE;
836 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
837 return FALSE;
838 return TRUE;
842 /***********************************************************************
843 * SNOOP_SetupDLL
845 * Setup snoop debugging for a native dll.
847 void SNOOP_SetupDLL(HMODULE hmod)
849 SNOOP_DLL **dll = &firstdll;
850 char *p, *name;
851 void *addr;
852 SIZE_T size;
853 ULONG size32;
854 IMAGE_EXPORT_DIRECTORY *exports;
856 RtlRunOnceExecuteOnce( &init_once, init_debug_lists, NULL, NULL );
858 exports = RtlImageDirectoryEntryToData( hmod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size32 );
859 if (!exports || !exports->NumberOfFunctions) return;
860 name = (char *)hmod + exports->Name;
861 size = size32;
863 TRACE_(snoop)("hmod=%p, name=%s\n", hmod, name);
865 while (*dll) {
866 if ((*dll)->hmod == hmod)
868 /* another dll, loaded at the same address */
869 addr = (*dll)->funs;
870 size = (*dll)->nrofordinals * sizeof(SNOOP_FUN);
871 NtFreeVirtualMemory(NtCurrentProcess(), &addr, &size, MEM_RELEASE);
872 break;
874 dll = &((*dll)->next);
876 if (*dll)
877 *dll = RtlReAllocateHeap(GetProcessHeap(),
878 HEAP_ZERO_MEMORY, *dll,
879 sizeof(SNOOP_DLL) + strlen(name));
880 else
881 *dll = RtlAllocateHeap(GetProcessHeap(),
882 HEAP_ZERO_MEMORY,
883 sizeof(SNOOP_DLL) + strlen(name));
884 (*dll)->hmod = hmod;
885 (*dll)->ordbase = exports->Base;
886 (*dll)->nrofordinals = exports->NumberOfFunctions;
887 strcpy( (*dll)->name, name );
888 p = (*dll)->name + strlen((*dll)->name) - 4;
889 if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
891 size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
892 addr = NULL;
893 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
894 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
895 if (!addr) {
896 RtlFreeHeap(GetProcessHeap(),0,*dll);
897 FIXME("out of memory\n");
898 return;
900 (*dll)->funs = addr;
901 memset((*dll)->funs,0,size);
905 /***********************************************************************
906 * SNOOP_GetProcAddress
908 * Return the proc address to use for a given function.
910 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports,
911 DWORD exp_size, FARPROC origfun, DWORD ordinal,
912 const WCHAR *user)
914 unsigned int i;
915 const char *ename;
916 const WORD *ordinals;
917 const DWORD *names;
918 SNOOP_DLL *dll = firstdll;
919 SNOOP_FUN *fun;
920 const IMAGE_SECTION_HEADER *sec;
922 if (!TRACE_ON(snoop)) return origfun;
923 if (!check_from_module( debug_from_snoop_includelist, debug_from_snoop_excludelist, user ))
924 return origfun; /* the calling module was explicitly excluded */
926 if (!*(LPBYTE)origfun) /* 0x00 is an impossible opcode, possible dataref. */
927 return origfun;
929 sec = RtlImageRvaToSection( RtlImageNtHeader(hmod), hmod, (char *)origfun - (char *)hmod );
931 if (!sec || !(sec->Characteristics & IMAGE_SCN_CNT_CODE))
932 return origfun; /* most likely a data reference */
934 while (dll) {
935 if (hmod == dll->hmod)
936 break;
937 dll = dll->next;
939 if (!dll) /* probably internal */
940 return origfun;
942 /* try to find a name for it */
943 ename = NULL;
944 names = (const DWORD *)((const char *)hmod + exports->AddressOfNames);
945 ordinals = (const WORD *)((const char *)hmod + exports->AddressOfNameOrdinals);
946 if (names) for (i = 0; i < exports->NumberOfNames; i++)
948 if (ordinals[i] == ordinal)
950 ename = (const char *)hmod + names[i];
951 break;
954 if (!SNOOP_ShowDebugmsgSnoop(dll->name,ordinal,ename))
955 return origfun;
956 assert(ordinal < dll->nrofordinals);
957 fun = dll->funs + ordinal;
958 if (!fun->name)
960 fun->name = ename;
961 fun->lcall = 0xe8;
962 /* NOTE: origreturn struct member MUST come directly after snoopentry */
963 fun->snoopentry = (char*)SNOOP_Entry-((char*)(&fun->nrofargs));
964 fun->origfun = origfun;
965 fun->nrofargs = -1;
967 return (FARPROC)&(fun->lcall);
970 static void SNOOP_PrintArg(DWORD x)
972 int i,nostring;
974 DPRINTF("%08x",x);
975 if (IS_INTARG(x) || TRACE_ON(seh)) return; /* trivial reject to avoid faults */
976 __TRY
978 LPBYTE s=(LPBYTE)x;
979 i=0;nostring=0;
980 while (i<80) {
981 if (s[i]==0) break;
982 if (s[i]<0x20) {nostring=1;break;}
983 if (s[i]>=0x80) {nostring=1;break;}
984 i++;
986 if (!nostring && i > 5)
987 DPRINTF(" %s",debugstr_an((LPSTR)x,i));
988 else /* try unicode */
990 LPWSTR s=(LPWSTR)x;
991 i=0;nostring=0;
992 while (i<80) {
993 if (s[i]==0) break;
994 if (s[i]<0x20) {nostring=1;break;}
995 if (s[i]>0x100) {nostring=1;break;}
996 i++;
998 if (!nostring && i > 5) DPRINTF(" %s",debugstr_wn((LPWSTR)x,i));
1001 __EXCEPT_PAGE_FAULT
1004 __ENDTRY
1007 #define CALLER1REF (*(DWORD*)context->Esp)
1009 void WINAPI __regs_SNOOP_Entry( CONTEXT *context )
1011 DWORD ordinal=0,entry = context->Eip - 5;
1012 SNOOP_DLL *dll = firstdll;
1013 SNOOP_FUN *fun = NULL;
1014 SNOOP_RETURNENTRIES **rets = &firstrets;
1015 SNOOP_RETURNENTRY *ret;
1016 int i=0, max;
1018 while (dll) {
1019 if ( ((char*)entry>=(char*)dll->funs) &&
1020 ((char*)entry<=(char*)(dll->funs+dll->nrofordinals))
1022 fun = (SNOOP_FUN*)entry;
1023 ordinal = fun-dll->funs;
1024 break;
1026 dll=dll->next;
1028 if (!dll) {
1029 FIXME("entrypoint 0x%08x not found\n",entry);
1030 return; /* oops */
1032 /* guess cdecl ... */
1033 if (fun->nrofargs<0) {
1034 /* Typical cdecl return frame is:
1035 * add esp, xxxxxxxx
1036 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
1037 * (after that 81 C2 xx xx xx xx)
1039 LPBYTE reteip = (LPBYTE)CALLER1REF;
1041 if (reteip) {
1042 if ((reteip[0]==0x83)&&(reteip[1]==0xc4))
1043 fun->nrofargs=reteip[2]/4;
1048 while (*rets) {
1049 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
1050 if (!(*rets)->entry[i].origreturn)
1051 break;
1052 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
1053 break;
1054 rets = &((*rets)->next);
1056 if (!*rets) {
1057 SIZE_T size = 4096;
1058 VOID* addr = NULL;
1060 NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size,
1061 MEM_COMMIT | MEM_RESERVE,
1062 PAGE_EXECUTE_READWRITE);
1063 if (!addr) return;
1064 *rets = addr;
1065 memset(*rets,0,4096);
1066 i = 0; /* entry 0 is free */
1068 ret = &((*rets)->entry[i]);
1069 ret->lcall = 0xe8;
1070 /* NOTE: origreturn struct member MUST come directly after snoopret */
1071 ret->snoopret = ((char*)SNOOP_Return)-(char*)(&ret->origreturn);
1072 ret->origreturn = (FARPROC)CALLER1REF;
1073 CALLER1REF = (DWORD)&ret->lcall;
1074 ret->dll = dll;
1075 ret->args = NULL;
1076 ret->ordinal = ordinal;
1077 ret->origESP = context->Esp;
1079 context->Eip = (DWORD)fun->origfun;
1081 if (TRACE_ON(timestamp))
1082 print_timestamp();
1083 if (fun->name) DPRINTF("%04x:CALL %s.%s(",GetCurrentThreadId(),dll->name,fun->name);
1084 else DPRINTF("%04x:CALL %s.%d(",GetCurrentThreadId(),dll->name,dll->ordbase+ordinal);
1085 if (fun->nrofargs>0) {
1086 max = fun->nrofargs; if (max>16) max=16;
1087 for (i=0;i<max;i++)
1089 SNOOP_PrintArg(*(DWORD*)(context->Esp + 4 + sizeof(DWORD)*i));
1090 if (i<fun->nrofargs-1) DPRINTF(",");
1092 if (max!=fun->nrofargs)
1093 DPRINTF(" ...");
1094 } else if (fun->nrofargs<0) {
1095 DPRINTF("<unknown, check return>");
1096 ret->args = RtlAllocateHeap(GetProcessHeap(),
1097 0,16*sizeof(DWORD));
1098 memcpy(ret->args,(LPBYTE)(context->Esp + 4),sizeof(DWORD)*16);
1100 DPRINTF(") ret=%08x\n",(DWORD)ret->origreturn);
1104 void WINAPI __regs_SNOOP_Return( CONTEXT *context )
1106 SNOOP_RETURNENTRY *ret = (SNOOP_RETURNENTRY*)(context->Eip - 5);
1107 SNOOP_FUN *fun = &ret->dll->funs[ret->ordinal];
1109 /* We haven't found out the nrofargs yet. If we called a cdecl
1110 * function it is too late anyway and we can just set '0' (which
1111 * will be the difference between orig and current ESP
1112 * If stdcall -> everything ok.
1114 if (ret->dll->funs[ret->ordinal].nrofargs<0)
1115 ret->dll->funs[ret->ordinal].nrofargs=(context->Esp - ret->origESP-4)/4;
1116 context->Eip = (DWORD)ret->origreturn;
1117 if (TRACE_ON(timestamp))
1118 print_timestamp();
1119 if (ret->args) {
1120 int i,max;
1122 if (fun->name)
1123 DPRINTF("%04x:RET %s.%s(", GetCurrentThreadId(), ret->dll->name, fun->name);
1124 else
1125 DPRINTF("%04x:RET %s.%d(", GetCurrentThreadId(),
1126 ret->dll->name,ret->dll->ordbase+ret->ordinal);
1128 max = fun->nrofargs;
1129 if (max>16) max=16;
1131 for (i=0;i<max;i++)
1133 SNOOP_PrintArg(ret->args[i]);
1134 if (i<max-1) DPRINTF(",");
1136 DPRINTF(") retval=%08x ret=%08x\n",
1137 context->Eax,(DWORD)ret->origreturn );
1138 RtlFreeHeap(GetProcessHeap(),0,ret->args);
1139 ret->args = NULL;
1141 else
1143 if (fun->name)
1144 DPRINTF("%04x:RET %s.%s() retval=%08x ret=%08x\n",
1145 GetCurrentThreadId(),
1146 ret->dll->name, fun->name, context->Eax, (DWORD)ret->origreturn);
1147 else
1148 DPRINTF("%04x:RET %s.%d() retval=%08x ret=%08x\n",
1149 GetCurrentThreadId(),
1150 ret->dll->name,ret->dll->ordbase+ret->ordinal,
1151 context->Eax, (DWORD)ret->origreturn);
1153 ret->origreturn = NULL; /* mark as empty */
1156 /* assembly wrappers that save the context */
1157 DEFINE_REGS_ENTRYPOINT( SNOOP_Entry, 0 )
1158 DEFINE_REGS_ENTRYPOINT( SNOOP_Return, 0 )
1160 #else /* __i386__ */
1162 FARPROC SNOOP_GetProcAddress( HMODULE hmod, const IMAGE_EXPORT_DIRECTORY *exports, DWORD exp_size,
1163 FARPROC origfun, DWORD ordinal, const WCHAR *user )
1165 return origfun;
1168 void SNOOP_SetupDLL( HMODULE hmod )
1170 FIXME("snooping works only on i386 for now.\n");
1173 #endif /* __i386__ */