Added ability to turn on/off debug channels.
[wine.git] / debugger / source.c
blob3c382167b6087e0b796cabee4876267039632c4f
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 #include <unistd.h>
34 #ifndef PATH_MAX
35 #define PATH_MAX MAX_PATH
36 #endif
38 #include "debugger.h"
40 struct searchlist
42 char * path;
43 struct searchlist * next;
47 struct open_filelist
49 char * path;
50 char * real_path;
51 struct open_filelist * next;
52 unsigned int size;
53 signed int nlines;
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;
64 void
65 DEBUG_ShowDir(void)
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");
77 void
78 DEBUG_AddPath(const char * path)
80 struct searchlist * sl;
82 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
83 if( sl == NULL )
85 return;
88 sl->next = listhead;
89 sl->path = DBG_strdup(path);
90 listhead = sl;
93 void
94 DEBUG_NukePath(void)
96 struct searchlist * sl;
97 struct searchlist * nxt;
99 for(sl = listhead; sl; sl = nxt)
101 nxt = sl->next;
102 DBG_free(sl->path);
103 DBG_free(sl);
106 listhead = NULL;
109 static
111 DEBUG_DisplaySource(char * sourcefile, int start, int end)
113 char * addr;
114 char buffer[1024];
115 int fd;
116 int i;
117 struct open_filelist * ol;
118 int nlines;
119 char * basename = NULL;
120 char * pnt;
121 int rtn;
122 struct searchlist * sl;
123 struct stat statbuf;
124 int status;
125 char tmppath[PATH_MAX];
128 * First see whether we have the file open already. If so, then
129 * use that, otherwise we have to try and open it.
131 for(ol = ofiles; ol; ol = ol->next)
133 if( strcmp(ol->path, sourcefile) == 0 )
135 break;
139 if( ol == NULL )
142 * Try again, stripping the path from the opened file.
144 basename = strrchr(sourcefile, '\\' );
145 if ( !basename )
146 basename = strrchr(sourcefile, '/' );
147 if ( !basename )
148 basename = sourcefile;
149 else
150 basename++;
152 for(ol = ofiles; ol; ol = ol->next)
154 if( strcmp(ol->path, basename) == 0 )
156 break;
162 if( ol == NULL )
165 * Crapola. We need to try and open the file.
167 status = stat(sourcefile, &statbuf);
168 if( status != -1 )
170 strcpy(tmppath, sourcefile);
172 else if( (status = stat(basename, &statbuf)) != -1 )
174 strcpy(tmppath, basename);
176 else
178 for(sl = listhead; sl; sl = sl->next)
180 strcpy(tmppath, sl->path);
181 if( tmppath[strlen(tmppath)-1] != '/' )
183 strcat(tmppath, "/");
186 * Now append the base file name.
188 strcat(tmppath, basename);
190 status = stat(tmppath, &statbuf);
191 if( status != -1 )
193 break;
197 if( sl == NULL )
199 char zbuf[256];
201 * Still couldn't find it. Ask user for path to add.
203 sprintf(zbuf, "Enter path to file '%s': ", sourcefile);
204 DEBUG_ReadLine(zbuf, tmppath, sizeof(tmppath), FALSE, FALSE);
206 if( tmppath[strlen(tmppath)-1] == '\n' )
208 tmppath[strlen(tmppath)-1] = '\0';
211 if( tmppath[strlen(tmppath)-1] != '/' )
213 strcat(tmppath, "/");
216 * Now append the base file name.
218 strcat(tmppath, basename);
220 status = stat(tmppath, &statbuf);
221 if( status == -1 )
224 * OK, I guess the user doesn't really want to see it
225 * after all.
227 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
228 ol->path = DBG_strdup(sourcefile);
229 ol->real_path = NULL;
230 ol->next = ofiles;
231 ol->nlines = 0;
232 ol->linelist = NULL;
233 ofiles = ol;
234 DEBUG_Printf(DBG_CHN_MESG,"Unable to open file %s\n", tmppath);
235 return FALSE;
240 * Create header for file.
242 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
243 ol->path = DBG_strdup(sourcefile);
244 ol->real_path = DBG_strdup(tmppath);
245 ol->next = ofiles;
246 ol->nlines = 0;
247 ol->linelist = NULL;
248 ol->size = statbuf.st_size;
249 ofiles = ol;
252 * Now open and map the file.
254 fd = open(tmppath, O_RDONLY);
255 if( fd == -1 )
257 return FALSE;
260 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
261 if( addr == (char *) -1 )
263 return FALSE;
267 * Now build up the line number mapping table.
269 ol->nlines = 1;
270 pnt = addr;
271 while(pnt < addr + ol->size )
273 if( *pnt++ == '\n' )
275 ol->nlines++;
279 ol->nlines++;
280 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
282 nlines = 0;
283 pnt = addr;
284 ol->linelist[nlines++] = 0;
285 while(pnt < addr + ol->size )
287 if( *pnt++ == '\n' )
289 ol->linelist[nlines++] = pnt - addr;
292 ol->linelist[nlines++] = pnt - addr;
295 else
298 * We know what the file is, we just need to reopen it and remap it.
300 fd = open(ol->real_path, O_RDONLY);
301 if( fd == -1 )
303 return FALSE;
306 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
307 if( addr == (char *) -1 )
309 return FALSE;
314 * All we need to do is to display the source lines here.
316 rtn = FALSE;
317 for(i=start - 1; i <= end - 1; i++)
319 if( i < 0 || i >= ol->nlines - 1)
321 continue;
324 rtn = TRUE;
325 memset(&buffer, 0, sizeof(buffer));
326 if( ol->linelist[i+1] != ol->linelist[i] )
328 memcpy(&buffer, addr + ol->linelist[i],
329 (ol->linelist[i+1] - ol->linelist[i]) - 1);
331 DEBUG_Printf(DBG_CHN_MESG,"%d\t%s\n", i + 1, buffer);
334 munmap(addr, ol->size);
335 close(fd);
337 return rtn;
341 void
342 DEBUG_List(struct list_id * source1, struct list_id * source2,
343 int delta)
345 int end;
346 int rtn;
347 int start;
348 char * sourcefile;
351 * We need to see what source file we need. Hopefully we only have
352 * one specified, otherwise we might as well punt.
354 if( source1 != NULL
355 && source2 != NULL
356 && source1->sourcefile != NULL
357 && source2->sourcefile != NULL
358 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
360 DEBUG_Printf(DBG_CHN_MESG, "Ambiguous source file specification.\n");
361 return;
364 sourcefile = NULL;
365 if( source1 != NULL && source1->sourcefile != NULL )
367 sourcefile = source1->sourcefile;
370 if( sourcefile == NULL
371 && source2 != NULL
372 && source2->sourcefile != NULL )
374 sourcefile = source2->sourcefile;
377 if( sourcefile == NULL )
379 sourcefile = (char *) &DEBUG_current_sourcefile;
382 if( sourcefile == NULL )
384 DEBUG_Printf(DBG_CHN_MESG, "No source file specified.\n");
385 return;
389 * Now figure out the line number range to be listed.
391 start = -1;
392 end = -1;
394 if( source1 != NULL )
396 start = source1->line;
399 if( source2 != NULL )
401 end = source2->line;
404 if( start == -1 && end == -1 )
406 if( delta < 0 )
408 end = DEBUG_start_sourceline;
409 start = end + delta;
411 else
413 start = DEBUG_end_sourceline;
414 end = start + delta;
417 else if( start == -1 )
419 start = end + delta;
421 else if (end == -1)
423 end = start + delta;
427 * Now call this function to do the dirty work.
429 rtn = DEBUG_DisplaySource(sourcefile, start, end);
431 if( sourcefile != (char *) &DEBUG_current_sourcefile )
433 strcpy(DEBUG_current_sourcefile, sourcefile);
435 DEBUG_start_sourceline = start;
436 DEBUG_end_sourceline = end;
439 DBG_ADDR DEBUG_LastDisassemble={0,0};
441 BOOL DEBUG_DisassembleInstruction(DBG_ADDR *addr)
443 char ch;
444 BOOL ret = TRUE;
446 DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, TRUE);
447 DEBUG_Printf(DBG_CHN_MESG, ": ");
448 if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear(addr), &ch, sizeof(ch))) {
449 DEBUG_Printf(DBG_CHN_MESG, "-- no code --");
450 ret = FALSE;
451 } else {
452 DEBUG_Disasm(addr, TRUE);
454 DEBUG_Printf(DBG_CHN_MESG,"\n");
455 return ret;
458 void
459 DEBUG_Disassemble(const DBG_VALUE *xstart,const DBG_VALUE *xend,int offset)
461 int i;
462 DBG_ADDR last;
463 DBG_VALUE end,start;
465 if (xstart) {
466 start = *xstart;
467 DEBUG_GrabAddress(&start, TRUE);
469 if (xend) {
470 end = *xend;
471 DEBUG_GrabAddress(&end, TRUE);
473 if (!xstart && !xend) {
474 last = DEBUG_LastDisassemble;
475 if (!last.seg && !last.off)
476 DEBUG_GetCurrentAddress( &last );
478 for (i=0;i<offset;i++)
479 if (!DEBUG_DisassembleInstruction(&last)) break;
480 DEBUG_LastDisassemble = last;
481 return;
483 last = start.addr;
484 if (!xend) {
485 for (i=0;i<offset;i++)
486 if (!DEBUG_DisassembleInstruction(&last)) break;
487 DEBUG_LastDisassemble = last;
488 return;
490 while (last.off <= end.addr.off)
491 if (!DEBUG_DisassembleInstruction(&last)) break;
492 DEBUG_LastDisassemble = last;
493 return;