Release 970202
[wine] / debugger / source.c
1 /*
2  * File source.c - source file handling for internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <sys/mman.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <limits.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <malloc.h>
18
19 #include "win.h"
20 #include "pe_image.h"
21 #include "peexe.h"
22 #include "debugger.h"
23 #include "peexe.h"
24 #include "xmalloc.h"
25
26 struct searchlist
27 {
28   char * path;
29   struct searchlist * next;
30 };
31
32
33 struct open_filelist
34 {
35   char                  * path;
36   char                  * real_path;
37   struct open_filelist  * next;
38   unsigned int            size;
39   signed int              nlines;
40   unsigned int          * linelist;
41 };
42
43 static struct open_filelist * ofiles;
44
45 static struct searchlist * listhead;
46 static char DEBUG_current_sourcefile[PATH_MAX];
47 static int DEBUG_start_sourceline = -1;
48 static int DEBUG_end_sourceline = -1;
49
50 void
51 DEBUG_ShowDir()
52 {
53   struct searchlist * sl;
54
55   fprintf(stderr,"Search list :\n");
56   for(sl = listhead; sl; sl = sl->next)
57     {
58       fprintf(stderr, "\t%s\n", sl->path);
59     }
60   fprintf(stderr, "\n");
61 }
62
63 void
64 DEBUG_AddPath(const char * path)
65 {
66   struct searchlist * sl;
67
68   sl = (struct searchlist *) xmalloc(sizeof(struct searchlist));
69   if( sl == NULL )
70     {
71       return;
72     }
73
74   sl->next = listhead;
75   sl->path = xstrdup(path);
76   listhead = sl;
77 }
78
79 void
80 DEBUG_NukePath()
81 {
82   struct searchlist * sl;
83   struct searchlist * nxt;
84
85   for(sl = listhead; sl; sl = nxt)
86     {
87       nxt = sl->next;
88       free(sl->path);
89       free(sl);
90     }
91
92   listhead = NULL;
93 }
94
95 static
96 int
97 DEBUG_DisplaySource(char * sourcefile, int start, int end)
98 {
99   char                        * addr;
100   char                          buffer[1024];
101   int                           fd;
102   int                           i;
103   struct open_filelist        * ol;
104   int                           nlines;
105   char                        * pnt;
106   int                           rtn;
107   struct searchlist           * sl;
108   struct stat                   statbuf;
109   int                           status;
110   char                          tmppath[PATH_MAX];
111
112   /*
113    * First see whether we have the file open already.  If so, then
114    * use that, otherwise we have to try and open it.
115    */
116   for(ol = ofiles; ol; ol = ol->next)
117     {
118       if( strcmp(ol->path, sourcefile) == 0 )
119         {
120           break;
121         }
122     }
123
124   if( ol == NULL )
125     {
126       /*
127        * Try again, stripping the path from the opened file.
128        */
129       for(ol = ofiles; ol; ol = ol->next)
130         {
131           pnt = strrchr(ol->path, '/');
132           if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
133             {
134               break;
135             }
136         }
137       
138     }
139
140   if( ol == NULL )
141     {
142       /*
143        * See if this is a DOS style name or not.
144        */
145       pnt = strchr(sourcefile, '\\' );
146       if( pnt == NULL )
147         {
148           pnt = strchr(sourcefile, '/' );
149           if( pnt == NULL )
150             {
151               pnt = sourcefile;
152             }
153         }
154
155       /*
156        * Crapola.  We need to try and open the file.
157        */
158       status = stat(sourcefile, &statbuf);
159       if( status != -1 )
160         {
161           strcpy(tmppath, sourcefile);
162         }
163       else
164         {
165           for(sl = listhead; sl; sl = sl->next)
166             {
167               strcpy(tmppath, sl->path);
168               if( tmppath[strlen(tmppath)-1] != '/' )
169                 {
170                   strcat(tmppath, "/");
171                 }
172               /*
173                * Now append the base file name.
174                */
175               strcat(tmppath, pnt);
176               
177               status = stat(tmppath, &statbuf);
178               if( status != -1 )
179                 {
180                   break;
181                 }
182             }
183           
184           if( sl == NULL )
185             {
186               /*
187                * Still couldn't find it.  Ask user for path to add.
188                */
189               fprintf(stderr,"Enter path to file %s: ", sourcefile);
190               fgets(tmppath, sizeof(tmppath), stdin);
191               
192               if( tmppath[strlen(tmppath)-1] == '\n' )
193                 {
194                   tmppath[strlen(tmppath)-1] = '\0';
195                 }
196               
197               if( tmppath[strlen(tmppath)-1] != '/' )
198                 {
199                   strcat(tmppath, "/");
200                 }
201               /*
202                * Now append the base file name.
203                */
204               strcat(tmppath, pnt);
205               
206               status = stat(tmppath, &statbuf);
207               if( status == -1 )
208                 {
209                   /*
210                    * OK, I guess the user doesn't really want to see it
211                    * after all.
212                    */
213                   ol = (struct open_filelist *) xmalloc(sizeof(*ol));
214                   ol->path = xstrdup(sourcefile);
215                   ol->real_path = NULL;
216                   ol->next = ofiles;
217                   ol->nlines = 0;
218                   ol->linelist = NULL;
219                   ofiles = ol;
220                   fprintf(stderr,"Unable to open file %s\n", tmppath);
221                   return FALSE;
222                 }
223             }
224         }
225       /*
226        * Create header for file.
227        */
228       ol = (struct open_filelist *) xmalloc(sizeof(*ol));
229       ol->path = xstrdup(sourcefile);
230       ol->real_path = xstrdup(tmppath);
231       ol->next = ofiles;
232       ol->nlines = 0;
233       ol->linelist = NULL;
234       ol->size = statbuf.st_size;
235       ofiles = ol;
236
237       /*
238        * Now open and map the file.
239        */
240       fd = open(tmppath, O_RDONLY);
241       if( fd == -1 )
242         {
243           return FALSE;
244         }
245
246       addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
247       if( addr == (char *) -1 )
248         {
249           return FALSE;
250         }
251
252       /*
253        * Now build up the line number mapping table.
254        */
255       ol->nlines = 1;
256       pnt = addr;
257       while(pnt < addr + ol->size )
258         {
259           if( *pnt++ == '\n' )
260             {
261               ol->nlines++;
262             }
263         }
264
265       ol->nlines++;
266       ol->linelist = (unsigned int*) xmalloc(ol->nlines * sizeof(unsigned int) );
267
268       nlines = 0;
269       pnt = addr;
270       ol->linelist[nlines++] = 0;
271       while(pnt < addr + ol->size )
272         {
273           if( *pnt++ == '\n' )
274             {
275               ol->linelist[nlines++] = pnt - addr;
276             }
277         }
278       ol->linelist[nlines++] = pnt - addr;
279
280     }
281   else
282     {
283       /*
284        * We know what the file is, we just need to reopen it and remap it.
285        */
286       fd = open(ol->real_path, O_RDONLY);
287       if( fd == -1 )
288         {
289           return FALSE;
290         }
291       
292       addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
293       if( addr == (char *) -1 )
294         {
295           return FALSE;
296         }
297     }
298   
299   /*
300    * All we need to do is to display the source lines here.
301    */
302   rtn = FALSE;
303   for(i=start - 1; i <= end - 1; i++)
304     {
305       if( i < 0 || i >= ol->nlines - 1)
306         {
307           continue;
308         }
309
310       rtn = TRUE;
311       memset(&buffer, 0, sizeof(buffer));
312       if( ol->linelist[i+1] != ol->linelist[i] )
313         {
314           memcpy(&buffer, addr + ol->linelist[i], 
315                  (ol->linelist[i+1] - ol->linelist[i]) - 1);
316         }
317       fprintf(stderr,"%d\t%s\n", i + 1,  buffer);
318     }
319
320   munmap(addr, ol->size);
321   close(fd);
322
323   return rtn;
324
325 }
326
327 void
328 DEBUG_List(struct list_id * source1, struct list_id * source2,
329                          int delta)
330 {
331   int    end;
332   int    rtn;
333   int    start;
334   char * sourcefile;
335
336   /*
337    * We need to see what source file we need.  Hopefully we only have
338    * one specified, otherwise we might as well punt.
339    */
340   if( source1 != NULL 
341       && source2 != NULL 
342       && source1->sourcefile != NULL
343       && source2->sourcefile != NULL 
344       && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
345     {
346       fprintf(stderr, "Ambiguous source file specification.\n");
347       return;
348     }
349
350   sourcefile = NULL;
351   if( source1 != NULL && source1->sourcefile != NULL )
352     {
353       sourcefile = source1->sourcefile;
354     }
355
356   if( sourcefile == NULL 
357       && source2 != NULL 
358       && source2->sourcefile != NULL )
359     {
360       sourcefile = source2->sourcefile;
361     }
362
363   if( sourcefile == NULL )
364     {
365       sourcefile = (char *) &DEBUG_current_sourcefile;
366     }
367
368   if( sourcefile == NULL )
369     {
370       fprintf(stderr, "No source file specified.\n");
371       return;
372     }
373
374   /*
375    * Now figure out the line number range to be listed.
376    */
377   start = -1;
378   end = -1;
379
380   if( source1 != NULL )
381     {
382       start = source1->line;
383     }
384
385   if( source2 != NULL )
386     {
387       end = source2->line;
388     }
389
390   if( start == -1 && end == -1 )
391     {
392       if( delta < 0 )
393         {
394           end = DEBUG_start_sourceline;
395           start = end + delta;
396         }
397       else
398         {
399           start = DEBUG_end_sourceline;
400           end = start + delta;
401         }
402     }
403   else if( start == -1 )
404     {
405       start = end + delta;
406     }
407   else if (end == -1)
408     {
409       end = start + delta;
410     }
411
412   /*
413    * Now call this function to do the dirty work.
414    */
415   rtn = DEBUG_DisplaySource(sourcefile, start, end);
416
417   if( sourcefile != (char *) &DEBUG_current_sourcefile )
418     {
419       strcpy(DEBUG_current_sourcefile, sourcefile);
420     }
421   DEBUG_start_sourceline = start;
422   DEBUG_end_sourceline = end;
423 }
424
425
426
427 #if 0
428 main()
429 {
430   int i, j;
431   DEBUG_AddPath("../../de");
432   while(1==1)
433     {
434       fscanf(stdin,"%d %d", &i, &j);
435       DEBUG_DisplaySource("dumpexe.c", i, j);
436     }
437   return 0;
438 }
439 #endif