First creation of registry entries missed AppData.
[wine/gsoc_dplay.git] / programs / winedbg / source.c
blob951195b848bddcfc021e4264ced7f402664b3cf7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
25 #ifndef PATH_MAX
26 #define PATH_MAX MAX_PATH
27 #endif
29 #include "debugger.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
34 struct search_list
36 char* path;
37 struct search_list* next;
41 struct open_file_list
43 char* path;
44 char* real_path;
45 struct open_file_list* next;
46 unsigned int size;
47 signed int nlines;
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);
64 dbg_printf("\n");
67 void source_add_path(const char* path)
69 struct search_list* sl;
71 if (!(sl = HeapAlloc(GetProcessHeap(), 0, sizeof(struct search_list))))
72 return;
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)
86 nxt = sl->next;
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)
96 HANDLE hFile;
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);
103 CloseHandle(hFile);
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);
111 CloseHandle(hMap);
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;
122 return ol;
125 static int source_display(const char* sourcefile, int start, int end)
127 char* addr;
128 int i;
129 struct open_file_list* ol;
130 int nlines;
131 const char* basename = NULL;
132 char* pnt;
133 int rtn;
134 struct search_list* sl;
135 HANDLE hMap;
136 DWORD status;
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);
145 if (ol == NULL)
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;
153 else basename++;
155 ol = source_search_open_file(basename);
158 if (ol == NULL)
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);
172 else
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;
190 if (sl == NULL)
192 if (dbg_interactiveP)
194 char zbuf[256];
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);
212 else
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
222 * after all.
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;
228 ol->nlines = 0;
229 ol->linelist = NULL;
230 source_ofiles = ol;
231 dbg_printf("Unable to open file '%s'\n", tmppath);
232 return FALSE;
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;
243 ol->nlines = 0;
244 ol->linelist = NULL;
245 ol->size = 0;
246 source_ofiles = ol;
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.
253 ol->nlines = 1;
254 pnt = addr;
255 while (pnt < addr + ol->size)
257 if (*pnt++ == '\n') ol->nlines++;
260 ol->nlines++;
261 ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
263 nlines = 0;
264 pnt = addr;
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;
273 else
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.
281 rtn = FALSE;
282 for (i = start - 1; i <= end - 1; i++)
284 char buffer[1024];
286 if (i < 0 || i >= ol->nlines - 1) continue;
288 rtn = TRUE;
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);
299 return rtn;
302 void source_list(IMAGEHLP_LINE* src1, IMAGEHLP_LINE* src2, int delta)
304 int end;
305 int start;
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");
316 return;
319 sourcefile = NULL;
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.
327 start = end = -1;
329 if (src1) start = src1->LineNumber;
330 if (src2) end = src2->LineNumber;
331 if (start == -1 && end == -1)
333 if (delta < 0)
335 end = source_start_line;
336 start = end + delta;
338 else
340 start = source_end_line;
341 end = start + delta;
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)
360 IMAGEHLP_LINE il;
361 ADDRESS la;
362 DWORD disp;
364 if (!addr)
366 memory_get_current_pc(&la);
367 addr = &la;
370 il.SizeOfStruct = sizeof(il);
371 if (SymGetLineFromAddr(dbg_curr_process->handle,
372 (unsigned long)memory_to_linear_addr(addr),
373 &disp, &il))
374 source_list(&il, NULL, nlines);