- the last event was not looked at when compressing console events
[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 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <limits.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #ifndef PATH_MAX
37 #define PATH_MAX MAX_PATH
38 #endif
39
40 #include "debugger.h"
41
42 struct searchlist
43 {
44   char * path;
45   struct searchlist * next;
46 };
47
48
49 struct open_filelist
50 {
51   char                  * path;
52   char                  * real_path;
53   struct open_filelist  * next;
54   unsigned int            size;
55   signed int              nlines;
56   unsigned int          * linelist;
57 };
58
59 static struct open_filelist * ofiles;
60
61 static struct searchlist * listhead;
62 static char DEBUG_current_sourcefile[PATH_MAX];
63 static int DEBUG_start_sourceline = -1;
64 static int DEBUG_end_sourceline = -1;
65
66 void
67 DEBUG_ShowDir(void)
68 {
69   struct searchlist * sl;
70
71   DEBUG_Printf(DBG_CHN_MESG,"Search list :\n");
72   for(sl = listhead; sl; sl = sl->next)
73     {
74       DEBUG_Printf(DBG_CHN_MESG, "\t%s\n", sl->path);
75     }
76   DEBUG_Printf(DBG_CHN_MESG, "\n");
77 }
78
79 void
80 DEBUG_AddPath(const char * path)
81 {
82   struct searchlist * sl;
83
84   sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
85   if( sl == NULL )
86     {
87       return;
88     }
89
90   sl->next = listhead;
91   sl->path = DBG_strdup(path);
92   listhead = sl;
93 }
94
95 void
96 DEBUG_NukePath(void)
97 {
98   struct searchlist * sl;
99   struct searchlist * nxt;
100
101   for(sl = listhead; sl; sl = nxt)
102     {
103       nxt = sl->next;
104       DBG_free(sl->path);
105       DBG_free(sl);
106     }
107
108   listhead = NULL;
109 }
110
111 static  void*   DEBUG_MapFile(const char* name, HANDLE* hMap, unsigned* size)
112 {
113     HANDLE              hFile;
114
115     hFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
116                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
117     if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
118     if (size != NULL && (*size = GetFileSize(hFile, NULL)) == -1) return (void*)-1;
119     *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
120     CloseHandle(hFile);
121     if (!*hMap) return (void*)-1;
122     return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
123 }
124
125 static void     DEBUG_UnmapFile(void* addr, HANDLE hMap)
126 {
127     UnmapViewOfFile(addr);
128     CloseHandle(hMap);
129 }
130
131 static struct open_filelist*    DEBUG_SearchOpenFile(const char* name)
132 {
133     struct open_filelist*       ol;
134
135     for (ol = ofiles; ol; ol = ol->next)
136     {
137         if (strcmp(ol->path, name) == 0) break;
138     }
139     return ol;
140 }
141
142 static
143 int
144 DEBUG_DisplaySource(char * sourcefile, int start, int end)
145 {
146     char*                       addr;
147     int                         i;
148     struct open_filelist*       ol;
149     int                         nlines;
150     char*                       basename = NULL;
151     char*                       pnt;
152     int                         rtn;
153     struct searchlist*          sl;
154     HANDLE                      hMap;
155     DWORD                       status;
156     char                        tmppath[PATH_MAX];
157
158     /*
159      * First see whether we have the file open already.  If so, then
160      * use that, otherwise we have to try and open it.
161      */
162     ol = DEBUG_SearchOpenFile(sourcefile);
163
164     if ( ol == NULL )
165     {
166         /*
167          * Try again, stripping the path from the opened file.
168          */
169         basename = strrchr(sourcefile, '\\' );
170         if ( !basename )
171             basename = strrchr(sourcefile, '/' );
172         if ( !basename )
173             basename = sourcefile;
174         else
175             basename++;
176
177         ol = DEBUG_SearchOpenFile(basename);
178     }
179
180     if ( ol == NULL )
181     {
182         /*
183          * Crapola.  We need to try and open the file.
184          */
185         status = GetFileAttributes(sourcefile);
186         if ( status != INVALID_FILE_ATTRIBUTES )
187         {
188             strcpy(tmppath, sourcefile);
189         }
190         else if ( (status = GetFileAttributes(basename)) != INVALID_FILE_ATTRIBUTES )
191         {
192             strcpy(tmppath, basename);
193         }
194         else
195         {
196             for (sl = listhead; sl; sl = sl->next)
197             {
198                 strcpy(tmppath, sl->path);
199                 if ( tmppath[strlen(tmppath)-1] != '/' && tmppath[strlen(tmppath)-1] != '\\' )
200                 {
201                     strcat(tmppath, "/");
202                 }
203                 /*
204                  * Now append the base file name.
205                  */
206                 strcat(tmppath, basename);
207
208                 status = GetFileAttributes(tmppath);
209                 if ( status != INVALID_FILE_ATTRIBUTES ) break;
210             }
211
212             if ( sl == NULL )
213             {
214                 if (DEBUG_InteractiveP)
215                 {
216                     char zbuf[256];
217                     /*
218                      * Still couldn't find it.  Ask user for path to add.
219                      */
220                     snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
221                     DEBUG_ReadLine(zbuf, tmppath, sizeof(tmppath));
222
223                     if ( tmppath[strlen(tmppath)-1] != '/' )
224                     {
225                         strcat(tmppath, "/");
226                     }
227                     /*
228                      * Now append the base file name.
229                      */
230                     strcat(tmppath, basename);
231
232                     status = GetFileAttributes(tmppath);
233                 }
234                 else
235                 {
236                     status = INVALID_FILE_ATTRIBUTES;
237                     strcpy(tmppath, sourcefile);
238                 }
239
240                 if ( status == INVALID_FILE_ATTRIBUTES )
241                 {
242                     /*
243                      * OK, I guess the user doesn't really want to see it
244                      * after all.
245                      */
246                     ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
247                     ol->path = DBG_strdup(sourcefile);
248                     ol->real_path = NULL;
249                     ol->next = ofiles;
250                     ol->nlines = 0;
251                     ol->linelist = NULL;
252                     ofiles = ol;
253                     DEBUG_Printf(DBG_CHN_MESG,"Unable to open file %s\n", tmppath);
254                     return FALSE;
255                 }
256             }
257         }
258         /*
259          * Create header for file.
260          */
261         ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
262         ol->path = DBG_strdup(sourcefile);
263         ol->real_path = DBG_strdup(tmppath);
264         ol->next = ofiles;
265         ol->nlines = 0;
266         ol->linelist = NULL;
267         ol->size = 0;
268         ofiles = ol;
269
270         addr = DEBUG_MapFile(tmppath, &hMap, &ol->size);
271         if ( addr == (char *) -1 )
272         {
273             return FALSE;
274         }
275         /*
276          * Now build up the line number mapping table.
277          */
278         ol->nlines = 1;
279         pnt = addr;
280         while (pnt < addr + ol->size )
281         {
282             if ( *pnt++ == '\n' )
283             {
284                 ol->nlines++;
285             }
286         }
287
288         ol->nlines++;
289         ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
290
291         nlines = 0;
292         pnt = addr;
293         ol->linelist[nlines++] = 0;
294         while(pnt < addr + ol->size )
295         {
296             if( *pnt++ == '\n' )
297             {
298                 ol->linelist[nlines++] = pnt - addr;
299             }
300         }
301         ol->linelist[nlines++] = pnt - addr;
302
303     }
304     else
305     {
306         addr = DEBUG_MapFile(ol->real_path, &hMap, NULL);
307         if ( addr == (char *) -1 )
308         {
309             return FALSE;
310         }
311     }
312     /*
313      * All we need to do is to display the source lines here.
314      */
315     rtn = FALSE;
316     for (i = start - 1; i <= end - 1; i++)
317     {
318         char    buffer[1024];
319
320         if (i < 0 || i >= ol->nlines - 1)
321         {
322             continue;
323         }
324
325         rtn = TRUE;
326         memset(&buffer, 0, sizeof(buffer));
327         if ( ol->linelist[i+1] != ol->linelist[i] )
328         {
329             memcpy(&buffer, addr + ol->linelist[i],
330                    (ol->linelist[i+1] - ol->linelist[i]) - 1);
331         }
332         DEBUG_Printf(DBG_CHN_MESG,"%d\t%s\n", i + 1,  buffer);
333     }
334
335     DEBUG_UnmapFile(addr, hMap);
336     return rtn;
337 }
338
339 void
340 DEBUG_List(struct list_id * source1, struct list_id * source2,
341                          int delta)
342 {
343   int    end;
344   int    rtn;
345   int    start;
346   char * sourcefile;
347
348   /*
349    * We need to see what source file we need.  Hopefully we only have
350    * one specified, otherwise we might as well punt.
351    */
352   if( source1 != NULL
353       && source2 != NULL
354       && source1->sourcefile != NULL
355       && source2->sourcefile != NULL
356       && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
357     {
358       DEBUG_Printf(DBG_CHN_MESG, "Ambiguous source file specification.\n");
359       return;
360     }
361
362   sourcefile = NULL;
363   if( source1 != NULL && source1->sourcefile != NULL )
364     {
365       sourcefile = source1->sourcefile;
366     }
367
368   if( sourcefile == NULL
369       && source2 != NULL
370       && source2->sourcefile != NULL )
371     {
372       sourcefile = source2->sourcefile;
373     }
374
375   if( sourcefile == NULL )
376     {
377       sourcefile = (char *) &DEBUG_current_sourcefile;
378     }
379
380   if( sourcefile == NULL )
381     {
382       DEBUG_Printf(DBG_CHN_MESG, "No source file specified.\n");
383       return;
384     }
385
386   /*
387    * Now figure out the line number range to be listed.
388    */
389   start = -1;
390   end = -1;
391
392   if( source1 != NULL )
393     {
394       start = source1->line;
395     }
396
397   if( source2 != NULL )
398     {
399       end = source2->line;
400     }
401
402   if( start == -1 && end == -1 )
403     {
404       if( delta < 0 )
405         {
406           end = DEBUG_start_sourceline;
407           start = end + delta;
408         }
409       else
410         {
411           start = DEBUG_end_sourceline;
412           end = start + delta;
413         }
414     }
415   else if( start == -1 )
416     {
417       start = end + delta;
418     }
419   else if (end == -1)
420     {
421       end = start + delta;
422     }
423
424   /*
425    * Now call this function to do the dirty work.
426    */
427   rtn = DEBUG_DisplaySource(sourcefile, start, end);
428
429   if( sourcefile != (char *) &DEBUG_current_sourcefile )
430     {
431       strcpy(DEBUG_current_sourcefile, sourcefile);
432     }
433   DEBUG_start_sourceline = start;
434   DEBUG_end_sourceline = end;
435 }
436
437 DBG_ADDR DEBUG_LastDisassemble={0,0};
438
439 BOOL DEBUG_DisassembleInstruction(DBG_ADDR *addr)
440 {
441    char         ch;
442    BOOL         ret = TRUE;
443
444    DEBUG_PrintAddress(addr, DEBUG_CurrThread->dbg_mode, TRUE);
445    DEBUG_Printf(DBG_CHN_MESG, ": ");
446    if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear(addr), &ch, sizeof(ch))) {
447       DEBUG_Printf(DBG_CHN_MESG, "-- no code --");
448       ret = FALSE;
449    } else {
450       DEBUG_Disasm(addr, TRUE);
451    }
452    DEBUG_Printf(DBG_CHN_MESG,"\n");
453    return ret;
454 }
455
456 void
457 DEBUG_Disassemble(const DBG_VALUE *xstart,const DBG_VALUE *xend,int offset)
458 {
459   int i;
460   DBG_ADDR      last;
461   DBG_VALUE     end,start;
462
463   if (xstart) {
464     start = *xstart;
465     DEBUG_GrabAddress(&start, TRUE);
466   }
467   if (xend) {
468     end = *xend;
469     DEBUG_GrabAddress(&end, TRUE);
470   }
471   if (!xstart && !xend) {
472     last = DEBUG_LastDisassemble;
473     if (!last.seg && !last.off)
474       DEBUG_GetCurrentAddress( &last );
475
476     for (i=0;i<offset;i++)
477       if (!DEBUG_DisassembleInstruction(&last)) break;
478     DEBUG_LastDisassemble = last;
479     return;
480   }
481   last = start.addr;
482   if (!xend) {
483     for (i=0;i<offset;i++)
484       if (!DEBUG_DisassembleInstruction(&last)) break;
485     DEBUG_LastDisassemble = last;
486     return;
487   }
488   while (last.off <= end.addr.off)
489     if (!DEBUG_DisassembleInstruction(&last)) break;
490   DEBUG_LastDisassemble = last;
491   return;
492 }