source.c 10.3 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * File source.c - source file handling for internal debugger.
 *
 * Copyright (C) 1997, Eric Youngdale.
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
22 23 24 25
#include <stdio.h>
#include <stdlib.h>

#include "debugger.h"
26 27

struct open_file_list
Alexandre Julliard's avatar
Alexandre Julliard committed
28
{
29 30 31 32 33 34
    char*                       path;
    char*                       real_path;
    struct open_file_list*      next;
    unsigned int	        size;
    signed int		        nlines;
    unsigned int*               linelist;
Alexandre Julliard's avatar
Alexandre Julliard committed
35 36
};

37
void source_show_path(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
38
{
39 40
    const char* ptr;
    const char* next;
Alexandre Julliard's avatar
Alexandre Julliard committed
41

42
    dbg_printf("Search list:\n");
43
    for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
44 45 46 47 48 49 50
    {
        next = strchr(ptr, ';');
        if (next)
            dbg_printf("\t%.*s\n", next++ - ptr, ptr);
        else
            dbg_printf("\t%s\n", ptr);
    }
51
    dbg_printf("\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
52 53
}

54
void source_add_path(const char* path)
Alexandre Julliard's avatar
Alexandre Julliard committed
55
{
56 57
    char*       new;
    unsigned    size;
Alexandre Julliard's avatar
Alexandre Julliard committed
58

59
    size = strlen(path) + 1;
60
    if (dbg_curr_process->search_path)
61
    {
62 63
        unsigned pos = strlen(dbg_curr_process->search_path) + 1;
        new = HeapReAlloc(GetProcessHeap(), 0, dbg_curr_process->search_path, pos + size);
64
        if (!new) return;
65 66 67 68 69 70 71 72 73
        new[pos - 1] = ';';
        strcpy(&new[pos], path);
    }
    else
    {
        new = HeapAlloc(GetProcessHeap(), 0, size);
        if (!new) return;
        strcpy(new, path);
    }
74
    dbg_curr_process->search_path = new;
Alexandre Julliard's avatar
Alexandre Julliard committed
75 76
}

77
void source_nuke_path(struct dbg_process* p)
Alexandre Julliard's avatar
Alexandre Julliard committed
78
{
79 80
    HeapFree(GetProcessHeap(), 0, p->search_path);
    p->search_path = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
81 82
}

83
static  void*   source_map_file(const char* name, HANDLE* hMap, unsigned* size)
Alexandre Julliard's avatar
Alexandre Julliard committed
84
{
85 86
    HANDLE              hFile;

87
    hFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL,
88 89 90 91 92 93 94 95
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
    if (size != NULL && (*size = GetFileSize(hFile, NULL)) == -1) return (void*)-1;
    *hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    CloseHandle(hFile);
    if (!*hMap) return (void*)-1;
    return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
}
Alexandre Julliard's avatar
Alexandre Julliard committed
96

97
static void     source_unmap_file(void* addr, HANDLE hMap)
98 99 100 101 102
{
    UnmapViewOfFile(addr);
    CloseHandle(hMap);
}

103
static struct open_file_list* source_search_open_file(const char* name)
104
{
105
    struct open_file_list*      ol;
106

107
    for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
Alexandre Julliard's avatar
Alexandre Julliard committed
108
    {
109
        if (strcmp(ol->path, name) == 0) break;
Alexandre Julliard's avatar
Alexandre Julliard committed
110
    }
111 112
    return ol;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
113

114 115 116 117 118 119 120 121 122
static BOOL     source_locate_file(const char* srcfile, char* path)
{
    BOOL        found = FALSE;

    if (GetFileAttributes(srcfile) != INVALID_FILE_ATTRIBUTES)
    {
        strcpy(path, srcfile);
        found = TRUE;
    }
123
    else if (dbg_curr_process->search_path)
124 125 126 127 128 129
    {
        const char* spath;

        spath = srcfile;
        while (!found)
        {
130 131
            while (*spath && *spath != '/' && *spath != '\\') spath++;
            if (!*spath) break;
132
            spath++;
133
            found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
134 135 136 137 138
        }
    }
    return found;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static struct open_file_list* source_add_file(const char* name, const char* realpath)
{
    struct open_file_list*      ol;
    size_t                      sz, nlen;

    sz = sizeof(*ol);
    nlen = strlen(name) + 1;
    if (realpath) sz += strlen(realpath) + 1;
    ol = HeapAlloc(GetProcessHeap(), 0, sz + nlen);
    if (!ol) return NULL;
    strcpy(ol->path = (char*)(ol + 1), name);
    if (realpath)
        strcpy(ol->real_path = ol->path + nlen, realpath);
    else
        ol->real_path = NULL;
    ol->next = dbg_curr_process->source_ofiles;
    ol->nlines = 0;
    ol->linelist = NULL;
    ol->size = 0;
    return dbg_curr_process->source_ofiles = ol;
}

161
static int source_display(const char* sourcefile, int start, int end)
162 163 164
{
    char*                       addr;
    int				i;
165
    struct open_file_list*      ol;
166
    int				nlines;
167
    const char*                 basename = NULL;
168 169 170
    char*                       pnt;
    int				rtn;
    HANDLE                      hMap;
171
    char			tmppath[MAX_PATH];
172

173 174 175 176
    /*
     * First see whether we have the file open already.  If so, then
     * use that, otherwise we have to try and open it.
     */
177
    ol = source_search_open_file(sourcefile);
178

179
    if (ol == NULL)
Alexandre Julliard's avatar
Alexandre Julliard committed
180
    {
181 182 183
        /*
         * Try again, stripping the path from the opened file.
         */
184 185 186 187
        basename = strrchr(sourcefile, '\\');
        if (!basename) basename = strrchr(sourcefile, '/');
        if (!basename) basename = sourcefile;
        else basename++;
188

189
        ol = source_search_open_file(basename);
Alexandre Julliard's avatar
Alexandre Julliard committed
190
    }
191

192
    if (ol == NULL)
Alexandre Julliard's avatar
Alexandre Julliard committed
193
    {
194 195 196
        /*
         * Crapola.  We need to try and open the file.
         */
197
        if (!source_locate_file(sourcefile, tmppath))
198
        {
199
            if (dbg_interactiveP)
200
            {
201 202 203 204 205 206 207 208
                char zbuf[256];
                /*
                 * Still couldn't find it.  Ask user for path to add.
                 */
                snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s': ", sourcefile);
                input_read_line(zbuf, tmppath, sizeof(tmppath));

                if (tmppath[strlen(tmppath) - 1] != '/')
209 210 211 212 213 214 215 216
                {
                    strcat(tmppath, "/");
                }
                /*
                 * Now append the base file name.
                 */
                strcat(tmppath, basename);
            }
217
            else tmppath[0] = '\0';
218

219
            if (GetFileAttributes(tmppath) == INVALID_FILE_ATTRIBUTES)
220
            {
221 222 223 224
                /*
                 * OK, I guess the user doesn't really want to see it
                 * after all.
                 */
225
                ol = source_add_file(sourcefile, NULL);
226 227
                dbg_printf("Unable to open file '%s'\n", tmppath);
                return FALSE;
228 229 230 231 232
            }
        }
        /*
         * Create header for file.
         */
233
        ol = source_add_file(sourcefile, tmppath);
234

235 236
        addr = source_map_file(tmppath, &hMap, &ol->size);
        if (addr == (char*)-1) return FALSE;
237 238 239 240 241
        /*
         * Now build up the line number mapping table.
         */
        ol->nlines = 1;
        pnt = addr;
242
        while (pnt < addr + ol->size)
243
        {
244
            if (*pnt++ == '\n') ol->nlines++;
245
        }
246

247
        ol->nlines++;
248
        ol->linelist = HeapAlloc(GetProcessHeap(), 0, ol->nlines * sizeof(unsigned int));
249

250 251 252
        nlines = 0;
        pnt = addr;
        ol->linelist[nlines++] = 0;
253
        while (pnt < addr + ol->size)
254
        {
255
            if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
256 257
        }
        ol->linelist[nlines++] = pnt - addr;
258

Alexandre Julliard's avatar
Alexandre Julliard committed
259
    }
260
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
261
    {
262 263
        addr = source_map_file(ol->real_path, &hMap, NULL);
        if (addr == (char*)-1) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
264
    }
265 266 267 268 269
    /*
     * All we need to do is to display the source lines here.
     */
    rtn = FALSE;
    for (i = start - 1; i <= end - 1; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
270
    {
271 272
        char    buffer[1024];

273
        if (i < 0 || i >= ol->nlines - 1) continue;
274

275 276
        rtn = TRUE;
        memset(&buffer, 0, sizeof(buffer));
277
        if (ol->linelist[i+1] != ol->linelist[i])
Alexandre Julliard's avatar
Alexandre Julliard committed
278
	{
279 280
            memcpy(&buffer, addr + ol->linelist[i],
                   (ol->linelist[i+1] - ol->linelist[i]) - 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
281
	}
282
        dbg_printf("%d\t%s\n", i + 1, buffer);
Alexandre Julliard's avatar
Alexandre Julliard committed
283
    }
284

285
    source_unmap_file(addr, hMap);
286
    return rtn;
Alexandre Julliard's avatar
Alexandre Julliard committed
287 288
}

289
void source_list(IMAGEHLP_LINE* src1, IMAGEHLP_LINE* src2, int delta)
Alexandre Julliard's avatar
Alexandre Julliard committed
290
{
291 292 293
    int         end;
    int         start;
    const char* sourcefile;
Alexandre Julliard's avatar
Alexandre Julliard committed
294

295 296 297 298 299 300
    /*
     * We need to see what source file we need.  Hopefully we only have
     * one specified, otherwise we might as well punt.
     */
    if (src1 && src2 && src1->FileName && src2->FileName &&
        strcmp(src1->FileName, src2->FileName) != 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
301
    {
302 303
        dbg_printf("Ambiguous source file specification.\n");
        return;
Alexandre Julliard's avatar
Alexandre Julliard committed
304 305
    }

306 307 308
    sourcefile = NULL;
    if (src1 && src1->FileName) sourcefile = src1->FileName;
    if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
309
    if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;
Alexandre Julliard's avatar
Alexandre Julliard committed
310

311 312 313 314
    /*
     * Now figure out the line number range to be listed.
     */
    start = end = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
315

316 317 318
    if (src1) start = src1->LineNumber;
    if (src2) end   = src2->LineNumber;
    if (start == -1 && end == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
319
    {
320
        if (delta < 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
321
	{
322
            end = dbg_curr_process->source_start_line;
323
            start = end + delta;
Alexandre Julliard's avatar
Alexandre Julliard committed
324
	}
325
        else
Alexandre Julliard's avatar
Alexandre Julliard committed
326
	{
327
            start = dbg_curr_process->source_end_line;
328
            end = start + delta;
Alexandre Julliard's avatar
Alexandre Julliard committed
329 330
	}
    }
331 332
    else if (start == -1) start = end + delta;
    else if (end == -1)   end = start + delta;
Alexandre Julliard's avatar
Alexandre Julliard committed
333

334 335 336 337
    /*
     * Now call this function to do the dirty work.
     */
    source_display(sourcefile, start, end);
Alexandre Julliard's avatar
Alexandre Julliard committed
338

339 340 341 342
    if (sourcefile != dbg_curr_process->source_current_file)
        strcpy(dbg_curr_process->source_current_file, sourcefile);
    dbg_curr_process->source_start_line = start;
    dbg_curr_process->source_end_line = end;
Alexandre Julliard's avatar
Alexandre Julliard committed
343 344
}

345
void source_list_from_addr(const ADDRESS64* addr, int nlines)
Alexandre Julliard's avatar
Alexandre Julliard committed
346
{
347
    IMAGEHLP_LINE       il;
348
    ADDRESS64           la;
349
    DWORD               disp;
Alexandre Julliard's avatar
Alexandre Julliard committed
350

351 352 353
    if (!addr)
    {
        memory_get_current_pc(&la);
354
        addr = &la;
355 356 357
    }

    il.SizeOfStruct = sizeof(il);
358 359
    if (SymGetLineFromAddr(dbg_curr_process->handle,
                           (unsigned long)memory_to_linear_addr(addr),
360
                           &disp, &il))
361
        source_list(&il, NULL, nlines);
Alexandre Julliard's avatar
Alexandre Julliard committed
362
}
363 364 365 366 367 368 369 370 371 372 373 374 375

void source_free_files(struct dbg_process* p)
{
    struct open_file_list*      ofile;
    struct open_file_list*      ofile_next;

    for (ofile = p->source_ofiles; ofile; ofile = ofile_next)
    {
        ofile_next = ofile->next;
        HeapFree(GetProcessHeap(), 0, ofile->linelist);
        HeapFree(GetProcessHeap(), 0, ofile);
    }
}