2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
12 #include <sys/types.h>
20 #define PATH_MAX _MAX_PATH
23 #include "wine/winbase16.h"
34 struct searchlist
* next
;
42 struct open_filelist
* next
;
45 unsigned int * linelist
;
48 static struct open_filelist
* ofiles
;
50 static struct searchlist
* listhead
;
51 static char DEBUG_current_sourcefile
[PATH_MAX
];
52 static int DEBUG_start_sourceline
= -1;
53 static int DEBUG_end_sourceline
= -1;
58 struct searchlist
* sl
;
60 fprintf(stderr
,"Search list :\n");
61 for(sl
= listhead
; sl
; sl
= sl
->next
)
63 fprintf(stderr
, "\t%s\n", sl
->path
);
65 fprintf(stderr
, "\n");
69 DEBUG_AddPath(const char * path
)
71 struct searchlist
* sl
;
73 sl
= (struct searchlist
*) xmalloc(sizeof(struct searchlist
));
80 sl
->path
= xstrdup(path
);
87 struct searchlist
* sl
;
88 struct searchlist
* nxt
;
90 for(sl
= listhead
; sl
; sl
= nxt
)
102 DEBUG_DisplaySource(char * sourcefile
, int start
, int end
)
108 struct open_filelist
* ol
;
112 struct searchlist
* sl
;
115 char tmppath
[PATH_MAX
];
118 * First see whether we have the file open already. If so, then
119 * use that, otherwise we have to try and open it.
121 for(ol
= ofiles
; ol
; ol
= ol
->next
)
123 if( strcmp(ol
->path
, sourcefile
) == 0 )
132 * Try again, stripping the path from the opened file.
134 for(ol
= ofiles
; ol
; ol
= ol
->next
)
136 pnt
= strrchr(ol
->path
, '/');
137 if( pnt
!= NULL
&& strcmp(pnt
+ 1, sourcefile
) == 0 )
148 * See if this is a DOS style name or not.
150 pnt
= strchr(sourcefile
, '\\' );
153 pnt
= strchr(sourcefile
, '/' );
161 * Crapola. We need to try and open the file.
163 status
= stat(sourcefile
, &statbuf
);
166 strcpy(tmppath
, sourcefile
);
170 for(sl
= listhead
; sl
; sl
= sl
->next
)
172 strcpy(tmppath
, sl
->path
);
173 if( tmppath
[strlen(tmppath
)-1] != '/' )
175 strcat(tmppath
, "/");
178 * Now append the base file name.
180 strcat(tmppath
, pnt
);
182 status
= stat(tmppath
, &statbuf
);
192 * Still couldn't find it. Ask user for path to add.
194 fprintf(stderr
,"Enter path to file %s: ", sourcefile
);
195 fgets(tmppath
, sizeof(tmppath
), stdin
);
197 if( tmppath
[strlen(tmppath
)-1] == '\n' )
199 tmppath
[strlen(tmppath
)-1] = '\0';
202 if( tmppath
[strlen(tmppath
)-1] != '/' )
204 strcat(tmppath
, "/");
207 * Now append the base file name.
209 strcat(tmppath
, pnt
);
211 status
= stat(tmppath
, &statbuf
);
215 * OK, I guess the user doesn't really want to see it
218 ol
= (struct open_filelist
*) xmalloc(sizeof(*ol
));
219 ol
->path
= xstrdup(sourcefile
);
220 ol
->real_path
= NULL
;
225 fprintf(stderr
,"Unable to open file %s\n", tmppath
);
231 * Create header for file.
233 ol
= (struct open_filelist
*) xmalloc(sizeof(*ol
));
234 ol
->path
= xstrdup(sourcefile
);
235 ol
->real_path
= xstrdup(tmppath
);
239 ol
->size
= statbuf
.st_size
;
243 * Now open and map the file.
245 fd
= open(tmppath
, O_RDONLY
);
251 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
252 if( addr
== (char *) -1 )
258 * Now build up the line number mapping table.
262 while(pnt
< addr
+ ol
->size
)
271 ol
->linelist
= (unsigned int*) xmalloc(ol
->nlines
* sizeof(unsigned int) );
275 ol
->linelist
[nlines
++] = 0;
276 while(pnt
< addr
+ ol
->size
)
280 ol
->linelist
[nlines
++] = pnt
- addr
;
283 ol
->linelist
[nlines
++] = pnt
- addr
;
289 * We know what the file is, we just need to reopen it and remap it.
291 fd
= open(ol
->real_path
, O_RDONLY
);
297 addr
= mmap(0, ol
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
298 if( addr
== (char *) -1 )
305 * All we need to do is to display the source lines here.
308 for(i
=start
- 1; i
<= end
- 1; i
++)
310 if( i
< 0 || i
>= ol
->nlines
- 1)
316 memset(&buffer
, 0, sizeof(buffer
));
317 if( ol
->linelist
[i
+1] != ol
->linelist
[i
] )
319 memcpy(&buffer
, addr
+ ol
->linelist
[i
],
320 (ol
->linelist
[i
+1] - ol
->linelist
[i
]) - 1);
322 fprintf(stderr
,"%d\t%s\n", i
+ 1, buffer
);
325 munmap(addr
, ol
->size
);
333 DEBUG_List(struct list_id
* source1
, struct list_id
* source2
,
342 * We need to see what source file we need. Hopefully we only have
343 * one specified, otherwise we might as well punt.
347 && source1
->sourcefile
!= NULL
348 && source2
->sourcefile
!= NULL
349 && strcmp(source1
->sourcefile
, source2
->sourcefile
) != 0 )
351 fprintf(stderr
, "Ambiguous source file specification.\n");
356 if( source1
!= NULL
&& source1
->sourcefile
!= NULL
)
358 sourcefile
= source1
->sourcefile
;
361 if( sourcefile
== NULL
363 && source2
->sourcefile
!= NULL
)
365 sourcefile
= source2
->sourcefile
;
368 if( sourcefile
== NULL
)
370 sourcefile
= (char *) &DEBUG_current_sourcefile
;
373 if( sourcefile
== NULL
)
375 fprintf(stderr
, "No source file specified.\n");
380 * Now figure out the line number range to be listed.
385 if( source1
!= NULL
)
387 start
= source1
->line
;
390 if( source2
!= NULL
)
395 if( start
== -1 && end
== -1 )
399 end
= DEBUG_start_sourceline
;
404 start
= DEBUG_end_sourceline
;
408 else if( start
== -1 )
418 * Now call this function to do the dirty work.
420 rtn
= DEBUG_DisplaySource(sourcefile
, start
, end
);
422 if( sourcefile
!= (char *) &DEBUG_current_sourcefile
)
424 strcpy(DEBUG_current_sourcefile
, sourcefile
);
426 DEBUG_start_sourceline
= start
;
427 DEBUG_end_sourceline
= end
;
430 DBG_ADDR DEBUG_LastDisassemble
={NULL
,0,0};
433 _disassemble(DBG_ADDR
*addr
)
435 DEBUG_PrintAddress( addr
, dbg_mode
, TRUE
);
436 fprintf(stderr
,": ");
437 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
438 DEBUG_Disasm( addr
, TRUE
);
439 fprintf(stderr
,"\n");
444 _disassemble_fixaddr(DBG_ADDR
*addr
) {
446 struct datatype
*testtype
;
448 DBG_FIX_ADDR_SEG(addr
,CS_reg(&DEBUG_context
));
449 if( addr
->type
!= NULL
)
451 if( addr
->type
== DEBUG_TypeIntConst
)
454 * We know that we have the actual offset stored somewhere
455 * else in 32-bit space. Grab it, and we
460 addr
->off
= DEBUG_GetExprValue(addr
, NULL
);
465 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return;
466 DEBUG_TypeDerefPointer(addr
, &testtype
);
467 if( testtype
!= NULL
|| addr
->type
== DEBUG_TypeIntConst
)
468 addr
->off
= DEBUG_GetExprValue(addr
, NULL
);
471 else if (!addr
->seg
&& !addr
->off
)
473 fprintf(stderr
,"Invalid expression\n");
479 DEBUG_Disassemble(const DBG_ADDR
*xstart
,const DBG_ADDR
*xend
,int offset
)
487 _disassemble_fixaddr(&start
);
491 _disassemble_fixaddr(&end
);
493 if (!xstart
&& !xend
) {
494 last
= DEBUG_LastDisassemble
;
495 if (!last
.seg
&& !last
.off
)
497 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
498 last
.seg
= CS_reg(&DEBUG_context
);
499 last
.off
= EIP_reg(&DEBUG_context
);
500 if (ISV86(&DEBUG_context
)) last
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
501 if (IS_SELECTOR_SYSTEM(last
.seg
)) last
.seg
= 0;
502 GlobalUnlock16( GetCurrentTask() );
504 for (i
=0;i
<offset
;i
++)
505 if (!_disassemble(&last
)) break;
506 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
511 for (i
=0;i
<offset
;i
++)
512 if (!_disassemble(&last
)) break;
513 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
516 while (last
.off
<= end
.off
)
517 if (!_disassemble(&last
)) break;
518 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
528 DEBUG_AddPath("../../de");
531 fscanf(stdin
,"%d %d", &i
, &j
);
532 DEBUG_DisplaySource("dumpexe.c", i
, j
);