Implementation of custom dialog messages and notifications.
[wine.git] / debugger / source.c
blobfb9dff3559c1232dcc353b32c2484195e1057e9a
1 /*
2 * File source.c - source file handling for internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
6 */
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdlib.h>
12 #include <sys/types.h>
13 #ifdef HAVE_SYS_MMAN_H
14 #include <sys/mman.h>
15 #endif
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <limits.h>
19 #include <string.h>
20 #include <unistd.h>
21 #ifndef PATH_MAX
22 #define PATH_MAX _MAX_PATH
23 #endif
25 #include "wine/winbase16.h"
26 #include "pe_image.h"
27 #include "peexe.h"
28 #include "debugger.h"
29 #include "peexe.h"
30 #include "task.h"
32 struct searchlist
34 char * path;
35 struct searchlist * next;
39 struct open_filelist
41 char * path;
42 char * real_path;
43 struct open_filelist * next;
44 unsigned int size;
45 signed int nlines;
46 unsigned int * linelist;
49 static struct open_filelist * ofiles;
51 static struct searchlist * listhead;
52 static char DEBUG_current_sourcefile[PATH_MAX];
53 static int DEBUG_start_sourceline = -1;
54 static int DEBUG_end_sourceline = -1;
56 void
57 DEBUG_ShowDir()
59 struct searchlist * sl;
61 fprintf(stderr,"Search list :\n");
62 for(sl = listhead; sl; sl = sl->next)
64 fprintf(stderr, "\t%s\n", sl->path);
66 fprintf(stderr, "\n");
69 void
70 DEBUG_AddPath(const char * path)
72 struct searchlist * sl;
74 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
75 if( sl == NULL )
77 return;
80 sl->next = listhead;
81 sl->path = DBG_strdup(path);
82 listhead = sl;
85 void
86 DEBUG_NukePath()
88 struct searchlist * sl;
89 struct searchlist * nxt;
91 for(sl = listhead; sl; sl = nxt)
93 nxt = sl->next;
94 DBG_free(sl->path);
95 DBG_free(sl);
98 listhead = NULL;
101 static
103 DEBUG_DisplaySource(char * sourcefile, int start, int end)
105 char * addr;
106 char buffer[1024];
107 int fd;
108 int i;
109 struct open_filelist * ol;
110 int nlines;
111 char * pnt;
112 int rtn;
113 struct searchlist * sl;
114 struct stat statbuf;
115 int status;
116 char tmppath[PATH_MAX];
119 * First see whether we have the file open already. If so, then
120 * use that, otherwise we have to try and open it.
122 for(ol = ofiles; ol; ol = ol->next)
124 if( strcmp(ol->path, sourcefile) == 0 )
126 break;
130 if( ol == NULL )
133 * Try again, stripping the path from the opened file.
135 for(ol = ofiles; ol; ol = ol->next)
137 pnt = strrchr(ol->path, '/');
138 if( pnt != NULL && strcmp(pnt + 1, sourcefile) == 0 )
140 break;
146 if( ol == NULL )
149 * See if this is a DOS style name or not.
151 pnt = strchr(sourcefile, '\\' );
152 if( pnt == NULL )
154 pnt = strchr(sourcefile, '/' );
155 if( pnt == NULL )
157 pnt = sourcefile;
162 * Crapola. We need to try and open the file.
164 status = stat(sourcefile, &statbuf);
165 if( status != -1 )
167 strcpy(tmppath, sourcefile);
169 else
171 for(sl = listhead; sl; sl = sl->next)
173 strcpy(tmppath, sl->path);
174 if( tmppath[strlen(tmppath)-1] != '/' )
176 strcat(tmppath, "/");
179 * Now append the base file name.
181 strcat(tmppath, pnt);
183 status = stat(tmppath, &statbuf);
184 if( status != -1 )
186 break;
190 if( sl == NULL )
193 * Still couldn't find it. Ask user for path to add.
195 fprintf(stderr,"Enter path to file %s: ", sourcefile);
196 fgets(tmppath, sizeof(tmppath), stdin);
198 if( tmppath[strlen(tmppath)-1] == '\n' )
200 tmppath[strlen(tmppath)-1] = '\0';
203 if( tmppath[strlen(tmppath)-1] != '/' )
205 strcat(tmppath, "/");
208 * Now append the base file name.
210 strcat(tmppath, pnt);
212 status = stat(tmppath, &statbuf);
213 if( status == -1 )
216 * OK, I guess the user doesn't really want to see it
217 * after all.
219 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
220 ol->path = DBG_strdup(sourcefile);
221 ol->real_path = NULL;
222 ol->next = ofiles;
223 ol->nlines = 0;
224 ol->linelist = NULL;
225 ofiles = ol;
226 fprintf(stderr,"Unable to open file %s\n", tmppath);
227 return FALSE;
232 * Create header for file.
234 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
235 ol->path = DBG_strdup(sourcefile);
236 ol->real_path = DBG_strdup(tmppath);
237 ol->next = ofiles;
238 ol->nlines = 0;
239 ol->linelist = NULL;
240 ol->size = statbuf.st_size;
241 ofiles = ol;
244 * Now open and map the file.
246 fd = open(tmppath, O_RDONLY);
247 if( fd == -1 )
249 return FALSE;
252 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
253 if( addr == (char *) -1 )
255 return FALSE;
259 * Now build up the line number mapping table.
261 ol->nlines = 1;
262 pnt = addr;
263 while(pnt < addr + ol->size )
265 if( *pnt++ == '\n' )
267 ol->nlines++;
271 ol->nlines++;
272 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
274 nlines = 0;
275 pnt = addr;
276 ol->linelist[nlines++] = 0;
277 while(pnt < addr + ol->size )
279 if( *pnt++ == '\n' )
281 ol->linelist[nlines++] = pnt - addr;
284 ol->linelist[nlines++] = pnt - addr;
287 else
290 * We know what the file is, we just need to reopen it and remap it.
292 fd = open(ol->real_path, O_RDONLY);
293 if( fd == -1 )
295 return FALSE;
298 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
299 if( addr == (char *) -1 )
301 return FALSE;
306 * All we need to do is to display the source lines here.
308 rtn = FALSE;
309 for(i=start - 1; i <= end - 1; i++)
311 if( i < 0 || i >= ol->nlines - 1)
313 continue;
316 rtn = TRUE;
317 memset(&buffer, 0, sizeof(buffer));
318 if( ol->linelist[i+1] != ol->linelist[i] )
320 memcpy(&buffer, addr + ol->linelist[i],
321 (ol->linelist[i+1] - ol->linelist[i]) - 1);
323 fprintf(stderr,"%d\t%s\n", i + 1, buffer);
326 munmap(addr, ol->size);
327 close(fd);
329 return rtn;
333 void
334 DEBUG_List(struct list_id * source1, struct list_id * source2,
335 int delta)
337 int end;
338 int rtn;
339 int start;
340 char * sourcefile;
343 * We need to see what source file we need. Hopefully we only have
344 * one specified, otherwise we might as well punt.
346 if( source1 != NULL
347 && source2 != NULL
348 && source1->sourcefile != NULL
349 && source2->sourcefile != NULL
350 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
352 fprintf(stderr, "Ambiguous source file specification.\n");
353 return;
356 sourcefile = NULL;
357 if( source1 != NULL && source1->sourcefile != NULL )
359 sourcefile = source1->sourcefile;
362 if( sourcefile == NULL
363 && source2 != NULL
364 && source2->sourcefile != NULL )
366 sourcefile = source2->sourcefile;
369 if( sourcefile == NULL )
371 sourcefile = (char *) &DEBUG_current_sourcefile;
374 if( sourcefile == NULL )
376 fprintf(stderr, "No source file specified.\n");
377 return;
381 * Now figure out the line number range to be listed.
383 start = -1;
384 end = -1;
386 if( source1 != NULL )
388 start = source1->line;
391 if( source2 != NULL )
393 end = source2->line;
396 if( start == -1 && end == -1 )
398 if( delta < 0 )
400 end = DEBUG_start_sourceline;
401 start = end + delta;
403 else
405 start = DEBUG_end_sourceline;
406 end = start + delta;
409 else if( start == -1 )
411 start = end + delta;
413 else if (end == -1)
415 end = start + delta;
419 * Now call this function to do the dirty work.
421 rtn = DEBUG_DisplaySource(sourcefile, start, end);
423 if( sourcefile != (char *) &DEBUG_current_sourcefile )
425 strcpy(DEBUG_current_sourcefile, sourcefile);
427 DEBUG_start_sourceline = start;
428 DEBUG_end_sourceline = end;
431 DBG_ADDR DEBUG_LastDisassemble={NULL,0,0};
433 static int
434 _disassemble(DBG_ADDR *addr)
436 DEBUG_PrintAddress( addr, dbg_mode, TRUE );
437 fprintf(stderr,": ");
438 if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
439 DEBUG_Disasm( addr, TRUE );
440 fprintf(stderr,"\n");
441 return 1;
444 void
445 _disassemble_fixaddr(DBG_ADDR *addr) {
446 DWORD seg2;
447 struct datatype *testtype;
449 DBG_FIX_ADDR_SEG(addr,CS_reg(&DEBUG_context));
450 if( addr->type != NULL )
452 if( addr->type == DEBUG_TypeIntConst )
455 * We know that we have the actual offset stored somewhere
456 * else in 32-bit space. Grab it, and we
457 * should be all set.
459 seg2 = addr->seg;
460 addr->seg = 0;
461 addr->off = DEBUG_GetExprValue(addr, NULL);
462 addr->seg = seg2;
464 else
466 if (!DBG_CHECK_READ_PTR( addr, 1 )) return;
467 DEBUG_TypeDerefPointer(addr, &testtype);
468 if( testtype != NULL || addr->type == DEBUG_TypeIntConst )
469 addr->off = DEBUG_GetExprValue(addr, NULL);
472 else if (!addr->seg && !addr->off)
474 fprintf(stderr,"Invalid expression\n");
475 return;
479 void
480 DEBUG_Disassemble(const DBG_ADDR *xstart,const DBG_ADDR *xend,int offset)
482 int i;
483 DBG_ADDR last;
484 DBG_ADDR end,start;
486 if (xstart) {
487 start=*xstart;
488 _disassemble_fixaddr(&start);
490 if (xend) {
491 end=*xend;
492 _disassemble_fixaddr(&end);
494 if (!xstart && !xend) {
495 last = DEBUG_LastDisassemble;
496 if (!last.seg && !last.off)
498 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
499 last.seg = CS_reg(&DEBUG_context);
500 last.off = EIP_reg(&DEBUG_context);
501 if (ISV86(&DEBUG_context)) last.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16; else
502 if (IS_SELECTOR_SYSTEM(last.seg)) last.seg = 0;
503 GlobalUnlock16( GetCurrentTask() );
505 for (i=0;i<offset;i++)
506 if (!_disassemble(&last)) break;
507 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
508 return;
510 last = start;
511 if (!xend) {
512 for (i=0;i<offset;i++)
513 if (!_disassemble(&last)) break;
514 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
515 return;
517 while (last.off <= end.off)
518 if (!_disassemble(&last)) break;
519 memcpy(&DEBUG_LastDisassemble,&last,sizeof(last));
520 return;
525 #if 0
526 main()
528 int i, j;
529 DEBUG_AddPath("../../de");
530 while(1==1)
532 fscanf(stdin,"%d %d", &i, &j);
533 DEBUG_DisplaySource("dumpexe.c", i, j);
535 return 0;
537 #endif