winedbg: Fill the dbg_internal_var array for ARM.
[wine] / programs / winedbg / source.c
1 /*
2  * File source.c - source file handling for internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "debugger.h"
26
27 struct open_file_list
28 {
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;
35 };
36
37 void source_show_path(void)
38 {
39     const char* ptr;
40     const char* next;
41
42     dbg_printf("Search list:\n");
43     for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
44     {
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);
50     }
51     dbg_printf("\n");
52 }
53
54 void source_add_path(const char* path)
55 {
56     char*       new;
57     unsigned    size;
58
59     size = strlen(path) + 1;
60     if (dbg_curr_process->search_path)
61     {
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);
67     }
68     else
69     {
70         new = HeapAlloc(GetProcessHeap(), 0, size);
71         if (!new) return;
72         strcpy(new, path);
73     }
74     dbg_curr_process->search_path = new;
75 }
76
77 void source_nuke_path(struct dbg_process* p)
78 {
79     HeapFree(GetProcessHeap(), 0, p->search_path);
80     p->search_path = NULL;
81 }
82
83 static  void*   source_map_file(const char* name, HANDLE* hMap, unsigned* size)
84 {
85     HANDLE              hFile;
86
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)) == -1) return (void*)-1;
91     *hMap = CreateFileMappingW(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);
95 }
96
97 static void     source_unmap_file(void* addr, HANDLE hMap)
98 {
99     UnmapViewOfFile(addr);
100     CloseHandle(hMap);
101 }
102
103 static struct open_file_list* source_search_open_file(const char* name)
104 {
105     struct open_file_list*      ol;
106
107     for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
108     {
109         if (strcmp(ol->path, name) == 0) break;
110     }
111     return ol;
112 }
113
114 static BOOL     source_locate_file(const char* srcfile, char* path)
115 {
116     BOOL        found = FALSE;
117
118     if (GetFileAttributesA(srcfile) != INVALID_FILE_ATTRIBUTES)
119     {
120         strcpy(path, srcfile);
121         found = TRUE;
122     }
123     else if (dbg_curr_process->search_path)
124     {
125         const char* spath;
126
127         spath = srcfile;
128         while (!found)
129         {
130             while (*spath && *spath != '/' && *spath != '\\') spath++;
131             if (!*spath) break;
132             spath++;
133             found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
134         }
135     }
136     return found;
137 }
138
139 static struct open_file_list* source_add_file(const char* name, const char* realpath)
140 {
141     struct open_file_list*      ol;
142     size_t                      sz, nlen;
143
144     sz = sizeof(*ol);
145     nlen = strlen(name) + 1;
146     if (realpath) sz += strlen(realpath) + 1;
147     ol = HeapAlloc(GetProcessHeap(), 0, sz + nlen);
148     if (!ol) return NULL;
149     strcpy(ol->path = (char*)(ol + 1), name);
150     if (realpath)
151         strcpy(ol->real_path = ol->path + nlen, realpath);
152     else
153         ol->real_path = NULL;
154     ol->next = dbg_curr_process->source_ofiles;
155     ol->nlines = 0;
156     ol->linelist = NULL;
157     ol->size = 0;
158     return dbg_curr_process->source_ofiles = ol;
159 }
160
161 static int source_display(const char* sourcefile, int start, int end)
162 {
163     char*                       addr;
164     int                         i;
165     struct open_file_list*      ol;
166     int                         nlines;
167     const char*                 basename = NULL;
168     char*                       pnt;
169     int                         rtn;
170     HANDLE                      hMap;
171     char                        tmppath[MAX_PATH];
172
173     /*
174      * First see whether we have the file open already.  If so, then
175      * use that, otherwise we have to try and open it.
176      */
177     ol = source_search_open_file(sourcefile);
178
179     if (ol == NULL)
180     {
181         /*
182          * Try again, stripping the path from the opened file.
183          */
184         basename = strrchr(sourcefile, '\\');
185         if (!basename) basename = strrchr(sourcefile, '/');
186         if (!basename) basename = sourcefile;
187         else basename++;
188
189         ol = source_search_open_file(basename);
190     }
191
192     if (ol == NULL)
193     {
194         /*
195          * Crapola.  We need to try and open the file.
196          */
197         if (!source_locate_file(sourcefile, tmppath))
198         {
199             if (dbg_interactiveP)
200             {
201                 char zbuf[256];
202
203                 for (;;)
204                 {
205                     size_t      len;
206                     /*
207                      * Still couldn't find it.  Ask user for path to add.
208                      */
209                     snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s' (<cr> to end search): ", sourcefile);
210                     input_read_line(zbuf, tmppath, sizeof(tmppath));
211                     if (!(len = strlen(tmppath))) break;
212
213                     /* append '/' if missing at the end */
214                     if (tmppath[len - 1] != '/' && tmppath[len - 1] != '\\')
215                         tmppath[len++] = '/';
216                     strcpy(&tmppath[len], basename);
217                     if (GetFileAttributesA(tmppath) != INVALID_FILE_ATTRIBUTES)
218                         break;
219                     dbg_printf("Unable to access file '%s'\n", tmppath);
220                 }
221             }
222             else
223             {
224                 dbg_printf("Unable to access file '%s'\n", sourcefile);
225                 tmppath[0] = '\0';
226             }
227
228             if (!tmppath[0])
229             {
230                 /*
231                  * OK, I guess the user doesn't really want to see it
232                  * after all.
233                  */
234                 ol = source_add_file(sourcefile, NULL);
235                 return FALSE;
236             }
237         }
238         /*
239          * Create header for file.
240          */
241         ol = source_add_file(sourcefile, tmppath);
242
243         addr = source_map_file(tmppath, &hMap, &ol->size);
244         if (addr == (char*)-1) return FALSE;
245         /*
246          * Now build up the line number mapping table.
247          */
248         ol->nlines = 1;
249         pnt = addr;
250         while (pnt < addr + ol->size)
251         {
252             if (*pnt++ == '\n') ol->nlines++;
253         }
254
255         ol->nlines++;
256         ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
257
258         nlines = 0;
259         pnt = addr;
260         ol->linelist[nlines++] = 0;
261         while (pnt < addr + ol->size)
262         {
263             if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
264         }
265         ol->linelist[nlines++] = pnt - addr;
266
267     }
268     else
269     {
270         addr = source_map_file(ol->real_path, &hMap, NULL);
271         if (addr == (char*)-1) return FALSE;
272     }
273     /*
274      * All we need to do is to display the source lines here.
275      */
276     rtn = FALSE;
277     for (i = start - 1; i <= end - 1; i++)
278     {
279         char    buffer[1024];
280
281         if (i < 0 || i >= ol->nlines - 1) continue;
282
283         rtn = TRUE;
284         memset(&buffer, 0, sizeof(buffer));
285         if (ol->linelist[i+1] != ol->linelist[i])
286         {
287             memcpy(&buffer, addr + ol->linelist[i],
288                    (ol->linelist[i+1] - ol->linelist[i]) - 1);
289         }
290         dbg_printf("%d\t%s\n", i + 1, buffer);
291     }
292
293     source_unmap_file(addr, hMap);
294     return rtn;
295 }
296
297 void source_list(IMAGEHLP_LINE64* src1, IMAGEHLP_LINE64* src2, int delta)
298 {
299     int         end;
300     int         start;
301     const char* sourcefile;
302
303     /*
304      * We need to see what source file we need.  Hopefully we only have
305      * one specified, otherwise we might as well punt.
306      */
307     if (src1 && src2 && src1->FileName && src2->FileName &&
308         strcmp(src1->FileName, src2->FileName) != 0)
309     {
310         dbg_printf("Ambiguous source file specification.\n");
311         return;
312     }
313
314     sourcefile = NULL;
315     if (src1 && src1->FileName) sourcefile = src1->FileName;
316     if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
317     if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;
318
319     /*
320      * Now figure out the line number range to be listed.
321      */
322     start = end = -1;
323
324     if (src1) start = src1->LineNumber;
325     if (src2) end   = src2->LineNumber;
326     if (start == -1 && end == -1)
327     {
328         if (delta < 0)
329         {
330             end = dbg_curr_process->source_start_line;
331             start = end + delta;
332         }
333         else
334         {
335             start = dbg_curr_process->source_end_line;
336             end = start + delta;
337         }
338     }
339     else if (start == -1) start = end + delta;
340     else if (end == -1)   end = start + delta;
341
342     /*
343      * Now call this function to do the dirty work.
344      */
345     source_display(sourcefile, start, end);
346
347     if (sourcefile != dbg_curr_process->source_current_file)
348         strcpy(dbg_curr_process->source_current_file, sourcefile);
349     dbg_curr_process->source_start_line = start;
350     dbg_curr_process->source_end_line = end;
351 }
352
353 void source_list_from_addr(const ADDRESS64* addr, int nlines)
354 {
355     IMAGEHLP_LINE64     il;
356     ADDRESS64           la;
357     DWORD               disp;
358
359     if (!addr)
360     {
361         memory_get_current_pc(&la);
362         addr = &la;
363     }
364
365     il.SizeOfStruct = sizeof(il);
366     if (SymGetLineFromAddr64(dbg_curr_process->handle,
367                              (DWORD_PTR)memory_to_linear_addr(addr),
368                              &disp, &il))
369         source_list(&il, NULL, nlines);
370 }
371
372 void source_free_files(struct dbg_process* p)
373 {
374     struct open_file_list*      ofile;
375     struct open_file_list*      ofile_next;
376
377     for (ofile = p->source_ofiles; ofile; ofile = ofile_next)
378     {
379         ofile_next = ofile->next;
380         HeapFree(GetProcessHeap(), 0, ofile->linelist);
381         HeapFree(GetProcessHeap(), 0, ofile);
382     }
383 }