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