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