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
26 #define PATH_MAX MAX_PATH
35 struct open_file_list
* next
;
38 unsigned int* linelist
;
41 static struct open_file_list
* source_ofiles
;
43 static char* search_path
; /* = NULL; */
44 static char source_current_file
[PATH_MAX
];
45 static int source_start_line
= -1;
46 static int source_end_line
= -1;
48 void source_show_path(void)
53 dbg_printf("Search list:\n");
54 for (ptr
= search_path
; ptr
; ptr
= next
)
56 next
= strchr(ptr
, ';');
58 dbg_printf("\t%.*s\n", next
++ - ptr
, ptr
);
60 dbg_printf("\t%s\n", ptr
);
65 void source_add_path(const char* path
)
70 size
= strlen(path
) + 1;
73 unsigned pos
= HeapSize(GetProcessHeap(), 0, search_path
);
74 new = HeapReAlloc(GetProcessHeap(), 0, search_path
, pos
+ size
);
75 if (!new || !pos
) return;
77 strcpy(&new[pos
], path
);
81 new = HeapAlloc(GetProcessHeap(), 0, size
);
88 void source_nuke_path(void)
90 HeapFree(GetProcessHeap(), 0, search_path
);
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
;
135 char tmppath
[PATH_MAX
];
138 * First see whether we have the file open already. If so, then
139 * use that, otherwise we have to try and open it.
141 ol
= source_search_open_file(sourcefile
);
146 * Try again, stripping the path from the opened file.
148 basename
= strrchr(sourcefile
, '\\');
149 if (!basename
) basename
= strrchr(sourcefile
, '/');
150 if (!basename
) basename
= sourcefile
;
153 ol
= source_search_open_file(basename
);
159 * Crapola. We need to try and open the file.
161 strcpy(tmppath
, sourcefile
);
162 if (GetFileAttributes(sourcefile
) == INVALID_FILE_ATTRIBUTES
&&
163 (!search_path
|| !SearchPathA(search_path
, sourcefile
, NULL
, MAX_PATH
, tmppath
, NULL
)))
165 if (dbg_interactiveP
)
169 * Still couldn't find it. Ask user for path to add.
171 snprintf(zbuf
, sizeof(zbuf
), "Enter path to file '%s': ", sourcefile
);
172 input_read_line(zbuf
, tmppath
, sizeof(tmppath
));
174 if (tmppath
[strlen(tmppath
) - 1] != '/')
176 strcat(tmppath
, "/");
179 * Now append the base file name.
181 strcat(tmppath
, basename
);
184 if (GetFileAttributes(tmppath
) == INVALID_FILE_ATTRIBUTES
)
187 * OK, I guess the user doesn't really want to see it
190 ol
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ol
));
191 ol
->path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile
) + 1), sourcefile
);
192 ol
->real_path
= NULL
;
193 ol
->next
= source_ofiles
;
198 dbg_printf("Unable to open file '%s'\n", tmppath
);
203 * Create header for file.
205 ol
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ol
));
206 ol
->path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(sourcefile
) + 1), sourcefile
);
207 ol
->real_path
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(tmppath
) + 1), tmppath
);
208 ol
->next
= source_ofiles
;
214 addr
= source_map_file(tmppath
, &hMap
, &ol
->size
);
215 if (addr
== (char*)-1) return FALSE
;
217 * Now build up the line number mapping table.
221 while (pnt
< addr
+ ol
->size
)
223 if (*pnt
++ == '\n') ol
->nlines
++;
227 ol
->linelist
= HeapAlloc(GetProcessHeap(), 0, ol
->nlines
* sizeof(unsigned int));
231 ol
->linelist
[nlines
++] = 0;
232 while (pnt
< addr
+ ol
->size
)
234 if (*pnt
++ == '\n') ol
->linelist
[nlines
++] = pnt
- addr
;
236 ol
->linelist
[nlines
++] = pnt
- addr
;
241 addr
= source_map_file(ol
->real_path
, &hMap
, NULL
);
242 if (addr
== (char*)-1) return FALSE
;
245 * All we need to do is to display the source lines here.
248 for (i
= start
- 1; i
<= end
- 1; i
++)
252 if (i
< 0 || i
>= ol
->nlines
- 1) continue;
255 memset(&buffer
, 0, sizeof(buffer
));
256 if (ol
->linelist
[i
+1] != ol
->linelist
[i
])
258 memcpy(&buffer
, addr
+ ol
->linelist
[i
],
259 (ol
->linelist
[i
+1] - ol
->linelist
[i
]) - 1);
261 dbg_printf("%d\t%s\n", i
+ 1, buffer
);
264 source_unmap_file(addr
, hMap
);
268 void source_list(IMAGEHLP_LINE
* src1
, IMAGEHLP_LINE
* src2
, int delta
)
272 const char* sourcefile
;
275 * We need to see what source file we need. Hopefully we only have
276 * one specified, otherwise we might as well punt.
278 if (src1
&& src2
&& src1
->FileName
&& src2
->FileName
&&
279 strcmp(src1
->FileName
, src2
->FileName
) != 0)
281 dbg_printf("Ambiguous source file specification.\n");
286 if (src1
&& src1
->FileName
) sourcefile
= src1
->FileName
;
287 if (!sourcefile
&& src2
&& src2
->FileName
) sourcefile
= src2
->FileName
;
288 if (!sourcefile
) sourcefile
= source_current_file
;
291 * Now figure out the line number range to be listed.
295 if (src1
) start
= src1
->LineNumber
;
296 if (src2
) end
= src2
->LineNumber
;
297 if (start
== -1 && end
== -1)
301 end
= source_start_line
;
306 start
= source_end_line
;
310 else if (start
== -1) start
= end
+ delta
;
311 else if (end
== -1) end
= start
+ delta
;
314 * Now call this function to do the dirty work.
316 source_display(sourcefile
, start
, end
);
318 if (sourcefile
!= source_current_file
)
319 strcpy(source_current_file
, sourcefile
);
320 source_start_line
= start
;
321 source_end_line
= end
;
324 void source_list_from_addr(const ADDRESS64
* addr
, int nlines
)
332 memory_get_current_pc(&la
);
336 il
.SizeOfStruct
= sizeof(il
);
337 if (SymGetLineFromAddr(dbg_curr_process
->handle
,
338 (unsigned long)memory_to_linear_addr(addr
),
340 source_list(&il
, NULL
, nlines
);