File: RegisterViewModelBase.h

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (547 lines) | stat: -rw-r--r-- 14,586 bytes parent folder | download | duplicates (3)
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

#ifndef REGISTER_VIEW_MODEL_BASE_H_20151206_
#define REGISTER_VIEW_MODEL_BASE_H_20151206_

#include "Register.h"
#include "util/Integer.h"
#include <QAbstractItemModel>
#include <deque>
#include <memory>
#include <vector>

Q_DECLARE_METATYPE(std::vector<NumberDisplayMode>)
Q_DECLARE_METATYPE(Register)

namespace RegisterViewModelBase {

class RegisterViewItem;
class AbstractRegisterItem;
class CategoriesHolder;
class Category;
class SIMDCategory;
class FPUCategory;

class EDB_EXPORT Model : public QAbstractItemModel {
	Q_OBJECT

public:
	enum Column {
		NAME_COLUMN,
		VALUE_COLUMN,
		COMMENT_COLUMN,
		NUM_COLS
	};

	enum class ElementSize {
		BYTE  = 1,
		WORD  = 2,
		DWORD = 4,
		QWORD = 8,
	};

	enum SizesOrder {
		BYTES_ROW,
		WORDS_ROW,
		DWORDS_ROW,
		QWORDS_ROW
	};

	enum FormatsOrder {
		SIMD_HEX_ROW,
		SIMD_SIGNED_ROW,
		SIMD_UNSIGNED_ROW,
		SIMD_FLOAT_ROW
	};

	enum FPUFormatsOrder {
		FPU_HEX_ROW,
		FPU_FLOAT_ROW
	};

	enum Role {
		// true if changed, false otherwise
		// Property of: register's value column
		RegisterChangedRole = Qt::UserRole,
		// fixed length of text (name, value) or undefined (0) if it's not fixed (comment)
		// Type: int
		FixedLengthRole,
		// QByteArray with raw data
		// Property of: register's value column
		RawValueRole,
		// What user chose to be current size of SIMD element.
		// Type: ElementSize
		// Property of: Category
		ChosenSIMDSizeRole,
		// What user chose to be current format of SIMD element.
		// Type: NumberDisplayMode
		// Property of: Category
		ChosenSIMDFormatRole,
		// What row to take in given register index to get chosen-sized elements root
		// Type: int
		// Property of: Category
		ChosenSIMDSizeRowRole,
		// What row to take in given sized element to get chosen-formatted element
		// Type: int
		// Property of: Category
		ChosenSIMDFormatRowRole,
		// Which SIMD formats are valid to be set.
		// Type: std::vector<NumberDisplayMode>
		// Property of: Category
		ValidSIMDFormatsRole,
		// Whether the index corresponds to a normal register, i.e. not a bit field nor a SIMD element
		// Type: bool
		IsNormalRegisterRole,
		// Whether the index corresponds to a bit field of a normal register
		// Type: bool
		IsBitFieldRole,
		// Whether the index corresponds to an element of a SIMD register
		// Type: bool
		IsSIMDElementRole,
		// Whether the index corresponds to any-sized FPU register
		// Type: bool
		IsFPURegisterRole,
		// Offset of bit field in the register, starting from least significant bit
		// Type: int
		BitFieldOffsetRole,
		// Length of bit field in bits
		// Type: int
		BitFieldLengthRole,
		// Value as EDB's Register
		// Type: Register
		ValueAsRegisterRole,

		// What user chose to be current format of FPU registers
		// Type: NumberDisplayMode
		// Property of: Category
		ChosenFPUFormatRole,
		// What row to take in given register to get chosen-formatted value
		// Type: int
		// Property of: Category
		ChosenFPUFormatRowRole,

		FirstConcreteRole = Qt::UserRole + 10000 // first role available for use in derived models
	};

	Model(QObject *parent = nullptr);
	~Model() override = default;

public:
	int rowCount(const QModelIndex &parent = QModelIndex()) const override;
	int columnCount(const QModelIndex &parent = QModelIndex()) const override;
	QModelIndex parent(const QModelIndex &index) const override;
	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
	bool setData(const QModelIndex &index, const QVariant &data, int role = Qt::EditRole) override;
	Qt::ItemFlags flags(const QModelIndex &index) const override;

public:
	void setActiveIndex(const QModelIndex &newActiveIndex);
	QModelIndex activeIndex() const;

public:
	void setChosenSIMDSize(const QModelIndex &index, ElementSize newSize);
	void setChosenSIMDFormat(const QModelIndex &index, NumberDisplayMode newFormat);
	void setChosenFPUFormat(const QModelIndex &index, NumberDisplayMode newFormat);

	// Should be called after updating all the data
	void dataUpdateFinished();
	// should be called when the debugger is about to resume, to save current register values to previous
	void saveValues();

protected:
	// All categories are there to stay after they've been inserted
	Category *addCategory(const QString &name);
	FPUCategory *addFPUCategory(const QString &name);
	SIMDCategory *addSIMDCategory(const QString &name, const std::vector<NumberDisplayMode> &validFormats);
	void hide(Category *cat);
	void show(Category *cat);
	void hide(AbstractRegisterItem *reg);
	void show(AbstractRegisterItem *reg);
	void hideAll();

private:
	std::unique_ptr<CategoriesHolder> rootItem;
	QPersistentModelIndex activeIndex_;

Q_SIGNALS:
	void SIMDDisplayFormatChanged();
	void FPUDisplayFormatChanged();
};

inline std::ostream &operator<<(std::ostream &os, Model::ElementSize size) {
	os << static_cast<std::underlying_type<Model::ElementSize>::type>(size);
	return os;
}

class RegisterViewItem {
protected:
	RegisterViewItem *parentItem = nullptr;
	int row_                     = -1;
	QString name_;

public:
	RegisterViewItem(const QString &name)
		: name_(name) {
	}

	virtual ~RegisterViewItem() = default;

public:
	void init(RegisterViewItem *parent, int row);

	virtual RegisterViewItem *parent() const {
		return parentItem;
	}

	QString name() const {
		return name_;
	}

	virtual int row() const {
		Q_ASSERT(row_ != -1);
		return row_;
	}

	virtual bool changed() const {
		return false;
	}

	virtual RegisterViewItem *child(int /*row*/) {
		return nullptr;
	}

	virtual int childCount() const {
		return 0;
	}

	virtual QVariant data(int /*column*/) const {
		return QVariant();
	}

	virtual int valueMaxLength() const {
		return 0;
	}

public:
	virtual QByteArray rawValue() const = 0;
};

class AbstractRegisterItem : public RegisterViewItem {
protected:
	AbstractRegisterItem(const QString &name)
		: RegisterViewItem(name) {
	}

public:
	// check whether it has some valid value (not unknown etc.)
	virtual bool valid() const = 0;
	// Should be used when EDB is about to resume execution of debuggee —
	// so that it's possible to check whether it changed on next stop
	virtual void saveValue() = 0;
	// clear all data, mark them unknown, both for current and previous states
	virtual void invalidate()                      = 0;
	virtual bool setValue(const QString &valueStr) = 0;
	virtual bool setValue(const QByteArray &value) = 0;
	virtual bool setValue(const Register &reg)     = 0;
};

template <class StoredType>
class RegisterItem : public AbstractRegisterItem {
protected:
	QString comment_;
	StoredType value_;
	StoredType prevValue_;
	bool valueKnown_     = false;
	bool prevValueKnown_ = false;

protected:
	virtual QString valueString() const;

public:
	RegisterItem(const QString &name);
	bool valid() const override;
	void saveValue() override;
	bool changed() const override;
	void invalidate() override;
	int childCount() const override;
	RegisterViewItem *child(int) override;
	QVariant data(int column) const override;
	QByteArray rawValue() const override;
	bool setValue(const QString &valueStr) override;
	bool setValue(const QByteArray &value) override;
	bool setValue(const Register &reg) override;
};

template <class StoredType>
class SimpleRegister : public RegisterItem<StoredType> {
public:
	SimpleRegister(const QString &name)
		: RegisterItem<StoredType>(name) {
	}

public:
	virtual void update(const StoredType &newValue, const QString &newComment);
	int valueMaxLength() const override;
};

struct BitFieldDescriptionEx {
	QString name;
	unsigned offset;
	unsigned length;
	std::vector<QString> explanations;

	// Prevent compiler warnings about missing initializer: make default argument explicitly default
	BitFieldDescriptionEx(QString name, unsigned offset, unsigned length, std::vector<QString> explanations = std::vector<QString>())
		: name(name), offset(offset), length(length), explanations(explanations) {
	}
};

class BitFieldProperties {
public:
	virtual ~BitFieldProperties()   = default;
	virtual unsigned offset() const = 0;
	virtual unsigned length() const = 0;
};

template <class UnderlyingType>
class FlagsRegister;

template <class UnderlyingType>
class BitFieldItem final : public RegisterViewItem, public BitFieldProperties {
protected:
	unsigned offset_;
	unsigned length_;
	std::vector<QString> explanations;

protected:
	FlagsRegister<UnderlyingType> *reg() const;
	UnderlyingType lengthToMask() const;
	UnderlyingType calcValue(UnderlyingType regVal) const;
	UnderlyingType value() const;
	UnderlyingType prevValue() const;

public:
	BitFieldItem(const BitFieldDescriptionEx &descr);

public:
	QVariant data(int column) const override;
	bool changed() const override;
	int valueMaxLength() const override;
	QByteArray rawValue() const override;

	unsigned offset() const override;
	unsigned length() const override;
};

template <class StoredType>
class FlagsRegister final : public SimpleRegister<StoredType> {
	template <class UnderlyingType>
	friend class BitFieldItem;

public:
	FlagsRegister(const QString &name, const std::vector<BitFieldDescriptionEx> &bitFields);

public:
	RegisterViewItem *child(int) override;
	int childCount() const override;

protected:
	std::vector<BitFieldItem<StoredType>> fields;
};

template <class StoredType>
class SIMDRegister;

template <class StoredType, class SizingType>
class SIMDFormatItem final : public RegisterViewItem {
public:
	SIMDFormatItem(NumberDisplayMode format);

public:
	using RegisterViewItem::name;

public:
	NumberDisplayMode format() const;
	QByteArray rawValue() const override;
	QVariant data(int column) const override;
	bool changed() const override;
	int valueMaxLength() const override;

private:
	QString name(NumberDisplayMode format) const;

private:
	NumberDisplayMode format_;
};

class SIMDElement {}; // generic non-templated class to dynamic_cast to

template <class StoredType, class SizingType>
class SIMDSizedElement final : public RegisterViewItem, public SIMDElement {
	friend class SIMDFormatItem<StoredType, SizingType>;

public:
	SIMDSizedElement(const QString &name, const std::vector<NumberDisplayMode> &validFormats);

public:
	QByteArray rawValue() const override;
	QVariant data(int column) const override;
	RegisterViewItem *child(int row) override;
	bool changed() const override;
	int childCount() const override;
	int valueMaxLength() const override;

private:
	QString valueString() const;
	SIMDRegister<StoredType> *reg() const;
	SizingType value() const;
	bool valid() const;

private:
	std::vector<SIMDFormatItem<StoredType, SizingType>> formats;
};

template <class StoredType>
class SIMDSizedElementsContainer final : public RegisterViewItem {
	template <class SizeType, class... Args>
	void addElement(Args &&... args);

protected:
	std::vector<std::unique_ptr<RegisterViewItem>> elements;

public:
	SIMDSizedElementsContainer(const QString &name, std::size_t size, const std::vector<NumberDisplayMode> &validFormats);
	SIMDSizedElementsContainer(SIMDSizedElementsContainer &&other) noexcept;
	RegisterViewItem *child(int row) override;
	int childCount() const override;
	QVariant data(int column) const override;
	QByteArray rawValue() const override;
	bool changed() const override;
};

template <class StoredType>
class SIMDRegister final : public SimpleRegister<StoredType> {
	template <class U, class V>
	friend class SIMDSizedElement;

protected:
	std::deque<SIMDSizedElementsContainer<StoredType>> sizedElementContainers;

	SIMDCategory *category() const;

public:
	SIMDRegister(const QString &name, const std::vector<NumberDisplayMode> &validFormats);
	int childCount() const override;
	RegisterViewItem *child(int) override;
	QVariant data(int column) const override;
};

class GenericFPURegister {}; // generic non-templated class to dynamic_cast to

template <class FloatType>
class FPURegister final : public SimpleRegister<FloatType>, public GenericFPURegister {
	template <class U, class V>
	friend class SIMDFormatItem;

public:
	FPURegister(const QString &name);

public:
	QString valueString() const override;
	RegisterViewItem *child(int) override;
	int childCount() const override;
	int valueMaxLength() const override;
	void saveValue() override;
	void update(const FloatType &newValue, const QString &newComment) override;

protected:
	FPUCategory *category() const;

private:
	FloatType value() const;

protected:
	std::vector<SIMDFormatItem<FloatType, FloatType>> formats;
};

class Category : public RegisterViewItem {
public:
	Category(const QString &name, int row);
	Category(Category &&other) noexcept;

public:
	AbstractRegisterItem *getRegister(std::size_t i) const;
	QByteArray rawValue() const override;
	QVariant data(int column) const override;
	RegisterViewItem *child(int row) override;
	bool visible() const;
	int childCount() const override;
	void addRegister(std::unique_ptr<AbstractRegisterItem> reg);
	void hide();
	void saveValues();
	void show();

private:
	std::vector<std::unique_ptr<AbstractRegisterItem>> registers;
	bool visible_ = true;
};

class SIMDCategory final : public Category {
public:
	SIMDCategory(const QString &name, int row, const std::vector<NumberDisplayMode> &validFormats);
	~SIMDCategory();

public:
	virtual Model::ElementSize chosenSize() const;
	virtual NumberDisplayMode chosenFormat() const;
	virtual void setChosenSize(Model::ElementSize newSize);
	virtual void setChosenFormat(NumberDisplayMode newFormat);

public:
	const std::vector<NumberDisplayMode> &validFormats() const;

private:
	bool sizeChanged_   = false;
	bool formatChanged_ = false;
	Model::ElementSize chosenSize_;
	NumberDisplayMode chosenFormat_;
	std::vector<NumberDisplayMode> const validFormats_;
};

class FPUCategory final : public Category {
public:
	FPUCategory(const QString &name, int row);
	~FPUCategory();

public:
	NumberDisplayMode chosenFormat() const;
	void setChosenFormat(NumberDisplayMode newFormat);

private:
	bool formatChanged_ = false;
	NumberDisplayMode chosenFormat_;
};

class CategoriesHolder final : public RegisterViewItem {
	friend class Model;

public:
	CategoriesHolder();

public:
	template <typename CategoryType = Category>
	CategoryType *insert(const QString &name);

	SIMDCategory *insertSimd(const QString &name, const std::vector<NumberDisplayMode> &validFormats);
	int childCount() const override;
	RegisterViewItem *child(int row) override;
	QVariant data(int column) const override;
	QByteArray rawValue() const override;

private:
	std::vector<std::unique_ptr<Category>> categories;
};

}

#endif