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};
434 _disassemble(DBG_ADDR
*addr
)
436 DEBUG_PrintAddress( addr
, dbg_mode
, TRUE
);
437 fprintf(stderr
,": ");
438 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return 0;
439 DEBUG_Disasm( addr
, TRUE
);
440 fprintf(stderr
,"\n");
445 _disassemble_fixaddr(DBG_ADDR
*addr
) {
447 struct datatype
*testtype
;
449 DBG_FIX_ADDR_SEG(addr
,CS_reg(&DEBUG_context
));
450 if( addr
->type
!= NULL
)
452 if( addr
->type
== DEBUG_TypeIntConst
)
455 * We know that we have the actual offset stored somewhere
456 * else in 32-bit space. Grab it, and we
461 addr
->off
= DEBUG_GetExprValue(addr
, NULL
);
466 if (!DBG_CHECK_READ_PTR( addr
, 1 )) return;
467 DEBUG_TypeDerefPointer(addr
, &testtype
);
468 if( testtype
!= NULL
|| addr
->type
== DEBUG_TypeIntConst
)
469 addr
->off
= DEBUG_GetExprValue(addr
, NULL
);
472 else if (!addr
->seg
&& !addr
->off
)
474 fprintf(stderr
,"Invalid expression\n");
480 DEBUG_Disassemble(const DBG_ADDR
*xstart
,const DBG_ADDR
*xend
,int offset
)
488 _disassemble_fixaddr(&start
);
492 _disassemble_fixaddr(&end
);
494 if (!xstart
&& !xend
) {
495 last
= DEBUG_LastDisassemble
;
496 if (!last
.seg
&& !last
.off
)
498 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
499 last
.seg
= CS_reg(&DEBUG_context
);
500 last
.off
= EIP_reg(&DEBUG_context
);
501 if (ISV86(&DEBUG_context
)) last
.seg
|= (DWORD
)(pTask
?(pTask
->hModule
):0)<<16; else
502 if (IS_SELECTOR_SYSTEM(last
.seg
)) last
.seg
= 0;
503 GlobalUnlock16( GetCurrentTask() );
505 for (i
=0;i
<offset
;i
++)
506 if (!_disassemble(&last
)) break;
507 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
512 for (i
=0;i
<offset
;i
++)
513 if (!_disassemble(&last
)) break;
514 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
517 while (last
.off
<= end
.off
)
518 if (!_disassemble(&last
)) break;
519 memcpy(&DEBUG_LastDisassemble
,&last
,sizeof(last
));
529 DEBUG_AddPath("../../de");
532 fscanf(stdin
,"%d %d", &i
, &j
);
533 DEBUG_DisplaySource("dumpexe.c", i
, j
);