Release 970509
[wine/multimedia.git] / debugger / source.c
blob56b686a49fce4637d4989b4ae68d68b79f22dbd3
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 "xmalloc.h"
29 struct searchlist
31 char * path;
32 struct searchlist * next;
36 struct open_filelist
38 char * path;
39 char * real_path;
40 struct open_filelist * next;
41 unsigned int size;
42 signed int nlines;
43 unsigned int * linelist;
46 static struct open_filelist * ofiles;
48 static struct searchlist * listhead;
49 static char DEBUG_current_sourcefile[PATH_MAX];
50 static int DEBUG_start_sourceline = -1;
51 static int DEBUG_end_sourceline = -1;
53 void
54 DEBUG_ShowDir()
56 struct searchlist * sl;
58 fprintf(stderr,"Search list :\n");
59 for(sl = listhead; sl; sl = sl->next)
61 fprintf(stderr, "\t%s\n", sl->path);
63 fprintf(stderr, "\n");
66 void
67 DEBUG_AddPath(const char * path)
69 struct searchlist * sl;
71 sl = (struct searchlist *) xmalloc(sizeof(struct searchlist));
72 if( sl == NULL )
74 return;
77 sl->next = listhead;
78 sl->path = xstrdup(path);
79 listhead = sl;
82 void
83 DEBUG_NukePath()
85 struct searchlist * sl;
86 struct searchlist * nxt;
88 for(sl = listhead; sl; sl = nxt)
90 nxt = sl->next;
91 free(sl->path);
92 free(sl);
95 listhead = NULL;
98 static
99 int
100 DEBUG_DisplaySource(char * sourcefile, int start, int end)
102 char * addr;
103 char buffer[1024];
104 int fd;
105 int i;
106 struct open_filelist * ol;
107 int nlines;
108 char * pnt;
109 int rtn;
110 struct searchlist * sl;
111 struct stat statbuf;
112 int status;
113 char tmppath[PATH_MAX];
116 * First see whether we have the file open already. If so, then
117 * use that, otherwise we have to try and open it.
119 for(ol = ofiles; ol; ol = ol->next)
121 if( strcmp(ol->path, sourcefile) == 0 )
123 break;
127 if( ol == NULL )
130 * Try again, stripping the path from the opened file.
132 for(ol = ofiles; ol; ol = ol->next)
134 pnt = strrchr(ol->path, '/');
135 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
137 break;
143 if( ol == NULL )
146 * See if this is a DOS style name or not.
148 pnt = strchr(sourcefile, '\\' );
149 if( pnt == NULL )
151 pnt = strchr(sourcefile, '/' );
152 if( pnt == NULL )
154 pnt = sourcefile;
159 * Crapola. We need to try and open the file.
161 status = stat(sourcefile, &statbuf);
162 if( status != -1 )
164 strcpy(tmppath, sourcefile);
166 else
168 for(sl = listhead; sl; sl = sl->next)
170 strcpy(tmppath, sl->path);
171 if( tmppath[strlen(tmppath)-1] != '/' )
173 strcat(tmppath, "/");
176 * Now append the base file name.
178 strcat(tmppath, pnt);
180 status = stat(tmppath, &statbuf);
181 if( status != -1 )
183 break;
187 if( sl == NULL )
190 * Still couldn't find it. Ask user for path to add.
192 fprintf(stderr,"Enter path to file %s: ", sourcefile);
193 fgets(tmppath, sizeof(tmppath), stdin);
195 if( tmppath[strlen(tmppath)-1] == '\n' )
197 tmppath[strlen(tmppath)-1] = '\0';
200 if( tmppath[strlen(tmppath)-1] != '/' )
202 strcat(tmppath, "/");
205 * Now append the base file name.
207 strcat(tmppath, pnt);
209 status = stat(tmppath, &statbuf);
210 if( status == -1 )
213 * OK, I guess the user doesn't really want to see it
214 * after all.
216 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
217 ol->path = xstrdup(sourcefile);
218 ol->real_path = NULL;
219 ol->next = ofiles;
220 ol->nlines = 0;
221 ol->linelist = NULL;
222 ofiles = ol;
223 fprintf(stderr,"Unable to open file %s\n", tmppath);
224 return FALSE;
229 * Create header for file.
231 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
232 ol->path = xstrdup(sourcefile);
233 ol->real_path = xstrdup(tmppath);
234 ol->next = ofiles;
235 ol->nlines = 0;
236 ol->linelist = NULL;
237 ol->size = statbuf.st_size;
238 ofiles = ol;
241 * Now open and map the file.
243 fd = open(tmppath, O_RDONLY);
244 if( fd == -1 )
246 return FALSE;
249 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
250 if( addr == (char *) -1 )
252 return FALSE;
256 * Now build up the line number mapping table.
258 ol->nlines = 1;
259 pnt = addr;
260 while(pnt < addr + ol->size )
262 if( *pnt++ == '\n' )
264 ol->nlines++;
268 ol->nlines++;
269 ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
271 nlines = 0;
272 pnt = addr;
273 ol->linelist[nlines++] = 0;
274 while(pnt < addr + ol->size )
276 if( *pnt++ == '\n' )
278 ol->linelist[nlines++] = pnt - addr;
281 ol->linelist[nlines++] = pnt - addr;
284 else
287 * We know what the file is, we just need to reopen it and remap it.
289 fd = open(ol->real_path, O_RDONLY);
290 if( fd == -1 )
292 return FALSE;
295 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
296 if( addr == (char *) -1 )
298 return FALSE;
303 * All we need to do is to display the source lines here.
305 rtn = FALSE;
306 for(i=start - 1; i <= end - 1; i++)
308 if( i < 0 || i >= ol->nlines - 1)
310 continue;
313 rtn = TRUE;
314 memset(&buffer, 0, sizeof(buffer));
315 if( ol->linelist[i+1] != ol->linelist[i] )
317 memcpy(&buffer, addr + ol->linelist[i],
318 (ol->linelist[i+1] - ol->linelist[i]) - 1);
320 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
323 munmap(addr, ol->size);
324 close(fd);
326 return rtn;
330 void
331 DEBUG_List(struct list_id * source1, struct list_id * source2,
332 int delta)
334 int end;
335 int rtn;
336 int start;
337 char * sourcefile;
340 * We need to see what source file we need. Hopefully we only have
341 * one specified, otherwise we might as well punt.
343 if( source1 != NULL
344 && source2 != NULL
345 && source1->sourcefile != NULL
346 && source2->sourcefile != NULL
347 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
349 fprintf(stderr, "Ambiguous source file specification.\n");
350 return;
353 sourcefile = NULL;
354 if( source1 != NULL && source1->sourcefile != NULL )
356 sourcefile = source1->sourcefile;
359 if( sourcefile == NULL
360 && source2 != NULL
361 && source2->sourcefile != NULL )
363 sourcefile = source2->sourcefile;
366 if( sourcefile == NULL )
368 sourcefile = (char *) &DEBUG_current_sourcefile;
371 if( sourcefile == NULL )
373 fprintf(stderr, "No source file specified.\n");
374 return;
378 * Now figure out the line number range to be listed.
380 start = -1;
381 end = -1;
383 if( source1 != NULL )
385 start = source1->line;
388 if( source2 != NULL )
390 end = source2->line;
393 if( start == -1 && end == -1 )
395 if( delta < 0 )
397 end = DEBUG_start_sourceline;
398 start = end + delta;
400 else
402 start = DEBUG_end_sourceline;
403 end = start + delta;
406 else if( start == -1 )
408 start = end + delta;
410 else if (end == -1)
412 end = start + delta;
416 * Now call this function to do the dirty work.
418 rtn = DEBUG_DisplaySource(sourcefile, start, end);
420 if( sourcefile != (char *) &DEBUG_current_sourcefile )
422 strcpy(DEBUG_current_sourcefile, sourcefile);
424 DEBUG_start_sourceline = start;
425 DEBUG_end_sourceline = end;
428 DBG_ADDR DEBUG_LastDisassemble={NULL,0,0};
430 static int
431 _disassemble(DBG_ADDR *addr)
433 DEBUG_PrintAddress( addr, dbg_mode, TRUE );
434 fprintf(stderr,": ");
435 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
436 DEBUG_Disasm( addr, TRUE );
437 fprintf(stderr,"\n");
438 return 1;
441 void
442 _disassemble_fixaddr(DBG_ADDR *addr) {
443 DWORD seg2;
444 struct datatype *testtype;
446 DBG_FIX_ADDR_SEG(addr,CS_reg(&DEBUG_context));
447 if( addr->type != NULL )
449 if( addr->type == DEBUG_TypeIntConst )
452 * We know that we have the actual offset stored somewhere
453 * else in 32-bit space. Grab it, and we
454 * should be all set.
456 seg2 = addr->seg;
457 addr->seg = 0;
458 addr->off = DEBUG_GetExprValue(addr, NULL);
459 addr->seg = seg2;
461 else
463 if (!DBG_CHECK_READ_PTR( addr, 1 )) return;
464 DEBUG_TypeDerefPointer(addr, &testtype);
465 if( testtype != NULL || addr->type == DEBUG_TypeIntConst )
466 addr->off = DEBUG_GetExprValue(addr, NULL);
469 else if (!addr->seg && !addr->off)
471 fprintf(stderr,"Invalid expression\n");
472 return;
476 void
477 DEBUG_Disassemble(const DBG_ADDR *xstart,const DBG_ADDR *xend,int offset)
479 int i;
480 DBG_ADDR last;
481 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) {
495 last.seg = (CS_reg(&DEBUG_context)==WINE_CODE_SELECTOR)?0:CS_reg(&DEBUG_context);
496 last.off = EIP_reg(&DEBUG_context);
498 for (i=0;i<offset;i++)
499 if (!_disassemble(&last)) break;
500 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
501 return;
503 last = start;
504 if (!xend) {
505 for (i=0;i<offset;i++)
506 if (!_disassemble(&last)) break;
507 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
508 return;
510 while (last.off <= end.off)
511 if (!_disassemble(&last)) break;
512 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
513 return;
518 #if 0
519 main()
521 int i, j;
522 DEBUG_AddPath("../../de");
523 while(1==1)
525 fscanf(stdin,"%d %d", &i, &j);
526 DEBUG_DisplaySource("dumpexe.c", i, j);
528 return 0;
530 #endif