2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
11 #include <sys/types.h>
19 #define PATH_MAX _MAX_PATH
33 struct searchlist * next;
41 struct open_filelist * next;
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;
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");
68 DEBUG_AddPath(const char * path)
70 struct searchlist * sl;
72 sl = (struct searchlist *) xmalloc(sizeof(struct searchlist));
79 sl->path = xstrdup(path);
86 struct searchlist * sl;
87 struct searchlist * nxt;
89 for(sl = listhead; sl; sl = nxt)
101 DEBUG_DisplaySource(char * sourcefile, int start, int end)
107 struct open_filelist * ol;
111 struct searchlist * sl;
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 )
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 )
147 * See if this is a DOS style name or not.
149 pnt = strchr(sourcefile, '\\' );
152 pnt = strchr(sourcefile, '/' );
160 * Crapola. We need to try and open the file.
162 status = stat(sourcefile, &statbuf);
165 strcpy(tmppath, sourcefile);
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);
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);
214 * OK, I guess the user doesn't really want to see it
217 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
218 ol->path = xstrdup(sourcefile);
219 ol->real_path = NULL;
224 fprintf(stderr,"Unable to open file %s\n", tmppath);
230 * Create header for file.
232 ol = (struct open_filelist *) xmalloc(sizeof(*ol));
233 ol->path = xstrdup(sourcefile);
234 ol->real_path = xstrdup(tmppath);
238 ol->size = statbuf.st_size;
242 * Now open and map the file.
244 fd = open(tmppath, O_RDONLY);
250 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
251 if( addr == (char *) -1 )
257 * Now build up the line number mapping table.
261 while(pnt < addr + ol->size )
270 ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
274 ol->linelist[nlines++] = 0;
275 while(pnt < addr + ol->size )
279 ol->linelist[nlines++] = pnt - addr;
282 ol->linelist[nlines++] = pnt - addr;
288 * We know what the file is, we just need to reopen it and remap it.
290 fd = open(ol->real_path, O_RDONLY);
296 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
297 if( addr == (char *) -1 )
304 * All we need to do is to display the source lines here.
307 for(i=start - 1; i <= end - 1; i++)
309 if( i < 0 || i >= ol->nlines - 1)
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);
332 DEBUG_List(struct list_id * source1, struct list_id * source2,
341 * We need to see what source file we need. Hopefully we only have
342 * one specified, otherwise we might as well punt.
346 && source1->sourcefile != NULL
347 && source2->sourcefile != NULL
348 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
350 fprintf(stderr, "Ambiguous source file specification.\n");
355 if( source1 != NULL && source1->sourcefile != NULL )
357 sourcefile = source1->sourcefile;
360 if( sourcefile == 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");
379 * Now figure out the line number range to be listed.
384 if( source1 != NULL )
386 start = source1->line;
389 if( source2 != NULL )
394 if( start == -1 && end == -1 )
398 end = DEBUG_start_sourceline;
403 start = DEBUG_end_sourceline;
407 else if( start == -1 )
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};
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");
443 _disassemble_fixaddr(DBG_ADDR *addr) {
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
459 addr->off = DEBUG_GetExprValue(addr, NULL);
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");
478 DEBUG_Disassemble(const DBG_ADDR *xstart,const DBG_ADDR *xend,int offset)
486 _disassemble_fixaddr(&start);
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));
510 for (i=0;i<offset;i++)
511 if (!_disassemble(&last)) break;
512 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
515 while (last.off <= end.off)
516 if (!_disassemble(&last)) break;
517 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
527 DEBUG_AddPath("../../de");
530 fscanf(stdin,"%d %d", &i, &j);
531 DEBUG_DisplaySource("dumpexe.c", i, j);