break.c 32.3 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 30 31 32 33 34 35 36 37 38
static int is_xpoint_break(int bpnum)
{
    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);
    return 0; /* never reached */
}

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

Alexandre Julliard's avatar
Alexandre Julliard committed
85
/***********************************************************************
86
 *           find_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
87 88 89 90
 *
 * Find the breakpoint for a given address. Return the breakpoint
 * number or -1 if none.
 */
91
static int find_xpoint(const ADDRESS64* addr, enum be_xpoint_type type)
Alexandre Julliard's avatar
Alexandre Julliard committed
92
{
93 94 95 96 97 98 99 100 101 102 103
    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;
104
}
Alexandre Julliard's avatar
Alexandre Julliard committed
105

106
/***********************************************************************
107
 *           init_xpoint
108 109 110
 *
 * Find an empty slot in BP table to add a new break/watch point
 */
111
static	int init_xpoint(int type, const ADDRESS64* addr)
112
{
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    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
133 134
}

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

    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)
    {
150
    case 8:	*val = *(DWORD64*)buf;	break;
151 152 153 154 155 156
    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;
157
}
Alexandre Julliard's avatar
Alexandre Julliard committed
158 159

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

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

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

191
    if ((num = init_xpoint(type, addr)) == -1)
192 193
        return FALSE;

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

198
    return TRUE;
199 200 201
}

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

210
    types_extract_as_address(lvalue, &addr);
211

212
    if (!break_add_break(&addr, TRUE, swbp))
213
    {
214 215
        if (!DBG_IVAR(CanDeferOnBPByAddr))
        {
216 217
            dbg_printf("Invalid address, can't set breakpoint\n"
                       "You can turn on deferring bp by address by setting $CanDeferOnBPByAddr to 1\n");
218 219
            return FALSE;
        }
220 221 222 223
        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);
224

225 226 227
        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;
228
        return TRUE;
229
    }
230
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
231 232
}

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

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

254 255
    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++)
256
    {
257 258 259
        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)
260 261
            return;
    }
262 263
    dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
                                                    sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
264

265 266 267
    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);
268
    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
269 270
}

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

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

291 292 293 294 295 296 297 298 299 300 301
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;
}

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

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

320 321 322 323
        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))
324
        {
325
            dbg_printf("Unable to add breakpoint (unknown address %lx)\n", linear);
326 327
            return;
        }
328 329
        filename = il.FileName;
        SymEnumLines(dbg_curr_process->handle, linear, NULL, filename, line_cb, &bkln);
330
    }
331 332 333 334 335 336 337 338 339 340 341
    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");
342
}
Alexandre Julliard's avatar
Alexandre Julliard committed
343

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

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

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

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

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

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

417 418 419 420 421 422 423 424 425
    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");
426 427
}

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

437
    types_extract_as_address(lvalue, &lval.addr);
438 439
    lval.type.id = dbg_itype_none;

440
    break_add_watch(&lval, is_write);
441 442
}

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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
465
/***********************************************************************
466
 *           break_delete_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
467 468 469
 *
 * Delete a breakpoint.
 */
470
void break_delete_xpoint(int num)
Alexandre Julliard's avatar
Alexandre Julliard committed
471
{
472 473 474 475
    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
476
    {
477
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
478 479
        return;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
480

481 482
    if (--bp[num].refcount > 0)
        return;
483

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

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

static inline BOOL module_is_container(const IMAGEHLP_MODULE* wmod_cntnr,
                                     const IMAGEHLP_MODULE* wmod_child)
{
    return wmod_cntnr->BaseOfImage <= wmod_child->BaseOfImage &&
499 500
        wmod_cntnr->BaseOfImage + wmod_cntnr->ImageSize >=
        wmod_child->BaseOfImage + wmod_child->ImageSize;
501 502 503 504 505 506 507
}

/******************************************************************
 *		break_delete_xpoints_from_module
 *
 * Remove all Xpoints from module which base is 'base'
 */
508
void break_delete_xpoints_from_module(DWORD64 base)
509
{
510
    IMAGEHLP_MODULE64           im, im_elf;
511
    int                         i;
512
    DWORD_PTR                   linear;
513 514 515 516 517
    struct dbg_breakpoint*      bp = dbg_curr_process->bp;

    /* FIXME: should do it also on the ELF sibbling if any */
    im.SizeOfStruct = sizeof(im);
    im_elf.SizeOfStruct = sizeof(im_elf);
518
    if (!SymGetModuleInfo64(dbg_curr_process->handle, base, &im)) return;
519 520

    /* try to get in fact the underlying ELF module (if any) */
521
    if (SymGetModuleInfo64(dbg_curr_process->handle, im.BaseOfImage - 1, &im_elf) &&
522
        im_elf.BaseOfImage <= im.BaseOfImage &&
523
        im_elf.BaseOfImage + im_elf.ImageSize >= im.BaseOfImage + im.ImageSize)
524 525 526 527
        im = im_elf;

    for (i = 0; i < dbg_curr_process->next_bp; i++)
    {
528
        if (bp[i].refcount && bp[i].enabled)
529
        {
530 531 532 533 534
            linear = (DWORD_PTR)memory_to_linear_addr(&bp[i].addr);
            if (im.BaseOfImage <= linear && linear < im.BaseOfImage + im.ImageSize)
            {
                break_delete_xpoint(i);
            }
535 536
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
537
}
Alexandre Julliard's avatar
Alexandre Julliard committed
538

Alexandre Julliard's avatar
Alexandre Julliard committed
539
/***********************************************************************
540
 *           break_enable_xpoint
Alexandre Julliard's avatar
Alexandre Julliard committed
541 542 543
 *
 * Enable or disable a break point.
 */
544
void break_enable_xpoint(int num, BOOL enable)
Alexandre Julliard's avatar
Alexandre Julliard committed
545
{
546 547
    if ((num <= 0) || (num >= dbg_curr_process->next_bp) || 
        dbg_curr_process->bp[num].refcount == 0)
Alexandre Julliard's avatar
Alexandre Julliard committed
548
    {
549
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
550 551
        return;
    }
552 553
    dbg_curr_process->bp[num].enabled = (enable) ? TRUE : FALSE;
    dbg_curr_process->bp[num].skipcount = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
554
}
Alexandre Julliard's avatar
Alexandre Julliard committed
555 556


557
/***********************************************************************
558
 *           find_triggered_watch
559 560
 *
 * Lookup the watchpoints to see if one has been triggered
561 562
 * Return >= (watch point index) if one is found
 * Return -1 if none found
563
 *
564
 * Unfortunately, Linux used to *NOT* (A REAL PITA) report with ptrace
565 566 567 568
 * the DR6 register value, so we have to look with our own need the
 * cause of the TRAP.
 * -EP
 */
569
static int find_triggered_watch(void)
570
{
571 572 573 574 575 576 577 578 579
    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++)
    {
580
        DWORD64 val = 0;
581

582
        if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
            (be_cpu->is_watchpoint_set(&dbg_context, bp[i].info)))
        {
            be_cpu->clear_watchpoint(&dbg_context, bp[i].info);

            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++)
    {
603
        DWORD64 val = 0;
604

605
        if (bp[i].refcount && bp[i].enabled && !is_xpoint_break(i) &&
606 607
            get_watched_value(i, &val))
        {
608
            if (val != bp[i].w.oldval)
609 610 611 612 613 614 615 616 617 618 619 620
            {
                be_cpu->clear_watchpoint(&dbg_context, bp[i].info);
                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;
621 622
}

Alexandre Julliard's avatar
Alexandre Julliard committed
623
/***********************************************************************
624
 *           break_info
Alexandre Julliard's avatar
Alexandre Julliard committed
625
 *
626
 * Display break & watch points information.
Alexandre Julliard's avatar
Alexandre Julliard committed
627
 */
628
void break_info(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
629
{
630 631 632 633 634 635 636 637 638
    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)
        {
639
            if (is_xpoint_break(i)) nbp++; else nwp++;
640 641
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
642

643
    if (nbp)
Alexandre Julliard's avatar
Alexandre Julliard committed
644
    {
645 646
        dbg_printf("Breakpoints:\n");
        for (i = 1; i < dbg_curr_process->next_bp; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
647
        {
648
            if (!bp[i].refcount || !is_xpoint_break(i))
649 650 651
                continue;
            dbg_printf("%d: %c ", i, bp[i].enabled ? 'y' : 'n');
            print_address(&bp[i].addr, TRUE);
652 653
            dbg_printf(" (%u)%s\n", bp[i].refcount, 
                       bp[i].xpoint_type == be_xpoint_watch_exec ? " (hardware assisted)" : "");
654
	    if (bp[i].condition != NULL)
655
	    {
656 657 658
	        dbg_printf("\t\tstop when  ");
 		expr_print(bp[i].condition);
		dbg_printf("\n");
659
	    }
Alexandre Julliard's avatar
Alexandre Julliard committed
660 661
        }
    }
662 663
    else dbg_printf("No breakpoints\n");
    if (nwp)
664
    {
665 666
        dbg_printf("Watchpoints:\n");
        for (i = 1; i < dbg_curr_process->next_bp; i++)
667
        {
668
            if (!bp[i].refcount || is_xpoint_break(i))
669 670 671 672 673 674 675
                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)
676
	    {
677 678 679
	        dbg_printf("\t\tstop when ");
 		expr_print(bp[i].condition);
		dbg_printf("\n");
680 681 682
	    }
        }
    }
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    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");
        }
    }
703 704 705
}

/***********************************************************************
706
 *           should_stop
707 708 709 710
 *
 * Check whether or not the condition (bp / skipcount) of a break/watch
 * point are met.
 */
711
static	BOOL should_stop(int bpnum)
712
{
713 714 715
    struct dbg_breakpoint*      bp = &dbg_curr_process->bp[bpnum];

    if (bp->condition != NULL)
716
    {
717
        struct dbg_lvalue lvalue = expr_eval(bp->condition);
718

719
        if (lvalue.type.id == dbg_itype_none)
720 721 722 723
        {
	    /*
	     * Something wrong - unable to evaluate this expression.
	     */
724 725 726 727
	    dbg_printf("Unable to evaluate expression ");
	    expr_print(bp->condition);
	    dbg_printf("\nTurning off condition\n");
	    break_add_condition(bpnum, NULL);
728
        }
729
        else if (!types_extract_as_integer(&lvalue))
730 731 732 733
        {
	    return FALSE;
        }
    }
734

735 736
    if (bp->skipcount > 0) bp->skipcount--;
    return bp->skipcount == 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
737
}
Alexandre Julliard's avatar
Alexandre Julliard committed
738

Alexandre Julliard's avatar
Alexandre Julliard committed
739
/***********************************************************************
740
 *           break_should_continue
Alexandre Julliard's avatar
Alexandre Julliard committed
741 742 743 744
 *
 * Determine if we should continue execution after a SIGTRAP signal when
 * executing in the given mode.
 */
745
BOOL break_should_continue(ADDRESS64* addr, DWORD code)
Alexandre Julliard's avatar
Alexandre Julliard committed
746
{
747
    enum dbg_exec_mode  mode = dbg_curr_thread->exec_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
748

Alexandre Julliard's avatar
Alexandre Julliard committed
749

750
    if (dbg_curr_thread->stopped_xpoint > 0)
751
    {
752
        if (!should_stop(dbg_curr_thread->stopped_xpoint)) return TRUE;
753

754 755 756 757 758 759 760 761 762 763 764 765
        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);
766 767
            dbg_printf(" new value %s\n",
                       wine_dbgstr_longlong(dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].w.oldval));
768
        }
769
        return FALSE;
770 771
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
772 773 774 775 776
    /*
     * 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.
     */
777
    if (mode == dbg_exec_step_over_line || mode == dbg_exec_step_into_line)
778
    {
779
	if (symbol_get_function_line_status(addr) == dbg_on_a_line_number)
780
            dbg_curr_thread->exec_count--;
781
    }
782
    else if (mode == dbg_exec_step_over_insn || mode == dbg_exec_step_into_insn)
783
        dbg_curr_thread->exec_count--;
Alexandre Julliard's avatar
Alexandre Julliard committed
784

785
    if (dbg_curr_thread->exec_count > 0 || mode == dbg_exec_finish)
786
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
787 788 789 790
	/*
	 * We still need to execute more instructions.
	 */
	return TRUE;
791
    }
792

793 794 795 796 797
    /* no breakpoint, continue if in continuous mode */
    return mode == dbg_exec_cont || mode == dbg_exec_finish;
}

/***********************************************************************
798
 *           break_adjust_pc
799
 *
800
 * Adjust PC to the address where the trap (if any) actually occurred
801 802
 * Also sets dbg_curr_thread->stopped_xpoint
 */
803
void break_adjust_pc(ADDRESS64* addr, DWORD code, BOOL first_chance, BOOL* is_break)
804
{
805 806 807 808 809 810 811
    /* break / watch points are handled on first chance */
    if ( !first_chance )
    {
        *is_break = TRUE;
        dbg_curr_thread->stopped_xpoint = -1;
        return;
    }
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    *is_break = FALSE;

    /* If not single-stepping, back up to the break instruction */
    if (code == EXCEPTION_BREAKPOINT)
        addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, TRUE);

    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)
827
            dbg_curr_thread->stopped_xpoint = find_triggered_watch();
828 829 830 831 832 833 834 835 836
        if (dbg_curr_thread->stopped_xpoint > 0)
        {
            /* If not single-stepping, do not back up over the break instruction */
            if (code == EXCEPTION_BREAKPOINT)
                addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
            return;
        }
    }

837
    /* If there's no breakpoint and we are not single-stepping, then
838
     * either we must have encountered a break insn in the Windows program
839 840
     * or someone is trying to stop us
     */
841
    if (dbg_curr_thread->stopped_xpoint == -1 && code == EXCEPTION_BREAKPOINT)
842
    {
843
        *is_break = TRUE;
844
        addr->Offset += be_cpu->adjust_pc_for_break(&dbg_context, FALSE);
845
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
846 847
}

848
/***********************************************************************
849
 *           break_suspend_execution
850
 *
851
 * Remove all bp before entering the debug loop
852
 */
853
void	break_suspend_execution(void)
854
{
855 856
    break_set_xpoints(FALSE);
    dbg_curr_process->bp[0] = dbg_curr_thread->step_over_bp;
857
}
Alexandre Julliard's avatar
Alexandre Julliard committed
858 859

/***********************************************************************
860
 *           break_restart_execution
Alexandre Julliard's avatar
Alexandre Julliard committed
861
 *
862
 * Set the bp to the correct state to restart execution
Alexandre Julliard's avatar
Alexandre Julliard committed
863 864
 * in the given mode.
 */
865
void break_restart_execution(int count)
Alexandre Julliard's avatar
Alexandre Julliard committed
866
{
867
    ADDRESS64                   addr;
868 869
    enum dbg_line_status        status;
    enum dbg_exec_mode          mode, ret_mode;
870
    ADDRESS64                   callee;
871
    void*                       linear;
Alexandre Julliard's avatar
Alexandre Julliard committed
872

873
    memory_get_current_pc(&addr);
874
    linear = memory_to_linear_addr(&addr);
Alexandre Julliard's avatar
Alexandre Julliard committed
875

Alexandre Julliard's avatar
Alexandre Julliard committed
876 877 878 879
    /*
     * This is the mode we will be running in after we finish.  We would like
     * to be able to modify this in certain cases.
     */
880
    ret_mode = mode = dbg_curr_thread->exec_mode;
Alexandre Julliard's avatar
Alexandre Julliard committed
881

882 883
    /* we've stopped on a xpoint (other than step over) */
    if (dbg_curr_thread->stopped_xpoint > 0)
884
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
885 886 887
	/*
	 * If we have set a new value, then save it in the BP number.
	 */
888 889
	if (count != 0 && mode == dbg_exec_cont)
        {
890
	    dbg_curr_process->bp[dbg_curr_thread->stopped_xpoint].skipcount = count;
891
        }
892 893 894
        /* 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;
895 896 897 898 899 900
    }
    else if (mode == dbg_exec_cont && count > 1)
    {
        dbg_printf("Not stopped at any breakpoint; argument ignored.\n");
    }

901
    if (mode == dbg_exec_finish && be_cpu->is_function_return(linear))
902 903 904 905
    {
	mode = ret_mode = dbg_exec_step_into_insn;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
906 907 908 909 910 911
    /*
     * 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.
     */
912
    if (be_cpu->is_function_call(linear, &callee))
913 914 915 916
    {
	status = symbol_get_function_line_status(&callee);
#if 0
        /* FIXME: we need to get the thunk type */
Alexandre Julliard's avatar
Alexandre Julliard committed
917 918 919
	/*
	 * Anytime we have a trampoline, step over it.
	 */
920 921 922 923 924
	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
925
	    mode = EXEC_STEP_OVER_TRAMPOLINE;
926 927 928 929 930 931 932 933 934
        }
#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;
        }
    }
935

936 937 938
    if (mode == dbg_exec_step_into_line && 
        symbol_get_function_line_status(&addr) == dbg_no_line_info)
    {
939
        dbg_printf("Single stepping until exit from function,\n"
940 941 942
                   "which has no line number information.\n");
        ret_mode = mode = dbg_exec_finish;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
943

944
    switch (mode)
Alexandre Julliard's avatar
Alexandre Julliard committed
945
    {
946 947 948
    case dbg_exec_cont: /* Continuous execution */
        be_cpu->single_step(&dbg_context, FALSE);
        break_set_xpoints(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
949 950
        break;

951
#if 0
Alexandre Julliard's avatar
Alexandre Julliard committed
952
    case EXEC_STEP_OVER_TRAMPOLINE:
953 954 955 956 957 958 959 960
        /*
         * 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);
961
        /* FIXME: we assume stack grows as on an i386 */
962 963 964 965 966 967 968
        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;
969 970
        dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
        dbg_curr_process->bp[0].condition = NULL;
971 972 973
        be_cpu->single_step(&dbg_context, FALSE);
        break_set_xpoints(TRUE);
        break;
974
#endif
975 976 977 978

    case dbg_exec_finish:
    case dbg_exec_step_over_insn:  /* Stepping over a call */
    case dbg_exec_step_over_line:  /* Stepping over a call */
979
        if (be_cpu->is_step_over_insn(linear))
Alexandre Julliard's avatar
Alexandre Julliard committed
980
        {
981 982 983 984 985
            be_cpu->disasm_one_insn(&addr, FALSE);
            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;
986 987
            dbg_curr_process->bp[0].xpoint_type = be_xpoint_break;
            dbg_curr_process->bp[0].condition = NULL;
988 989
            be_cpu->single_step(&dbg_context, FALSE);
            break_set_xpoints(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
990 991 992
            break;
        }
        /* else fall through to single-stepping */
Alexandre Julliard's avatar
Alexandre Julliard committed
993

994 995 996
    case dbg_exec_step_into_line: /* Single-stepping a line */
    case dbg_exec_step_into_insn: /* Single-stepping an instruction */
        be_cpu->single_step(&dbg_context, TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
997
        break;
998
    default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
999
    }
1000 1001
    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
1002 1003
}

1004
int break_add_condition(int num, struct expr* exp)
Alexandre Julliard's avatar
Alexandre Julliard committed
1005
{
1006 1007
    if (num <= 0 || num >= dbg_curr_process->next_bp || 
        !dbg_curr_process->bp[num].refcount)
Alexandre Julliard's avatar
Alexandre Julliard committed
1008
    {
1009
        dbg_printf("Invalid breakpoint number %d\n", num);
Alexandre Julliard's avatar
Alexandre Julliard committed
1010 1011 1012
        return FALSE;
    }

1013 1014 1015 1016 1017
    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
1018

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

1021
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1022
}