2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
35 #define PATH_MAX MAX_PATH
43 struct searchlist * next;
51 struct open_filelist * next;
54 unsigned int * linelist;
57 static struct open_filelist * ofiles;
59 static struct searchlist * listhead;
60 static char DEBUG_current_sourcefile[PATH_MAX];
61 static int DEBUG_start_sourceline = -1;
62 static int DEBUG_end_sourceline = -1;
67 struct searchlist * sl;
69 DEBUG_Printf(DBG_CHN_MESG,"Search list :\n");
70 for(sl = listhead; sl; sl = sl->next)
72 DEBUG_Printf(DBG_CHN_MESG, "\t%s\n", sl->path);
74 DEBUG_Printf(DBG_CHN_MESG, "\n");
78 DEBUG_AddPath(const char * path)
80 struct searchlist * sl;
82 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
89 sl->path = DBG_strdup(path);
96 struct searchlist * sl;
97 struct searchlist * nxt;
99 for(sl = listhead; sl; sl = nxt)
109 static void* DEBUG_MapFile(const char* name, HANDLE* hMap, unsigned* size)
113 hFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
114 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
115 if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
116 if (size != NULL && (*size = GetFileSize(hFile, NULL)) == -1) return (void*)-1;
117 *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
119 if (!*hMap) return (void*)-1;
120 return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
123 static void DEBUG_UnmapFile(void* addr, HANDLE hMap)
125 UnmapViewOfFile(addr);
129 static struct open_filelist* DEBUG_SearchOpenFile(const char* name)
131 struct open_filelist* ol;
133 for (ol = ofiles; ol; ol = ol->next)
135 if (strcmp(ol->path, name) == 0) break;
142 DEBUG_DisplaySource(char * sourcefile, int start, int end)
146 struct open_filelist* ol;
148 char* basename = NULL;
151 struct searchlist* sl;
154 char tmppath[PATH_MAX];
157 * First see whether we have the file open already. If so, then
158 * use that, otherwise we have to try and open it.
160 ol = DEBUG_SearchOpenFile(sourcefile);
165 * Try again, stripping the path from the opened file.
167 basename = strrchr(sourcefile, '\\' );
169 basename = strrchr(sourcefile, '/' );
171 basename = sourcefile;
175 ol = DEBUG_SearchOpenFile(basename);
181 * Crapola. We need to try and open the file.
183 status = GetFileAttributes(sourcefile);
186 strcpy(tmppath, sourcefile);
188 else if ( (status = GetFileAttributes(basename)) != -1 )
190 strcpy(tmppath, basename);
194 for (sl = listhead; sl; sl = sl->next)
196 strcpy(tmppath, sl->path);
197 if ( tmppath[strlen(tmppath)-1] != '/' && tmppath[strlen(tmppath)-1] != '\\' )
199 strcat(tmppath, "/");
202 * Now append the base file name.
204 strcat(tmppath, basename);
206 status = GetFileAttributes(tmppath);
207 if ( status != -1 ) break;
212 if (DEBUG_InteractiveP)
216 * Still couldn't find it. Ask user for path to add.
218 sprintf(zbuf, "Enter path to file '%s': ", sourcefile);
219 DEBUG_ReadLine(zbuf, tmppath, sizeof(tmppath));
221 if ( tmppath[strlen(tmppath)-1] != '/' )
223 strcat(tmppath, "/");
226 * Now append the base file name.
228 strcat(tmppath, basename);
230 status = GetFileAttributes(tmppath);
235 strcpy(tmppath, sourcefile);
241 * OK, I guess the user doesn't really want to see it
244 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
245 ol->path = DBG_strdup(sourcefile);
246 ol->real_path = NULL;
251 DEBUG_Printf(DBG_CHN_MESG,"Unable to open file %s\n", tmppath);
257 * Create header for file.
259 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
260 ol->path = DBG_strdup(sourcefile);
261 ol->real_path = DBG_strdup(tmppath);
268 addr = DEBUG_MapFile(tmppath, &hMap, &ol->size);
269 if ( addr == (char *) -1 )
274 * Now build up the line number mapping table.
278 while (pnt < addr + ol->size )
280 if ( *pnt++ == '\n' )
287 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
291 ol->linelist[nlines++] = 0;
292 while(pnt < addr + ol->size )
296 ol->linelist[nlines++] = pnt - addr;
299 ol->linelist[nlines++] = pnt - addr;
304 addr = DEBUG_MapFile(ol->real_path, &hMap, NULL);
305 if ( addr == (char *) -1 )
311 * All we need to do is to display the source lines here.
314 for (i = start - 1; i <= end - 1; i++)
318 if (i < 0 || i >= ol->nlines - 1)
324 memset(&buffer, 0, sizeof(buffer));
325 if ( ol->linelist[i+1] != ol->linelist[i] )
327 memcpy(&buffer, addr + ol->linelist[i],
328 (ol->linelist[i+1] - ol->linelist[i]) - 1);
330 DEBUG_Printf(DBG_CHN_MESG,"%d\t%s\n", i + 1, buffer);
333 DEBUG_UnmapFile(addr, hMap);
338 DEBUG_List(struct list_id * source1, struct list_id * source2,
347 * We need to see what source file we need. Hopefully we only have
348 * one specified, otherwise we might as well punt.
352 && source1->sourcefile != NULL
353 && source2->sourcefile != NULL
354 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
356 DEBUG_Printf(DBG_CHN_MESG, "Ambiguous source file specification.\n");
361 if( source1 != NULL && source1->sourcefile != NULL )
363 sourcefile = source1->sourcefile;
366 if( sourcefile == NULL
368 && source2->sourcefile != NULL )
370 sourcefile = source2->sourcefile;
373 if( sourcefile == NULL )
375 sourcefile = (char *) &DEBUG_current_sourcefile;
378 if( sourcefile == NULL )
380 DEBUG_Printf(DBG_CHN_MESG, "No source file specified.\n");
385 * Now figure out the line number range to be listed.
390 if( source1 != NULL )
392 start = source1->line;
395 if( source2 != NULL )
400 if( start == -1 && end == -1 )
404 end = DEBUG_start_sourceline;
409 start = DEBUG_end_sourceline;
413 else if( start == -1 )
423 * Now call this function to do the dirty work.
425 rtn = DEBUG_DisplaySource(sourcefile, start, end);
427 if( sourcefile != (char *) &DEBUG_current_sourcefile )
429 strcpy(DEBUG_current_sourcefile, sourcefile);
431 DEBUG_start_sourceline = start;
432 DEBUG_end_sourceline = end;
435 DBG_ADDR DEBUG_LastDisassemble={0,0};
437 BOOL DEBUG_DisassembleInstruction(DBG_ADDR *addr)
442 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, TRUE);
443 DEBUG_Printf(DBG_CHN_MESG, ": ");
444 if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear(addr), &ch, sizeof(ch))) {
445 DEBUG_Printf(DBG_CHN_MESG, "-- no code --");
448 DEBUG_Disasm(addr, TRUE);
450 DEBUG_Printf(DBG_CHN_MESG,"\n");
455 DEBUG_Disassemble(const DBG_VALUE *xstart,const DBG_VALUE *xend,int offset)
463 DEBUG_GrabAddress(&start, TRUE);
467 DEBUG_GrabAddress(&end, TRUE);
469 if (!xstart && !xend) {
470 last = DEBUG_LastDisassemble;
471 if (!last.seg && !last.off)
472 DEBUG_GetCurrentAddress( &last );
474 for (i=0;i<offset;i++)
475 if (!DEBUG_DisassembleInstruction(&last)) break;
476 DEBUG_LastDisassemble = last;
481 for (i=0;i<offset;i++)
482 if (!DEBUG_DisassembleInstruction(&last)) break;
483 DEBUG_LastDisassemble = last;
486 while (last.off <= end.addr.off)
487 if (!DEBUG_DisassembleInstruction(&last)) break;
488 DEBUG_LastDisassemble = last;