Generate Perl modules defining prototypes for exported functions.
[wine] / if1632 / relay.c
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Alexandre Julliard
4  */
5
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 #include "wine/winbase16.h"
12 #include "winnt.h"
13 #include "module.h"
14 #include "stackframe.h"
15 #include "selectors.h"
16 #include "builtin16.h"
17 #include "syslevel.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(relay);
21
22 /***********************************************************************
23  *           RELAY_Init
24  */
25 BOOL RELAY_Init(void)
26 {
27 #ifdef __i386__
28     WORD codesel;
29
30       /* Allocate the code selector for CallTo16 routines */
31
32     extern void Call16_Ret_Start(), Call16_Ret_End();
33     extern void CallTo16_Ret();
34     extern void CALL32_CBClient_Ret();
35     extern void CALL32_CBClientEx_Ret();
36     extern SEGPTR CallTo16_RetAddr;
37     extern DWORD CallTo16_DataSelector;
38     extern SEGPTR CALL32_CBClient_RetAddr;
39     extern SEGPTR CALL32_CBClientEx_RetAddr;
40
41     codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
42                                    (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
43                                    WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
44     if (!codesel) return FALSE;
45
46       /* Patch the return addresses for CallTo16 routines */
47
48     CallTo16_DataSelector = __get_ds();
49     CallTo16_RetAddr = 
50         MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
51     CALL32_CBClient_RetAddr = 
52         MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
53     CALL32_CBClientEx_RetAddr = 
54         MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
55 #endif
56     return TRUE;
57 }
58
59 /*
60  * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
61  * (these will never be called but need to be present to satisfy the linker ...)
62  */
63 #ifndef __i386__
64 /***********************************************************************
65  *              wine_call_to_16_word (KERNEL32.@)
66  */
67 WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs )
68 {
69     assert( FALSE );
70 }
71
72 /***********************************************************************
73  *              wine_call_to_16_long (KERNEL32.@)
74  */
75 LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs )
76 {
77     assert( FALSE );
78 }
79
80 /***********************************************************************
81  *              wine_call_to_16_regs_short (KERNEL32.@)
82  */
83 void WINAPI wine_call_to_16_regs_short( CONTEXT86 *context, INT nArgs )
84 {
85     assert( FALSE );
86 }
87
88 /***********************************************************************
89  *              wine_call_to_16_regs_long (KERNEL32.@)
90  */
91 void WINAPI wine_call_to_16_regs_long ( CONTEXT86 *context, INT nArgs )
92 {
93     assert( FALSE );
94 }
95
96 /***********************************************************************
97  *              __wine_call_from_16_word (KERNEL32.@)
98  */
99 WORD __wine_call_from_16_word()
100 {
101     assert( FALSE );
102 }
103
104 /***********************************************************************
105  *              __wine_call_from_16_long (KERNEL32.@)
106  */
107 LONG __wine_call_from_16_long()
108 {
109     assert( FALSE );
110 }
111
112 /***********************************************************************
113  *              __wine_call_from_16_regs (KERNEL32.@)
114  */
115 void __wine_call_from_16_regs()
116 {
117     assert( FALSE );
118 }
119
120 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
121 { assert( FALSE ); }
122
123 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
124 { assert( FALSE ); }
125 #endif
126
127
128 /* from relay32/relay386.c */
129 extern char **debug_relay_excludelist,**debug_relay_includelist;
130 extern int RELAY_ShowDebugmsgRelay(const char *func);
131
132
133 /***********************************************************************
134  *           get_entry_point
135  *
136  * Return the ordinal, name, and type info corresponding to a CS:IP address.
137  */
138 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
139 {
140     WORD i, max_offset;
141     register BYTE *p;
142     NE_MODULE *pModule;
143     ET_BUNDLE *bundle;
144     ET_ENTRY *entry;
145
146     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
147         return NULL;
148
149     max_offset = 0;
150     *pOrd = 0;
151     bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
152     do 
153     {
154         entry = (ET_ENTRY *)((BYTE *)bundle+6);
155         for (i = bundle->first + 1; i <= bundle->last; i++)
156         {
157             if ((entry->offs < frame->entry_ip)
158             && (entry->segnum == 1) /* code segment ? */
159             && (entry->offs >= max_offset))
160             {
161                 max_offset = entry->offs;
162                 *pOrd = i;
163             }
164             entry++;
165         }
166     } while ( (bundle->next)
167            && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
168
169     /* Search for the name in the resident names table */
170     /* (built-in modules have no non-resident table)   */
171     
172     p = (BYTE *)pModule + pModule->name_table;
173     while (*p)
174     {
175         p += *p + 1 + sizeof(WORD);
176         if (*(WORD *)(p + *p + 1) == *pOrd) break;
177     }
178
179     sprintf( name, "%.*s.%d: %.*s",
180              *((BYTE *)pModule + pModule->name_table),
181              (char *)pModule + pModule->name_table + 1,
182              *pOrd, *p, (char *)(p + 1) );
183
184     /* Retrieve entry point call structure */
185     p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
186     /* p now points to lret, get the start of CALLFROM16 structure */
187     return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->lret);
188 }
189
190
191 /***********************************************************************
192  *           RELAY_DebugCallFrom16
193  */
194 void RELAY_DebugCallFrom16( CONTEXT86 *context )
195 {
196     STACK16FRAME *frame;
197     WORD ordinal;
198     char *args16, funstr[80];
199     const CALLFROM16 *call;
200     int i;
201
202     if (!TRACE_ON(relay)) return;
203
204     frame = CURRENT_STACK16;
205     call = get_entry_point( frame, funstr, &ordinal );
206     if (!call) return; /* happens for the two snoop register relays */
207     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
208     DPRINTF( "%08lx:Call %s(",GetCurrentThreadId(),funstr);
209     VA_START16( args16 );
210
211     if (call->lret == 0xcb66)  /* cdecl */
212     {
213         for (i = 0; i < 20; i++)
214         {
215             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
216
217             if (type == ARG_NONE) break;
218             if (i) DPRINTF( "," );
219             switch(type)
220             {
221             case ARG_WORD:
222             case ARG_SWORD:
223                 DPRINTF( "%04x", *(WORD *)args16 );
224                 args16 += sizeof(WORD);
225                 break;
226             case ARG_LONG:
227                 DPRINTF( "%08x", *(int *)args16 );
228                 args16 += sizeof(int);
229                 break;
230             case ARG_PTR:
231                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
232                 args16 += sizeof(SEGPTR);
233                 break;
234             case ARG_STR:
235                 DPRINTF( "%08x %s", *(int *)args16,
236                          debugres_a( MapSL(*(SEGPTR *)args16 )));
237                 args16 += sizeof(int);
238                 break;
239             case ARG_SEGSTR:
240                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
241                          debugres_a( MapSL(*(SEGPTR *)args16 )) );
242                 args16 += sizeof(SEGPTR);
243                 break;
244             default:
245                 break;
246             }
247         }
248     }
249     else  /* not cdecl */
250     {
251         /* Start with the last arg */
252         args16 += call->nArgs;
253         for (i = 0; i < 20; i++)
254         {
255             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
256
257             if (type == ARG_NONE) break;
258             if (i) DPRINTF( "," );
259             switch(type)
260             {
261             case ARG_WORD:
262             case ARG_SWORD:
263                 args16 -= sizeof(WORD);
264                 DPRINTF( "%04x", *(WORD *)args16 );
265                 break;
266             case ARG_LONG:
267                 args16 -= sizeof(int);
268                 DPRINTF( "%08x", *(int *)args16 );
269                 break;
270             case ARG_PTR:
271                 args16 -= sizeof(SEGPTR);
272                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
273                 break;
274             case ARG_STR:
275                 args16 -= sizeof(int);
276                 DPRINTF( "%08x %s", *(int *)args16,
277                          debugres_a( MapSL(*(SEGPTR *)args16 )));
278                 break;
279             case ARG_SEGSTR:
280                 args16 -= sizeof(SEGPTR);
281                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
282                          debugres_a( MapSL(*(SEGPTR *)args16 )) );
283                 break;
284             default:
285                 break;
286             }
287         }
288     }
289
290     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
291     VA_END16( args16 );
292
293     if (call->arg_types[0] & ARG_REGISTER)
294         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
295                 AX_reg(context), BX_reg(context), CX_reg(context),
296                 DX_reg(context), SI_reg(context), DI_reg(context),
297                 (WORD)context->SegEs, context->EFlags );
298
299     SYSLEVEL_CheckNotLevel( 2 );
300 }
301
302
303 /***********************************************************************
304  *           RELAY_DebugCallFrom16Ret
305  */
306 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
307 {
308     STACK16FRAME *frame;
309     WORD ordinal;
310     char funstr[80];
311     const CALLFROM16 *call;
312
313     if (!TRACE_ON(relay)) return;
314     frame = CURRENT_STACK16;
315     call = get_entry_point( frame, funstr, &ordinal );
316     if (!call) return;
317     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
318     DPRINTF( "%08lx:Ret  %s() ",GetCurrentThreadId(),funstr);
319
320     if (call->arg_types[0] & ARG_REGISTER)
321     {
322         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
323                 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
324         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
325                 AX_reg(context), BX_reg(context), CX_reg(context),
326                 DX_reg(context), SI_reg(context), DI_reg(context),
327                 (WORD)context->SegEs, context->EFlags );
328     }
329     else if (call->arg_types[0] & ARG_RET16)
330     {
331         DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
332                  ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
333     }
334     else
335     {
336         DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
337                  ret_val, frame->cs, frame->ip, frame->ds );
338     }
339     SYSLEVEL_CheckNotLevel( 2 );
340 }
341
342
343 /***********************************************************************
344  *           RELAY_DebugCallTo16
345  *
346  * 'target' contains either the function to call (normal CallTo16)
347  * or a pointer to the CONTEXT86 struct (register CallTo16).
348  * 'nb_args' is the number of argument bytes on the 16-bit stack; 
349  * 'reg_func' specifies whether we have a register CallTo16 or not.
350  */
351 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
352 {
353     WORD *stack16;
354     TEB *teb;
355
356     if (!TRACE_ON(relay)) return;
357     teb = NtCurrentTeb();
358     stack16 = (WORD *)THREAD_STACK16(teb);
359
360     nb_args /= sizeof(WORD);
361
362     if ( reg_func )
363     {
364         CONTEXT86 *context = (CONTEXT86 *)target;
365
366         DPRINTF("%08lx:CallTo16(func=%04lx:%04x,ds=%04lx",
367                 GetCurrentThreadId(),
368                 context->SegCs, LOWORD(context->Eip), context->SegDs );
369         while (nb_args--) DPRINTF( ",%04x", *--stack16 );
370         DPRINTF(") ss:sp=%04x:%04x", SELECTOROF(teb->cur_stack),
371                 OFFSETOF(teb->cur_stack) );
372         DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
373                 AX_reg(context), BX_reg(context), CX_reg(context),
374                 DX_reg(context), SI_reg(context), DI_reg(context),
375                 BP_reg(context), (WORD)context->SegEs, (WORD)context->SegFs );
376     }
377     else
378     {
379         DPRINTF("%08lx:CallTo16(func=%04x:%04x,ds=%04x",
380                 GetCurrentThreadId(),
381                 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
382         while (nb_args--) DPRINTF( ",%04x", *--stack16 );
383         DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
384                 OFFSETOF(teb->cur_stack) );
385     }
386
387     SYSLEVEL_CheckNotLevel( 2 );
388 }
389
390
391 /***********************************************************************
392  *           RELAY_DebugCallTo16Ret
393  */
394 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
395 {
396     if (!TRACE_ON(relay)) return;
397
398     if (!reg_func)
399     {
400         DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
401                 GetCurrentThreadId(),
402                 SELECTOROF(NtCurrentTeb()->cur_stack),
403                 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
404     }
405     else
406     {
407         CONTEXT86 *context = (CONTEXT86 *)ret_val;
408
409         DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x ",
410                 GetCurrentThreadId(),
411                 SELECTOROF(NtCurrentTeb()->cur_stack),
412                 OFFSETOF(NtCurrentTeb()->cur_stack));
413         DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
414                 AX_reg(context), BX_reg(context), CX_reg(context),
415                 DX_reg(context), BP_reg(context), LOWORD(context->Esp));
416     }
417
418     SYSLEVEL_CheckNotLevel( 2 );
419 }