2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
29 struct searchlist
* next
;
37 struct open_filelist
* next
;
40 unsigned int * linelist
;
43 static struct open_filelist
* ofiles
;
45 static struct searchlist
* listhead
;
46 static char DEBUG_current_sourcefile
[PATH_MAX
];
47 static int DEBUG_start_sourceline
= -1;
48 static int DEBUG_end_sourceline
= -1;
53 struct searchlist
* sl
;
55 fprintf(stderr
,"Search list :\n");
56 for(sl
= listhead
; sl
; sl
= sl
->next
)
58 fprintf(stderr
, "\t%s\n", sl
->path
);
60 fprintf(stderr
, "\n");
64 DEBUG_AddPath(const char * path
)
66 struct searchlist
* sl
;
68 sl
= (struct searchlist
*) xmalloc(sizeof(struct searchlist
));
75 sl
->path
= xstrdup(path
);
82 struct searchlist
* sl
;
83 struct searchlist
* nxt
;
85 for(sl
= listhead
; sl
; sl
= nxt
)
97 DEBUG_DisplaySource(char * sourcefile
, int start
, int end
)
103 struct open_filelist
* ol
;
107 struct searchlist
* sl
;
110 char tmppath
[PATH_MAX
];
113 * First see whether we have the file open already. If so, then
114 * use that, otherwise we have to try and open it.
116 for(ol
= ofiles
; ol
; ol
= ol
->next
)
118 if( strcmp(ol
->path
, sourcefile
) == 0 )
127 * Try again, stripping the path from the opened file.
129 for(ol
= ofiles
; ol
; ol
= ol
->next
)
131 pnt
= strrchr(ol
->path
, '/');
132 if( pnt
!= NULL
&& strcmp(pnt
+ 1, sourcefile
) == 0 )
143 * See if this is a DOS style name or not.
145 pnt
= strchr(sourcefile
, '\\' );
148 pnt
= strchr(sourcefile
, '/' );
156 * Crapola. We need to try and open the file.
158 status
= stat(sourcefile
, &statbuf
);
161 strcpy(tmppath
, sourcefile
);
165 for(sl
= listhead
; sl
; sl
= sl
->next
)
167 strcpy(tmppath
, sl
->path
);
168 if( tmppath
[strlen(tmppath
)-1] != '/' )
170 strcat(tmppath
, "/");
173 * Now append the base file name.
175 strcat(tmppath
, pnt
);
177 status
= stat(tmppath
, &statbuf
);
187 * Still couldn't find it. Ask user for path to add.
189 fprintf(stderr
,"Enter path to file %s: ", sourcefile
);
190 fgets(tmppath
, sizeof(tmppath
), stdin
);
192 if( tmppath
[strlen(tmppath
)-1] == '\n' )
194 tmppath
[strlen(tmppath
)-1] = '\0';
197 if( tmppath
[strlen(tmppath
)-1] != '/' )
199 strcat(tmppath
, "/");
202 * Now append the base file name.
204 strcat(tmppath
, pnt
);
206 status
= stat(tmppath
, &statbuf
);
210 * OK, I guess the user doesn't really want to see it
213 ol
= (struct open_filelist
*) xmalloc(sizeof(*ol
));
214 ol
->path
= xstrdup(sourcefile
);
215 ol
->real_path
= NULL
;
220 fprintf(stderr
,"Unable to open file %s\n", tmppath
);
226 * Create header for file.
228 ol
= (struct open_filelist
*) xmalloc(sizeof(*ol
));
229 ol
->path
= xstrdup(sourcefile
);
230 ol
->real_path
= xstrdup(tmppath
);
234 ol
->size
= statbuf
.st_size
;
238 * Now open and map the file.
240 fd
= open(tmppath
, O_RDONLY
);
246 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
247 if( addr
== (char *) -1 )
253 * Now build up the line number mapping table.
257 while(pnt
< addr
+ ol
->size
)
266 ol
->linelist
= (unsigned int*) xmalloc(ol
->nlines
* sizeof(unsigned int) );
270 ol
->linelist
[nlines
++] = 0;
271 while(pnt
< addr
+ ol
->size
)
275 ol
->linelist
[nlines
++] = pnt
- addr
;
278 ol
->linelist
[nlines
++] = pnt
- addr
;
284 * We know what the file is, we just need to reopen it and remap it.
286 fd
= open(ol
->real_path
, O_RDONLY
);
292 addr
= mmap(0, ol
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
293 if( addr
== (char *) -1 )
300 * All we need to do is to display the source lines here.
303 for(i
=start
- 1; i
<= end
- 1; i
++)
305 if( i
< 0 || i
>= ol
->nlines
- 1)
311 memset(&buffer
, 0, sizeof(buffer
));
312 if( ol
->linelist
[i
+1] != ol
->linelist
[i
] )
314 memcpy(&buffer
, addr
+ ol
->linelist
[i
],
315 (ol
->linelist
[i
+1] - ol
->linelist
[i
]) - 1);
317 fprintf(stderr
,"%d\t%s\n", i
+ 1, buffer
);
320 munmap(addr
, ol
->size
);
328 DEBUG_List(struct list_id
* source1
, struct list_id
* source2
,
337 * We need to see what source file we need. Hopefully we only have
338 * one specified, otherwise we might as well punt.
342 && source1
->sourcefile
!= NULL
343 && source2
->sourcefile
!= NULL
344 && strcmp(source1
->sourcefile
, source2
->sourcefile
) != 0 )
346 fprintf(stderr
, "Ambiguous source file specification.\n");
351 if( source1
!= NULL
&& source1
->sourcefile
!= NULL
)
353 sourcefile
= source1
->sourcefile
;
356 if( sourcefile
== NULL
358 && source2
->sourcefile
!= NULL
)
360 sourcefile
= source2
->sourcefile
;
363 if( sourcefile
== NULL
)
365 sourcefile
= (char *) &DEBUG_current_sourcefile
;
368 if( sourcefile
== NULL
)
370 fprintf(stderr
, "No source file specified.\n");
375 * Now figure out the line number range to be listed.
380 if( source1
!= NULL
)
382 start
= source1
->line
;
385 if( source2
!= NULL
)
390 if( start
== -1 && end
== -1 )
394 end
= DEBUG_start_sourceline
;
399 start
= DEBUG_end_sourceline
;
403 else if( start
== -1 )
413 * Now call this function to do the dirty work.
415 rtn
= DEBUG_DisplaySource(sourcefile
, start
, end
);
417 if( sourcefile
!= (char *) &DEBUG_current_sourcefile
)
419 strcpy(DEBUG_current_sourcefile
, sourcefile
);
421 DEBUG_start_sourceline
= start
;
422 DEBUG_end_sourceline
= end
;
431 DEBUG_AddPath("../../de");
434 fscanf(stdin
,"%d %d", &i
, &j
);
435 DEBUG_DisplaySource("dumpexe.c", i
, j
);