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