break.c 32 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * Debugger break-points handling
 *
 * Copyright 1994 Martin von Loewis
 * Copyright 1995 Alexandre Julliard
6
 * Copyright 1999,2000 Eric Pouech
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
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
22

23
#include "config.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include "debugger.h"
25 26 27
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
Alexandre Julliard's avatar
Alexandre Julliard committed
28

29
static BOOL is_xpoint_break(int bpnum)
30 31 32 33 34 35
{
    int type = dbg_curr_process->bp[bpnum].xpoint_type;

    if (type == be_xpoint_break || type == be_xpoint_watch_exec) return TRUE;
    if (type == be_xpoint_watch_read || type == be_xpoint_watch_write) return FALSE;
    RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
36
    return FALSE; /* never reached */
37 38
}

Alexandre Julliard's avatar
Alexandre Julliard committed
39
/***********************************************************************
40
 *           break_set_xpoints
Alexandre Julliard's avatar
Alexandre Julliard committed
41
 *
42
 * Set or remove all the breakpoints & watchpoints
Alexandre Julliard's avatar
Alexandre Julliard committed
43
 */
44
void  break_set_xpoints(BOOL set)
Alexandre Julliard's avatar
Alexandre Julliard committed
45
{
46
    static BOOL                 last; /* = 0 = FALSE */
47

48
    unsigned int                i, ret, size;
49 50
    void*                       addr;
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;
51

52 53
    if (set == last) return;
    last = set;
54

55 56
    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
57
        if (!bp[i].refcount || !bp[i].enabled) continue;
58

59
        if (is_xpoint_break(i))
60 61 62
            size = 0;
        else
            size = bp[i].w.len + 1;
63
        addr = memory_to_linear_addr(&bp[i].addr);
64

65
        if (set)
66 67 68
            ret = dbg_curr_process->be_cpu->insert_Xpoint(dbg_curr_process->handle,
                dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
                addr, &bp[i].info, size);
69
        else
70 71 72
            ret = dbg_curr_process->be_cpu->remove_Xpoint(dbg_curr_process->handle,
                dbg_curr_process->process_io, &dbg_context, bp[i].xpoint_type,
                addr, bp[i].info, size);
73 74
        if (!ret)
        {
75 76 77
            dbg_printf("Invalid address (");
            print_address(&bp[i].addr, FALSE);
            dbg_printf(") for breakpoint %d, disabling it\n", i);
78 79 80
            bp[i].enabled = FALSE;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
81 82
}

Alexandre Julliard's avatar
Alexandre Julliard committed
83
/***********************************************************************
84
 *           find_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86 87 88
 *
 * Find the breakpoint for a given address. Return the breakpoint
 * number or -1 if none.
 */
89
static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type)
Alexandre Julliard's avatar
Alexandre Julliard committed
90
{
91 92 93 94 95 96 97 98 99 100 101
    int                         i;
    void*                       lin = memory_to_linear_addr(addr);
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
        if (bp[i].refcount && bp[i].enabled && bp[i].xpoint_type == type &&
            memory_to_linear_addr(&bp[i].addr) == lin)
            return i;
    }
    return -1;
102
}
Alexandre Julliard's avatar
Alexandre Julliard committed
103

104
/***********************************************************************
105
 *           init_xpoint
106 107 108
 *
 * Find an empty slot in BP table to add a new break/watch point
 */
109
static	int init_xpoint(int type, const ADDRESS64* addr)
110
{
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    int	                        num;
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    for (num = (dbg_curr_process->next_bp < MAX_BREAKPOINTS) ? 
             dbg_curr_process->next_bp++ : 1;
         num < MAX_BREAKPOINTS; num++)
    {
        if (bp[num].refcount == 0)
        {
            bp[num].refcount    = 1;
            bp[num].enabled     = TRUE;
            bp[num].xpoint_type = type;
            bp[num].skipcount   = 0;
            bp[num].addr        = *addr;
            return num;
        }
    }

    dbg_printf("Too many bp. Please delete some.\n");
    return -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
131 132
}

133
/***********************************************************************
134
 *           get_watched_value
135 136 137
 *
 * Returns the value watched by watch point 'num'.
 */
138
static	BOOL	get_watched_value(int num, DWORD64* val)
139
{
140
    DWORD64     buf[1];
141 142 143 144 145 146 147

    if (!dbg_read_memory(memory_to_linear_addr(&dbg_curr_process->bp[num].addr),
                         buf, dbg_curr_process->bp[num].w.len + 1))
        return FALSE;

    switch (dbg_curr_process->bp[num].w.len + 1)
    {
148
    case 8:	*val = *(DWORD64*)buf;	break;
149 150 151 152 153 154
    case 4:	*val = *(DWORD*)buf;	break;
    case 2:	*val = *(WORD*)buf;	break;
    case 1:	*val = *(BYTE*)buf;	break;
    default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
    }
    return TRUE;
155
}
Alexandre Julliard's avatar
Alexandre Julliard committed
156 157

/***********************************************************************
158
 *           break_add_break
Alexandre Julliard's avatar
Alexandre Julliard committed
159 160 161
 *
 * Add a breakpoint.
 */
162
BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp)
Alexandre Julliard's avatar
Alexandre Julliard committed
163
{
164 165 166
    int                         num;
    BYTE                        ch;
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;
167
    int                         type = swbp ? be_xpoint_break : be_xpoint_watch_exec;
Alexandre Julliard's avatar
Alexandre Julliard committed
168

169
    if ((num = find_xpoint(addr, type)) >= 1)
170
    {
171 172 173 174
        bp[num].refcount++;
        dbg_printf("Breakpoint %d at ", num);
        print_address(&bp[num].addr, TRUE);
        dbg_printf(" (refcount=%d)\n", bp[num].refcount);
175 176 177
        return TRUE;
    }

178
    if (!dbg_read_memory(memory_to_linear_addr(addr), &ch, sizeof(ch)))
179 180
    {
        if (verbose)
181 182 183 184 185
        {
            dbg_printf("Invalid address ");
            print_bare_address(addr);
            dbg_printf(", can't set breakpoint\n");
        }
186 187 188
        return FALSE;
    }

189
    if ((num = init_xpoint(type, addr)) == -1)
190 191
        return FALSE;

192 193 194
    dbg_printf("Breakpoint %d at ", num);
    print_address(&bp[num].addr, TRUE);
    dbg_printf("\n");
195

196
    return TRUE;
197 198 199
}

/***********************************************************************
200
 *           break_add_break_from_lvalue
201 202 203
 *
 * Add a breakpoint.
 */
204
BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
205
{
206
    ADDRESS64   addr;
207

208
    types_extract_as_address(lvalue, &addr);
209

210
    if (!break_add_break(&addr, TRUE, swbp))
211
    {
212 213
        if (!DBG_IVAR(CanDeferOnBPByAddr))
        {
214 215
            dbg_printf("Invalid address, can't set breakpoint\n"
                       "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
216 217
            return FALSE;
        }
218 219 220 221
        dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
        dbg_curr_process->delayed_bp = 
            dbg_heap_realloc(dbg_curr_process->delayed_bp,
                             sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
222

223 224 225
        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol   = FALSE;
        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.addr      = addr;
226
        return TRUE;
227
    }
228
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
229 230
}

231
/***********************************************************************
232
 *           break_add_break_from_id
233 234 235
 *
 * Add a breakpoint from a function name (and eventually a line #)
 */
236
void	break_add_break_from_id(const char *name, int lineno, BOOL swbp)
237
{
238 239
    struct dbg_lvalue 	lvalue;
    int		        i;
240

241
    switch (symbol_get_lvalue(name, lineno, &lvalue, TRUE))
242
    {
243
    case sglv_found:
244
        break_add_break(&lvalue.addr, TRUE, swbp);
245
        return;
246
    case sglv_unknown:
247
        break;
248
    case sglv_aborted: /* user aborted symbol lookup */
249
        return;
250
    }
251

252 253
    dbg_printf("Unable to add breakpoint, will check again when a new DLL is loaded\n");
    for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
254
    {
255 256 257
        if (dbg_curr_process->delayed_bp[i].is_symbol &&
            !strcmp(name, dbg_curr_process->delayed_bp[i].u.symbol.name) &&
            lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
258 259
            return;
    }
260 261
    dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
                                                    sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
262

263 264 265
    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol       = TRUE;
    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp     = swbp;
    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.name   = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
266
    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
267 268
}

269 270
struct cb_break_lineno
{
271
    const char* filename;
272
    int         lineno;
273
    ADDRESS64   addr;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
};

static BOOL CALLBACK line_cb(SRCCODEINFO* sci, void* user)
{
    struct cb_break_lineno*      bkln = user;

    if (bkln->lineno == sci->LineNumber)
    {
        bkln->addr.Mode = AddrModeFlat;
        bkln->addr.Offset = sci->Address;
        return FALSE;
    }
    return TRUE;
}

289 290 291 292 293 294 295 296 297 298 299
static BOOL CALLBACK mcb(PCWSTR module, DWORD64 base, void* user)
{
    struct cb_break_lineno*      bkln = user;

    SymEnumLines(dbg_curr_process->handle, base, NULL, bkln->filename, line_cb, bkln);
    /* continue module enum if no addr found
     * FIXME: we don't report when several addresses match the same filename/lineno pair
     */
    return !bkln->addr.Offset;
}

300
/***********************************************************************
301
 *           break_add_break_from_lineno
302 303 304
 *
 * Add a breakpoint from a line number in current file
 */
305
void break_add_break_from_lineno(const char *filename, int lineno, BOOL swbp)
306
{
307
    struct cb_break_lineno      bkln;
308 309
    bkln.addr.Offset = 0;
    bkln.lineno = lineno;
310

311
    if (!filename)
312
    {
313 314
        DWORD disp;
        ADDRESS64 curr;
315
        IMAGEHLP_LINE64 il;
316
        DWORD_PTR linear;
317

318 319 320 321
        memory_get_current_pc(&curr);
        linear = (DWORD_PTR)memory_to_linear_addr(&curr);
        il.SizeOfStruct = sizeof(il);
        if (!SymGetLineFromAddr64(dbg_curr_process->handle, linear, &disp, &il))
322
        {
323
            dbg_printf("Unable to add breakpoint (unknown address %lx)\n", linear);
324 325
            return;
        }
326 327
        filename = il.FileName;
        SymEnumLines(dbg_curr_process->handle, linear, NULL, filename, line_cb, &bkln);
328
    }
329 330 331 332 333 334 335 336 337 338 339
    else
    {
        /* we have to enumerate across modules */
        bkln.filename = filename;
        SymEnumerateModulesW64(dbg_curr_process->handle, mcb, &bkln);
    }
    if (bkln.addr.Offset)
        break_add_break(&bkln.addr, TRUE, swbp);
    else
        dbg_printf("Unknown line number\n"
                   "(either out of file, or no code at given line number)\n");
340
}
Alexandre Julliard's avatar
Alexandre Julliard committed
341

342
/***********************************************************************
343
 *           break_check_delayed_bp
344 345 346
 *
 * Check is a registered delayed BP is now available.
 */
347
void break_check_delayed_bp(void)
348
{
349 350 351
    struct dbg_lvalue	        lvalue;
    int			        i;
    struct dbg_delayed_bp*	dbp = dbg_curr_process->delayed_bp;
352
    char                        hexbuf[MAX_OFFSET_TO_STR_LEN];
353

354
    for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
355 356 357
    {
        if (dbp[i].is_symbol)
        {
358 359
            if (symbol_get_lvalue(dbp[i].u.symbol.name, dbp[i].u.symbol.lineno,
                                  &lvalue, TRUE) != sglv_found)
360
                continue;
361
            if (lvalue.cookie != DLV_TARGET) continue;
362 363
        }
        else
364
            lvalue.addr = dbp[i].u.addr;
365
        WINE_TRACE("trying to add delayed %s-bp\n", dbp[i].is_symbol ? "S" : "A");
366
        if (!dbp[i].is_symbol)
367 368 369
            WINE_TRACE("\t%04x:%s\n", 
                       dbp[i].u.addr.Segment,
                       memory_offset_to_string(hexbuf, dbp[i].u.addr.Offset, 0));
370
        else
371
            WINE_TRACE("\t'%s' @ %d\n", 
372
                       dbp[i].u.symbol.name, dbp[i].u.symbol.lineno);
373

374
        if (break_add_break(&lvalue.addr, FALSE, dbp[i].software_bp))
375
            memmove(&dbp[i], &dbp[i+1], (--dbg_curr_process->num_delayed_bp - i) * sizeof(*dbp));
376
    }
377 378
}

379
/***********************************************************************
380
 *           break_add_watch
381 382 383
 *
 * Add a watchpoint.
 */
384
static void break_add_watch(const struct dbg_lvalue* lvalue, BOOL is_write)
385
{
386
    int         num;
387
    DWORD64     l = 4;
388

389 390 391 392 393
    if (lvalue->cookie == DLV_HOST)
    {
        dbg_printf("Cannot set a watch point on register or register-based variable\n");
        return;
    }
394 395 396
    num = init_xpoint((is_write) ? be_xpoint_watch_write : be_xpoint_watch_read,
                      &lvalue->addr);
    if (num == -1) return;
397

398
    if (lvalue->type.id != dbg_itype_none)
399
    {
400
        if (types_get_info(&lvalue->type, TI_GET_LENGTH, &l))
401 402 403 404 405
        {
            switch (l)
            {
            case 4: case 2: case 1: break;
            default:
406 407
                dbg_printf("Unsupported length (%s) for watch-points, defaulting to 4\n",
                           wine_dbgstr_longlong(l));
408 409 410 411 412
                break;
            }
        }
        else dbg_printf("Cannot get watch size, defaulting to 4\n");
    }
413
    dbg_curr_process->bp[num].w.len = (DWORD)l - 1;
414

415 416 417 418 419 420 421 422 423
    if (!get_watched_value(num, &dbg_curr_process->bp[num].w.oldval))
    {
        dbg_printf("Bad address. Watchpoint not set\n");
        dbg_curr_process->bp[num].refcount = 0;
        return;
    }
    dbg_printf("Watchpoint %d at ", num);
    print_address(&dbg_curr_process->bp[num].addr, TRUE);
    dbg_printf("\n");
424 425
}

426 427 428 429 430
/******************************************************************
 *		break_add_watch_from_lvalue
 *
 * Adds a watch point from an address (stored in a lvalue)
 */
431
void break_add_watch_from_lvalue(const struct dbg_lvalue* lvalue,BOOL is_write)
432 433 434
{
    struct dbg_lvalue   lval;

435
    types_extract_as_address(lvalue, &lval.addr);
436 437
    lval.type.id = dbg_itype_none;

438
    break_add_watch(&lval, is_write);
439 440
}

441
/***********************************************************************
442
 *           break_add_watch_from_id
443
 *
444
 * Add a watchpoint from a symbol name
445
 */
446
void	break_add_watch_from_id(const char *name, BOOL is_write)
447
{
448 449 450 451 452
    struct dbg_lvalue    lvalue;

    switch (symbol_get_lvalue(name, -1, &lvalue, TRUE))
    {
    case sglv_found:
453
        break_add_watch(&lvalue, is_write);
454 455 456 457 458 459 460
        break;
    case sglv_unknown:
        dbg_printf("Unable to add watchpoint\n");
        break;
    case sglv_aborted: /* user aborted symbol lookup */
        break;
    }
461 462
}

Alexandre Julliard's avatar
Alexandre Julliard committed
463
/***********************************************************************
464
 *           break_delete_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
465 466 467
 *
 * Delete a breakpoint.
 */
468
void break_delete_xpoint(int num)
Alexandre Julliard's avatar
Alexandre Julliard committed
469
{
470 471 472 473
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    if ((num <= 0) || (num >= dbg_curr_process->next_bp) ||
        bp[num].refcount == 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
474
    {
475
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
476 477
        return;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
478

479 480
    if (--bp[num].refcount > 0)
        return;
481

482
    if (bp[num].condition != NULL)
483
    {
484 485
        expr_free(bp[num].condition);
        bp[num].condition = NULL;
486
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
487

488 489 490 491 492 493 494 495 496 497
    bp[num].enabled = FALSE;
    bp[num].refcount = 0;
    bp[num].skipcount = 0;
}

/******************************************************************
 *		break_delete_xpoints_from_module
 *
 * Remove all Xpoints from module which base is 'base'
 */
498
void break_delete_xpoints_from_module(DWORD64 base)
499
{
500
    IMAGEHLP_MODULE64           im, im_elf;
501
    int                         i;
502
    DWORD_PTR                   linear;
503 504
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

505
    /* FIXME: should do it also on the ELF sibling if any */
506 507
    im.SizeOfStruct = sizeof(im);
    im_elf.SizeOfStruct = sizeof(im_elf);
508
    if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
509 510

    /* try to get in fact the underlying ELF module (if any) */
511
    if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
512
        im_elf.BaseOfImage <= im.BaseOfImage &&
513
        im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
514 515 516 517
        im = im_elf;

    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
518
        if (bp[i].refcount && bp[i].enabled)
519
        {
520 521 522 523 524
            linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
            if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
            {
                break_delete_xpoint(i);
            }
525 526
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
527
}
Alexandre Julliard's avatar
Alexandre Julliard committed
528

Alexandre Julliard's avatar
Alexandre Julliard committed
529
/***********************************************************************
530
 *           break_enable_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
531 532 533
 *
 * Enable or disable a break point.
 */
534
void break_enable_xpoint(int num, BOOL enable)
Alexandre Julliard's avatar
Alexandre Julliard committed
535
{
536 537
    if ((num <= 0) || (num >= dbg_curr_process->next_bp) || 
        dbg_curr_process->bp[num].refcount == 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
538
    {
539
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
540 541
        return;
    }
542
    dbg_curr_process->bp[num].enabled = enable != 0;
543
    dbg_curr_process->bp[num].skipcount = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
544
}
Alexandre Julliard's avatar
Alexandre Julliard committed
545 546


547
/***********************************************************************
548
 *           find_triggered_watch
549 550
 *
 * Lookup the watchpoints to see if one has been triggered
551 552
 * Return >= (watch point index) if one is found
 * Return -1 if none found
553
 *
554
 * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
555 556 557 558
 * the DR6 register value, so we have to look with our own need the
 * cause of the TRAP.
 * -EP
 */
559
static int find_triggered_watch(void)
560
{
561 562 563 564 565 566 567 568 569
    int                         found = -1;
    int                         i;
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    /* Method 1 => get triggered watchpoint from context (doesn't work on Linux
     * 2.2.x). This should be fixed in >= 2.2.16
     */
    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
570
        DWORD64 val = 0;
571

572
        if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
573
            (dbg_curr_process->be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
574
        {
575
            dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

            if (get_watched_value(i, &val))
            {
                bp[i].w.oldval = val;
                return i;
            }
        }
    }

    /* Method 1 failed, trying method 2 */

    /* Method 2 => check if value has changed among registered watchpoints
     * this really sucks, but this is how gdb 4.18 works on my linux box
     * -EP
     */
    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
593
        DWORD64 val = 0;
594

595
        if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
596 597
            get_watched_value(i, &val))
        {
598
            if (val != bp[i].w.oldval)
599
            {
600
                dbg_curr_process->be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
601 602 603 604 605 606 607 608 609 610
                bp[i].w.oldval = val;
                found = i;
                /* cannot break, because two watch points may have been triggered on
                 * the same access
                 * only one will be reported to the user (FIXME ?)
                 */
            }
        }
    }
    return found;
611 612
}

Alexandre Julliard's avatar
Alexandre Julliard committed
613
/***********************************************************************
614
 *           break_info
Alexandre Julliard's avatar
Alexandre Julliard committed
615
 *
616
 * Display break & watch points information.
Alexandre Julliard's avatar
Alexandre Julliard committed
617
 */
618
void break_info(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
619
{
620 621 622 623 624 625 626 627 628
    int                         i;
    int                         nbp = 0, nwp = 0;
    struct dbg_delayed_bp*	dbp = dbg_curr_process->delayed_bp;
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    for (i = 1; i < dbg_curr_process->next_bp; i++)
    {
        if (bp[i].refcount)
        {
629
            if (is_xpoint_break(i)) nbp++; else nwp++;
630 631
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
632

633
    if (nbp)
Alexandre Julliard's avatar
Alexandre Julliard committed
634
    {
635 636
        dbg_printf("Breakpoints:\n");
        for (i = 1; i < dbg_curr_process->next_bp; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
637
        {
638
            if (!bp[i].refcount || !is_xpoint_break(i))
639 640 641
                continue;
            dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
            print_address(&bp[i].addr, TRUE);
642 643
            dbg_printf(" (%u)%s\n", bp[i].refcount, 
                       bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
644
	    if (bp[i].condition != NULL)
645
	    {
646 647 648
	        dbg_printf("\t\tstop when  ");
 		expr_print(bp[i].condition);
		dbg_printf("\n");
649
	    }
Alexandre Julliard's avatar
Alexandre Julliard committed
650 651
        }
    }
652 653
    else dbg_printf("No breakpoints\n");
    if (nwp)
654
    {
655 656
        dbg_printf("Watchpoints:\n");
        for (i = 1; i < dbg_curr_process->next_bp; i++)
657
        {
658
            if (!bp[i].refcount || is_xpoint_break(i))
659 660 661 662 663 664 665
                continue;
            dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
            print_address(&bp[i].addr, TRUE);
            dbg_printf(" on %d byte%s (%c)\n",
                       bp[i].w.len + 1, bp[i].w.len > 0 ? "s" : "",
                       bp[i].xpoint_type == be_xpoint_watch_write ? 'W' : 'R');
	    if (bp[i].condition != NULL)
666
	    {
667 668 669
	        dbg_printf("\t\tstop when ");
 		expr_print(bp[i].condition);
		dbg_printf("\n");
670 671 672
	    }
        }
    }
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    else dbg_printf("No watchpoints\n");
    if (dbg_curr_process->num_delayed_bp)
    {
        dbg_printf("Delayed breakpoints:\n");
        for (i = 0; i < dbg_curr_process->num_delayed_bp; i++)
        {
            if (dbp[i].is_symbol)
            {
                dbg_printf("%d: %s", i, dbp[i].u.symbol.name);
                if (dbp[i].u.symbol.lineno != -1)
                    dbg_printf(" at line %u", dbp[i].u.symbol.lineno);
            }
            else
            {
                dbg_printf("%d: ", i);
                print_address(&dbp[i].u.addr, FALSE);
            }
            dbg_printf("\n");
        }
    }
693 694 695
}

/***********************************************************************
696
 *           should_stop
697 698 699 700
 *
 * Check whether or not the condition (bp / skipcount) of a break/watch
 * point are met.
 */
701
static	BOOL should_stop(int bpnum)
702
{
703 704 705
    struct dbg_breakpoint*      bp = &dbg_curr_process->bp[bpnum];

    if (bp->condition != NULL)
706
    {
707
        struct dbg_lvalue lvalue = expr_eval(bp->condition);
708

709
        if (lvalue.type.id == dbg_itype_none)
710 711 712 713
        {
	    /*
	     * Something wrong - unable to evaluate this expression.
	     */
714 715 716 717
	    dbg_printf("Unable to evaluate expression ");
	    expr_print(bp->condition);
	    dbg_printf("\nTurning off condition\n");
	    break_add_condition(bpnum, NULL);
718
        }
719
        else if (!types_extract_as_integer(&lvalue))
720 721 722 723
        {
	    return FALSE;
        }
    }
724

725 726
    if (bp->skipcount > 0) bp->skipcount--;
    return bp->skipcount == 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
727
}
Alexandre Julliard's avatar
Alexandre Julliard committed
728

Alexandre Julliard's avatar
Alexandre Julliard committed
729
/***********************************************************************
730
 *           break_should_continue
Alexandre Julliard's avatar
Alexandre Julliard committed
731 732 733 734
 *
 * Determine if we should continue execution after a SIGTRAP signal when
 * executing in the given mode.
 */
735
BOOL break_should_continue(ADDRESS64* addr, DWORD code)
Alexandre Julliard's avatar
Alexandre Julliard committed
736
{
737
    enum dbg_exec_mode  mode = dbg_curr_thread->exec_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
738

Alexandre Julliard's avatar
Alexandre Julliard committed
739

740
    if (dbg_curr_thread->stopped_xpoint > 0)
741
    {
742
        if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
743

744 745 746 747 748 749 750 751 752 753 754 755
        switch (dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].xpoint_type)
        {
        case be_xpoint_break:
        case be_xpoint_watch_exec:
            dbg_printf("Stopped on breakpoint %d at ", dbg_curr_thread->stopped_xpoint);
            print_address(&dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].addr, TRUE);
            dbg_printf("\n");
            break;
        case be_xpoint_watch_read:
        case be_xpoint_watch_write:
            dbg_printf("Stopped on watchpoint %d at ", dbg_curr_thread->stopped_xpoint);
            print_address(addr, TRUE);
756 757
            dbg_printf(" new value %s\n",
                       wine_dbgstr_longlong(dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval));
758
        }
759
        return FALSE;
760 761
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
762 763 764 765 766
    /*
     * If our mode indicates that we are stepping line numbers,
     * get the current function, and figure out if we are exactly
     * on a line number or not.
     */
767
    if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
768
    {
769
	if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
770
            dbg_curr_thread->exec_count--;
771
    }
772
    else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
773
        dbg_curr_thread->exec_count--;
Alexandre Julliard's avatar
Alexandre Julliard committed
774

775
    if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
776
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
777 778 779 780
	/*
	 * We still need to execute more instructions.
	 */
	return TRUE;
781
    }
782

783 784 785 786 787
    /* no breakpoint, continue if in continuous mode */
    return mode == dbg_exec_cont || mode == dbg_exec_finish;
}

/***********************************************************************
788
 *           break_adjust_pc
789
 *
790
 * Adjust PC to the address where the trap (if any) actually occurred
791 792
 * Also sets dbg_curr_thread->stopped_xpoint
 */
793
void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
794
{
795 796 797 798 799 800 801
    /* break / watch points are handled on first chance */
    if ( !first_chance )
    {
        *is_break = TRUE;
        dbg_curr_thread->stopped_xpoint = -1;
        return;
    }
802 803 804 805
    *is_break = FALSE;

    /* If not single-stepping, back up to the break instruction */
    if (code == EXCEPTION_BREAKPOINT)
806
        addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, TRUE);
807 808 809 810 811 812 813 814 815 816

    dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_break);
    dbg_curr_process->bp[0].enabled = FALSE;  /* disable the step-over breakpoint */

    if (dbg_curr_thread->stopped_xpoint > 0) return;

    if (dbg_curr_thread->stopped_xpoint < 0)
    {
        dbg_curr_thread->stopped_xpoint = find_xpoint(addr, be_xpoint_watch_exec);
        if (dbg_curr_thread->stopped_xpoint < 0)
817
            dbg_curr_thread->stopped_xpoint = find_triggered_watch();
818 819 820 821
        if (dbg_curr_thread->stopped_xpoint > 0)
        {
            /* If not single-stepping, do not back up over the break instruction */
            if (code == EXCEPTION_BREAKPOINT)
822
                addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
823 824 825 826
            return;
        }
    }

827
    /* If there's no breakpoint and we are not single-stepping, then
828
     * either we must have encountered a break insn in the Windows program
829 830
     * or someone is trying to stop us
     */
831
    if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
832
    {
833
        *is_break = TRUE;
834
        addr->Offset += dbg_curr_process->be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
835
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
836 837
}

838
/***********************************************************************
839
 *           break_suspend_execution
840
 *
841
 * Remove all bp before entering the debug loop
842
 */
843
void	break_suspend_execution(void)
844
{
845 846
    break_set_xpoints(FALSE);
    dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
847
}
Alexandre Julliard's avatar
Alexandre Julliard committed
848 849

/***********************************************************************
850
 *           break_restart_execution
Alexandre Julliard's avatar
Alexandre Julliard committed
851
 *
852
 * Set the bp to the correct state to restart execution
Alexandre Julliard's avatar
Alexandre Julliard committed
853 854
 * in the given mode.
 */
855
void break_restart_execution(int count)
Alexandre Julliard's avatar
Alexandre Julliard committed
856
{
857
    ADDRESS64                   addr;
858 859
    enum dbg_line_status        status;
    enum dbg_exec_mode          mode, ret_mode;
860
    ADDRESS64                   callee;
861
    void*                       linear;
Alexandre Julliard's avatar
Alexandre Julliard committed
862

863
    memory_get_current_pc(&addr);
864
    linear = memory_to_linear_addr(&addr);
Alexandre Julliard's avatar
Alexandre Julliard committed
865

Alexandre Julliard's avatar
Alexandre Julliard committed
866 867 868 869
    /*
     * This is the mode we will be running in after we finish.  We would like
     * to be able to modify this in certain cases.
     */
870
    ret_mode = mode = dbg_curr_thread->exec_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
871

872 873
    /* we've stopped on a xpoint (other than step over) */
    if (dbg_curr_thread->stopped_xpoint > 0)
874
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
875 876 877
	/*
	 * If we have set a new value, then save it in the BP number.
	 */
878 879
	if (count != 0 && mode == dbg_exec_cont)
        {
880
	    dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
881
        }
882 883 884
        /* If we've stopped on a breakpoint, single step over it (, then run) */
        if (is_xpoint_break(dbg_curr_thread->stopped_xpoint))
            mode = dbg_exec_step_into_insn;
885 886 887 888 889 890
    }
    else if (mode == dbg_exec_cont && count > 1)
    {
        dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
    }

891
    if (mode == dbg_exec_finish && dbg_curr_process->be_cpu->is_function_return(linear))
892 893 894 895
    {
	mode = ret_mode = dbg_exec_step_into_insn;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
896 897 898 899 900 901
    /*
     * See if the function we are stepping into has debug info
     * and line numbers.  If not, then we step over it instead.
     * FIXME - we need to check for things like thunks or trampolines,
     * as the actual function may in fact have debug info.
     */
902
    if (dbg_curr_process->be_cpu->is_function_call(linear, &callee))
903 904 905 906
    {
	status = symbol_get_function_line_status(&callee);
#if 0
        /* FIXME: we need to get the thunk type */
Alexandre Julliard's avatar
Alexandre Julliard committed
907 908 909
	/*
	 * Anytime we have a trampoline, step over it.
	 */
910 911 912 913 914
	if ((mode == EXEC_STEP_OVER || mode == EXEC_STEPI_OVER)
	    && status == dbg_in_a_thunk)
        {
            WINE_WARN("Not stepping into trampoline at %p (no lines)\n", 
                      memory_to_linear_addr(&callee));
Alexandre Julliard's avatar
Alexandre Julliard committed
915
	    mode = EXEC_STEP_OVER_TRAMPOLINE;
916 917 918 919 920 921 922 923 924
        }
#endif
	if (mode == dbg_exec_step_into_line && status == dbg_no_line_info)
        {
            WINE_WARN("Not stepping into function at %p (no lines)\n",
                      memory_to_linear_addr(&callee));
	    mode = dbg_exec_step_over_line;
        }
    }
925

926 927 928
    if (mode == dbg_exec_step_into_line && 
        symbol_get_function_line_status(&addr) == dbg_no_line_info)
    {
929
        dbg_printf("Single stepping until exit from function,\n"
930 931 932
                   "which has no line number information.\n");
        ret_mode = mode = dbg_exec_finish;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
933

934
    switch (mode)
Alexandre Julliard's avatar
Alexandre Julliard committed
935
    {
936
    case dbg_exec_cont: /* Continuous execution */
937
        dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
938
        break_set_xpoints(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
939 940
        break;

941
#if 0
Alexandre Julliard's avatar
Alexandre Julliard committed
942
    case EXEC_STEP_OVER_TRAMPOLINE:
943 944 945 946 947 948 949 950
        /*
         * This is the means by which we step over our conversion stubs
         * in callfrom*.s and callto*.s.  We dig the appropriate address
         * off the stack, and we set the breakpoint there instead of the
         * address just after the call.
         */
        be_cpu->get_addr(dbg_curr_thread->handle, &dbg_context,
                         be_cpu_addr_stack, &addr);
951
        /* FIXME: we assume stack grows as on an i386 */
952 953 954 955 956 957 958
        addr.Offset += 2 * sizeof(unsigned int);
        dbg_read_memory(memory_to_linear_addr(&addr),
                        &addr.Offset, sizeof(addr.Offset));
        dbg_curr_process->bp[0].addr = addr;
        dbg_curr_process->bp[0].enabled = TRUE;
        dbg_curr_process->bp[0].refcount = 1;
        dbg_curr_process->bp[0].skipcount = 0;
959 960
        dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
        dbg_curr_process->bp[0].condition = NULL;
961 962 963
        be_cpu->single_step(&dbg_context, FALSE);
        break_set_xpoints(TRUE);
        break;
964
#endif
965 966 967 968

    case dbg_exec_finish:
    case dbg_exec_step_over_insn:  /* Stepping over a call */
    case dbg_exec_step_over_line:  /* Stepping over a call */
969
        if (dbg_curr_process->be_cpu->is_step_over_insn(linear))
Alexandre Julliard's avatar
Alexandre Julliard committed
970
        {
971
            dbg_curr_process->be_cpu->disasm_one_insn(&addr, FALSE);
972 973 974 975
            dbg_curr_process->bp[0].addr = addr;
            dbg_curr_process->bp[0].enabled = TRUE;
            dbg_curr_process->bp[0].refcount = 1;
	    dbg_curr_process->bp[0].skipcount = 0;
976 977
            dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
            dbg_curr_process->bp[0].condition = NULL;
978
            dbg_curr_process->be_cpu->single_step(&dbg_context, FALSE);
979
            break_set_xpoints(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
980 981 982
            break;
        }
        /* else fall through to single-stepping */
Alexandre Julliard's avatar
Alexandre Julliard committed
983

984 985
    case dbg_exec_step_into_line: /* Single-stepping a line */
    case dbg_exec_step_into_insn: /* Single-stepping an instruction */
986
        dbg_curr_process->be_cpu->single_step(&dbg_context, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
987
        break;
988
    default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
989
    }
990 991
    dbg_curr_thread->step_over_bp = dbg_curr_process->bp[0];
    dbg_curr_thread->exec_mode = ret_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
992 993
}

994
BOOL break_add_condition(int num, struct expr* exp)
Alexandre Julliard's avatar
Alexandre Julliard committed
995
{
996 997
    if (num <= 0 || num >= dbg_curr_process->next_bp || 
        !dbg_curr_process->bp[num].refcount)
Alexandre Julliard's avatar
Alexandre Julliard committed
998
    {
999
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
1000 1001 1002
        return FALSE;
    }

1003 1004 1005 1006 1007
    if (dbg_curr_process->bp[num].condition != NULL)
    {
	expr_free(dbg_curr_process->bp[num].condition);
	dbg_curr_process->bp[num].condition = NULL;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1008

1009
    if (exp != NULL) dbg_curr_process->bp[num].condition = expr_clone(exp, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1010

1011
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1012
}