Remove the last remaining wine options when running programs.
[wine/hacks.git] / programs / winedbg / source.c
blobe6b43453c2213519c40d2f9d231026ef09d079d3
1 /*
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
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <limits.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #ifndef PATH_MAX
37 #define PATH_MAX MAX_PATH
38 #endif
40 #include "debugger.h"
42 struct searchlist
44 char * path;
45 struct searchlist * next;
49 struct open_filelist
51 char * path;
52 char * real_path;
53 struct open_filelist * next;
54 unsigned int size;
55 signed int nlines;
56 unsigned int * linelist;
59 static struct open_filelist * ofiles;
61 static struct searchlist * listhead;
62 static char DEBUG_current_sourcefile[PATH_MAX];
63 static int DEBUG_start_sourceline = -1;
64 static int DEBUG_end_sourceline = -1;
66 void
67 DEBUG_ShowDir(void)
69 struct searchlist * sl;
71 DEBUG_Printf("Search list :\n");
72 for(sl = listhead; sl; sl = sl->next)
74 DEBUG_Printf("\t%s\n", sl->path);
76 DEBUG_Printf("\n");
79 void
80 DEBUG_AddPath(const char * path)
82 struct searchlist * sl;
84 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
85 if( sl == NULL )
87 return;
90 sl->next = listhead;
91 sl->path = DBG_strdup(path);
92 listhead = sl;
95 void
96 DEBUG_NukePath(void)
98 struct searchlist * sl;
99 struct searchlist * nxt;
101 for(sl = listhead; sl; sl = nxt)
103 nxt = sl->next;
104 DBG_free(sl->path);
105 DBG_free(sl);
108 listhead = NULL;
111 static void* DEBUG_MapFile(const char* name, HANDLE* hMap, unsigned* size)
113 HANDLE hFile;
115 hFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
116 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
117 if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
118 if (size != NULL && (*size = GetFileSize(hFile, NULL)) == -1) return (void*)-1;
119 *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
120 CloseHandle(hFile);
121 if (!*hMap) return (void*)-1;
122 return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
125 static void DEBUG_UnmapFile(void* addr, HANDLE hMap)
127 UnmapViewOfFile(addr);
128 CloseHandle(hMap);
131 static struct open_filelist* DEBUG_SearchOpenFile(const char* name)
133 struct open_filelist* ol;
135 for (ol = ofiles; ol; ol = ol->next)
137 if (strcmp(ol->path, name) == 0) break;
139 return ol;
142 static
144 DEBUG_DisplaySource(char * sourcefile, int start, int end)
146 char* addr;
147 int i;
148 struct open_filelist* ol;
149 int nlines;
150 char* basename = NULL;
151 char* pnt;
152 int rtn;
153 struct searchlist* sl;
154 HANDLE hMap;
155 DWORD status;
156 char tmppath[PATH_MAX];
159 * First see whether we have the file open already. If so, then
160 * use that, otherwise we have to try and open it.
162 ol = DEBUG_SearchOpenFile(sourcefile);
164 if ( ol == NULL )
167 * Try again, stripping the path from the opened file.
169 basename = strrchr(sourcefile, '\\' );
170 if ( !basename )
171 basename = strrchr(sourcefile, '/' );
172 if ( !basename )
173 basename = sourcefile;
174 else
175 basename++;
177 ol = DEBUG_SearchOpenFile(basename);
180 if ( ol == NULL )
183 * Crapola. We need to try and open the file.
185 status = GetFileAttributes(sourcefile);
186 if ( status != INVALID_FILE_ATTRIBUTES )
188 strcpy(tmppath, sourcefile);
190 else if ( (status = GetFileAttributes(basename)) != INVALID_FILE_ATTRIBUTES )
192 strcpy(tmppath, basename);
194 else
196 for (sl = listhead; sl; sl = sl->next)
198 strcpy(tmppath, sl->path);
199 if ( tmppath[strlen(tmppath)-1] != '/' && tmppath[strlen(tmppath)-1] != '\\' )
201 strcat(tmppath, "/");
204 * Now append the base file name.
206 strcat(tmppath, basename);
208 status = GetFileAttributes(tmppath);
209 if ( status != INVALID_FILE_ATTRIBUTES ) break;
212 if ( sl == NULL )
214 if (DEBUG_InteractiveP)
216 char zbuf[256];
218 * Still couldn't find it. Ask user for path to add.
220 snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
221 DEBUG_ReadLine(zbuf, tmppath, sizeof(tmppath));
223 if ( tmppath[strlen(tmppath)-1] != '/' )
225 strcat(tmppath, "/");
228 * Now append the base file name.
230 strcat(tmppath, basename);
232 status = GetFileAttributes(tmppath);
234 else
236 status = INVALID_FILE_ATTRIBUTES;
237 strcpy(tmppath, sourcefile);
240 if ( status == INVALID_FILE_ATTRIBUTES )
243 * OK, I guess the user doesn't really want to see it
244 * after all.
246 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
247 ol->path = DBG_strdup(sourcefile);
248 ol->real_path = NULL;
249 ol->next = ofiles;
250 ol->nlines = 0;
251 ol->linelist = NULL;
252 ofiles = ol;
253 DEBUG_Printf("Unable to open file %s\n", tmppath);
254 return FALSE;
259 * Create header for file.
261 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
262 ol->path = DBG_strdup(sourcefile);
263 ol->real_path = DBG_strdup(tmppath);
264 ol->next = ofiles;
265 ol->nlines = 0;
266 ol->linelist = NULL;
267 ol->size = 0;
268 ofiles = ol;
270 addr = DEBUG_MapFile(tmppath, &hMap, &ol->size);
271 if ( addr == (char *) -1 )
273 return FALSE;
276 * Now build up the line number mapping table.
278 ol->nlines = 1;
279 pnt = addr;
280 while (pnt < addr + ol->size )
282 if ( *pnt++ == '\n' )
284 ol->nlines++;
288 ol->nlines++;
289 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
291 nlines = 0;
292 pnt = addr;
293 ol->linelist[nlines++] = 0;
294 while(pnt < addr + ol->size )
296 if( *pnt++ == '\n' )
298 ol->linelist[nlines++] = pnt - addr;
301 ol->linelist[nlines++] = pnt - addr;
304 else
306 addr = DEBUG_MapFile(ol->real_path, &hMap, NULL);
307 if ( addr == (char *) -1 )
309 return FALSE;
313 * All we need to do is to display the source lines here.
315 rtn = FALSE;
316 for (i = start - 1; i <= end - 1; i++)
318 char buffer[1024];
320 if (i < 0 || i >= ol->nlines - 1)
322 continue;
325 rtn = TRUE;
326 memset(&buffer, 0, sizeof(buffer));
327 if ( ol->linelist[i+1] != ol->linelist[i] )
329 memcpy(&buffer, addr + ol->linelist[i],
330 (ol->linelist[i+1] - ol->linelist[i]) - 1);
332 DEBUG_Printf("%d\t%s\n", i + 1, buffer);
335 DEBUG_UnmapFile(addr, hMap);
336 return rtn;
339 void
340 DEBUG_List(struct list_id * source1, struct list_id * source2,
341 int delta)
343 int end;
344 int rtn;
345 int start;
346 char * sourcefile;
349 * We need to see what source file we need. Hopefully we only have
350 * one specified, otherwise we might as well punt.
352 if( source1 != NULL
353 && source2 != NULL
354 && source1->sourcefile != NULL
355 && source2->sourcefile != NULL
356 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
358 DEBUG_Printf("Ambiguous source file specification.\n");
359 return;
362 sourcefile = NULL;
363 if( source1 != NULL && source1->sourcefile != NULL )
365 sourcefile = source1->sourcefile;
368 if( sourcefile == NULL
369 && source2 != NULL
370 && source2->sourcefile != NULL )
372 sourcefile = source2->sourcefile;
375 if( sourcefile == NULL )
377 sourcefile = (char *) &DEBUG_current_sourcefile;
380 if( sourcefile == NULL )
382 DEBUG_Printf("No source file specified.\n");
383 return;
387 * Now figure out the line number range to be listed.
389 start = -1;
390 end = -1;
392 if( source1 != NULL )
394 start = source1->line;
397 if( source2 != NULL )
399 end = source2->line;
402 if( start == -1 && end == -1 )
404 if( delta < 0 )
406 end = DEBUG_start_sourceline;
407 start = end + delta;
409 else
411 start = DEBUG_end_sourceline;
412 end = start + delta;
415 else if( start == -1 )
417 start = end + delta;
419 else if (end == -1)
421 end = start + delta;
425 * Now call this function to do the dirty work.
427 rtn = DEBUG_DisplaySource(sourcefile, start, end);
429 if( sourcefile != (char *) &DEBUG_current_sourcefile )
431 strcpy(DEBUG_current_sourcefile, sourcefile);
433 DEBUG_start_sourceline = start;
434 DEBUG_end_sourceline = end;
437 DBG_ADDR DEBUG_LastDisassemble={0,0};
439 BOOL DEBUG_DisassembleInstruction(DBG_ADDR *addr)
441 char ch;
442 BOOL ret = TRUE;
444 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, TRUE);
445 DEBUG_Printf(": ");
446 if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear(addr), &ch, sizeof(ch))) {
447 DEBUG_Printf("-- no code --");
448 ret = FALSE;
449 } else {
450 DEBUG_Disasm(addr, TRUE);
452 DEBUG_Printf("\n");
453 return ret;
456 void
457 DEBUG_Disassemble(const DBG_VALUE *xstart,const DBG_VALUE *xend,int offset)
459 int i;
460 DBG_ADDR last;
461 DBG_VALUE end,start;
463 if (xstart) {
464 start = *xstart;
465 DEBUG_GrabAddress(&start, TRUE);
467 if (xend) {
468 end = *xend;
469 DEBUG_GrabAddress(&end, TRUE);
471 if (!xstart && !xend) {
472 last = DEBUG_LastDisassemble;
473 if (!last.seg && !last.off)
474 DEBUG_GetCurrentAddress( &last );
476 for (i=0;i<offset;i++)
477 if (!DEBUG_DisassembleInstruction(&last)) break;
478 DEBUG_LastDisassemble = last;
479 return;
481 last = start.addr;
482 if (!xend) {
483 for (i=0;i<offset;i++)
484 if (!DEBUG_DisassembleInstruction(&last)) break;
485 DEBUG_LastDisassemble = last;
486 return;
488 while (last.off <= end.addr.off)
489 if (!DEBUG_DisassembleInstruction(&last)) break;
490 DEBUG_LastDisassemble = last;
491 return;