Fixed regression on 'bt <tid>' command.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #ifndef PATH_MAX
26 #define PATH_MAX MAX_PATH
27 #endif
28
29 #include "debugger.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
33
34 struct search_list
35 {
36     char*               path;
37     struct search_list*  next;
38 };
39
40
41 struct open_file_list
42 {
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;
49 };
50
51 static struct open_file_list*   source_ofiles;
52
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;
57
58 void source_show_path(void)
59 {
60     struct search_list* sl;
61
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");
65 }
66
67 void source_add_path(const char* path)
68 {
69     struct search_list* sl;
70
71     if (!(sl = HeapAlloc(GetProcessHeap(), 0, sizeof(struct search_list))))
72         return;
73
74     sl->next = source_list_head;
75     sl->path = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(path) + 1), path);
76     source_list_head = sl;
77 }
78
79 void source_nuke_path(void)
80 {
81     struct search_list* sl;
82     struct search_list* nxt;
83
84     for (sl = source_list_head; sl; sl = nxt)
85     {
86         nxt = sl->next;
87         HeapFree(GetProcessHeap(), 0, sl->path);
88         HeapFree(GetProcessHeap(), 0, sl);
89     }
90
91     source_list_head = NULL;
92 }
93
94 static  void*   source_map_file(const char* name, HANDLE* hMap, unsigned* size)
95 {
96     HANDLE              hFile;
97
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);
106 }
107
108 static void     source_unmap_file(void* addr, HANDLE hMap)
109 {
110     UnmapViewOfFile(addr);
111     CloseHandle(hMap);
112 }
113
114 static struct open_file_list* source_search_open_file(const char* name)
115 {
116     struct open_file_list*      ol;
117
118     for (ol = source_ofiles; ol; ol = ol->next)
119     {
120         if (strcmp(ol->path, name) == 0) break;
121     }
122     return ol;
123 }
124
125 static int source_display(const char* sourcefile, int start, int end)
126 {
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];
138
139     /*
140      * First see whether we have the file open already.  If so, then
141      * use that, otherwise we have to try and open it.
142      */
143     ol = source_search_open_file(sourcefile);
144
145     if (ol == NULL)
146     {
147         /*
148          * Try again, stripping the path from the opened file.
149          */
150         basename = strrchr(sourcefile, '\\');
151         if (!basename) basename = strrchr(sourcefile, '/');
152         if (!basename) basename = sourcefile;
153         else basename++;
154
155         ol = source_search_open_file(basename);
156     }
157
158     if (ol == NULL)
159     {
160         /*
161          * Crapola.  We need to try and open the file.
162          */
163         status = GetFileAttributes(sourcefile);
164         if (status != INVALID_FILE_ATTRIBUTES)
165         {
166             strcpy(tmppath, sourcefile);
167         }
168         else if ((status = GetFileAttributes(basename)) != INVALID_FILE_ATTRIBUTES)
169         {
170             strcpy(tmppath, basename);
171         }
172         else
173         {
174             for (sl = source_list_head; sl; sl = sl->next)
175             {
176                 strcpy(tmppath, sl->path);
177                 if (tmppath[strlen(tmppath) - 1] != '/' && tmppath[strlen(tmppath) - 1] != '\\')
178                 {
179                     strcat(tmppath, "/");
180                 }
181                 /*
182                  * Now append the base file name.
183                  */
184                 strcat(tmppath, basename);
185
186                 status = GetFileAttributes(tmppath);
187                 if (status != INVALID_FILE_ATTRIBUTES) break;
188             }
189
190             if (sl == NULL)
191             {
192                 if (dbg_interactiveP)
193                 {
194                     char zbuf[256];
195                     /*
196                      * Still couldn't find it.  Ask user for path to add.
197                      */
198                     snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
199                     input_read_line(zbuf, tmppath, sizeof(tmppath));
200
201                     if (tmppath[strlen(tmppath) - 1] != '/')
202                     {
203                         strcat(tmppath, "/");
204                     }
205                     /*
206                      * Now append the base file name.
207                      */
208                     strcat(tmppath, basename);
209
210                     status = GetFileAttributes(tmppath);
211                 }
212                 else
213                 {
214                     status = INVALID_FILE_ATTRIBUTES;
215                     strcpy(tmppath, sourcefile);
216                 }
217
218                 if (status == INVALID_FILE_ATTRIBUTES)
219                 {
220                     /*
221                      * OK, I guess the user doesn't really want to see it
222                      * after all.
223                      */
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;
233                 }
234             }
235         }
236         /*
237          * Create header for file.
238          */
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;
247
248         addr = source_map_file(tmppath, &hMap, &ol->size);
249         if (addr == (char*)-1) return FALSE;
250         /*
251          * Now build up the line number mapping table.
252          */
253         ol->nlines = 1;
254         pnt = addr;
255         while (pnt < addr + ol->size)
256         {
257             if (*pnt++ == '\n') ol->nlines++;
258         }
259
260         ol->nlines++;
261         ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
262
263         nlines = 0;
264         pnt = addr;
265         ol->linelist[nlines++] = 0;
266         while (pnt < addr + ol->size)
267         {
268             if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
269         }
270         ol->linelist[nlines++] = pnt - addr;
271
272     }
273     else
274     {
275         addr = source_map_file(ol->real_path, &hMap, NULL);
276         if (addr == (char*)-1) return FALSE;
277     }
278     /*
279      * All we need to do is to display the source lines here.
280      */
281     rtn = FALSE;
282     for (i = start - 1; i <= end - 1; i++)
283     {
284         char    buffer[1024];
285
286         if (i < 0 || i >= ol->nlines - 1) continue;
287
288         rtn = TRUE;
289         memset(&buffer, 0, sizeof(buffer));
290         if (ol->linelist[i+1] != ol->linelist[i])
291         {
292             memcpy(&buffer, addr + ol->linelist[i],
293                    (ol->linelist[i+1] - ol->linelist[i]) - 1);
294         }
295         dbg_printf("%d\t%s\n", i + 1, buffer);
296     }
297
298     source_unmap_file(addr, hMap);
299     return rtn;
300 }
301
302 void source_list(IMAGEHLP_LINE* src1, IMAGEHLP_LINE* src2, int delta)
303 {
304     int         end;
305     int         start;
306     const char* sourcefile;
307
308     /*
309      * We need to see what source file we need.  Hopefully we only have
310      * one specified, otherwise we might as well punt.
311      */
312     if (src1 && src2 && src1->FileName && src2->FileName &&
313         strcmp(src1->FileName, src2->FileName) != 0)
314     {
315         dbg_printf("Ambiguous source file specification.\n");
316         return;
317     }
318
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;
323
324     /*
325      * Now figure out the line number range to be listed.
326      */
327     start = end = -1;
328
329     if (src1) start = src1->LineNumber;
330     if (src2) end   = src2->LineNumber;
331     if (start == -1 && end == -1)
332     {
333         if (delta < 0)
334         {
335             end = source_start_line;
336             start = end + delta;
337         }
338         else
339         {
340             start = source_end_line;
341             end = start + delta;
342         }
343     }
344     else if (start == -1) start = end + delta;
345     else if (end == -1)   end = start + delta;
346
347     /*
348      * Now call this function to do the dirty work.
349      */
350     source_display(sourcefile, start, end);
351
352     if (sourcefile != source_current_file)
353         strcpy(source_current_file, sourcefile);
354     source_start_line = start;
355     source_end_line = end;
356 }
357
358 void source_list_from_addr(const ADDRESS* addr, int nlines)
359 {
360     IMAGEHLP_LINE       il;
361     DWORD               lin;
362
363     if (!addr)
364     {
365         ADDRESS la;
366         memory_get_current_pc(&la);
367         lin = (unsigned long)memory_to_linear_addr(&la);
368     }
369     else lin = (unsigned long)memory_to_linear_addr(addr);
370
371     il.SizeOfStruct = sizeof(il);
372     if (SymGetLineFromAddr(dbg_curr_process->handle, lin, NULL, &il))
373         source_list(&il, NULL, nlines);
374 }