summaryrefslogtreecommitdiffstats
path: root/languages/cpp/debugger/breakpoint.cpp
blob: 9d1860e4e427da52b7f8dce8db2ab9ebc9b5074b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/***************************************************************************
    begin                : Tue May 13 2003
    copyright            : (C) 2003 by John Birch
    email                : jbb@kdevelop.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "breakpoint.h"
#include "gdbcontroller.h"
#include "gdbcommand.h"

#include <klocale.h>
#include <kdebug.h>

#include <tqfileinfo.h>
#include <tqfontmetrics.h>
#include <tqpainter.h>
#include <tqregexp.h>
#include <tqstring.h>

#include <stdio.h>
#include <typeinfo>

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

namespace GDBDebugger
{

static int BPKey_ = 0;

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

Breakpoint::Breakpoint(bool temporary, bool enabled)
    : s_pending_(true),
      s_actionAdd_(true),
      s_actionClear_(false),
      s_actionModify_(false),
      s_actionDie_(false),
      s_dbgProcessing_(false),
      s_enabled_(enabled),
      s_temporary_(temporary),
      s_hardwareBP_(false),
      s_tracingEnabled_(false),
      s_traceFormatStringEnabled_(false),

      dbgId_(-1),
      hits_(0),
      key_(BPKey_++),
      active_(-1),
      ignoreCount_(0),
      condition_("")
{
}

/***************************************************************************/

Breakpoint::~Breakpoint()
{
}

void Breakpoint::sendToGdb(GDBController* controller)
{
    // Need to issue 'modifyBreakpoint' when setting breakpoint is done
    controller_ = controller;

    // FIXME: should either make sure this widget is disabled
    // when needed, or implement simular logic.
    if (controller->stateIsOn(s_dbgNotStarted))
    {
        // Can't modify breakpoint now, will try again later.
        setPending(true);
        return;
    }

    setPending(false);

    bool restart = false;
    // FIXME: this will only catch command for which gdb 
    // produces the "^running" marker.
    // FIXME: this probably won't work if there are other
    // run commands in the thread already.
    if (controller->stateIsOn(s_appRunning)
        && !controller->stateIsOn(s_explicitBreakInto))
    {
        kdDebug(9012) << "PAUSING APP\n";
        controller->pauseApp();
        restart = true;
    }
    
    if (isActionAdd())
    {
        // This prevents us from sending breakpoint command to
        // gdb for empty breakpoints, when user haven't even
        // typed function name, or address, or variable.
        //
        // Check for isDbgProcessing makes sure we don't issue
        // several -break-insert commands before getting
        // output from the first one.
        if (isValid() && !isDbgProcessing())
        {
            setBreakpoint(controller);
        }
    }
    else
    {
        if (isActionClear())
        {
            clearBreakpoint(controller);
        }
        else
        {
            if (isActionModify())
            {
                modifyBreakpoint(controller); 
            }
        }
    }

    if (restart) {
        kdDebug(9012) << "RESTARING APP\n";
        GDBCommand *cmd = new GDBCommand("-exec-continue");
        cmd->setRun(true);
        controller->addCommand(cmd);
    }
}

void Breakpoint::clearBreakpoint(GDBController* /*c*/)
{
    controller()->addCommandBeforeRun(
        new GDBCommand(dbgRemoveCommand(), 
                       this,
                       &Breakpoint::handleDeleted)); 
}

void Breakpoint::applicationExited(GDBController*) 
{
}


void Breakpoint::setBreakpoint(GDBController* controller)
{
    setDbgProcessing(true);

    // Don't use handler mechanism yet, because the reply
    // should contain internal id of breakpoint (not gdb id), so that we
    // can match gdb id with the breakpoint instance we've set.
            
    // Note that at startup we issue several breakpoint commands, so can't
    // just store last breakpoint. Need to stack of last breakpoint commands,
    // but that for later.
    //
    // When this command is finished, slotParseGDBBreakpointSet
    // will be called by the controller.
    controller->addCommandBeforeRun(
        new GDBCommand(dbgSetCommand(controller),                        
                       this,
                       &Breakpoint::handleSet, true));
}


void Breakpoint::modifyBreakpoint(GDBController* controller)
{
    controller->
        addCommandBeforeRun(
            new ModifyBreakpointCommand(TQString("-break-condition %1 ") +
                                        conditional(), this));
    controller->
        addCommandBeforeRun(
            new ModifyBreakpointCommand(TQString("-break-after %1 ") +
                                        TQString::number(ignoreCount()), this));

    controller->
        addCommandBeforeRun(
            new ModifyBreakpointCommand(isEnabled() ? 
                                        TQString("-break-enable %1") 
                                        : TQString("-break-disable %1"), this));
}

void Breakpoint::removedInGdb()
{
    setActionDie();
    emit modified(this);
}


bool Breakpoint::match(const Breakpoint* breakpoint) const
{
    // simple case
    if (this == breakpoint)
        return true;

    // Type case
    if (typeid(*this) != typeid(*breakpoint))
        return false;

    return match_data(breakpoint);
}

/***************************************************************************/

TQString Breakpoint::dbgRemoveCommand() const
{
    if (dbgId_>0)
        return TQString("-break-delete %1").tqarg(dbgId_); // gdb command - not translatable

    return TQString();
}


/***************************************************************************/

// called when debugger ends
void Breakpoint::reset()
{
    dbgId_                = -1;
    s_pending_            = true;
    s_actionAdd_          = true;     // waiting for the debugger to start
    s_actionClear_        = false;
    // All breakpoint properties will be automatically sent to
    // gdb when breakpoint is first added, no matter what value
    // this field has.
    s_actionModify_       = false;
    s_dbgProcessing_      = false;
    s_hardwareBP_         = false;
    hits_                 = 0;
    active_               = -1;
}

/***************************************************************************/

void Breakpoint::setActive(int active, int id)
{
    active_           = active;
    dbgId_            = id;

    if (s_pending_ && !(s_actionAdd_ && s_actionModify_)) {
        s_pending_ = false;
        s_actionModify_ = false;
    }

    s_actionAdd_          = false;
    s_actionClear_        = false;
    s_actionDie_          = false;
    s_dbgProcessing_      = false;
}

/***************************************************************************/

TQString Breakpoint::statusDisplay(int activeFlag) const
{
    TQString status="";
    if (!s_enabled_)
        status = i18n("Disabled");
    else
    if (s_pending_)
    {
        if (s_actionAdd_)
            status = i18n("Pending (add)");
        if (s_actionClear_)
            status = i18n("Pending (clear)");
        if (s_actionModify_)
            status = i18n("Pending (modify)");
    }
    else
    if (isActive(activeFlag))
        status = i18n("Active");

    return status;
}

TQString Breakpoint::traceRealFormatString() const
{
    TQString result;

    if (traceFormatStringEnabled())
    {
        result = traceFormatString();
    }
    else
    {
        result = "Tracepoint";
        if (const FilePosBreakpoint* fb 
            = dynamic_cast<const FilePosBreakpoint*>(this))
        {
            result += " at " + fb->location() + ": ";
        }
        else
        {
            result += " " + TQString::number(key()) + ": ";
        }
        for(TQStringList::const_iterator i = tracedExpressions_.begin(),
                e = tracedExpressions_.end(); i != e; ++i)
        {
            result += " " + *i + " = %d";
        }
    }

    // Quote the thing
    result = "\"" + result + "\\n\"";
    
    for(TQStringList::const_iterator i = tracedExpressions_.begin(),
            e = tracedExpressions_.end(); i != e; ++i)
    {
        result += ", " + *i;
    }    

    return result;
}

void Breakpoint::handleSet(const GDBMI::ResultRecord& r)
{
    // Try to find gdb id. It's a bit harder that it should be,
    // because field names differ depending on breakpoint type.

    int id = -1;
    
    if (r.hasField("bkpt"))
        id = r["bkpt"]["number"].literal().toInt();
    else if (r.hasField("wpt"))
        id = r["wpt"]["number"].literal().toInt();
    else if (r.hasField("hw-rwpt"))
        id = r["hw-rwpt"]["number"].literal().toInt();
    // We don't have access watchpoints in UI yet, but
    // for future.
    else if (r.hasField("hw-awpt"))
        id = r["hw-awpt"]["number"].literal().toInt();
    
    if (id == -1)
    {
        // If can't set because file not found yet,
        // will need to try later.
        setPending(true);
    }
    else
    {
        setActive(0 /* unused m_activeFlag */, id);
    }
    
    // Need to do this so that if breakpoint is not set
    // (because the file is not found)
    // we unset isDbgProcessing flag, so that breakpoint can
    // be set on next stop.
    setDbgProcessing(false);

    // Immediately call modifyBreakpoint to set all breakpoint
    // properties, such as condition.
    modifyBreakpoint(controller_);

    emit modified(this);
}

void Breakpoint::handleDeleted(const GDBMI::ResultRecord& /*r*/)
{
    kdDebug(9012) << "inside handleDeleted\n";
    setActionDie();
    if (FilePosBreakpoint* fp = dynamic_cast<FilePosBreakpoint*>(this))
    {
        kdDebug(9012) << "handleDeleted, line is " << fp->lineNum() << "\n";
    }
    emit modified(this);
}

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

FilePosBreakpoint::FilePosBreakpoint()
: subtype_(filepos),
  line_(-1)
{}

FilePosBreakpoint::FilePosBreakpoint(const TQString &fileName, int lineNum,
                                     bool temporary, bool enabled)
    : Breakpoint(temporary, enabled)
{
    // Sets 'subtype'
    setLocation(TQString("%1:%2").tqarg(fileName).tqarg(lineNum));
}

FilePosBreakpoint::~FilePosBreakpoint()
{
}

TQString FilePosBreakpoint::dbgSetCommand(GDBController *c) const
{
    TQString cmdStr = "-break-insert";
    
    if (isTemporary())
        cmdStr = cmdStr + " -t";

    if (c->miPendingBreakpoints())
        cmdStr = cmdStr + " -f";

    return cmdStr + " " + location_;
}

bool FilePosBreakpoint::match_data(const Breakpoint *xb) const
{
    const FilePosBreakpoint* b = static_cast<const FilePosBreakpoint*>(xb);

    if (b)
        return location_ == b->location_;
    else
        return false;
}

TQString FilePosBreakpoint::displayType() const
{
    return i18n("Code breakpoint", "Code"); 
}

bool FilePosBreakpoint::isValid() const
{
    return !location_.isEmpty();
}

bool FilePosBreakpoint::hasFileAndLine() const
{
    return line_ != -1;
}

TQString FilePosBreakpoint::fileName() const
{
    return fileName_;
}

unsigned FilePosBreakpoint::lineNum() const
{
    return line_;
}



TQString FilePosBreakpoint::location(bool compact) const
{
    if (subtype_ == filepos && hasFileAndLine() && compact)
    {
        return TQFileInfo(fileName_).fileName()+":"+TQString::number(line_);
    }
    else
    {
        return location_;
    }
}

/***************************************************************************/

void FilePosBreakpoint::setLocation(const TQString& location)
{
    location_ = location;
   
    TQRegExp regExp1("(.*):(\\d+)$");
    regExp1.setMinimal(true);
    if ( regExp1.search(location, 0) >= 0 )
    {
        subtype_ = filepos;

        TQString t = regExp1.cap(1);
        TQString dirPath = TQFileInfo(t).dirPath();
        if ( dirPath == "." )
        {
            TQString existingDirPath = TQFileInfo(fileName_).dirPath();
            if (existingDirPath != ".")
                fileName_ = existingDirPath+"/"+regExp1.cap(1);
            else
                fileName_ = regExp1.cap(1);
        }
        else
            fileName_ = regExp1.cap(1);

        line_ = regExp1.cap(2).toInt();

        location_ = TQString("%1:%2").tqarg(fileName_).tqarg(regExp1.cap(2));
    }
    else
    {
        // Could be address as well, but it's treated absolutely
        // the same everywhere.
        subtype_ = function;
    }
}

void FilePosBreakpoint::handleSet(const GDBMI::ResultRecord& r)
{
    // Below logic gets filename and line from gdb response, and
    // allows us to show breakpoint marker even for function
    // breakpoints. Unfortunately, 'fullname' field is available only in 
    // post-6.4 versions of gdb and if we try to use 'file', then
    // KDevelop won't be able to find that file to show the marker.
    if (r.hasField("bkpt"))
    {
        const GDBMI::Value& v = r["bkpt"];
        if (v.hasField("fullname") && v.hasField("line"))
        {
            fileName_ = v["fullname"].literal();
            line_ = v["line"].literal().toInt();        
        }
    }

    Breakpoint::handleSet(r);
}

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

Watchpoint::Watchpoint(const TQString& varName, bool temporary, bool enabled)
    : Breakpoint(temporary, enabled),
      varName_(varName)    
{    
}

/***************************************************************************/

Watchpoint::~Watchpoint()
{
}

void Watchpoint::setBreakpoint(GDBController* controller)
{    
    if (isEnabled())
    {
        setDbgProcessing(true);

        controller->addCommandBeforeRun(
            new GDBCommand(
                TQString("-data-evaluate-expression &%1").tqarg(varName_),
                this,
                &Watchpoint::handleAddressComputed));
    }
}

void Watchpoint::handleAddressComputed(const GDBMI::ResultRecord& r)
{
    address_ = r["value"].literal().toULongLong(0, 16);
    controller()->addCommandBeforeRun(
        new GDBCommand(
            TQString("-break-watch *%1").tqarg(r["value"].literal()),
            static_cast<Breakpoint*>(this),
            &Watchpoint::handleSet));
}

void Watchpoint::applicationExited(GDBController* c)
{
    if (!c->stateIsOn(s_dbgNotStarted))
    {
        // Note: not using 'clearBreakpoint' as it will delete breakpoint
        // completely.

        controller()->addCommand(
            new GDBCommand(dbgRemoveCommand()));
        setDbgId(-1);
        setEnabled(false);
        setActionAdd(true);
        address_ = static_cast<unsigned long long>(-1);
        emit modified(this);
    }
}

void Watchpoint::removedInGdb()
{
    // Do nothing. Watchpoints must be preserved
    // even if they are gone in gdb.
}

/***************************************************************************/

TQString Watchpoint::dbgSetCommand(GDBController *) const
{
    return TQString("-break-watch ")+varName_;    // gdb command - not translatable
}

/***************************************************************************/

bool Watchpoint::match_data(const Breakpoint* xb) const
{
    const Watchpoint* b = static_cast<const Watchpoint*>(xb);

    return (varName_ == b->varName_);
}

ReadWatchpoint::ReadWatchpoint(const TQString& varName, bool temporary, bool enabled)
    : Watchpoint(varName, temporary, enabled)
{
}

TQString ReadWatchpoint::dbgSetCommand(GDBController *) const
{
   return TQString("-break-watch -r ")+varName();
}

bool ReadWatchpoint::match_data(const Breakpoint* xb) const
{
    const ReadWatchpoint* b = static_cast<const ReadWatchpoint*>(xb);

    return (varName() == b->varName());
}

/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

//ExitBreakpoint::ExitBreakpoint(bool temporary, bool enabled) :
//  Breakpoint(temporary, enabled)
//{
//}
//
///***************************************************************************/
//
//ExitBreakpoint::~ExitBreakpoint()
//{
//}
//
///***************************************************************************/
//
//TQString ExitBreakpoint::dbgSetCommand() const
//{
//  return "";
//}
//
///***************************************************************************/
//
//bool ExitBreakpoint::match(const Breakpoint* brkpt) const
//{
//  // simple case
//  if (this == brkpt)
//    return true;
//
//  // Type case
//  const ExitBreakpoint* check = dynamic_cast<const ExitBreakpoint*>(brkpt);
//  if (!check)
//    return false;
//
//  // member case
//  return true;
//}
//
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
//
// These are implemented in gdb but can cause a lot of breakpoints
// to be set. This needs more thought before being implemented

//RegExpBreakpoint::RegExpBreakpoint(bool temporary, bool enabled) :
//  Breakpoint(temporary, enabled)
//{
//}
//
///***************************************************************************/
//
//RegExpBreakpoint::~RegExpBreakpoint()
//{
//}
//
///***************************************************************************/
//
//TQString RegExpBreakpoint::dbgSetCommand() const
//{
//  return "";
//}
//
///***************************************************************************/
//
////TQString RegExpBreakpoint::dbgRemoveCommand() const
////{
////  return "";
////}
//
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

// Most catch options arn't implemented in gdb so ignore this for now.

//CatchBreakpoint::CatchBreakpoint(bool temporary, bool enabled) :
//  Breakpoint(temporary, enabled)
//{
//}
//
///***************************************************************************/
//
//CatchBreakpoint::~CatchBreakpoint()
//{
//}
//
///***************************************************************************/
//
//TQString CatchBreakpoint::dbgSetCommand() const
//{
//  return "";
//}
//
///***************************************************************************/
//
////TQString CatchBreakpoint::dbgRemoveCommand() const
////{
////  return "";
////}
//
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/

}

#include "breakpoint.moc"