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
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "registerhandler.h"
#include "watchdelegatewidgets.h"
#if USE_REGISTER_MODEL_TEST
#include <modeltest.h>
#endif
#include <utils/qtcassert.h>
namespace Debugger {
namespace Internal {
//////////////////////////////////////////////////////////////////
//
// Register
//
//////////////////////////////////////////////////////////////////
enum RegisterType
{
RegisterUnknown,
//RegisterDummy, // like AH if EAX is present.
RegisterI8,
RegisterI16,
RegisterI32,
RegisterI64,
RegisterI128,
RegisterF32,
RegisterF64,
RegisterF80,
RegisterXMM,
RegisterMMX,
RegisterNeon,
RegisterFlags32
};
static struct RegisterNameAndType
{
const char *name;
RegisterType type;
} theNameAndType[] = {
// ARM
{ "r0", RegisterI32 },
{ "r1", RegisterI32 },
{ "r2", RegisterI32 },
{ "r3", RegisterI32 },
{ "r4", RegisterI32 },
{ "r5", RegisterI32 },
{ "r6", RegisterI32 },
{ "r7", RegisterI32 },
{ "r8", RegisterI32 },
{ "r9", RegisterI32 },
{ "r10", RegisterI32 },
{ "r11", RegisterI32 },
{ "r12", RegisterI32 },
{ "sp", RegisterI32 },
{ "lr", RegisterI32 },
{ "pc", RegisterI32 },
{ "cpsr", RegisterFlags32 },
{ "d0", RegisterI64 },
{ "d1", RegisterI64 },
{ "d2", RegisterI64 },
{ "d3", RegisterI64 },
{ "d4", RegisterI64 },
{ "d5", RegisterI64 },
{ "d6", RegisterI64 },
{ "d7", RegisterI64 },
{ "d8", RegisterI64 },
{ "d9", RegisterI64 },
{ "d10", RegisterI64 },
{ "d11", RegisterI64 },
{ "d12", RegisterI64 },
{ "d13", RegisterI64 },
{ "d14", RegisterI64 },
{ "d15", RegisterI64 },
{ "d16", RegisterI64 },
{ "d17", RegisterI64 },
{ "d18", RegisterI64 },
{ "d19", RegisterI64 },
{ "d20", RegisterI64 },
{ "d21", RegisterI64 },
{ "d22", RegisterI64 },
{ "d23", RegisterI64 },
{ "d24", RegisterI64 },
{ "d25", RegisterI64 },
{ "d26", RegisterI64 },
{ "d27", RegisterI64 },
{ "d28", RegisterI64 },
{ "d29", RegisterI64 },
{ "d30", RegisterI64 },
{ "d31", RegisterI64 },
{ "fpscr", RegisterFlags32 },
{ "s0", RegisterI32 },
{ "s1", RegisterI32 },
{ "s2", RegisterI32 },
{ "s3", RegisterI32 },
{ "s4", RegisterI32 },
{ "s5", RegisterI32 },
{ "s6", RegisterI32 },
{ "s7", RegisterI32 },
{ "s8", RegisterI32 },
{ "s9", RegisterI32 },
{ "s10", RegisterI32 },
{ "s11", RegisterI32 },
{ "s12", RegisterI32 },
{ "s13", RegisterI32 },
{ "s14", RegisterI32 },
{ "s15", RegisterI32 },
{ "s16", RegisterI32 },
{ "s17", RegisterI32 },
{ "s18", RegisterI32 },
{ "s19", RegisterI32 },
{ "s20", RegisterI32 },
{ "s21", RegisterI32 },
{ "s22", RegisterI32 },
{ "s23", RegisterI32 },
{ "s24", RegisterI32 },
{ "s25", RegisterI32 },
{ "s26", RegisterI32 },
{ "s27", RegisterI32 },
{ "s28", RegisterI32 },
{ "s29", RegisterI32 },
{ "s30", RegisterI32 },
{ "s31", RegisterI32 },
{ "q0", RegisterI128 },
{ "q1", RegisterI128 },
{ "q2", RegisterI128 },
{ "q3", RegisterI128 },
{ "q4", RegisterI128 },
{ "q5", RegisterI128 },
{ "q6", RegisterI128 },
{ "q7", RegisterI128 },
{ "q8", RegisterI128 },
{ "q9", RegisterI128 },
{ "q10", RegisterI128 },
{ "q11", RegisterI128 },
{ "q12", RegisterI128 },
{ "q13", RegisterI128 },
{ "q14", RegisterI128 },
{ "q15", RegisterI128 },
// Intel
{ "eax", RegisterI32 },
{ "ecx", RegisterI32 },
{ "edx", RegisterI32 },
{ "ebx", RegisterI32 },
{ "esp", RegisterI32 },
{ "ebp", RegisterI32 },
{ "esi", RegisterI32 },
{ "edi", RegisterI32 },
{ "eip", RegisterI32 },
{ "eflags", RegisterFlags32 },
{ "cs", RegisterI32 },
{ "ss", RegisterI32 },
{ "ds", RegisterI32 },
{ "es", RegisterI32 },
{ "fs", RegisterI32 },
{ "gs", RegisterI32 },
{ "st0", RegisterF80 },
{ "st1", RegisterF80 },
{ "st2", RegisterF80 },
{ "st3", RegisterF80 },
{ "st4", RegisterF80 },
{ "st5", RegisterF80 },
{ "st6", RegisterF80 },
{ "st7", RegisterF80 },
{ "fctrl", RegisterFlags32 },
{ "fstat", RegisterFlags32 },
{ "ftag", RegisterFlags32 },
{ "fiseg", RegisterFlags32 },
{ "fioff", RegisterFlags32 },
{ "foseg", RegisterFlags32 },
{ "fooff", RegisterFlags32 },
{ "fop", RegisterFlags32 },
{ "xmm0", RegisterXMM },
{ "xmm1", RegisterXMM },
{ "xmm2", RegisterXMM },
{ "xmm3", RegisterXMM },
{ "xmm4", RegisterXMM },
{ "xmm5", RegisterXMM },
{ "xmm6", RegisterXMM },
{ "xmm7", RegisterXMM },
{ "mxcsr", RegisterFlags32 },
{ "orig_eax", RegisterI32 },
{ "al", RegisterI8 },
{ "cl", RegisterI8 },
{ "dl", RegisterI8 },
{ "bl", RegisterI8 },
{ "ah", RegisterI8 },
{ "ch", RegisterI8 },
{ "dh", RegisterI8 },
{ "bh", RegisterI8 },
{ "ax", RegisterI16 },
{ "cx", RegisterI16 },
{ "dx", RegisterI16 },
{ "bx", RegisterI16 },
{ "bp", RegisterI16 },
{ "si", RegisterI16 },
{ "di", RegisterI16 },
{ "mm0", RegisterMMX },
{ "mm1", RegisterMMX },
{ "mm2", RegisterMMX },
{ "mm3", RegisterMMX },
{ "mm4", RegisterMMX },
{ "mm5", RegisterMMX },
{ "mm6", RegisterMMX },
{ "mm7", RegisterMMX }
};
static RegisterType guessType(const QByteArray &name)
{
static QHash<QByteArray, RegisterType> theTypes;
if (theTypes.isEmpty()) {
for (int i = 0; i != sizeof(theNameAndType) / sizeof(theNameAndType[0]); ++i)
theTypes[theNameAndType[i].name] = theNameAndType[i].type;
}
return theTypes.value(name, RegisterUnknown);
}
static int childCountFromType(int type)
{
switch (type) {
case RegisterUnknown: return 0;
case RegisterI8: return 0;
case RegisterI16: return 1;
case RegisterI32: return 2;
case RegisterI64: return 3;
case RegisterI128: return 4;
case RegisterF32: return 0;
case RegisterF64: return 0;
case RegisterF80: return 0;
case RegisterXMM: return 3;
case RegisterMMX: return 3;
case RegisterNeon: return 3;
case RegisterFlags32: return 0;
}
QTC_ASSERT(false, /**/);
return 0;
}
static int bitWidthFromType(int type, int subType)
{
const uint integer[] = { 8, 16, 32, 64, 128 };
const uint xmm[] = { 8, 16, 32, 64, 128 };
const uint mmx[] = { 8, 16, 32, 64, 128 };
const uint neon[] = { 8, 16, 32, 64, 128 };
switch (type) {
case RegisterUnknown: return 0;
case RegisterI8: return 8;
case RegisterI16: return integer[subType];
case RegisterI32: return integer[subType];
case RegisterI64: return integer[subType];
case RegisterI128: return integer[subType];
case RegisterF32: return 0;
case RegisterF64: return 0;
case RegisterF80: return 0;
case RegisterXMM: return xmm[subType];
case RegisterMMX: return mmx[subType];
case RegisterNeon: return neon[subType];
case RegisterFlags32: return 0;
}
QTC_ASSERT(false, /**/);
return 0;
}
static const uint TopLevelId = UINT_MAX;
static bool isTopLevelItem(const QModelIndex &index)
{
return quintptr(index.internalId()) == quintptr(TopLevelId);
}
Register::Register(const QByteArray &name_)
: name(name_), changed(true)
{
type = guessType(name);
}
//////////////////////////////////////////////////////////////////
//
// RegisterHandler
//
//////////////////////////////////////////////////////////////////
RegisterHandler::RegisterHandler()
{
setObjectName(QLatin1String("RegisterModel"));
m_base = 16;
calculateWidth();
#if USE_REGISTER_MODEL_TEST
new ModelTest(this, 0);
#endif
}
int RegisterHandler::rowCount(const QModelIndex &idx) const
{
if (idx.column() > 0)
return 0;
if (!idx.isValid())
return m_registers.size(); // Top level.
if (!isTopLevelItem(idx))
return 0; // Sub-Items don't have children.
if (idx.row() >= m_registers.size())
return 0;
return childCountFromType(m_registers[idx.row()].type);
}
int RegisterHandler::columnCount(const QModelIndex &idx) const
{
if (idx.column() > 0)
return 0;
if (!idx.isValid())
return 2;
if (!isTopLevelItem(idx))
return 0; // Sub-Items don't have children.
return 2;
}
QModelIndex RegisterHandler::index(int row, int col, const QModelIndex &parent) const
{
if (row < 0 || col < 0 || col >= 2)
return QModelIndex();
if (!parent.isValid()) // Top level.
return createIndex(row, col, TopLevelId);
if (!isTopLevelItem(parent)) // Sub-Item has no children.
return QModelIndex();
if (parent.column() > 0)
return QModelIndex();
return createIndex(row, col, parent.row());
}
QModelIndex RegisterHandler::parent(const QModelIndex &idx) const
{
if (!idx.isValid())
return QModelIndex();
if (!isTopLevelItem(idx))
return createIndex(idx.internalId(), 0, TopLevelId);
return QModelIndex();
}
// Editor value: Preferably number, else string.
QVariant Register::editValue() const
{
bool ok = true;
// Try to convert to number?
const qulonglong v = value.toULongLong(&ok, 0); // Autodetect format
if (ok)
return QVariant(v);
return QVariant(value);
}
// Editor value: Preferably padded number, else padded string.
QString Register::displayValue(int base, int strlen) const
{
const QVariant editV = editValue();
if (editV.type() == QVariant::ULongLong)
return QString::fromLatin1("%1").arg(editV.toULongLong(), strlen, base);
const QString stringValue = editV.toString();
if (stringValue.size() < strlen)
return QString(strlen - stringValue.size(), QLatin1Char(' ')) + QLatin1String(value);
return stringValue;
}
QVariant RegisterHandler::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QModelIndex topLevel = index.parent();
const int mainRow = topLevel.isValid() ? topLevel.row() : index.row();
if (mainRow >= m_registers.size())
return QVariant();
const Register ® = m_registers.at(mainRow);
if (topLevel.isValid()) {
//
// Nested
//
int subType = index.row();
int bitWidth = bitWidthFromType(reg.type, subType);
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0: {
switch (bitWidth) {
case 8: return QLatin1String("[Bytes]");
case 16: return QLatin1String("[Words]");
case 32: return QLatin1String("[DWords]");
case 64: return QLatin1String("[QWords]");
case 128: return QLatin1String("[TWords]");
case -32: return QLatin1String("[Single]");
case -64: return QLatin1String("[Double]");
return QVariant(bitWidth);
}
}
}
default:
break;
}
} else {
//
// Toplevel
//
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0: {
const QString padding = QLatin1String(" ");
return QVariant(padding + QLatin1String(reg.name) + padding);
//return QVariant(reg.name);
}
case 1: // Display: Pad value for alignment
return reg.displayValue(m_base, m_strlen);
} // switch column
case Qt::EditRole: // Edit: Unpadded for editing
return reg.editValue();
case Qt::TextAlignmentRole:
return index.column() == 1 ? QVariant(Qt::AlignRight) : QVariant();
default:
break;
}
}
return QVariant();
}
QVariant RegisterHandler::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0: return tr("Name");
case 1: return tr("Value (Base %1)").arg(m_base);
};
}
return QVariant();
}
Qt::ItemFlags RegisterHandler::flags(const QModelIndex &idx) const
{
if (!idx.isValid())
return Qt::ItemFlags();
const Qt::ItemFlags notEditable = Qt::ItemIsSelectable|Qt::ItemIsEnabled;
// Can edit registers if they are hex numbers and not arrays.
if (idx.column() == 1
&& IntegerWatchLineEdit::isUnsignedHexNumber(QLatin1String(m_registers.at(idx.row()).value)))
return notEditable | Qt::ItemIsEditable;
return notEditable;
}
void RegisterHandler::removeAll()
{
beginResetModel();
m_registers.clear();
endResetModel();
}
bool RegisterHandler::isEmpty() const
{
return m_registers.isEmpty();
}
// Compare register sets by name
static inline bool compareRegisterSet(const Registers &r1, const Registers &r2)
{
if (r1.size() != r2.size())
return false;
const int size = r1.size();
for (int r = 0; r < size; r++)
if (r1.at(r).name != r2.at(r).name)
return false;
return true;
}
void RegisterHandler::setRegisters(const Registers ®isters)
{
beginResetModel();
m_registers = registers;
const int size = m_registers.size();
for (int r = 0; r < size; r++)
m_registers[r].changed = false;
calculateWidth();
endResetModel();
}
void RegisterHandler::setAndMarkRegisters(const Registers ®isters)
{
if (!compareRegisterSet(m_registers, registers)) {
setRegisters(registers);
return;
}
const int size = m_registers.size();
for (int r = 0; r != size; ++r) {
const QModelIndex regIndex = index(r, 1, QModelIndex());
if (m_registers.at(r).value != registers.at(r).value) {
// Indicate red if values change, keep changed.
m_registers[r].changed = m_registers.at(r).changed
|| !m_registers.at(r).value.isEmpty();
m_registers[r].value = registers.at(r).value;
emit dataChanged(regIndex, regIndex);
}
emit registerSet(regIndex); // Notify attached memory views.
}
}
Registers RegisterHandler::registers() const
{
return m_registers;
}
void RegisterHandler::calculateWidth()
{
m_strlen = (m_base == 2 ? 64 : m_base == 8 ? 32 : m_base == 10 ? 26 : 16);
}
void RegisterHandler::setNumberBase(int base)
{
if (m_base != base) {
beginResetModel();
m_base = base;
calculateWidth();
endResetModel();
}
}
} // namespace Internal
} // namespace Debugger
|