ncrypt/tests: Test for symmetric keys support.
[wine.git] / programs / winedbg / source.c
blob363cf2bf6549172ca6bde911c2efe0d6b8c8ec38
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 <stdio.h>
22 #include <stdlib.h>
24 #include "debugger.h"
26 struct open_file_list
28 char* path;
29 char* real_path;
30 struct open_file_list* next;
31 unsigned int size;
32 signed int nlines;
33 unsigned int* linelist;
36 void source_show_path(void)
38 const char* ptr;
39 const char* next;
41 dbg_printf("Search list:\n");
42 for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
44 next = strchr(ptr, ';');
45 if (next)
46 dbg_printf("\t%.*s\n", (int)(next++ - ptr), ptr);
47 else
48 dbg_printf("\t%s\n", ptr);
50 dbg_printf("\n");
53 void source_add_path(const char* path)
55 char* new;
56 unsigned size;
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);
63 if (!new) return;
64 new[pos - 1] = ';';
65 strcpy(&new[pos], path);
67 else
69 new = HeapAlloc(GetProcessHeap(), 0, size);
70 if (!new) return;
71 strcpy(new, path);
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)
84 HANDLE hFile;
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) {
90 CloseHandle(hFile);
91 return (void*)-1;
93 *hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
94 CloseHandle(hFile);
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);
102 CloseHandle(hMap);
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;
113 return ol;
116 static BOOL source_locate_file(const char* srcfile, char* path)
118 BOOL found = FALSE;
120 if (GetFileAttributesA(srcfile) != INVALID_FILE_ATTRIBUTES)
122 strcpy(path, srcfile);
123 found = TRUE;
125 else if (dbg_curr_process->search_path)
127 const char* spath;
129 spath = srcfile;
130 while (!found)
132 while (*spath && *spath != '/' && *spath != '\\') spath++;
133 if (!*spath) break;
134 spath++;
135 found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
138 return found;
141 static struct open_file_list* source_add_file(const char* name, const char* realpath)
143 struct open_file_list* ol;
144 size_t sz, nlen;
146 sz = sizeof(*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);
152 if (realpath)
153 strcpy(ol->real_path = ol->path + nlen, realpath);
154 else
155 ol->real_path = NULL;
156 ol->next = dbg_curr_process->source_ofiles;
157 ol->nlines = 0;
158 ol->linelist = NULL;
159 ol->size = 0;
160 return dbg_curr_process->source_ofiles = ol;
163 static int source_display(const char* sourcefile, int start, int end)
165 char* addr;
166 int i;
167 struct open_file_list* ol;
168 int nlines;
169 const char* basename = NULL;
170 char* pnt;
171 int rtn;
172 HANDLE hMap;
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);
181 if (ol == NULL)
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;
189 else basename++;
191 ol = source_search_open_file(basename);
194 if (ol == NULL)
197 * Crapola. We need to try and open the file.
199 if (!source_locate_file(sourcefile, tmppath))
201 if (dbg_interactiveP)
203 char zbuf[256];
205 for (;;)
207 size_t len;
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)
220 break;
221 dbg_printf("Unable to access file '%s'\n", tmppath);
224 else
226 dbg_printf("Unable to access file '%s'\n", sourcefile);
227 tmppath[0] = '\0';
230 if (!tmppath[0])
233 * OK, I guess the user doesn't really want to see it
234 * after all.
236 source_add_file(sourcefile, NULL);
237 return FALSE;
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.
250 ol->nlines = 1;
251 pnt = addr;
252 while (pnt < addr + ol->size)
254 if (*pnt++ == '\n') ol->nlines++;
257 ol->nlines++;
258 ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
260 nlines = 0;
261 pnt = addr;
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;
270 else
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.
278 rtn = FALSE;
279 for (i = start - 1; i <= end - 1; i++)
281 char buffer[1024];
283 if (i < 0 || i >= ol->nlines - 1) continue;
285 rtn = TRUE;
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);
296 return rtn;
299 void source_list(IMAGEHLP_LINE64* src1, IMAGEHLP_LINE64* src2, int delta)
301 int end;
302 int start;
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");
313 return;
316 sourcefile = NULL;
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.
324 start = end = -1;
326 if (src1) start = src1->LineNumber;
327 if (src2) end = src2->LineNumber;
328 if (start == -1 && end == -1)
330 if (delta < 0)
332 end = dbg_curr_process->source_start_line;
333 start = end + delta;
335 else
337 start = dbg_curr_process->source_end_line;
338 end = start + delta;
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)
357 IMAGEHLP_LINE64 il;
358 ADDRESS64 la;
359 DWORD disp;
361 if (!addr)
363 memory_get_current_pc(&la);
364 addr = &la;
367 il.SizeOfStruct = sizeof(il);
368 if (SymGetLineFromAddr64(dbg_curr_process->handle,
369 (DWORD_PTR)memory_to_linear_addr(addr),
370 &disp, &il))
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);