2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/port.h"
31 #include "wine/winbase16.h"
33 #include "kernel_private.h"
34 #include "kernel16_private.h"
35 #include "wine/unicode.h"
36 #include "wine/library.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(relay);
43 static const WCHAR **debug_relay_excludelist;
44 static const WCHAR **debug_relay_includelist;
45 static const WCHAR **debug_snoop_excludelist;
46 static const WCHAR **debug_snoop_includelist;
48 /* compare an ASCII and a Unicode string without depending on the current codepage */
49 inline static int strcmpiAW( const char *strA, const WCHAR *strW )
51 while (*strA && (toupperW((unsigned char)*strA) == toupperW(*strW))) { strA++; strW++; }
52 return toupperW((unsigned char)*strA) - toupperW(*strW);
55 /* compare an ASCII and a Unicode string without depending on the current codepage */
56 inline static int strncmpiAW( const char *strA, const WCHAR *strW, int n )
59 for ( ; n > 0; n--, strA++, strW++)
60 if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
64 /***********************************************************************
67 * Build a function list from a ';'-separated string.
69 static const WCHAR **build_list( const WCHAR *buffer )
72 const WCHAR *p = buffer;
75 while ((p = strchrW( p, ';' )))
80 /* allocate count+1 pointers, plus the space for a copy of the string */
81 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
82 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
84 WCHAR *str = (WCHAR *)(ret + count + 1);
87 strcpyW( str, buffer );
92 if (!(p = strchrW( p, ';' ))) break;
101 /***********************************************************************
102 * RELAY16_InitDebugLists
104 * Build the relay include/exclude function lists.
106 void RELAY16_InitDebugLists(void)
108 OBJECT_ATTRIBUTES attr;
114 static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
115 'W','i','n','e','\\',
116 'D','e','b','u','g',0};
117 static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
118 static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
119 static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
120 static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
122 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
123 attr.Length = sizeof(attr);
124 attr.RootDirectory = root;
125 attr.ObjectName = &name;
127 attr.SecurityDescriptor = NULL;
128 attr.SecurityQualityOfService = NULL;
129 RtlInitUnicodeString( &name, configW );
131 /* @@ Wine registry key: HKCU\Software\Wine\Debug */
132 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
136 str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
137 RtlInitUnicodeString( &name, RelayIncludeW );
138 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
140 debug_relay_includelist = build_list( str );
143 RtlInitUnicodeString( &name, RelayExcludeW );
144 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
146 debug_relay_excludelist = build_list( str );
149 RtlInitUnicodeString( &name, SnoopIncludeW );
150 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
152 debug_snoop_includelist = build_list( str );
155 RtlInitUnicodeString( &name, SnoopExcludeW );
156 if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
158 debug_snoop_excludelist = build_list( str );
164 /***********************************************************************
167 * Check if a given module and function is in the list.
169 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
173 sprintf( ord_str, "%d", ordinal );
176 const WCHAR *p = strrchrW( *list, '.' );
177 if (p && p > *list) /* check module and function */
180 if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
181 if (p[1] == '*' && !p[2]) return TRUE;
182 if (!strcmpiAW( ord_str, p + 1 )) return TRUE;
183 if (func && !strcmpiAW( func, p + 1 )) return TRUE;
185 else /* function only */
187 if (func && !strcmpiAW( func, *list )) return TRUE;
194 /***********************************************************************
195 * RELAY_ShowDebugmsgRelay
197 * Simple function to decide if a particular debugging message is
200 static BOOL RELAY_ShowDebugmsgRelay(const char *module, int ordinal, const char *func)
202 if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
204 if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
210 /***********************************************************************
211 * SNOOP16_ShowDebugmsgSnoop
213 * Simple function to decide if a particular debugging message is
216 int SNOOP16_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
218 if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
220 if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
226 /***********************************************************************
229 * Return the ordinal, name, and type info corresponding to a CS:IP address.
231 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR module, LPSTR func, WORD *pOrd )
240 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
244 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
247 entry = (ET_ENTRY *)((BYTE *)bundle+6);
248 for (i = bundle->first + 1; i <= bundle->last; i++)
250 if ((entry->offs < frame->entry_ip)
251 && (entry->segnum == 1) /* code segment ? */
252 && (entry->offs >= max_offset))
254 max_offset = entry->offs;
259 } while ( (bundle->next)
260 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
262 /* Search for the name in the resident names table */
263 /* (built-in modules have no non-resident table) */
265 p = (BYTE *)pModule + pModule->ne_restab;
266 memcpy( module, p + 1, *p );
271 p += *p + 1 + sizeof(WORD);
272 if (*(WORD *)(p + *p + 1) == *pOrd) break;
274 memcpy( func, p + 1, *p );
277 /* Retrieve entry point call structure */
278 p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
279 /* p now points to lret, get the start of CALLFROM16 structure */
280 return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->ret);
284 extern int call_entry_point( void *func, int nb_args, const int *args );
285 __ASM_GLOBAL_FUNC( call_entry_point,
288 "\tmovl 12(%ebp),%ecx\n"
289 "\tmovl 16(%ebp),%edx\n"
291 "2:\tpushl -4(%edx,%ecx,4)\n"
293 "1:\tcall *8(%ebp)\n"
299 /***********************************************************************
300 * relay_call_from_16_no_debug
302 * Same as relay_call_from_16 but doesn't print any debug information.
304 static int relay_call_from_16_no_debug( void *entry_point, unsigned char *args16, CONTEXT86 *context,
305 const CALLFROM16 *call )
307 unsigned int i, j, nb_args = 0;
310 /* look for the ret instruction */
311 for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
312 if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
314 if (call->ret[j] == 0xcb66) /* cdecl */
316 for (i = 0; i < 20; i++, nb_args++)
318 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
320 if (type == ARG_NONE) break;
324 args32[nb_args] = *(WORD *)args16;
325 args16 += sizeof(WORD);
328 args32[nb_args] = *(short *)args16;
329 args16 += sizeof(WORD);
333 args32[nb_args] = *(int *)args16;
334 args16 += sizeof(int);
338 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
339 args16 += sizeof(SEGPTR);
342 args32[nb_args] = (int)args16;
351 /* Start with the last arg */
352 args16 += call->ret[j + 1];
353 for (i = 0; i < 20; i++, nb_args++)
355 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
357 if (type == ARG_NONE) break;
361 args16 -= sizeof(WORD);
362 args32[nb_args] = *(WORD *)args16;
365 args16 -= sizeof(WORD);
366 args32[nb_args] = *(short *)args16;
370 args16 -= sizeof(int);
371 args32[nb_args] = *(int *)args16;
375 args16 -= sizeof(SEGPTR);
376 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
384 if (!j) /* register function */
385 args32[nb_args++] = (int)context;
387 SYSLEVEL_CheckNotLevel( 2 );
389 return call_entry_point( entry_point, nb_args, args32 );
393 /***********************************************************************
396 * Replacement for the 16-bit relay functions when relay debugging is on.
398 int relay_call_from_16( void *entry_point, unsigned char *args16, CONTEXT86 *context )
402 unsigned int i, j, nb_args = 0;
403 int ret_val, args32[20];
404 char module[10], func[64];
405 const CALLFROM16 *call;
407 frame = CURRENT_STACK16;
408 call = get_entry_point( frame, module, func, &ordinal );
409 if (!TRACE_ON(relay) || !RELAY_ShowDebugmsgRelay( module, ordinal, func ))
410 return relay_call_from_16_no_debug( entry_point, args16, context, call );
412 DPRINTF( "%04lx:Call %s.%d: %s(",GetCurrentThreadId(), module, ordinal, func );
414 /* look for the ret instruction */
415 for (j = 0; j < sizeof(call->ret)/sizeof(call->ret[0]); j++)
416 if (call->ret[j] == 0xca66 || call->ret[j] == 0xcb66) break;
418 if (call->ret[j] == 0xcb66) /* cdecl */
420 for (i = 0; i < 20; i++, nb_args++)
422 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
424 if (type == ARG_NONE) break;
425 if (i) DPRINTF( "," );
429 DPRINTF( "%04x", *(WORD *)args16 );
430 args32[nb_args] = *(WORD *)args16;
431 args16 += sizeof(WORD);
434 DPRINTF( "%04x", *(WORD *)args16 );
435 args32[nb_args] = *(short *)args16;
436 args16 += sizeof(WORD);
439 DPRINTF( "%08x", *(int *)args16 );
440 args32[nb_args] = *(int *)args16;
441 args16 += sizeof(int);
444 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
445 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
446 args16 += sizeof(SEGPTR);
449 DPRINTF( "%08x %s", *(int *)args16,
450 debugstr_a( MapSL(*(SEGPTR *)args16 )));
451 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
452 args16 += sizeof(int);
455 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
456 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
457 args32[nb_args] = *(SEGPTR *)args16;
458 args16 += sizeof(SEGPTR);
462 args32[nb_args] = (int)args16;
471 /* Start with the last arg */
472 args16 += call->ret[j + 1];
473 for (i = 0; i < 20; i++, nb_args++)
475 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
477 if (type == ARG_NONE) break;
478 if (i) DPRINTF( "," );
482 args16 -= sizeof(WORD);
483 args32[nb_args] = *(WORD *)args16;
484 DPRINTF( "%04x", *(WORD *)args16 );
487 args16 -= sizeof(WORD);
488 args32[nb_args] = *(short *)args16;
489 DPRINTF( "%04x", *(WORD *)args16 );
492 args16 -= sizeof(int);
493 args32[nb_args] = *(int *)args16;
494 DPRINTF( "%08x", *(int *)args16 );
497 args16 -= sizeof(SEGPTR);
498 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
499 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
502 args16 -= sizeof(int);
503 args32[nb_args] = (int)MapSL( *(SEGPTR *)args16 );
504 DPRINTF( "%08x %s", *(int *)args16,
505 debugstr_a( MapSL(*(SEGPTR *)args16 )));
508 args16 -= sizeof(SEGPTR);
509 args32[nb_args] = *(SEGPTR *)args16;
510 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
511 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
515 args32[nb_args] = (int)args16;
523 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
525 if (!j) /* register function */
527 args32[nb_args++] = (int)context;
528 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
529 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
530 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
531 (WORD)context->SegEs, context->EFlags );
534 SYSLEVEL_CheckNotLevel( 2 );
536 ret_val = call_entry_point( entry_point, nb_args, args32 );
538 SYSLEVEL_CheckNotLevel( 2 );
540 DPRINTF( "%04lx:Ret %s.%d: %s() ",GetCurrentThreadId(), module, ordinal, func );
541 if (!j) /* register function */
543 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
544 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
545 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
546 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
547 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
548 (WORD)context->SegEs, context->EFlags );
552 frame = CURRENT_STACK16; /* might have be changed by the entry point */
553 if (j == 1) /* 16-bit return sequence */
554 DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
555 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
557 DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
558 ret_val, frame->cs, frame->ip, frame->ds );
566 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
567 * (these will never be called but need to be present to satisfy the linker ...)
570 /***********************************************************************
571 * __wine_call_from_16_regs (KERNEL32.@)
573 void __wine_call_from_16_regs()
578 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
581 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
584 #endif /* __i386__ */