stack.c 13.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Debugger stack handling
 *
 * Copyright 1995 Alexandre Julliard
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 * Copyright 1996 Eric Youngdale
6
 * Copyright 1999 Ove Kven
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

23
#include "config.h"
24

Alexandre Julliard's avatar
Alexandre Julliard committed
25
#include <stdlib.h>
26
#include <stdio.h>
27

Alexandre Julliard's avatar
Alexandre Julliard committed
28
#include "debugger.h"
29
#include "winbase.h"
30
#include "wine/winbase16.h"
31
#include "tlhelp32.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
32 33

/***********************************************************************
34
 *           stack_info
Alexandre Julliard's avatar
Alexandre Julliard committed
35 36 37
 *
 * Dump the top of the stack
 */
38
void stack_info(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
39
{
40 41 42
    struct dbg_lvalue lvalue;

    lvalue.cookie = 0;
43
    lvalue.type.id = dbg_itype_segptr;
44
    lvalue.type.module = 0;
45

46
    /* FIXME: we assume stack grows the same way as on i386 */
47 48
    if (!memory_get_current_stack(&lvalue.addr))
        dbg_printf("Bad segment (%d)\n", lvalue.addr.Segment);
Alexandre Julliard's avatar
Alexandre Julliard committed
49

50
    dbg_printf("Stack dump:\n");
51
    switch (lvalue.addr.Mode)
52
    {
53 54
    case AddrModeFlat: /* 32-bit mode */
    case AddrMode1632: /* 32-bit mode */
55
        memory_examine(&lvalue, 24, 'x');
56 57 58
        break;
    case AddrModeReal:  /* 16-bit mode */
    case AddrMode1616:
59
        memory_examine(&lvalue, 24, 'w');
60
	break;
Alexandre Julliard's avatar
Alexandre Julliard committed
61 62 63
    }
}

64
static BOOL stack_set_frame_internal(int newframe)
Alexandre Julliard's avatar
Alexandre Julliard committed
65
{
66 67 68 69
    if (newframe >= dbg_curr_thread->num_frames)
        newframe = dbg_curr_thread->num_frames - 1;
    if (newframe < 0)
        newframe = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
70

71 72 73
    if (dbg_curr_thread->curr_frame != newframe)
    {
        IMAGEHLP_STACK_FRAME    ihsf;
74

75 76 77 78 79 80 81
        dbg_curr_thread->curr_frame = newframe;
        stack_get_current_frame(&ihsf);
        SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
    }
    return TRUE;
}

82
static BOOL stack_get_frame(int nf, IMAGEHLP_STACK_FRAME* ihsf)
83
{
84
    memset(ihsf, 0, sizeof(*ihsf));
85 86
    ihsf->InstructionOffset = dbg_curr_thread->frames[nf].linear_pc;
    ihsf->FrameOffset = dbg_curr_thread->frames[nf].linear_frame;
87 88 89
    return TRUE;
}

90 91 92 93 94 95
BOOL stack_get_current_frame(IMAGEHLP_STACK_FRAME* ihsf)
{
    /*
     * If we don't have a valid backtrace, then just return.
     */
    if (dbg_curr_thread->frames == NULL) return FALSE;
96
    return stack_get_frame(dbg_curr_thread->curr_frame, ihsf);
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
BOOL stack_get_register_current_frame(unsigned regno, DWORD** pval)
{
    enum be_cpu_addr            kind;

    if (dbg_curr_thread->frames == NULL) return FALSE;

    if (!be_cpu->get_register_info(regno, &kind)) return FALSE;

    switch (kind)
    {
    case be_cpu_addr_pc:
        *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_pc;
        break;
    case be_cpu_addr_stack:
        *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_stack;
        break;
    case be_cpu_addr_frame:
        *pval = &dbg_curr_thread->frames[dbg_curr_thread->curr_frame].linear_frame;
        break;
    }
    return TRUE;
}

122 123
BOOL stack_set_frame(int newframe)
{
124
    ADDRESS64   addr;
125
    if (!stack_set_frame_internal(newframe)) return FALSE;
126
    addr.Mode = AddrModeFlat;
127
    addr.Offset = (unsigned long)memory_to_linear_addr(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_pc);
128
    source_list_from_addr(&addr, 0);
129
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
130
}
Alexandre Julliard's avatar
Alexandre Julliard committed
131

132 133 134 135 136 137
/******************************************************************
 *		stack_get_current_symbol
 *
 * Retrieves the symbol information for the current frame element
 */
BOOL stack_get_current_symbol(SYMBOL_INFO* symbol)
Alexandre Julliard's avatar
Alexandre Julliard committed
138
{
139 140
    IMAGEHLP_STACK_FRAME        ihsf;
    DWORD64                     disp;
141

142 143 144
    if (!stack_get_current_frame(&ihsf)) return FALSE;
    return SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
                       &disp, symbol);
145
}
146

147
static BOOL CALLBACK stack_read_mem(HANDLE hProc, DWORD64 addr, 
148 149
                                    PVOID buffer, DWORD size, PDWORD written)
{
150 151 152
    SIZE_T sz;
    BOOL ret;

153 154
    struct dbg_process* pcs = dbg_get_process_h(hProc);
    if (!pcs) return FALSE;
155 156
    ret = pcs->process_io->read(hProc, (const void*)(DWORD_PTR)addr, buffer,
                                size, &sz);
157 158
    if (written != NULL) *written = sz;
    return ret;
159 160
}

161
/******************************************************************
162
 *		stack_fetch_frames
163
 *
164
 * Do a backtrace on the current thread
165
 */
166
unsigned stack_fetch_frames(void)
167
{
168 169
    STACKFRAME64 sf;
    unsigned     nf = 0;
170 171 172 173
    /* as native stackwalk can modify the context passed to it, simply copy
     * it to avoid any damage
     */
    CONTEXT      ctx = dbg_context;
174

175 176 177
    HeapFree(GetProcessHeap(), 0, dbg_curr_thread->frames);
    dbg_curr_thread->frames = NULL;

178 179 180 181
    memset(&sf, 0, sizeof(sf));
    memory_get_current_frame(&sf.AddrFrame);
    memory_get_current_pc(&sf.AddrPC);

182 183 184 185 186 187 188
    /* don't confuse StackWalk by passing in inconsistent addresses */
    if ((sf.AddrPC.Mode == AddrModeFlat) && (sf.AddrFrame.Mode != AddrModeFlat))
    {
        sf.AddrFrame.Offset = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
        sf.AddrFrame.Mode = AddrModeFlat;
    }

189
    while (StackWalk64(IMAGE_FILE_MACHINE_I386, dbg_curr_process->handle, 
190
                       dbg_curr_thread->handle, &sf, &ctx, stack_read_mem,
191
                       SymFunctionTableAccess64, SymGetModuleBase64, NULL))
Alexandre Julliard's avatar
Alexandre Julliard committed
192
    {
193 194
        dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames, 
                                                   (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
Alexandre Julliard's avatar
Alexandre Julliard committed
195

196 197 198 199 200 201
        dbg_curr_thread->frames[nf].addr_pc      = sf.AddrPC;
        dbg_curr_thread->frames[nf].linear_pc    = (DWORD)memory_to_linear_addr(&sf.AddrPC);
        dbg_curr_thread->frames[nf].addr_frame   = sf.AddrFrame;
        dbg_curr_thread->frames[nf].linear_frame = (DWORD)memory_to_linear_addr(&sf.AddrFrame);
        dbg_curr_thread->frames[nf].addr_stack   = sf.AddrStack;
        dbg_curr_thread->frames[nf].linear_stack = (DWORD)memory_to_linear_addr(&sf.AddrStack);
202
        nf++;
203
        /* we've probably gotten ourselves into an infinite loop so bail */
204 205
        if (nf > 200) break;
    }
206 207 208 209
    dbg_curr_thread->curr_frame = -1;
    dbg_curr_thread->num_frames = nf;
    stack_set_frame_internal(0);
    return nf;
210 211
}

212 213 214
struct sym_enum
{
    DWORD       frame;
215
    BOOL        first;
216 217
};

218
static BOOL WINAPI sym_enum_cb(PSYMBOL_INFO sym_info, ULONG size, PVOID user)
219 220 221
{
    struct sym_enum*    se = (struct sym_enum*)user;

222
    if (sym_info->Flags & SYMFLAG_PARAMETER)
223
    {
224 225
        if (!se->first) dbg_printf(", "); else se->first = FALSE;
        symbol_print_local(sym_info, se->frame, FALSE);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    }
    return TRUE;
}

static void stack_print_addr_and_args(int nf)
{
    char                        buffer[sizeof(SYMBOL_INFO) + 256];
    SYMBOL_INFO*                si = (SYMBOL_INFO*)buffer;
    IMAGEHLP_STACK_FRAME        ihsf;
    IMAGEHLP_LINE               il;
    IMAGEHLP_MODULE             im;
    DWORD64                     disp64;

    print_bare_address(&dbg_curr_thread->frames[nf].addr_pc);

241
    stack_get_frame(nf, &ihsf);
242 243 244 245 246 247 248 249 250 251 252 253 254 255

    /* grab module where symbol is. If we don't have a module, we cannot print more */
    im.SizeOfStruct = sizeof(im);
    if (!SymGetModuleInfo(dbg_curr_process->handle, ihsf.InstructionOffset, &im))
        return;

    si->SizeOfStruct = sizeof(*si);
    si->MaxNameLen   = 256;
    if (SymFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset, &disp64, si))
    {
        struct sym_enum se;
        DWORD           disp;

        dbg_printf(" %s", si->Name);
256
        if (disp64) dbg_printf("+0x%lx", (DWORD_PTR)disp64);
257 258

        SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
259
        se.first = TRUE;
260
        se.frame = ihsf.FrameOffset;
261
        dbg_printf("(");
262
        SymEnumSymbols(dbg_curr_process->handle, 0, NULL, sym_enum_cb, &se);
263
        dbg_printf(")");
264 265 266 267

        il.SizeOfStruct = sizeof(il);
        if (SymGetLineFromAddr(dbg_curr_process->handle, ihsf.InstructionOffset,
                               &disp, &il))
268
            dbg_printf(" [%s:%u]", il.FileName, il.LineNumber);
269 270 271 272 273 274
        dbg_printf(" in %s", im.ModuleName);
    }
    else dbg_printf(" in %s (+0x%lx)", 
                    im.ModuleName, (DWORD_PTR)(ihsf.InstructionOffset - im.BaseOfImage));
}

275 276 277
/******************************************************************
 *		backtrace
 *
278
 * Do a backtrace on the current thread
279
 */
280
static void backtrace(void)
281
{
282
    unsigned                    cf = dbg_curr_thread->curr_frame;
283
    IMAGEHLP_STACK_FRAME        ihsf;
284 285

    dbg_printf("Backtrace:\n");
286 287 288
    for (dbg_curr_thread->curr_frame = 0;
         dbg_curr_thread->curr_frame < dbg_curr_thread->num_frames;
         dbg_curr_thread->curr_frame++)
289 290
    {
        dbg_printf("%s%d ", 
291 292 293
                   (cf == dbg_curr_thread->curr_frame ? "=>" : "  "),
                   dbg_curr_thread->curr_frame + 1);
        stack_print_addr_and_args(dbg_curr_thread->curr_frame);
294
        dbg_printf(" (");
295
        print_bare_address(&dbg_curr_thread->frames[dbg_curr_thread->curr_frame].addr_frame);
296
        dbg_printf(")\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
297
    }
298
    /* reset context to current stack frame */
299
    dbg_curr_thread->curr_frame = cf;
300
    if (!dbg_curr_thread->frames) return;
301
    stack_get_frame(dbg_curr_thread->curr_frame, &ihsf);
302
    SymSetContext(dbg_curr_process->handle, &ihsf, NULL);
303 304 305 306 307 308 309 310
}

/******************************************************************
 *		backtrace_tid
 *
 * Do a backtrace on a thread from its process and its identifier
 * (preserves current thread and context information)
 */
311
static void backtrace_tid(struct dbg_process* pcs, DWORD tid)
312 313
{
    struct dbg_thread*  thread = dbg_curr_thread;
Alexandre Julliard's avatar
Alexandre Julliard committed
314

315
    if (!(dbg_curr_thread = dbg_get_thread(pcs, tid)))
316
        dbg_printf("Unknown thread id (%04x) in process (%04x)\n", tid, pcs->pid);
317 318 319 320 321 322 323 324 325 326 327
    else
    {
        CONTEXT saved_ctx = dbg_context;

        dbg_curr_tid = dbg_curr_thread->tid;
        memset(&dbg_context, 0, sizeof(dbg_context));
        dbg_context.ContextFlags = CONTEXT_FULL;
        if (SuspendThread(dbg_curr_thread->handle) != -1)
        {
            if (!GetThreadContext(dbg_curr_thread->handle, &dbg_context))
            {
328
                dbg_printf("Can't get context for thread %04x in current process\n",
329 330
                           tid);
            }
331 332 333 334 335
            else
            {
                stack_fetch_frames();
                backtrace();
            }
336 337
            ResumeThread(dbg_curr_thread->handle);
        }
338
        else dbg_printf("Can't suspend thread %04x in current process\n", tid);
339 340 341 342 343 344 345 346 347 348 349 350 351 352
        dbg_context = saved_ctx;
    }
    dbg_curr_thread = thread;
    dbg_curr_tid = thread ? thread->tid : 0;
}

/******************************************************************
 *		backtrace_all
 *
 * Do a backtrace on every running thread in the system (except the debugger)
 * (preserves current process information)
 */
static void backtrace_all(void)
{
353
    struct dbg_process* process = dbg_curr_process;
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    THREADENTRY32       entry;
    HANDLE              snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);

    if (snapshot == INVALID_HANDLE_VALUE)
    {
        dbg_printf("Unable to create toolhelp snapshot\n");
        return;
    }

    entry.dwSize = sizeof(entry);
    if (Thread32First(snapshot, &entry))
    {
        do
        {
            if (entry.th32OwnerProcessID == GetCurrentProcessId()) continue;
369
            if (dbg_curr_process && dbg_curr_pid != entry.th32OwnerProcessID)
370
                dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
371

372
            if (entry.th32OwnerProcessID != dbg_curr_pid)
373
            {
374
                if (!dbg_attach_debuggee(entry.th32OwnerProcessID, FALSE))
375
                {
376
                    dbg_printf("\nwarning: could not attach to %04x\n",
377 378 379 380
                               entry.th32OwnerProcessID);
                    continue;
                }
                dbg_curr_pid = dbg_curr_process->pid;
381
                dbg_active_wait_for_first_exception();
382 383
            }

384
            dbg_printf("\nBacktracing for thread %04x in process %04x (%s):\n",
385
                       entry.th32ThreadID, dbg_curr_pid, dbg_curr_process->imageName);
386
            backtrace_tid(dbg_curr_process, entry.th32ThreadID);
387 388 389
        }
        while (Thread32Next(snapshot, &entry));

390
        if (dbg_curr_process)
391
            dbg_curr_process->process_io->close_process(dbg_curr_process, FALSE);
392 393
    }
    CloseHandle(snapshot);
394 395
    dbg_curr_process = process;
    dbg_curr_pid = process ? process->pid : 0;
396 397
}

398
void stack_backtrace(DWORD tid)
399 400 401 402 403 404 405 406 407 408 409 410
{
    /* backtrace every thread in every process except the debugger itself,
     * invoking via "bt all"
     */
    if (tid == -1) return backtrace_all();

    if (!dbg_curr_process) 
    {
        dbg_printf("You must be attached to a process to run this command.\n");
        return;
    }
    
411
    if (tid == dbg_curr_tid)
Alexandre Julliard's avatar
Alexandre Julliard committed
412
    {
413
        backtrace();
Alexandre Julliard's avatar
Alexandre Julliard committed
414
    }
415
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
416
    {
417
        backtrace_tid(dbg_curr_process, tid);
Alexandre Julliard's avatar
Alexandre Julliard committed
418 419
    }
}