File: source.c

package info (click to toggle)
wine 4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 209,096 kB
  • sloc: ansic: 2,906,412; perl: 18,817; yacc: 15,629; makefile: 9,134; objc: 6,543; lex: 4,315; python: 1,786; cpp: 1,042; sh: 771; java: 742; xml: 557; awk: 69; cs: 17
file content (386 lines) | stat: -rw-r--r-- 10,944 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * File source.c - source file handling for internal debugger.
 *
 * Copyright (C) 1997, Eric Youngdale.
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>

#include "debugger.h"

struct open_file_list
{
    char*                       path;
    char*                       real_path;
    struct open_file_list*      next;
    unsigned int	        size;
    signed int		        nlines;
    unsigned int*               linelist;
};

void source_show_path(void)
{
    const char* ptr;
    const char* next;

    dbg_printf("Search list:\n");
    for (ptr = dbg_curr_process->search_path; ptr; ptr = next)
    {
        next = strchr(ptr, ';');
        if (next)
            dbg_printf("\t%.*s\n", (int)(next++ - ptr), ptr);
        else
            dbg_printf("\t%s\n", ptr);
    }
    dbg_printf("\n");
}

void source_add_path(const char* path)
{
    char*       new;
    unsigned    size;

    size = strlen(path) + 1;
    if (dbg_curr_process->search_path)
    {
        unsigned pos = strlen(dbg_curr_process->search_path) + 1;
        new = HeapReAlloc(GetProcessHeap(), 0, dbg_curr_process->search_path, pos + size);
        if (!new) return;
        new[pos - 1] = ';';
        strcpy(&new[pos], path);
    }
    else
    {
        new = HeapAlloc(GetProcessHeap(), 0, size);
        if (!new) return;
        strcpy(new, path);
    }
    dbg_curr_process->search_path = new;
}

void source_nuke_path(struct dbg_process* p)
{
    HeapFree(GetProcessHeap(), 0, p->search_path);
    p->search_path = NULL;
}

static  void*   source_map_file(const char* name, HANDLE* hMap, unsigned* size)
{
    HANDLE              hFile;

    hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) return (void*)-1;
    if (size != NULL && (*size = GetFileSize(hFile, NULL)) == INVALID_FILE_SIZE) {
        CloseHandle(hFile);
        return (void*)-1;
    }
    *hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    CloseHandle(hFile);
    if (!*hMap) return (void*)-1;
    return MapViewOfFile(*hMap, FILE_MAP_READ, 0, 0, 0);
}

static void     source_unmap_file(void* addr, HANDLE hMap)
{
    UnmapViewOfFile(addr);
    CloseHandle(hMap);
}

static struct open_file_list* source_search_open_file(const char* name)
{
    struct open_file_list*      ol;

    for (ol = dbg_curr_process->source_ofiles; ol; ol = ol->next)
    {
        if (strcmp(ol->path, name) == 0) break;
    }
    return ol;
}

static BOOL     source_locate_file(const char* srcfile, char* path)
{
    BOOL        found = FALSE;

    if (GetFileAttributesA(srcfile) != INVALID_FILE_ATTRIBUTES)
    {
        strcpy(path, srcfile);
        found = TRUE;
    }
    else if (dbg_curr_process->search_path)
    {
        const char* spath;

        spath = srcfile;
        while (!found)
        {
            while (*spath && *spath != '/' && *spath != '\\') spath++;
            if (!*spath) break;
            spath++;
            found = SearchPathA(dbg_curr_process->search_path, spath, NULL, MAX_PATH, path, NULL);
        }
    }
    return found;
}

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;
}

static int source_display(const char* sourcefile, int start, int end)
{
    char*                       addr;
    int				i;
    struct open_file_list*      ol;
    int				nlines;
    const char*                 basename = NULL;
    char*                       pnt;
    int				rtn;
    HANDLE                      hMap;
    char			tmppath[MAX_PATH];

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

    if (ol == NULL)
    {
        /*
         * Try again, stripping the path from the opened file.
         */
        basename = strrchr(sourcefile, '\\');
        if (!basename) basename = strrchr(sourcefile, '/');
        if (!basename) basename = sourcefile;
        else basename++;

        ol = source_search_open_file(basename);
    }

    if (ol == NULL)
    {
        /*
         * Crapola.  We need to try and open the file.
         */
        if (!source_locate_file(sourcefile, tmppath))
        {
            if (dbg_interactiveP)
            {
                char zbuf[256];

                for (;;)
                {
                    size_t      len;
                    /*
                     * Still couldn't find it.  Ask user for path to add.
                     */
                    snprintf(zbuf, sizeof(zbuf), "Enter path to file '%s' (<cr> to end search): ", sourcefile);
                    input_read_line(zbuf, tmppath, sizeof(tmppath));
                    if (!(len = strlen(tmppath))) break;

                    /* append '/' if missing at the end */
                    if (tmppath[len - 1] != '/' && tmppath[len - 1] != '\\')
                        tmppath[len++] = '/';
                    strcpy(&tmppath[len], basename);
                    if (GetFileAttributesA(tmppath) != INVALID_FILE_ATTRIBUTES)
                        break;
                    dbg_printf("Unable to access file '%s'\n", tmppath);
                }
            }
            else
            {
                dbg_printf("Unable to access file '%s'\n", sourcefile);
                tmppath[0] = '\0';
            }

            if (!tmppath[0])
            {
                /*
                 * OK, I guess the user doesn't really want to see it
                 * after all.
                 */
                source_add_file(sourcefile, NULL);
                return FALSE;
            }
        }
        /*
         * Create header for file.
         */
        ol = source_add_file(sourcefile, tmppath);

        addr = source_map_file(tmppath, &hMap, &ol->size);
        if (addr == (char*)-1) return FALSE;
        /*
         * Now build up the line number mapping table.
         */
        ol->nlines = 1;
        pnt = addr;
        while (pnt < addr + ol->size)
        {
            if (*pnt++ == '\n') ol->nlines++;
        }

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

        nlines = 0;
        pnt = addr;
        ol->linelist[nlines++] = 0;
        while (pnt < addr + ol->size)
        {
            if (*pnt++ == '\n') ol->linelist[nlines++] = pnt - addr;
        }
        ol->linelist[nlines++] = pnt - addr;

    }
    else
    {
        addr = source_map_file(ol->real_path, &hMap, NULL);
        if (addr == (char*)-1) return FALSE;
    }
    /*
     * All we need to do is to display the source lines here.
     */
    rtn = FALSE;
    for (i = start - 1; i <= end - 1; i++)
    {
        char    buffer[1024];

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

        rtn = TRUE;
        memset(&buffer, 0, sizeof(buffer));
        if (ol->linelist[i+1] != ol->linelist[i])
	{
            memcpy(&buffer, addr + ol->linelist[i],
                   (ol->linelist[i+1] - ol->linelist[i]) - 1);
	}
        dbg_printf("%d\t%s\n", i + 1, buffer);
    }

    source_unmap_file(addr, hMap);
    return rtn;
}

void source_list(IMAGEHLP_LINE64* src1, IMAGEHLP_LINE64* src2, int delta)
{
    int         end;
    int         start;
    const char* sourcefile;

    /*
     * 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)
    {
        dbg_printf("Ambiguous source file specification.\n");
        return;
    }

    sourcefile = NULL;
    if (src1 && src1->FileName) sourcefile = src1->FileName;
    if (!sourcefile && src2 && src2->FileName) sourcefile = src2->FileName;
    if (!sourcefile) sourcefile = dbg_curr_process->source_current_file;

    /*
     * Now figure out the line number range to be listed.
     */
    start = end = -1;

    if (src1) start = src1->LineNumber;
    if (src2) end   = src2->LineNumber;
    if (start == -1 && end == -1)
    {
        if (delta < 0)
	{
            end = dbg_curr_process->source_start_line;
            start = end + delta;
	}
        else
	{
            start = dbg_curr_process->source_end_line;
            end = start + delta;
	}
    }
    else if (start == -1) start = end + delta;
    else if (end == -1)   end = start + delta;

    /*
     * Now call this function to do the dirty work.
     */
    source_display(sourcefile, start, end);

    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;
}

void source_list_from_addr(const ADDRESS64* addr, int nlines)
{
    IMAGEHLP_LINE64     il;
    ADDRESS64           la;
    DWORD               disp;

    if (!addr)
    {
        memory_get_current_pc(&la);
        addr = &la;
    }

    il.SizeOfStruct = sizeof(il);
    if (SymGetLineFromAddr64(dbg_curr_process->handle,
                             (DWORD_PTR)memory_to_linear_addr(addr),
			     &disp, &il))
        source_list(&il, NULL, nlines);
}

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);
    }
}