ddraw: Add full implementation of IDirect3DDevice7_Load.
[wine.git] / programs / winedbg / source.c
blob5519c57a595173b950c8f0f823a6b5d9406a93d2
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", 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 = CreateFile(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)) == -1) return (void*)-1;
91 *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
92 CloseHandle(hFile);
93 if (!*hMap) return (void*)-1;
94 return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
97 static void source_unmap_file(void* addr, HANDLE hMap)
99 UnmapViewOfFile(addr);
100 CloseHandle(hMap);
103 static struct open_file_list* source_search_open_file(const char* name)
105 struct open_file_list* ol;
107 for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
109 if (strcmp(ol->path, name) == 0) break;
111 return ol;
114 static BOOL source_locate_file(const char* srcfile, char* path)
116 BOOL found = FALSE;
118 if (GetFileAttributes(srcfile) != INVALID_FILE_ATTRIBUTES)
120 strcpy(path, srcfile);
121 found = TRUE;
123 else if (dbg_curr_process->search_path)
125 const char* spath;
127 spath = srcfile;
128 while (!found)
130 spath = strchr(spath, '\\');
131 if (!spath) spath = strchr(spath, '/');
132 if (!spath) break;
133 spath++;
134 found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
137 return found;
140 static struct open_file_list* source_add_file(const char* name, const char* realpath)
142 struct open_file_list* ol;
143 size_t sz, nlen;
145 sz = sizeof(*ol);
146 nlen = strlen(name) + 1;
147 if (realpath) sz += strlen(realpath) + 1;
148 ol = HeapAlloc(GetProcessHeap(), 0, sz + nlen);
149 if (!ol) return NULL;
150 strcpy(ol->path = (char*)(ol + 1), name);
151 if (realpath)
152 strcpy(ol->real_path = ol->path + nlen, realpath);
153 else
154 ol->real_path = NULL;
155 ol->next = dbg_curr_process->source_ofiles;
156 ol->nlines = 0;
157 ol->linelist = NULL;
158 ol->size = 0;
159 return dbg_curr_process->source_ofiles = ol;
162 static int source_display(const char* sourcefile, int start, int end)
164 char* addr;
165 int i;
166 struct open_file_list* ol;
167 int nlines;
168 const char* basename = NULL;
169 char* pnt;
170 int rtn;
171 HANDLE hMap;
172 char tmppath[MAX_PATH];
175 * First see whether we have the file open already. If so, then
176 * use that, otherwise we have to try and open it.
178 ol = source_search_open_file(sourcefile);
180 if (ol == NULL)
183 * Try again, stripping the path from the opened file.
185 basename = strrchr(sourcefile, '\\');
186 if (!basename) basename = strrchr(sourcefile, '/');
187 if (!basename) basename = sourcefile;
188 else basename++;
190 ol = source_search_open_file(basename);
193 if (ol == NULL)
196 * Crapola. We need to try and open the file.
198 if (!source_locate_file(sourcefile, tmppath))
200 if (dbg_interactiveP)
202 char zbuf[256];
204 * Still couldn't find it. Ask user for path to add.
206 snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
207 input_read_line(zbuf, tmppath, sizeof(tmppath));
209 if (tmppath[strlen(tmppath) - 1] != '/')
211 strcat(tmppath, "/");
214 * Now append the base file name.
216 strcat(tmppath, basename);
218 else tmppath[0] = '\0';
220 if (GetFileAttributes(tmppath) == INVALID_FILE_ATTRIBUTES)
223 * OK, I guess the user doesn't really want to see it
224 * after all.
226 ol = source_add_file(sourcefile, NULL);
227 dbg_printf("Unable to open file '%s'\n", tmppath);
228 return FALSE;
232 * Create header for file.
234 ol = source_add_file(sourcefile, tmppath);
236 addr = source_map_file(tmppath, &hMap, &ol->size);
237 if (addr == (char*)-1) return FALSE;
239 * Now build up the line number mapping table.
241 ol->nlines = 1;
242 pnt = addr;
243 while (pnt < addr + ol->size)
245 if (*pnt++ == '\n') ol->nlines++;
248 ol->nlines++;
249 ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
251 nlines = 0;
252 pnt = addr;
253 ol->linelist[nlines++] = 0;
254 while (pnt < addr + ol->size)
256 if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
258 ol->linelist[nlines++] = pnt - addr;
261 else
263 addr = source_map_file(ol->real_path, &hMap, NULL);
264 if (addr == (char*)-1) return FALSE;
267 * All we need to do is to display the source lines here.
269 rtn = FALSE;
270 for (i = start - 1; i <= end - 1; i++)
272 char buffer[1024];
274 if (i < 0 || i >= ol->nlines - 1) continue;
276 rtn = TRUE;
277 memset(&buffer, 0, sizeof(buffer));
278 if (ol->linelist[i+1] != ol->linelist[i])
280 memcpy(&buffer, addr + ol->linelist[i],
281 (ol->linelist[i+1] - ol->linelist[i]) - 1);
283 dbg_printf("%d\t%s\n", i + 1, buffer);
286 source_unmap_file(addr, hMap);
287 return rtn;
290 void source_list(IMAGEHLP_LINE* src1, IMAGEHLP_LINE* src2, int delta)
292 int end;
293 int start;
294 const char* sourcefile;
297 * We need to see what source file we need. Hopefully we only have
298 * one specified, otherwise we might as well punt.
300 if (src1 && src2 && src1->FileName && src2->FileName &&
301 strcmp(src1->FileName, src2->FileName) != 0)
303 dbg_printf("Ambiguous source file specification.\n");
304 return;
307 sourcefile = NULL;
308 if (src1 && src1->FileName) sourcefile = src1->FileName;
309 if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
310 if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;
313 * Now figure out the line number range to be listed.
315 start = end = -1;
317 if (src1) start = src1->LineNumber;
318 if (src2) end = src2->LineNumber;
319 if (start == -1 && end == -1)
321 if (delta < 0)
323 end = dbg_curr_process->source_start_line;
324 start = end + delta;
326 else
328 start = dbg_curr_process->source_end_line;
329 end = start + delta;
332 else if (start == -1) start = end + delta;
333 else if (end == -1) end = start + delta;
336 * Now call this function to do the dirty work.
338 source_display(sourcefile, start, end);
340 if (sourcefile != dbg_curr_process->source_current_file)
341 strcpy(dbg_curr_process->source_current_file, sourcefile);
342 dbg_curr_process->source_start_line = start;
343 dbg_curr_process->source_end_line = end;
346 void source_list_from_addr(const ADDRESS64* addr, int nlines)
348 IMAGEHLP_LINE il;
349 ADDRESS64 la;
350 DWORD disp;
352 if (!addr)
354 memory_get_current_pc(&la);
355 addr = &la;
358 il.SizeOfStruct = sizeof(il);
359 if (SymGetLineFromAddr(dbg_curr_process->handle,
360 (unsigned long)memory_to_linear_addr(addr),
361 &disp, &il))
362 source_list(&il, NULL, nlines);
365 void source_free_files(struct dbg_process* p)
367 struct open_file_list* ofile;
368 struct open_file_list* ofile_next;
370 for (ofile = p->source_ofiles; ofile; ofile = ofile_next)
372 ofile_next = ofile->next;
373 HeapFree(GetProcessHeap(), 0, ofile->linelist);
374 HeapFree(GetProcessHeap(), 0, ofile);