SendASPI32Command32 has to be __cdecl.
[wine/multimedia.git] / debugger / source.c
blob29e2c03dc4b3d34e4785a95b45f8baf3879aceba
1 /*
2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/mman.h>
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <limits.h>
16 #include <string.h>
17 #include <unistd.h>
18 #ifndef PATH_MAX
19 #define PATH_MAX _MAX_PATH
20 #endif
22 #include "win.h"
23 #include "pe_image.h"
24 #include "peexe.h"
25 #include "debugger.h"
26 #include "peexe.h"
27 #include "task.h"
28 #include "xmalloc.h"
30 struct searchlist
32 char * path;
33 struct searchlist * next;
37 struct open_filelist
39 char * path;
40 char * real_path;
41 struct open_filelist * next;
42 unsigned int size;
43 signed int nlines;
44 unsigned int * linelist;
47 static struct open_filelist * ofiles;
49 static struct searchlist * listhead;
50 static char DEBUG_current_sourcefile[PATH_MAX];
51 static int DEBUG_start_sourceline = -1;
52 static int DEBUG_end_sourceline = -1;
54 void
55 DEBUG_ShowDir()
57 struct searchlist * sl;
59 fprintf(stderr,"Search list :\n");
60 for(sl = listhead; sl; sl = sl->next)
62 fprintf(stderr, "\t%s\n", sl->path);
64 fprintf(stderr, "\n");
67 void
68 DEBUG_AddPath(const char * path)
70 struct searchlist * sl;
72 sl = (struct searchlist *) xmalloc(sizeof(struct searchlist));
73 if( sl == NULL )
75 return;
78 sl->next = listhead;
79 sl->path = xstrdup(path);
80 listhead = sl;
83 void
84 DEBUG_NukePath()
86 struct searchlist * sl;
87 struct searchlist * nxt;
89 for(sl = listhead; sl; sl = nxt)
91 nxt = sl->next;
92 free(sl->path);
93 free(sl);
96 listhead = NULL;
99 static
101 DEBUG_DisplaySource(char * sourcefile, int start, int end)
103 char * addr;
104 char buffer[1024];
105 int fd;
106 int i;
107 struct open_filelist * ol;
108 int nlines;
109 char * pnt;
110 int rtn;
111 struct searchlist * sl;
112 struct stat statbuf;
113 int status;
114 char tmppath[PATH_MAX];
117 * First see whether we have the file open already. If so, then
118 * use that, otherwise we have to try and open it.
120 for(ol = ofiles; ol; ol = ol->next)
122 if( strcmp(ol->path, sourcefile) == 0 )
124 break;
128 if( ol == NULL )
131 * Try again, stripping the path from the opened file.
133 for(ol = ofiles; ol; ol = ol->next)
135 pnt = strrchr(ol->path, '/');
136 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
138 break;
144 if( ol == NULL )
147 * See if this is a DOS style name or not.
149 pnt = strchr(sourcefile, '\\' );
150 if( pnt == NULL )
152 pnt = strchr(sourcefile, '/' );
153 if( pnt == NULL )
155 pnt = sourcefile;
160 * Crapola. We need to try and open the file.
162 status = stat(sourcefile, &statbuf);
163 if( status != -1 )
165 strcpy(tmppath, sourcefile);
167 else
169 for(sl = listhead; sl; sl = sl->next)
171 strcpy(tmppath, sl->path);
172 if( tmppath[strlen(tmppath)-1] != '/' )
174 strcat(tmppath, "/");
177 * Now append the base file name.
179 strcat(tmppath, pnt);
181 status = stat(tmppath, &statbuf);
182 if( status != -1 )
184 break;
188 if( sl == NULL )
191 * Still couldn't find it. Ask user for path to add.
193 fprintf(stderr,"Enter path to file %s: ", sourcefile);
194 fgets(tmppath, sizeof(tmppath), stdin);
196 if( tmppath[strlen(tmppath)-1] == '\n' )
198 tmppath[strlen(tmppath)-1] = '\0';
201 if( tmppath[strlen(tmppath)-1] != '/' )
203 strcat(tmppath, "/");
206 * Now append the base file name.
208 strcat(tmppath, pnt);
210 status = stat(tmppath, &statbuf);
211 if( status == -1 )
214 * OK, I guess the user doesn't really want to see it
215 * after all.
217 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
218 ol->path = xstrdup(sourcefile);
219 ol->real_path = NULL;
220 ol->next = ofiles;
221 ol->nlines = 0;
222 ol->linelist = NULL;
223 ofiles = ol;
224 fprintf(stderr,"Unable to open file %s\n", tmppath);
225 return FALSE;
230 * Create header for file.
232 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
233 ol->path = xstrdup(sourcefile);
234 ol->real_path = xstrdup(tmppath);
235 ol->next = ofiles;
236 ol->nlines = 0;
237 ol->linelist = NULL;
238 ol->size = statbuf.st_size;
239 ofiles = ol;
242 * Now open and map the file.
244 fd = open(tmppath, O_RDONLY);
245 if( fd == -1 )
247 return FALSE;
250 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
251 if( addr == (char *) -1 )
253 return FALSE;
257 * Now build up the line number mapping table.
259 ol->nlines = 1;
260 pnt = addr;
261 while(pnt < addr + ol->size )
263 if( *pnt++ == '\n' )
265 ol->nlines++;
269 ol->nlines++;
270 ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
272 nlines = 0;
273 pnt = addr;
274 ol->linelist[nlines++] = 0;
275 while(pnt < addr + ol->size )
277 if( *pnt++ == '\n' )
279 ol->linelist[nlines++] = pnt - addr;
282 ol->linelist[nlines++] = pnt - addr;
285 else
288 * We know what the file is, we just need to reopen it and remap it.
290 fd = open(ol->real_path, O_RDONLY);
291 if( fd == -1 )
293 return FALSE;
296 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
297 if( addr == (char *) -1 )
299 return FALSE;
304 * All we need to do is to display the source lines here.
306 rtn = FALSE;
307 for(i=start - 1; i <= end - 1; i++)
309 if( i < 0 || i >= ol->nlines - 1)
311 continue;
314 rtn = TRUE;
315 memset(&buffer, 0, sizeof(buffer));
316 if( ol->linelist[i+1] != ol->linelist[i] )
318 memcpy(&buffer, addr + ol->linelist[i],
319 (ol->linelist[i+1] - ol->linelist[i]) - 1);
321 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
324 munmap(addr, ol->size);
325 close(fd);
327 return rtn;
331 void
332 DEBUG_List(struct list_id * source1, struct list_id * source2,
333 int delta)
335 int end;
336 int rtn;
337 int start;
338 char * sourcefile;
341 * We need to see what source file we need. Hopefully we only have
342 * one specified, otherwise we might as well punt.
344 if( source1 != NULL
345 && source2 != NULL
346 && source1->sourcefile != NULL
347 && source2->sourcefile != NULL
348 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
350 fprintf(stderr, "Ambiguous source file specification.\n");
351 return;
354 sourcefile = NULL;
355 if( source1 != NULL && source1->sourcefile != NULL )
357 sourcefile = source1->sourcefile;
360 if( sourcefile == NULL
361 && source2 != NULL
362 && source2->sourcefile != NULL )
364 sourcefile = source2->sourcefile;
367 if( sourcefile == NULL )
369 sourcefile = (char *) &DEBUG_current_sourcefile;
372 if( sourcefile == NULL )
374 fprintf(stderr, "No source file specified.\n");
375 return;
379 * Now figure out the line number range to be listed.
381 start = -1;
382 end = -1;
384 if( source1 != NULL )
386 start = source1->line;
389 if( source2 != NULL )
391 end = source2->line;
394 if( start == -1 && end == -1 )
396 if( delta < 0 )
398 end = DEBUG_start_sourceline;
399 start = end + delta;
401 else
403 start = DEBUG_end_sourceline;
404 end = start + delta;
407 else if( start == -1 )
409 start = end + delta;
411 else if (end == -1)
413 end = start + delta;
417 * Now call this function to do the dirty work.
419 rtn = DEBUG_DisplaySource(sourcefile, start, end);
421 if( sourcefile != (char *) &DEBUG_current_sourcefile )
423 strcpy(DEBUG_current_sourcefile, sourcefile);
425 DEBUG_start_sourceline = start;
426 DEBUG_end_sourceline = end;
429 DBG_ADDR DEBUG_LastDisassemble={NULL,0,0};
431 static int
432 _disassemble(DBG_ADDR *addr)
434 DEBUG_PrintAddress( addr, dbg_mode, TRUE );
435 fprintf(stderr,": ");
436 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
437 DEBUG_Disasm( addr, TRUE );
438 fprintf(stderr,"\n");
439 return 1;
442 void
443 _disassemble_fixaddr(DBG_ADDR *addr) {
444 DWORD seg2;
445 struct datatype *testtype;
447 DBG_FIX_ADDR_SEG(addr,CS_reg(&DEBUG_context));
448 if( addr->type != NULL )
450 if( addr->type == DEBUG_TypeIntConst )
453 * We know that we have the actual offset stored somewhere
454 * else in 32-bit space. Grab it, and we
455 * should be all set.
457 seg2 = addr->seg;
458 addr->seg = 0;
459 addr->off = DEBUG_GetExprValue(addr, NULL);
460 addr->seg = seg2;
462 else
464 if (!DBG_CHECK_READ_PTR( addr, 1 )) return;
465 DEBUG_TypeDerefPointer(addr, &testtype);
466 if( testtype != NULL || addr->type == DEBUG_TypeIntConst )
467 addr->off = DEBUG_GetExprValue(addr, NULL);
470 else if (!addr->seg && !addr->off)
472 fprintf(stderr,"Invalid expression\n");
473 return;
477 void
478 DEBUG_Disassemble(const DBG_ADDR *xstart,const DBG_ADDR *xend,int offset)
480 int i;
481 DBG_ADDR last;
482 DBG_ADDR end,start;
484 if (xstart) {
485 start=*xstart;
486 _disassemble_fixaddr(&start);
488 if (xend) {
489 end=*xend;
490 _disassemble_fixaddr(&end);
492 if (!xstart && !xend) {
493 last = DEBUG_LastDisassemble;
494 if (!last.seg && !last.off)
496 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
497 last.seg = CS_reg(&DEBUG_context);
498 last.off = EIP_reg(&DEBUG_context);
499 if (ISV86(&DEBUG_context)) last.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16; else
500 if (IS_SELECTOR_SYSTEM(last.seg)) last.seg = 0;
501 GlobalUnlock16( GetCurrentTask() );
503 for (i=0;i<offset;i++)
504 if (!_disassemble(&last)) break;
505 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
506 return;
508 last = start;
509 if (!xend) {
510 for (i=0;i<offset;i++)
511 if (!_disassemble(&last)) break;
512 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
513 return;
515 while (last.off <= end.off)
516 if (!_disassemble(&last)) break;
517 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
518 return;
523 #if 0
524 main()
526 int i, j;
527 DEBUG_AddPath("../../de");
528 while(1==1)
530 fscanf(stdin,"%d %d", &i, &j);
531 DEBUG_DisplaySource("dumpexe.c", i, j);
533 return 0;
535 #endif