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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 struct open_file_list
* next
;
33 unsigned int* linelist
;
36 void source_show_path(void)
41 dbg_printf("Search list:\n");
42 for (ptr
= dbg_curr_process
->search_path
; ptr
; ptr
= next
)
44 next
= strchr(ptr
, ';');
46 dbg_printf("\t%.*s\n", (int)(next
++ - ptr
), ptr
);
48 dbg_printf("\t%s\n", ptr
);
53 void source_add_path(const char* path
)
58 size
= strlen(path
) + 1;
59 if (dbg_curr_process
->search_path
)
61 unsigned pos
= strlen(dbg_curr_process
->search_path
) + 1;
62 new = HeapReAlloc(GetProcessHeap(), 0, dbg_curr_process
->search_path
, pos
+ size
);
65 strcpy(&new[pos
], path
);
69 new = HeapAlloc(GetProcessHeap(), 0, size
);
73 dbg_curr_process
->search_path
= new;
76 void source_nuke_path(struct dbg_process
* p
)
78 HeapFree(GetProcessHeap(), 0, p
->search_path
);
79 p
->search_path
= NULL
;
82 static void* source_map_file(const char* name
, HANDLE
* hMap
, unsigned* size
)
86 hFile
= CreateFileA(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
87 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
88 if (hFile
== INVALID_HANDLE_VALUE
) return (void*)-1;
89 if (size
!= NULL
&& (*size
= GetFileSize(hFile
, NULL
)) == INVALID_FILE_SIZE
) {
93 *hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
95 if (!*hMap
) return (void*)-1;
96 return MapViewOfFile(*hMap
, FILE_MAP_READ
, 0, 0, 0);
99 static void source_unmap_file(void* addr
, HANDLE hMap
)
101 UnmapViewOfFile(addr
);
105 static struct open_file_list
* source_search_open_file(const char* name
)
107 struct open_file_list
* ol
;
109 for (ol
= dbg_curr_process
->source_ofiles
; ol
; ol
= ol
->next
)
111 if (strcmp(ol
->path
, name
) == 0) break;
116 static BOOL
source_locate_file(const char* srcfile
, char* path
)
120 if (GetFileAttributesA(srcfile
) != INVALID_FILE_ATTRIBUTES
)
122 strcpy(path
, srcfile
);
125 else if (dbg_curr_process
->search_path
)
132 while (*spath
&& *spath
!= '/' && *spath
!= '\\') spath
++;
135 found
= SearchPathA(dbg_curr_process
->search_path
, spath
, NULL
, MAX_PATH
, path
, NULL
);
141 static struct open_file_list
* source_add_file(const char* name
, const char* realpath
)
143 struct open_file_list
* ol
;
147 nlen
= strlen(name
) + 1;
148 if (realpath
) sz
+= strlen(realpath
) + 1;
149 ol
= HeapAlloc(GetProcessHeap(), 0, sz
+ nlen
);
150 if (!ol
) return NULL
;
151 strcpy(ol
->path
= (char*)(ol
+ 1), name
);
153 strcpy(ol
->real_path
= ol
->path
+ nlen
, realpath
);
155 ol
->real_path
= NULL
;
156 ol
->next
= dbg_curr_process
->source_ofiles
;
160 return dbg_curr_process
->source_ofiles
= ol
;
163 static int source_display(const char* sourcefile
, int start
, int end
)
167 struct open_file_list
* ol
;
169 const char* basename
= NULL
;
173 char tmppath
[MAX_PATH
];
176 * First see whether we have the file open already. If so, then
177 * use that, otherwise we have to try and open it.
179 ol
= source_search_open_file(sourcefile
);
184 * Try again, stripping the path from the opened file.
186 basename
= strrchr(sourcefile
, '\\');
187 if (!basename
) basename
= strrchr(sourcefile
, '/');
188 if (!basename
) basename
= sourcefile
;
191 ol
= source_search_open_file(basename
);
197 * Crapola. We need to try and open the file.
199 if (!source_locate_file(sourcefile
, tmppath
))
201 if (dbg_interactiveP
)
209 * Still couldn't find it. Ask user for path to add.
211 snprintf(zbuf
, sizeof(zbuf
), "Enter path to file '%s' (<cr> to end search): ", sourcefile
);
212 input_read_line(zbuf
, tmppath
, sizeof(tmppath
));
213 if (!(len
= strlen(tmppath
))) break;
215 /* append '/' if missing at the end */
216 if (tmppath
[len
- 1] != '/' && tmppath
[len
- 1] != '\\')
217 tmppath
[len
++] = '/';
218 strcpy(&tmppath
[len
], basename
);
219 if (GetFileAttributesA(tmppath
) != INVALID_FILE_ATTRIBUTES
)
221 dbg_printf("Unable to access file '%s'\n", tmppath
);
226 dbg_printf("Unable to access file '%s'\n", sourcefile
);
233 * OK, I guess the user doesn't really want to see it
236 source_add_file(sourcefile
, NULL
);
241 * Create header for file.
243 ol
= source_add_file(sourcefile
, tmppath
);
245 addr
= source_map_file(tmppath
, &hMap
, &ol
->size
);
246 if (addr
== (char*)-1) return FALSE
;
248 * Now build up the line number mapping table.
252 while (pnt
< addr
+ ol
->size
)
254 if (*pnt
++ == '\n') ol
->nlines
++;
258 ol
->linelist
= HeapAlloc(GetProcessHeap(), 0, ol
->nlines
* sizeof(unsigned int));
262 ol
->linelist
[nlines
++] = 0;
263 while (pnt
< addr
+ ol
->size
)
265 if (*pnt
++ == '\n') ol
->linelist
[nlines
++] = pnt
- addr
;
267 ol
->linelist
[nlines
++] = pnt
- addr
;
272 addr
= source_map_file(ol
->real_path
, &hMap
, NULL
);
273 if (addr
== (char*)-1) return FALSE
;
276 * All we need to do is to display the source lines here.
279 for (i
= start
- 1; i
<= end
- 1; i
++)
283 if (i
< 0 || i
>= ol
->nlines
- 1) continue;
286 memset(&buffer
, 0, sizeof(buffer
));
287 if (ol
->linelist
[i
+1] != ol
->linelist
[i
])
289 memcpy(&buffer
, addr
+ ol
->linelist
[i
],
290 (ol
->linelist
[i
+1] - ol
->linelist
[i
]) - 1);
292 dbg_printf("%d\t%s\n", i
+ 1, buffer
);
295 source_unmap_file(addr
, hMap
);
299 void source_list(IMAGEHLP_LINE64
* src1
, IMAGEHLP_LINE64
* src2
, int delta
)
303 const char* sourcefile
;
306 * We need to see what source file we need. Hopefully we only have
307 * one specified, otherwise we might as well punt.
309 if (src1
&& src2
&& src1
->FileName
&& src2
->FileName
&&
310 strcmp(src1
->FileName
, src2
->FileName
) != 0)
312 dbg_printf("Ambiguous source file specification.\n");
317 if (src1
&& src1
->FileName
) sourcefile
= src1
->FileName
;
318 if (!sourcefile
&& src2
&& src2
->FileName
) sourcefile
= src2
->FileName
;
319 if (!sourcefile
) sourcefile
= dbg_curr_process
->source_current_file
;
322 * Now figure out the line number range to be listed.
326 if (src1
) start
= src1
->LineNumber
;
327 if (src2
) end
= src2
->LineNumber
;
328 if (start
== -1 && end
== -1)
332 end
= dbg_curr_process
->source_start_line
;
337 start
= dbg_curr_process
->source_end_line
;
341 else if (start
== -1) start
= end
+ delta
;
342 else if (end
== -1) end
= start
+ delta
;
345 * Now call this function to do the dirty work.
347 source_display(sourcefile
, start
, end
);
349 if (sourcefile
!= dbg_curr_process
->source_current_file
)
350 strcpy(dbg_curr_process
->source_current_file
, sourcefile
);
351 dbg_curr_process
->source_start_line
= start
;
352 dbg_curr_process
->source_end_line
= end
;
355 void source_list_from_addr(const ADDRESS64
* addr
, int nlines
)
363 memory_get_current_pc(&la
);
367 il
.SizeOfStruct
= sizeof(il
);
368 if (SymGetLineFromAddr64(dbg_curr_process
->handle
,
369 (DWORD_PTR
)memory_to_linear_addr(addr
),
371 source_list(&il
, NULL
, nlines
);
374 void source_free_files(struct dbg_process
* p
)
376 struct open_file_list
* ofile
;
377 struct open_file_list
* ofile_next
;
379 for (ofile
= p
->source_ofiles
; ofile
; ofile
= ofile_next
)
381 ofile_next
= ofile
->next
;
382 HeapFree(GetProcessHeap(), 0, ofile
->linelist
);
383 HeapFree(GetProcessHeap(), 0, ofile
);