msxml3: Fold in reset_output_buffer.
[wine/zf.git] / programs / winedbg / source.c
blobcabb75f18134a965a7d3d0cc674691c3f14639de
1 /*
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
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
25 #include "debugger.h"
27 struct open_file_list
29 char* path;
30 char* real_path;
31 struct open_file_list* next;
32 unsigned int size;
33 signed int nlines;
34 unsigned int* linelist;
37 void source_show_path(void)
39 const char* ptr;
40 const char* next;
42 dbg_printf("Search list:\n");
43 for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
45 next = strchr(ptr, ';');
46 if (next)
47 dbg_printf("\t%.*s\n", (int)(next++ - ptr), ptr);
48 else
49 dbg_printf("\t%s\n", ptr);
51 dbg_printf("\n");
54 void source_add_path(const char* path)
56 char* new;
57 unsigned size;
59 size = strlen(path) + 1;
60 if (dbg_curr_process->search_path)
62 unsigned pos = strlen(dbg_curr_process->search_path) + 1;
63 new = HeapReAlloc(GetProcessHeap(), 0, dbg_curr_process->search_path, pos + size);
64 if (!new) return;
65 new[pos - 1] = ';';
66 strcpy(&new[pos], path);
68 else
70 new = HeapAlloc(GetProcessHeap(), 0, size);
71 if (!new) return;
72 strcpy(new, path);
74 dbg_curr_process->search_path = new;
77 void source_nuke_path(struct dbg_process* p)
79 HeapFree(GetProcessHeap(), 0, p->search_path);
80 p->search_path = NULL;
83 static void* source_map_file(const char* name, HANDLE* hMap, unsigned* size)
85 HANDLE hFile;
87 hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
88 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
90 if (size != NULL && (*size = GetFileSize(hFile, NULL)) == INVALID_FILE_SIZE) {
91 CloseHandle(hFile);
92 return (void*)-1;
94 *hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
95 CloseHandle(hFile);
96 if (!*hMap) return (void*)-1;
97 return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
100 static void source_unmap_file(void* addr, HANDLE hMap)
102 UnmapViewOfFile(addr);
103 CloseHandle(hMap);
106 static struct open_file_list* source_search_open_file(const char* name)
108 struct open_file_list* ol;
110 for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
112 if (strcmp(ol->path, name) == 0) break;
114 return ol;
117 static BOOL source_locate_file(const char* srcfile, char* path)
119 BOOL found = FALSE;
121 if (GetFileAttributesA(srcfile) != INVALID_FILE_ATTRIBUTES)
123 strcpy(path, srcfile);
124 found = TRUE;
126 else if (dbg_curr_process->search_path)
128 const char* spath;
130 spath = srcfile;
131 while (!found)
133 while (*spath && *spath != '/' && *spath != '\\') spath++;
134 if (!*spath) break;
135 spath++;
136 found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
139 return found;
142 static struct open_file_list* source_add_file(const char* name, const char* realpath)
144 struct open_file_list* ol;
145 size_t sz, nlen;
147 sz = sizeof(*ol);
148 nlen = strlen(name) + 1;
149 if (realpath) sz += strlen(realpath) + 1;
150 ol = HeapAlloc(GetProcessHeap(), 0, sz + nlen);
151 if (!ol) return NULL;
152 strcpy(ol->path = (char*)(ol + 1), name);
153 if (realpath)
154 strcpy(ol->real_path = ol->path + nlen, realpath);
155 else
156 ol->real_path = NULL;
157 ol->next = dbg_curr_process->source_ofiles;
158 ol->nlines = 0;
159 ol->linelist = NULL;
160 ol->size = 0;
161 return dbg_curr_process->source_ofiles = ol;
164 static int source_display(const char* sourcefile, int start, int end)
166 char* addr;
167 int i;
168 struct open_file_list* ol;
169 int nlines;
170 const char* basename = NULL;
171 char* pnt;
172 int rtn;
173 HANDLE hMap;
174 char tmppath[MAX_PATH];
177 * First see whether we have the file open already. If so, then
178 * use that, otherwise we have to try and open it.
180 ol = source_search_open_file(sourcefile);
182 if (ol == NULL)
185 * Try again, stripping the path from the opened file.
187 basename = strrchr(sourcefile, '\\');
188 if (!basename) basename = strrchr(sourcefile, '/');
189 if (!basename) basename = sourcefile;
190 else basename++;
192 ol = source_search_open_file(basename);
195 if (ol == NULL)
198 * Crapola. We need to try and open the file.
200 if (!source_locate_file(sourcefile, tmppath))
202 if (dbg_interactiveP)
204 char zbuf[256];
206 for (;;)
208 size_t len;
210 * Still couldn't find it. Ask user for path to add.
212 snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s' (<cr> to end search): ", sourcefile);
213 input_read_line(zbuf, tmppath, sizeof(tmppath));
214 if (!(len = strlen(tmppath))) break;
216 /* append '/' if missing at the end */
217 if (tmppath[len - 1] != '/' && tmppath[len - 1] != '\\')
218 tmppath[len++] = '/';
219 strcpy(&tmppath[len], basename);
220 if (GetFileAttributesA(tmppath) != INVALID_FILE_ATTRIBUTES)
221 break;
222 dbg_printf("Unable to access file '%s'\n", tmppath);
225 else
227 dbg_printf("Unable to access file '%s'\n", sourcefile);
228 tmppath[0] = '\0';
231 if (!tmppath[0])
234 * OK, I guess the user doesn't really want to see it
235 * after all.
237 source_add_file(sourcefile, NULL);
238 return FALSE;
242 * Create header for file.
244 ol = source_add_file(sourcefile, tmppath);
246 addr = source_map_file(tmppath, &hMap, &ol->size);
247 if (addr == (char*)-1) return FALSE;
249 * Now build up the line number mapping table.
251 ol->nlines = 1;
252 pnt = addr;
253 while (pnt < addr + ol->size)
255 if (*pnt++ == '\n') ol->nlines++;
258 ol->nlines++;
259 ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
261 nlines = 0;
262 pnt = addr;
263 ol->linelist[nlines++] = 0;
264 while (pnt < addr + ol->size)
266 if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
268 ol->linelist[nlines++] = pnt - addr;
271 else
273 addr = source_map_file(ol->real_path, &hMap, NULL);
274 if (addr == (char*)-1) return FALSE;
277 * All we need to do is to display the source lines here.
279 rtn = FALSE;
280 for (i = start - 1; i <= end - 1; i++)
282 char buffer[1024];
284 if (i < 0 || i >= ol->nlines - 1) continue;
286 rtn = TRUE;
287 memset(&buffer, 0, sizeof(buffer));
288 if (ol->linelist[i+1] != ol->linelist[i])
290 memcpy(&buffer, addr + ol->linelist[i],
291 (ol->linelist[i+1] - ol->linelist[i]) - 1);
293 dbg_printf("%d\t%s\n", i + 1, buffer);
296 source_unmap_file(addr, hMap);
297 return rtn;
300 void source_list(IMAGEHLP_LINE64* src1, IMAGEHLP_LINE64* src2, int delta)
302 int end;
303 int start;
304 const char* sourcefile;
307 * We need to see what source file we need. Hopefully we only have
308 * one specified, otherwise we might as well punt.
310 if (src1 && src2 && src1->FileName && src2->FileName &&
311 strcmp(src1->FileName, src2->FileName) != 0)
313 dbg_printf("Ambiguous source file specification.\n");
314 return;
317 sourcefile = NULL;
318 if (src1 && src1->FileName) sourcefile = src1->FileName;
319 if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
320 if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;
323 * Now figure out the line number range to be listed.
325 start = end = -1;
327 if (src1) start = src1->LineNumber;
328 if (src2) end = src2->LineNumber;
329 if (start == -1 && end == -1)
331 if (delta < 0)
333 end = dbg_curr_process->source_start_line;
334 start = end + delta;
336 else
338 start = dbg_curr_process->source_end_line;
339 end = start + delta;
342 else if (start == -1) start = end + delta;
343 else if (end == -1) end = start + delta;
346 * Now call this function to do the dirty work.
348 source_display(sourcefile, start, end);
350 if (sourcefile != dbg_curr_process->source_current_file)
351 strcpy(dbg_curr_process->source_current_file, sourcefile);
352 dbg_curr_process->source_start_line = start;
353 dbg_curr_process->source_end_line = end;
356 void source_list_from_addr(const ADDRESS64* addr, int nlines)
358 IMAGEHLP_LINE64 il;
359 ADDRESS64 la;
360 DWORD disp;
362 if (!addr)
364 memory_get_current_pc(&la);
365 addr = &la;
368 il.SizeOfStruct = sizeof(il);
369 if (SymGetLineFromAddr64(dbg_curr_process->handle,
370 (DWORD_PTR)memory_to_linear_addr(addr),
371 &disp, &il))
372 source_list(&il, NULL, nlines);
375 void source_free_files(struct dbg_process* p)
377 struct open_file_list* ofile;
378 struct open_file_list* ofile_next;
380 for (ofile = p->source_ofiles; ofile; ofile = ofile_next)
382 ofile_next = ofile->next;
383 HeapFree(GetProcessHeap(), 0, ofile->linelist);
384 HeapFree(GetProcessHeap(), 0, ofile);