Implemented TOOLBAR_MouseLeave to handle WM_MOUSELEAVE messages.
[wine/multimedia.git] / debugger / source.c
blobe4f23652c813258b9b6880657f4f027d7374fdc3
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 "debugger.h"
27 struct searchlist
29 char * path;
30 struct searchlist * next;
34 struct open_filelist
36 char * path;
37 char * real_path;
38 struct open_filelist * next;
39 unsigned int size;
40 signed int nlines;
41 unsigned int * linelist;
44 static struct open_filelist * ofiles;
46 static struct searchlist * listhead;
47 static char DEBUG_current_sourcefile[PATH_MAX];
48 static int DEBUG_start_sourceline = -1;
49 static int DEBUG_end_sourceline = -1;
51 void
52 DEBUG_ShowDir(void)
54 struct searchlist * sl;
56 DEBUG_Printf(DBG_CHN_MESG,"Search list :\n");
57 for(sl = listhead; sl; sl = sl->next)
59 DEBUG_Printf(DBG_CHN_MESG, "\t%s\n", sl->path);
61 DEBUG_Printf(DBG_CHN_MESG, "\n");
64 void
65 DEBUG_AddPath(const char * path)
67 struct searchlist * sl;
69 sl = (struct searchlist *) DBG_alloc(sizeof(struct searchlist));
70 if( sl == NULL )
72 return;
75 sl->next = listhead;
76 sl->path = DBG_strdup(path);
77 listhead = sl;
80 void
81 DEBUG_NukePath(void)
83 struct searchlist * sl;
84 struct searchlist * nxt;
86 for(sl = listhead; sl; sl = nxt)
88 nxt = sl->next;
89 DBG_free(sl->path);
90 DBG_free(sl);
93 listhead = NULL;
96 static
97 int
98 DEBUG_DisplaySource(char * sourcefile, int start, int end)
100 char * addr;
101 char buffer[1024];
102 int fd;
103 int i;
104 struct open_filelist * ol;
105 int nlines;
106 char * basename;
107 char * pnt;
108 int rtn;
109 struct searchlist * sl;
110 struct stat statbuf;
111 int status;
112 char tmppath[PATH_MAX];
115 * First see whether we have the file open already. If so, then
116 * use that, otherwise we have to try and open it.
118 for(ol = ofiles; ol; ol = ol->next)
120 if( strcmp(ol->path, sourcefile) == 0 )
122 break;
126 if( ol == NULL )
129 * Try again, stripping the path from the opened file.
131 basename = strrchr(sourcefile, '\\' );
132 if ( !basename )
133 basename = strrchr(sourcefile, '/' );
134 if ( !basename )
135 basename = sourcefile;
136 else
137 basename++;
139 for(ol = ofiles; ol; ol = ol->next)
141 if( strcmp(ol->path, basename) == 0 )
143 break;
149 if( ol == NULL )
152 * Crapola. We need to try and open the file.
154 status = stat(sourcefile, &statbuf);
155 if( status != -1 )
157 strcpy(tmppath, sourcefile);
159 else if( (status = stat(basename, &statbuf)) != -1 )
161 strcpy(tmppath, basename);
163 else
165 for(sl = listhead; sl; sl = sl->next)
167 strcpy(tmppath, sl->path);
168 if( tmppath[strlen(tmppath)-1] != '/' )
170 strcat(tmppath, "/");
173 * Now append the base file name.
175 strcat(tmppath, basename);
177 status = stat(tmppath, &statbuf);
178 if( status != -1 )
180 break;
184 if( sl == NULL )
187 * Still couldn't find it. Ask user for path to add.
189 DEBUG_Printf(DBG_CHN_MESG,"Enter path to file %s: ", sourcefile);
190 fgets(tmppath, sizeof(tmppath), stdin);
192 if( tmppath[strlen(tmppath)-1] == '\n' )
194 tmppath[strlen(tmppath)-1] = '\0';
197 if( tmppath[strlen(tmppath)-1] != '/' )
199 strcat(tmppath, "/");
202 * Now append the base file name.
204 strcat(tmppath, basename);
206 status = stat(tmppath, &statbuf);
207 if( status == -1 )
210 * OK, I guess the user doesn't really want to see it
211 * after all.
213 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
214 ol->path = DBG_strdup(sourcefile);
215 ol->real_path = NULL;
216 ol->next = ofiles;
217 ol->nlines = 0;
218 ol->linelist = NULL;
219 ofiles = ol;
220 DEBUG_Printf(DBG_CHN_MESG,"Unable to open file %s\n", tmppath);
221 return FALSE;
226 * Create header for file.
228 ol = (struct open_filelist *) DBG_alloc(sizeof(*ol));
229 ol->path = DBG_strdup(sourcefile);
230 ol->real_path = DBG_strdup(tmppath);
231 ol->next = ofiles;
232 ol->nlines = 0;
233 ol->linelist = NULL;
234 ol->size = statbuf.st_size;
235 ofiles = ol;
238 * Now open and map the file.
240 fd = open(tmppath, O_RDONLY);
241 if( fd == -1 )
243 return FALSE;
246 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
247 if( addr == (char *) -1 )
249 return FALSE;
253 * Now build up the line number mapping table.
255 ol->nlines = 1;
256 pnt = addr;
257 while(pnt < addr + ol->size )
259 if( *pnt++ == '\n' )
261 ol->nlines++;
265 ol->nlines++;
266 ol->linelist = (unsigned int*) DBG_alloc( ol->nlines * sizeof(unsigned int) );
268 nlines = 0;
269 pnt = addr;
270 ol->linelist[nlines++] = 0;
271 while(pnt < addr + ol->size )
273 if( *pnt++ == '\n' )
275 ol->linelist[nlines++] = pnt - addr;
278 ol->linelist[nlines++] = pnt - addr;
281 else
284 * We know what the file is, we just need to reopen it and remap it.
286 fd = open(ol->real_path, O_RDONLY);
287 if( fd == -1 )
289 return FALSE;
292 addr = mmap(0, ol->size, PROT_READ, MAP_PRIVATE, fd, 0);
293 if( addr == (char *) -1 )
295 return FALSE;
300 * All we need to do is to display the source lines here.
302 rtn = FALSE;
303 for(i=start - 1; i <= end - 1; i++)
305 if( i < 0 || i >= ol->nlines - 1)
307 continue;
310 rtn = TRUE;
311 memset(&buffer, 0, sizeof(buffer));
312 if( ol->linelist[i+1] != ol->linelist[i] )
314 memcpy(&buffer, addr + ol->linelist[i],
315 (ol->linelist[i+1] - ol->linelist[i]) - 1);
317 DEBUG_Printf(DBG_CHN_MESG,"%d\t%s\n", i + 1, buffer);
320 munmap(addr, ol->size);
321 close(fd);
323 return rtn;
327 void
328 DEBUG_List(struct list_id * source1, struct list_id * source2,
329 int delta)
331 int end;
332 int rtn;
333 int start;
334 char * sourcefile;
337 * We need to see what source file we need. Hopefully we only have
338 * one specified, otherwise we might as well punt.
340 if( source1 != NULL
341 && source2 != NULL
342 && source1->sourcefile != NULL
343 && source2->sourcefile != NULL
344 && strcmp(source1->sourcefile, source2->sourcefile) != 0 )
346 DEBUG_Printf(DBG_CHN_MESG, "Ambiguous source file specification.\n");
347 return;
350 sourcefile = NULL;
351 if( source1 != NULL && source1->sourcefile != NULL )
353 sourcefile = source1->sourcefile;
356 if( sourcefile == NULL
357 && source2 != NULL
358 && source2->sourcefile != NULL )
360 sourcefile = source2->sourcefile;
363 if( sourcefile == NULL )
365 sourcefile = (char *) &DEBUG_current_sourcefile;
368 if( sourcefile == NULL )
370 DEBUG_Printf(DBG_CHN_MESG, "No source file specified.\n");
371 return;
375 * Now figure out the line number range to be listed.
377 start = -1;
378 end = -1;
380 if( source1 != NULL )
382 start = source1->line;
385 if( source2 != NULL )
387 end = source2->line;
390 if( start == -1 && end == -1 )
392 if( delta < 0 )
394 end = DEBUG_start_sourceline;
395 start = end + delta;
397 else
399 start = DEBUG_end_sourceline;
400 end = start + delta;
403 else if( start == -1 )
405 start = end + delta;
407 else if (end == -1)
409 end = start + delta;
413 * Now call this function to do the dirty work.
415 rtn = DEBUG_DisplaySource(sourcefile, start, end);
417 if( sourcefile != (char *) &DEBUG_current_sourcefile )
419 strcpy(DEBUG_current_sourcefile, sourcefile);
421 DEBUG_start_sourceline = start;
422 DEBUG_end_sourceline = end;
425 DBG_ADDR DEBUG_LastDisassemble={0,0};
427 static int
428 _disassemble(DBG_ADDR *addr)
430 char ch;
432 DEBUG_PrintAddress( addr, DEBUG_CurrThread->dbg_mode, TRUE );
433 DEBUG_Printf(DBG_CHN_MESG,": ");
434 if (!DEBUG_READ_MEM_VERBOSE((void*)DEBUG_ToLinear(addr), &ch, sizeof(ch))) return 0;
435 DEBUG_Disasm( addr, TRUE );
436 DEBUG_Printf(DBG_CHN_MESG,"\n");
437 return 1;
440 void
441 _disassemble_fixaddr(DBG_VALUE *value) {
442 DWORD seg2;
443 struct datatype *testtype;
445 assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
447 DEBUG_FixAddress(&value->addr, DEBUG_context.SegCs);
449 if( value->type != NULL )
451 if( value->type == DEBUG_TypeIntConst )
454 * We know that we have the actual offset stored somewhere
455 * else in 32-bit space. Grab it, and we
456 * should be all set.
458 seg2 = value->addr.seg;
459 value->addr.seg = 0;
460 value->addr.off = DEBUG_GetExprValue(value, NULL);
461 value->addr.seg = seg2;
463 else
465 DEBUG_TypeDerefPointer(value, &testtype);
466 if( testtype != NULL || value->type == DEBUG_TypeIntConst )
467 value->addr.off = DEBUG_GetExprValue(value, NULL);
470 else if (!value->addr.seg && !value->addr.off)
472 DEBUG_Printf(DBG_CHN_MESG,"Invalid expression\n");
473 return;
477 void
478 DEBUG_Disassemble(const DBG_VALUE *xstart,const DBG_VALUE *xend,int offset)
480 int i;
481 DBG_ADDR last;
482 DBG_VALUE end,start;
484 if (xstart) {
485 start = *xstart;
486 _disassemble_fixaddr(&start);
488 if (xend) {
489 end = *xend;
490 _disassemble_fixaddr(&end);
492 if (!xstart && !xend) {
493 last = DEBUG_LastDisassemble;
494 if (!last.seg && !last.off)
495 DEBUG_GetCurrentAddress( &last );
497 for (i=0;i<offset;i++)
498 if (!_disassemble(&last)) break;
499 DEBUG_LastDisassemble = last;
500 return;
502 last = start.addr;
503 if (!xend) {
504 for (i=0;i<offset;i++)
505 if (!_disassemble(&last)) break;
506 DEBUG_LastDisassemble = last;
507 return;
509 while (last.off <= end.addr.off)
510 if (!_disassemble(&last)) break;
511 DEBUG_LastDisassemble = last;
512 return;
517 #if 0
518 main(void)
520 int i, j;
521 DEBUG_AddPath("../../de");
522 while(1==1)
524 fscanf(stdin,"%d %d", &i, &j);
525 DEBUG_DisplaySource("dumpexe.c", i, j);
527 return 0;
529 #endif