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
26 #define PATH_MAX MAX_PATH
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg
);
37 struct search_list
* next
;
45 struct open_file_list
* next
;
48 unsigned int* linelist
;
51 static struct open_file_list
* source_ofiles
;
53 static struct search_list
* source_list_head
;
54 static char source_current_file
[PATH_MAX
];
55 static int source_start_line
= -1;
56 static int source_end_line
= -1;
58 void source_show_path(void)
60 struct search_list
* sl
;
62 dbg_printf("Search list:\n");
63 for (sl
= source_list_head
; sl
; sl
= sl
->next
) dbg_printf("\t%s\n", sl
->path
);
67 void source_add_path(const char* path
)
69 struct search_list
* sl
;
71 if (!(sl
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct search_list
))))
74 sl
->next
= source_list_head
;
75 sl
->path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(path
) + 1), path
);
76 source_list_head
= sl
;
79 void source_nuke_path(void)
81 struct search_list
* sl
;
82 struct search_list
* nxt
;
84 for (sl
= source_list_head
; sl
; sl
= nxt
)
87 HeapFree(GetProcessHeap(), 0, sl
->path
);
88 HeapFree(GetProcessHeap(), 0, sl
);
91 source_list_head
= NULL
;
94 static void* source_map_file(const char* name
, HANDLE
* hMap
, unsigned* size
)
98 hFile
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
99 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
100 if (hFile
== INVALID_HANDLE_VALUE
) return (void*)-1;
101 if (size
!= NULL
&& (*size
= GetFileSize(hFile
, NULL
)) == -1) return (void*)-1;
102 *hMap
= CreateFileMapping(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
104 if (!*hMap
) return (void*)-1;
105 return MapViewOfFile(*hMap
, FILE_MAP_READ
, 0, 0, 0);
108 static void source_unmap_file(void* addr
, HANDLE hMap
)
110 UnmapViewOfFile(addr
);
114 static struct open_file_list
* source_search_open_file(const char* name
)
116 struct open_file_list
* ol
;
118 for (ol
= source_ofiles
; ol
; ol
= ol
->next
)
120 if (strcmp(ol
->path
, name
) == 0) break;
125 static int source_display(const char* sourcefile
, int start
, int end
)
129 struct open_file_list
* ol
;
131 const char* basename
= NULL
;
134 struct search_list
* sl
;
137 char tmppath
[PATH_MAX
];
140 * First see whether we have the file open already. If so, then
141 * use that, otherwise we have to try and open it.
143 ol
= source_search_open_file(sourcefile
);
148 * Try again, stripping the path from the opened file.
150 basename
= strrchr(sourcefile
, '\\');
151 if (!basename
) basename
= strrchr(sourcefile
, '/');
152 if (!basename
) basename
= sourcefile
;
155 ol
= source_search_open_file(basename
);
161 * Crapola. We need to try and open the file.
163 status
= GetFileAttributes(sourcefile
);
164 if (status
!= INVALID_FILE_ATTRIBUTES
)
166 strcpy(tmppath
, sourcefile
);
168 else if ((status
= GetFileAttributes(basename
)) != INVALID_FILE_ATTRIBUTES
)
170 strcpy(tmppath
, basename
);
174 for (sl
= source_list_head
; sl
; sl
= sl
->next
)
176 strcpy(tmppath
, sl
->path
);
177 if (tmppath
[strlen(tmppath
) - 1] != '/' && tmppath
[strlen(tmppath
) - 1] != '\\')
179 strcat(tmppath
, "/");
182 * Now append the base file name.
184 strcat(tmppath
, basename
);
186 status
= GetFileAttributes(tmppath
);
187 if (status
!= INVALID_FILE_ATTRIBUTES
) break;
192 if (dbg_interactiveP
)
196 * Still couldn't find it. Ask user for path to add.
198 snprintf(zbuf
, sizeof(zbuf
), "Enter path to file '%s': ", sourcefile
);
199 input_read_line(zbuf
, tmppath
, sizeof(tmppath
));
201 if (tmppath
[strlen(tmppath
) - 1] != '/')
203 strcat(tmppath
, "/");
206 * Now append the base file name.
208 strcat(tmppath
, basename
);
210 status
= GetFileAttributes(tmppath
);
214 status
= INVALID_FILE_ATTRIBUTES
;
215 strcpy(tmppath
, sourcefile
);
218 if (status
== INVALID_FILE_ATTRIBUTES
)
221 * OK, I guess the user doesn't really want to see it
224 ol
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ol
));
225 ol
->path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile
) + 1), sourcefile
);
226 ol
->real_path
= NULL
;
227 ol
->next
= source_ofiles
;
231 dbg_printf("Unable to open file '%s'\n", tmppath
);
237 * Create header for file.
239 ol
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ol
));
240 ol
->path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile
) + 1), sourcefile
);
241 ol
->real_path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(tmppath
) + 1), tmppath
);
242 ol
->next
= source_ofiles
;
248 addr
= source_map_file(tmppath
, &hMap
, &ol
->size
);
249 if (addr
== (char*)-1) return FALSE
;
251 * Now build up the line number mapping table.
255 while (pnt
< addr
+ ol
->size
)
257 if (*pnt
++ == '\n') ol
->nlines
++;
261 ol
->linelist
= HeapAlloc(GetProcessHeap(), 0, ol
->nlines
* sizeof(unsigned int));
265 ol
->linelist
[nlines
++] = 0;
266 while (pnt
< addr
+ ol
->size
)
268 if (*pnt
++ == '\n') ol
->linelist
[nlines
++] = pnt
- addr
;
270 ol
->linelist
[nlines
++] = pnt
- addr
;
275 addr
= source_map_file(ol
->real_path
, &hMap
, NULL
);
276 if (addr
== (char*)-1) return FALSE
;
279 * All we need to do is to display the source lines here.
282 for (i
= start
- 1; i
<= end
- 1; i
++)
286 if (i
< 0 || i
>= ol
->nlines
- 1) continue;
289 memset(&buffer
, 0, sizeof(buffer
));
290 if (ol
->linelist
[i
+1] != ol
->linelist
[i
])
292 memcpy(&buffer
, addr
+ ol
->linelist
[i
],
293 (ol
->linelist
[i
+1] - ol
->linelist
[i
]) - 1);
295 dbg_printf("%d\t%s\n", i
+ 1, buffer
);
298 source_unmap_file(addr
, hMap
);
302 void source_list(IMAGEHLP_LINE
* src1
, IMAGEHLP_LINE
* src2
, int delta
)
306 const char* sourcefile
;
309 * We need to see what source file we need. Hopefully we only have
310 * one specified, otherwise we might as well punt.
312 if (src1
&& src2
&& src1
->FileName
&& src2
->FileName
&&
313 strcmp(src1
->FileName
, src2
->FileName
) != 0)
315 dbg_printf("Ambiguous source file specification.\n");
320 if (src1
&& src1
->FileName
) sourcefile
= src1
->FileName
;
321 if (!sourcefile
&& src2
&& src2
->FileName
) sourcefile
= src2
->FileName
;
322 if (!sourcefile
) sourcefile
= source_current_file
;
325 * Now figure out the line number range to be listed.
329 if (src1
) start
= src1
->LineNumber
;
330 if (src2
) end
= src2
->LineNumber
;
331 if (start
== -1 && end
== -1)
335 end
= source_start_line
;
340 start
= source_end_line
;
344 else if (start
== -1) start
= end
+ delta
;
345 else if (end
== -1) end
= start
+ delta
;
348 * Now call this function to do the dirty work.
350 source_display(sourcefile
, start
, end
);
352 if (sourcefile
!= source_current_file
)
353 strcpy(source_current_file
, sourcefile
);
354 source_start_line
= start
;
355 source_end_line
= end
;
358 void source_list_from_addr(const ADDRESS
* addr
, int nlines
)
366 memory_get_current_pc(&la
);
370 il
.SizeOfStruct
= sizeof(il
);
371 if (SymGetLineFromAddr(dbg_curr_process
->handle
,
372 (unsigned long)memory_to_linear_addr(addr
),
374 source_list(&il
, NULL
, nlines
);