summaryrefslogtreecommitdiffstats
path: root/languages/cpp/debugger/memviewdlg.cpp
blob: c594d1340f3a7852d648bfaf8cc7751bf5c4aa72 (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
/***************************************************************************
    begin                : Tue Oct 5 1999
    copyright            : (C) 1999 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 "memviewdlg.h"
#include "gdbcontroller.h"
#include "gdbcommand.h"

#include <kbuttonbox.h>
#include <klineedit.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kstdguiitem.h>
#include <kdeversion.h>
#include <kdebug.h>
#include <kiconloader.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqmultilineedit.h>
#include <tqpushbutton.h>
#include <tqvariant.h>
#include <tqpopupmenu.h>
#include <tqhbox.h>
#include <tqtoolbox.h>
#include <tqtextedit.h>

#include <kmessagebox.h>

#include <khexedit/byteseditinterface.h>

#include <ctype.h>

// **************************************************************************
//
// Dialog allows the user to enter
//  - A starting address
//  - An ending address
//
//  this can be in the form
//            functiom/method name
//            variable address (ie &Var, str)
//            Memory address 0x8040abc
//
//  When disassembling and you enter a method name without an
//  ending address then the whole method is disassembled.
//  No data means disassemble the method we're curently in.(from the
//  start of the method)
//
// click ok buton to send the request to gdb
// the output is returned (some time later) in the raw data slot
// and displayed as is, so it's rather crude, but it works!
// **************************************************************************

namespace GDBDebugger
{
    /** Container for controls that select memory range.
        
        The memory range selection is embedded into memory view widget,
        it's not a standalone dialog. However, we want to have easy way
        to hide/show all controls, so we group them in this class.
    */
    class MemoryRangeSelector : public TQWidget
    {
    public:
        KLineEdit* startAddressLineEdit;
        KLineEdit* amountLineEdit;
        TQPushButton* okButton;
        TQPushButton* cancelButton;

        MemoryRangeSelector(TQWidget* tqparent) 
        : TQWidget(tqparent)
        {
            TQVBoxLayout* l = new TQVBoxLayout(this);

            // Grid tqlayout: labels + address field
            TQGridLayout* gl = new TQGridLayout(l);

            gl->setColSpacing(0, 2);
            gl->setColSpacing(1, 4);
            gl->setRowSpacing(1, 2);
            
            TQLabel* l1 = new TQLabel(i18n("Start"), this);
            gl->addWidget(l1, 0, 1);
            
            startAddressLineEdit = new KLineEdit(this);
            gl->addWidget(startAddressLineEdit, 0, 3);
            
            TQLabel* l2 = new TQLabel(i18n("Amount"), this);
            gl->addWidget(l2, 2, 1);
            
            amountLineEdit = new KLineEdit(this);
            gl->addWidget(amountLineEdit, 2, 3);

            l->addSpacing(2);
                        
            TQHBoxLayout* hb = new TQHBoxLayout(l);
            hb->addStretch();
            
            okButton = new TQPushButton(i18n("OK"), this);
            hb->addWidget(okButton);
            
            cancelButton = new TQPushButton(i18n("Cancel"), this);
            hb->addWidget(cancelButton);            

            l->addSpacing(2);

            connect(startAddressLineEdit, TQT_SIGNAL(returnPressed()),
                    okButton, TQT_SLOT(animateClick()));

            connect(amountLineEdit, TQT_SIGNAL(returnPressed()),
                    okButton, TQT_SLOT(animateClick()));
        }
    };


    
    MemoryView::MemoryView(GDBController* controller,
                           TQWidget* tqparent, const char* name)
    : TQWidget(tqparent, name), 
      controller_(controller),
      // New memory view can be created only when debugger is active,
      // so don't set s_appNotStarted here.
      khexedit2_real_widget(0),
      amount_(0), data_(0),
      debuggerState_(0)
    {
        setCaption(i18n("Memory view"));
        emit captionChanged(caption());

        initWidget();

        if (isOk())
            slotEnableOrDisable();
    }

    void MemoryView::initWidget()
    {
        TQVBoxLayout *l = new TQVBoxLayout(this, 0, 0);

        khexedit2_widget = KHE::createBytesEditWidget(this);               

        bool ok_ = false;

        if (khexedit2_widget)
        {
            TQWidget* real_widget = (TQWidget*)
                khexedit2_widget->child("BytesEdit");
            
            if (real_widget)
            {
                ok_ = true;

                connect(real_widget, TQT_SIGNAL(bufferChanged(int, int)),
                        this, TQT_SLOT(memoryEdited(int, int)));
                
                khexedit2_real_widget = real_widget;

                TQVariant resize_style(2); // full size usage.
                real_widget->setProperty("ResizeStyle", resize_style);
                                
                //TQVariant group(8);
                //real_widget->setProperty("StartOffset", start);
                //real_widget->setProperty("NoOfBytesPerLine", group);

                // HACK: use hardcoded constant taht should match
                // khexedit2
                // 3 -- binary
                // 1 -- decimal
                // 0 -- hex
                //TQVariant coding(3);
                //real_widget->setProperty("Coding", coding);

                //TQVariant gap(32);
                //real_widget->setProperty("BinaryGapWidth", gap);
                
            }   
            else
            {
                delete khexedit2_widget;
            }            
        }            

        if (ok_) {

            rangeSelector_ = new MemoryRangeSelector(this);
            l->addWidget(rangeSelector_);
            
            connect(rangeSelector_->okButton, TQT_SIGNAL(clicked()),
                    this,                     TQT_SLOT(slotChangeMemoryRange()));
            
            
            connect(rangeSelector_->cancelButton,  TQT_SIGNAL(clicked()), 
                    this,                         TQT_SLOT(slotHideRangeDialog()));
            
            connect(rangeSelector_->startAddressLineEdit,
                    TQT_SIGNAL(textChanged(const TQString&)),
                    this,
                    TQT_SLOT(slotEnableOrDisable()));
            
            connect(rangeSelector_->amountLineEdit,
                    TQT_SIGNAL(textChanged(const TQString&)),
                    this,
                    TQT_SLOT(slotEnableOrDisable()));

            l->addWidget(khexedit2_widget);

        } else {

            TQTextEdit* edit = new TQTextEdit(this);
            l->addWidget(edit);

            edit->setText(
                "<h1>Not available</h1>"
                "<p>Could not open the khexedit2 library. "
                "Make sure that the KHexEdit package (part of kdeutils) is installed. "
                "Specifically, check for the following files:"
                "<ul><li>libkhexeditcommon.so.0.0.0\n"
                "<li>libkbyteseditwidget.so\n"
                "<li>kbyteseditwidget.desktop\n"
                "</ul>");
        }

    }

    void MemoryView::debuggerStateChanged(int state)
    {
        if (isOk())
        {
            debuggerState_ = state;
            slotEnableOrDisable();
        }
    }


    void MemoryView::slotHideRangeDialog()
    {
        rangeSelector_->hide();
    }

    void MemoryView::slotChangeMemoryRange()
    {
        controller_->addCommand(
            new ExpressionValueCommand(
                rangeSelector_->amountLineEdit->text(),
                this, &MemoryView::sizeComputed));
    }

    void MemoryView::sizeComputed(const TQString& size)
    {
        controller_->addCommand(
            new 
            GDBCommand(
                TQString("-data-read-memory %1 x 1 1 %2")
                .tqarg(rangeSelector_->startAddressLineEdit->text())
                .tqarg(size).ascii(),
                this,
                &MemoryView::memoryRead));
    }

    void MemoryView::memoryRead(const GDBMI::ResultRecord& r)
    {
        const GDBMI::Value& content = r["memory"][0]["data"];

        amount_ = content.size();

        startAsString_ = rangeSelector_->startAddressLineEdit->text();
        amountAsString_ = rangeSelector_->amountLineEdit->text();
        start_ = startAsString_.toUInt(0, 0);

        setCaption(TQString("%1 (%2 bytes)")
                   .tqarg(startAsString_).tqarg(amount_));
        emit captionChanged(caption());

        KHE::BytesEditInterface* bytesEditor
            = KHE::bytesEditInterface(khexedit2_widget);

        delete[] this->data_;
        this->data_ = new char[amount_];
        for(unsigned i = 0; i < content.size(); ++i)
        {
            this->data_[i] = content[i].literal().toInt(0, 16);
        }

                    
        bytesEditor->setData( this->data_, amount_ );
        bytesEditor->setReadOnly(false);
        // Overwrite data, not insert new
        bytesEditor->setOverwriteMode( true );
        // Not sure this is needed, but prevent
        // inserting new data.
        bytesEditor->setOverwriteOnly( true );
                   
        TQVariant start_v(start_);
        khexedit2_real_widget->setProperty("FirstLineOffset", start_v);

        //TQVariant bsw(0);
        //khexedit2_real_widget->setProperty("ByteSpacingWidth", bsw);
        
        // HACK: use hardcoded constant taht should match
        // khexedit2
        // 3 -- binary
        // 1 -- decimal
        // 0 -- hex
        //TQVariant coding(1);
        //khexedit2_real_widget->setProperty("Coding", coding);


        slotHideRangeDialog();                   
    }

    
    void MemoryView::memoryEdited(int start, int end)
    {        
        for(int i = start; i <= end; ++i)
        {
            controller_->addCommand(
                new GDBCommand(
                    TQString("set *(char*)(%1 + %2) = %3")
                        .tqarg(start_)
                        .tqarg(i)
                        .tqarg(TQString::number(data_[i]))));
        }
    }
    
    void MemoryView::contextMenuEvent ( TQContextMenuEvent * e ) 
    {
        if (!isOk())
            return;
        
        TQPopupMenu menu;

        bool app_running = !(debuggerState_ & s_appNotStarted);

        int idRange = menu.insertItem(i18n("Change memory range"));
        // If address selector is show, 'set memory range' can't
        // do anything more.
        menu.setItemEnabled(idRange, 
                            app_running && !rangeSelector_->isShown());
        int idReload = menu.insertItem(i18n("Reload"));
        // If amount is zero, it means there's not data yet, so
        // reloading does not make sense.
        menu.setItemEnabled(idReload, app_running && amount_ != 0);        
        int idClose = menu.insertItem(i18n("Close this view"));
        
        int result = menu.exec(e->globalPos());

        if (result == idRange)
        {
            rangeSelector_->startAddressLineEdit->setText(startAsString_);
            rangeSelector_->amountLineEdit->setText(amountAsString_);

            rangeSelector_->show();
            rangeSelector_->startAddressLineEdit->setFocus();
        }        
        if (result == idReload)
        {
            // We use numeric start_ and amount_ stored in this, 
            // not textual startAsString_ and amountAsString_,
            // because program position might have changes and expressions
            // are no longer valid.
            controller_->addCommand(
                new 
                GDBCommand(
                    TQString("-data-read-memory %1 x 1 1 %2")
                    .tqarg(start_).tqarg(amount_).ascii(),
                    this,
                    &MemoryView::memoryRead));
        }

        if (result == idClose)
            delete this;

        
    }        

    bool MemoryView::isOk() const
    {
        return khexedit2_real_widget;
    }

    void MemoryView::slotEnableOrDisable()
    {
        bool app_started = !(debuggerState_ & s_appNotStarted);
       
        bool enabled_ = app_started &&           
            !rangeSelector_->startAddressLineEdit->text().isEmpty() &&
            !rangeSelector_->amountLineEdit->text().isEmpty();

        rangeSelector_->okButton->setEnabled(enabled_);
    }


    ViewerWidget::ViewerWidget(GDBController* controller,
                               TQWidget* tqparent, 
                               const char* name) 
    : TQWidget(tqparent, name),
      controller_(controller)
    {
        setIcon(SmallIcon("math_brace"));
        
        TQVBoxLayout *l = new TQVBoxLayout(this, 0, 0);
        
        toolBox_ = new TQToolBox(this);
        l->addWidget(toolBox_);
    }

    void ViewerWidget::slotAddMemoryView()
    {
        // For unclear reasons, this call, that indirectly
        // does
        //
        //    mainWindow()->setViewAvailable(this)
        //    mainWindow()->raiseView(this)
        //
        // should be done before creating the child widget.
        // Otherwise, the child widget won't be freely resizable --
        // there will be not-so-small minimum size.
        // Problem exists both with KMDI and S/IDEAL.

        setViewShown(true);

        MemoryView* widget = new MemoryView(controller_, this);
        toolBox_->addItem(widget, widget->caption());
        toolBox_->setCurrentItem(widget);
        memoryViews_.push_back(widget);

        connect(widget, TQT_SIGNAL(captionChanged(const TQString&)),
                this, TQT_SLOT(slotChildCaptionChanged(const TQString&)));

        connect(widget, TQT_SIGNAL(destroyed(TQObject*)),
                this, TQT_SLOT(slotChildDestroyed(TQObject*)));
    }

    void ViewerWidget::slotDebuggerState(const TQString&, int state)
    {
        for(unsigned i = 0; i < memoryViews_.size(); ++i)
        {
            memoryViews_[i]->debuggerStateChanged(state);
        }
    }

    void ViewerWidget::slotChildCaptionChanged(const TQString& caption)
    {
        const TQWidget* s = static_cast<const TQWidget*>(sender());
        TQWidget* ncs = const_cast<TQWidget*>(s);
        TQString cap = caption;
        // Prevent intepreting '&' as accelerator specifier.
        cap.replace("&", "&&");
        toolBox_->setItemLabel(toolBox_->indexOf(ncs), cap);
    }

    void ViewerWidget::slotChildDestroyed(TQObject* child)
    {
        TQValueVector<MemoryView*>::iterator i, e;
        for(i = memoryViews_.begin(), e = memoryViews_.end(); i != e; ++i)
        {
            if (TQT_BASE_OBJECT(*i) == TQT_BASE_OBJECT(child))
            {
                memoryViews_.erase(i);
                break;
            }
        }

        if (toolBox_->count() == 0)
            setViewShown(false);
    }

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

}

#include "memviewdlg.moc"