Specify enough buffer for id[] (20 chars are not enough).
[wine] / dlls / kernel / relay16.c
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Alexandre Julliard
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wine/winbase16.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "kernel_private.h"
35 #include "kernel16_private.h"
36 #include "wine/unicode.h"
37 #include "wine/library.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(relay);
41
42 #ifdef __i386__
43
44 static const WCHAR **debug_relay_excludelist;
45 static const WCHAR **debug_relay_includelist;
46 static const WCHAR **debug_snoop_excludelist;
47 static const WCHAR **debug_snoop_includelist;
48
49 /* compare an ASCII and a Unicode string without depending on the current codepage */
50 inline static int strcmpiAW( const char *strA, const WCHAR *strW )
51 {
52     while (*strA && (toupperW((unsigned char)*strA) == toupperW(*strW))) { strA++; strW++; }
53     return toupperW((unsigned char)*strA) - toupperW(*strW);
54 }
55
56 /* compare an ASCII and a Unicode string without depending on the current codepage */
57 inline static int strncmpiAW( const char *strA, const WCHAR *strW, int n )
58 {
59     int ret = 0;
60     for ( ; n > 0; n--, strA++, strW++)
61         if ((ret = toupperW((unsigned char)*strA) - toupperW(*strW)) || !*strA) break;
62     return ret;
63 }
64
65 /***********************************************************************
66  *           build_list
67  *
68  * Build a function list from a ';'-separated string.
69  */
70 static const WCHAR **build_list( const WCHAR *buffer )
71 {
72     int count = 1;
73     const WCHAR *p = buffer;
74     const WCHAR **ret;
75
76     while ((p = strchrW( p, ';' )))
77     {
78         count++;
79         p++;
80     }
81     /* allocate count+1 pointers, plus the space for a copy of the string */
82     if ((ret = RtlAllocateHeap( GetProcessHeap(), 0,
83                                 (count+1) * sizeof(WCHAR*) + (strlenW(buffer)+1) * sizeof(WCHAR) )))
84     {
85         WCHAR *str = (WCHAR *)(ret + count + 1);
86         WCHAR *p = str;
87
88         strcpyW( str, buffer );
89         count = 0;
90         for (;;)
91         {
92             ret[count++] = p;
93             if (!(p = strchrW( p, ';' ))) break;
94             *p++ = 0;
95         }
96         ret[count++] = NULL;
97     }
98     return ret;
99 }
100
101
102 /***********************************************************************
103  *           RELAY16_InitDebugLists
104  *
105  * Build the relay include/exclude function lists.
106  */
107 void RELAY16_InitDebugLists(void)
108 {
109     OBJECT_ATTRIBUTES attr;
110     UNICODE_STRING name;
111     char buffer[1024];
112     HANDLE root, hkey;
113     DWORD count;
114     WCHAR *str;
115     static const WCHAR configW[] = {'S','o','f','t','w','a','r','e','\\',
116                                     'W','i','n','e','\\',
117                                     'D','e','b','u','g',0};
118     static const WCHAR RelayIncludeW[] = {'R','e','l','a','y','I','n','c','l','u','d','e',0};
119     static const WCHAR RelayExcludeW[] = {'R','e','l','a','y','E','x','c','l','u','d','e',0};
120     static const WCHAR SnoopIncludeW[] = {'S','n','o','o','p','I','n','c','l','u','d','e',0};
121     static const WCHAR SnoopExcludeW[] = {'S','n','o','o','p','E','x','c','l','u','d','e',0};
122
123     RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
124     attr.Length = sizeof(attr);
125     attr.RootDirectory = root;
126     attr.ObjectName = &name;
127     attr.Attributes = 0;
128     attr.SecurityDescriptor = NULL;
129     attr.SecurityQualityOfService = NULL;
130     RtlInitUnicodeString( &name, configW );
131
132     /* @@ Wine registry key: HKCU\Software\Wine\Debug */
133     if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
134     NtClose( root );
135     if (!hkey) return;
136
137     str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
138     RtlInitUnicodeString( &name, RelayIncludeW );
139     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
140     {
141         debug_relay_includelist = build_list( str );
142     }
143
144     RtlInitUnicodeString( &name, RelayExcludeW );
145     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
146     {
147         debug_relay_excludelist = build_list( str );
148     }
149
150     RtlInitUnicodeString( &name, SnoopIncludeW );
151     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
152     {
153         debug_snoop_includelist = build_list( str );
154     }
155
156     RtlInitUnicodeString( &name, SnoopExcludeW );
157     if (!NtQueryValueKey( hkey, &name, KeyValuePartialInformation, buffer, sizeof(buffer), &count ))
158     {
159         debug_snoop_excludelist = build_list( str );
160     }
161     NtClose( hkey );
162 }
163
164
165 /***********************************************************************
166  *           check_list
167  *
168  * Check if a given module and function is in the list.
169  */
170 static BOOL check_list( const char *module, int ordinal, const char *func, const WCHAR **list )
171 {
172     char ord_str[10];
173
174     sprintf( ord_str, "%d", ordinal );
175     for(; *list; list++)
176     {
177         const WCHAR *p = strrchrW( *list, '.' );
178         if (p && p > *list)  /* check module and function */
179         {
180             int len = p - *list;
181             if (strncmpiAW( module, *list, len-1 ) || module[len]) continue;
182             if (p[1] == '*' && !p[2]) return TRUE;
183             if (!strcmpiAW( ord_str, p + 1 )) return TRUE;
184             if (func && !strcmpiAW( func, p + 1 )) return TRUE;
185         }
186         else  /* function only */
187         {
188             if (func && !strcmpiAW( func, *list )) return TRUE;
189         }
190     }
191     return FALSE;
192 }
193
194
195 /***********************************************************************
196  *           RELAY_ShowDebugmsgRelay
197  *
198  * Simple function to decide if a particular debugging message is
199  * wanted.
200  */
201 static BOOL RELAY_ShowDebugmsgRelay(const char *module, int ordinal, const char *func)
202 {
203     if (debug_relay_excludelist && check_list( module, ordinal, func, debug_relay_excludelist ))
204         return FALSE;
205     if (debug_relay_includelist && !check_list( module, ordinal, func, debug_relay_includelist ))
206         return FALSE;
207     return TRUE;
208 }
209
210
211 /***********************************************************************
212  *          SNOOP16_ShowDebugmsgSnoop
213  *
214  * Simple function to decide if a particular debugging message is
215  * wanted.
216  */
217 int SNOOP16_ShowDebugmsgSnoop(const char *module, int ordinal, const char *func)
218 {
219     if (debug_snoop_excludelist && check_list( module, ordinal, func, debug_snoop_excludelist ))
220         return FALSE;
221     if (debug_snoop_includelist && !check_list( module, ordinal, func, debug_snoop_includelist ))
222         return FALSE;
223     return TRUE;
224 }
225
226
227 /***********************************************************************
228  *           get_entry_point
229  *
230  * Return the ordinal, name, and type info corresponding to a CS:IP address.
231  */
232 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR module, LPSTR func, WORD *pOrd )
233 {
234     WORD i, max_offset;
235     register BYTE *p;
236     NE_MODULE *pModule;
237     ET_BUNDLE *bundle;
238     ET_ENTRY *entry;
239
240     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
241         return NULL;
242
243     max_offset = 0;
244     *pOrd = 0;
245     bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
246     do
247     {
248         entry = (ET_ENTRY *)((BYTE *)bundle+6);
249         for (i = bundle->first + 1; i <= bundle->last; i++)
250         {
251             if ((entry->offs < frame->entry_ip)
252             && (entry->segnum == 1) /* code segment ? */
253             && (entry->offs >= max_offset))
254             {
255                 max_offset = entry->offs;
256                 *pOrd = i;
257             }
258             entry++;
259         }
260     } while ( (bundle->next)
261            && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
262
263     /* Search for the name in the resident names table */
264     /* (built-in modules have no non-resident table)   */
265
266     p = (BYTE *)pModule + pModule->ne_restab;
267     memcpy( module, p + 1, *p );
268     module[*p] = 0;
269
270     while (*p)
271     {
272         p += *p + 1 + sizeof(WORD);
273         if (*(WORD *)(p + *p + 1) == *pOrd) break;
274     }
275     memcpy( func, p + 1, *p );
276     func[*p] = 0;
277
278     /* Retrieve entry point call structure */
279     p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
280     /* p now points to lret, get the start of CALLFROM16 structure */
281     return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->lret);
282 }
283
284
285 /***********************************************************************
286  *           RELAY_DebugCallFrom16
287  */
288 void RELAY_DebugCallFrom16( CONTEXT86 *context )
289 {
290     STACK16FRAME *frame;
291     WORD ordinal;
292     char *args16, module[10], func[64];
293     const CALLFROM16 *call;
294     int i;
295
296     if (!TRACE_ON(relay)) return;
297
298     frame = CURRENT_STACK16;
299     call = get_entry_point( frame, module, func, &ordinal );
300     if (!call) return; /* happens for the two snoop register relays */
301     if (!RELAY_ShowDebugmsgRelay( module, ordinal, func )) return;
302     DPRINTF( "%04lx:Call %s.%d: %s(",GetCurrentThreadId(), module, ordinal, func );
303     args16 = (char *)(frame + 1);
304
305     if (call->lret == 0xcb66)  /* cdecl */
306     {
307         for (i = 0; i < 20; i++)
308         {
309             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
310
311             if (type == ARG_NONE) break;
312             if (i) DPRINTF( "," );
313             switch(type)
314             {
315             case ARG_WORD:
316             case ARG_SWORD:
317                 DPRINTF( "%04x", *(WORD *)args16 );
318                 args16 += sizeof(WORD);
319                 break;
320             case ARG_LONG:
321                 DPRINTF( "%08x", *(int *)args16 );
322                 args16 += sizeof(int);
323                 break;
324             case ARG_PTR:
325                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
326                 args16 += sizeof(SEGPTR);
327                 break;
328             case ARG_STR:
329                 DPRINTF( "%08x %s", *(int *)args16,
330                          debugstr_a( MapSL(*(SEGPTR *)args16 )));
331                 args16 += sizeof(int);
332                 break;
333             case ARG_SEGSTR:
334                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
335                          debugstr_a( MapSL(*(SEGPTR *)args16 )) );
336                 args16 += sizeof(SEGPTR);
337                 break;
338             default:
339                 break;
340             }
341         }
342     }
343     else  /* not cdecl */
344     {
345         /* Start with the last arg */
346         args16 += call->nArgs;
347         for (i = 0; i < 20; i++)
348         {
349             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
350
351             if (type == ARG_NONE) break;
352             if (i) DPRINTF( "," );
353             switch(type)
354             {
355             case ARG_WORD:
356             case ARG_SWORD:
357                 args16 -= sizeof(WORD);
358                 DPRINTF( "%04x", *(WORD *)args16 );
359                 break;
360             case ARG_LONG:
361                 args16 -= sizeof(int);
362                 DPRINTF( "%08x", *(int *)args16 );
363                 break;
364             case ARG_PTR:
365                 args16 -= sizeof(SEGPTR);
366                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
367                 break;
368             case ARG_STR:
369                 args16 -= sizeof(int);
370                 DPRINTF( "%08x %s", *(int *)args16,
371                          debugstr_a( MapSL(*(SEGPTR *)args16 )));
372                 break;
373             case ARG_SEGSTR:
374                 args16 -= sizeof(SEGPTR);
375                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
376                          debugstr_a( MapSL(*(SEGPTR *)args16 )) );
377                 break;
378             default:
379                 break;
380             }
381         }
382     }
383
384     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
385
386     if (call->arg_types[0] & ARG_REGISTER)
387         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
388                 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
389                 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
390                 (WORD)context->SegEs, context->EFlags );
391
392     SYSLEVEL_CheckNotLevel( 2 );
393 }
394
395
396 /***********************************************************************
397  *           RELAY_DebugCallFrom16Ret
398  */
399 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
400 {
401     STACK16FRAME *frame;
402     WORD ordinal;
403     char module[10], func[64];
404     const CALLFROM16 *call;
405
406     if (!TRACE_ON(relay)) return;
407     frame = CURRENT_STACK16;
408     call = get_entry_point( frame, module, func, &ordinal );
409     if (!call) return;
410     if (!RELAY_ShowDebugmsgRelay( module, ordinal, func )) return;
411     DPRINTF( "%04lx:Ret  %s.%d: %s() ", GetCurrentThreadId(), module, ordinal, func );
412
413     if (call->arg_types[0] & ARG_REGISTER)
414     {
415         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
416                 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
417         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
418                 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
419                 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
420                 (WORD)context->SegEs, context->EFlags );
421     }
422     else if (call->arg_types[0] & ARG_RET16)
423     {
424         DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
425                  ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
426     }
427     else
428     {
429         DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
430                  ret_val, frame->cs, frame->ip, frame->ds );
431     }
432     SYSLEVEL_CheckNotLevel( 2 );
433 }
434
435 #else /* __i386__ */
436
437 /*
438  * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
439  * (these will never be called but need to be present to satisfy the linker ...)
440  */
441
442 /***********************************************************************
443  *              __wine_call_from_16_word (KERNEL32.@)
444  */
445 WORD __wine_call_from_16_word()
446 {
447     assert( FALSE );
448 }
449
450 /***********************************************************************
451  *              __wine_call_from_16_long (KERNEL32.@)
452  */
453 LONG __wine_call_from_16_long()
454 {
455     assert( FALSE );
456 }
457
458 /***********************************************************************
459  *              __wine_call_from_16_regs (KERNEL32.@)
460  */
461 void __wine_call_from_16_regs()
462 {
463     assert( FALSE );
464 }
465
466 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
467 { assert( FALSE ); }
468
469 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
470 { assert( FALSE ); }
471
472 #endif  /* __i386__ */