2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
12 #include <sys/types.h>
13 #ifdef HAVE_SYS_MMAN_H
22 #define PATH_MAX _MAX_PATH
25 #include "wine/winbase16.h"
35 struct searchlist * next;
43 struct open_filelist * next;
46 unsigned int * linelist;
49 static struct open_filelist * ofiles;
51 static struct searchlist * listhead;
52 static char DEBUG_current_sourcefile[PATH_MAX];
53 static int DEBUG_start_sourceline = -1;
54 static int DEBUG_end_sourceline = -1;
59 struct searchlist * sl;
61 fprintf(stderr,"Search list :\n");
62 for(sl = listhead; sl; sl = sl->next)
64 fprintf(stderr, "\t%s\n", sl->path);
66 fprintf(stderr, "\n");
70 DEBUG_AddPath(const char * path)
72 struct searchlist * sl;
74 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
81 sl->path = DBG_strdup(path);
88 struct searchlist * sl;
89 struct searchlist * nxt;
91 for(sl = listhead; sl; sl = nxt)
103 DEBUG_DisplaySource(char * sourcefile, int start, int end)
109 struct open_filelist * ol;
113 struct searchlist * sl;
116 char tmppath[PATH_MAX];
119 * First see whether we have the file open already. If so, then
120 * use that, otherwise we have to try and open it.
122 for(ol = ofiles; ol; ol = ol->next)
124 if( strcmp(ol->path, sourcefile) == 0 )
133 * Try again, stripping the path from the opened file.
135 for(ol = ofiles; ol; ol = ol->next)
137 pnt = strrchr(ol->path, '/');
138 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
149 * See if this is a DOS style name or not.
151 pnt = strchr(sourcefile, '\\' );
154 pnt = strchr(sourcefile, '/' );
162 * Crapola. We need to try and open the file.
164 status = stat(sourcefile, &statbuf);
167 strcpy(tmppath, sourcefile);
171 for(sl = listhead; sl; sl = sl->next)
173 strcpy(tmppath, sl->path);
174 if( tmppath[strlen(tmppath)-1] != '/' )
176 strcat(tmppath, "/");
179 * Now append the base file name.
181 strcat(tmppath, pnt);
183 status = stat(tmppath, &statbuf);
193 * Still couldn't find it. Ask user for path to add.
195 fprintf(stderr,"Enter path to file %s: ", sourcefile);
196 fgets(tmppath, sizeof(tmppath), stdin);
198 if( tmppath[strlen(tmppath)-1] == '\n' )
200 tmppath[strlen(tmppath)-1] = '\0';
203 if( tmppath[strlen(tmppath)-1] != '/' )
205 strcat(tmppath, "/");
208 * Now append the base file name.
210 strcat(tmppath, pnt);
212 status = stat(tmppath, &statbuf);
216 * OK, I guess the user doesn't really want to see it
219 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
220 ol->path = DBG_strdup(sourcefile);
221 ol->real_path = NULL;
226 fprintf(stderr,"Unable to open file %s\n", tmppath);
232 * Create header for file.
234 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
235 ol->path = DBG_strdup(sourcefile);
236 ol->real_path = DBG_strdup(tmppath);
240 ol->size = statbuf.st_size;
244 * Now open and map the file.
246 fd = open(tmppath, O_RDONLY);
252 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
253 if( addr == (char *) -1 )
259 * Now build up the line number mapping table.
263 while(pnt < addr + ol->size )
272 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
276 ol->linelist[nlines++] = 0;
277 while(pnt < addr + ol->size )
281 ol->linelist[nlines++] = pnt - addr;
284 ol->linelist[nlines++] = pnt - addr;
290 * We know what the file is, we just need to reopen it and remap it.
292 fd = open(ol->real_path, O_RDONLY);
298 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
299 if( addr == (char *) -1 )
306 * All we need to do is to display the source lines here.
309 for(i=start - 1; i <= end - 1; i++)
311 if( i < 0 || i >= ol->nlines - 1)
317 memset(&buffer, 0, sizeof(buffer));
318 if( ol->linelist[i+1] != ol->linelist[i] )
320 memcpy(&buffer, addr + ol->linelist[i],
321 (ol->linelist[i+1] - ol->linelist[i]) - 1);
323 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
326 munmap(addr, ol->size);
334 DEBUG_List(struct list_id * source1, struct list_id * source2,
343 * We need to see what source file we need. Hopefully we only have
344 * one specified, otherwise we might as well punt.
348 && source1->sourcefile != NULL
349 && source2->sourcefile != NULL
350 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
352 fprintf(stderr, "Ambiguous source file specification.\n");
357 if( source1 != NULL && source1->sourcefile != NULL )
359 sourcefile = source1->sourcefile;
362 if( sourcefile == NULL
364 && source2->sourcefile != NULL )
366 sourcefile = source2->sourcefile;
369 if( sourcefile == NULL )
371 sourcefile = (char *) &DEBUG_current_sourcefile;
374 if( sourcefile == NULL )
376 fprintf(stderr, "No source file specified.\n");
381 * Now figure out the line number range to be listed.
386 if( source1 != NULL )
388 start = source1->line;
391 if( source2 != NULL )
396 if( start == -1 && end == -1 )
400 end = DEBUG_start_sourceline;
405 start = DEBUG_end_sourceline;
409 else if( start == -1 )
419 * Now call this function to do the dirty work.
421 rtn = DEBUG_DisplaySource(sourcefile, start, end);
423 if( sourcefile != (char *) &DEBUG_current_sourcefile )
425 strcpy(DEBUG_current_sourcefile, sourcefile);
427 DEBUG_start_sourceline = start;
428 DEBUG_end_sourceline = end;
431 DBG_ADDR DEBUG_LastDisassemble={NULL,0,0};
433 void DEBUG_GetCurrentAddress( DBG_ADDR *addr )
436 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
439 addr->seg = CS_reg(&DEBUG_context);
440 addr->off = EIP_reg(&DEBUG_context);
442 if (ISV86(&DEBUG_context)) addr->seg |= (DWORD)(pTask? pTask->hModule : 0) << 16;
443 else if (IS_SELECTOR_SYSTEM(addr->seg)) addr->seg = 0;
445 GlobalUnlock16( GetCurrentTask() );
449 addr->off = (DWORD)GET_IP(&DEBUG_context);
455 _disassemble(DBG_ADDR *addr)
457 DEBUG_PrintAddress( addr, dbg_mode, TRUE );
458 fprintf(stderr,": ");
459 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
460 DEBUG_Disasm( addr, TRUE );
461 fprintf(stderr,"\n");
466 _disassemble_fixaddr(DBG_ADDR *addr) {
468 struct datatype *testtype;
470 DBG_FIX_ADDR_SEG(addr,CS_reg(&DEBUG_context));
471 if( addr->type != NULL )
473 if( addr->type == DEBUG_TypeIntConst )
476 * We know that we have the actual offset stored somewhere
477 * else in 32-bit space. Grab it, and we
482 addr->off = DEBUG_GetExprValue(addr, NULL);
487 if (!DBG_CHECK_READ_PTR( addr, 1 )) return;
488 DEBUG_TypeDerefPointer(addr, &testtype);
489 if( testtype != NULL || addr->type == DEBUG_TypeIntConst )
490 addr->off = DEBUG_GetExprValue(addr, NULL);
493 else if (!addr->seg && !addr->off)
495 fprintf(stderr,"Invalid expression\n");
501 DEBUG_Disassemble(const DBG_ADDR *xstart,const DBG_ADDR *xend,int offset)
509 _disassemble_fixaddr(&start);
513 _disassemble_fixaddr(&end);
515 if (!xstart && !xend) {
516 last = DEBUG_LastDisassemble;
517 if (!last.seg && !last.off)
518 DEBUG_GetCurrentAddress( &last );
520 for (i=0;i<offset;i++)
521 if (!_disassemble(&last)) break;
522 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
527 for (i=0;i<offset;i++)
528 if (!_disassemble(&last)) break;
529 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
532 while (last.off <= end.off)
533 if (!_disassemble(&last)) break;
534 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
544 DEBUG_AddPath("../../de");
547 fscanf(stdin,"%d %d", &i, &j);
548 DEBUG_DisplaySource("dumpexe.c", i, j);