@theia/memory-inspector 1.45.1 → 1.46.0-next.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +71 -71
  2. package/lib/browser/diff-widget/memory-diff-options-widget.d.ts +36 -36
  3. package/lib/browser/diff-widget/memory-diff-options-widget.js +132 -132
  4. package/lib/browser/diff-widget/memory-diff-select-widget.d.ts +50 -50
  5. package/lib/browser/diff-widget/memory-diff-select-widget.js +137 -137
  6. package/lib/browser/diff-widget/memory-diff-table-widget.d.ts +77 -77
  7. package/lib/browser/diff-widget/memory-diff-table-widget.js +314 -314
  8. package/lib/browser/diff-widget/memory-diff-widget-types.d.ts +41 -41
  9. package/lib/browser/diff-widget/memory-diff-widget-types.js +23 -23
  10. package/lib/browser/editable-widget/memory-editable-table-widget.d.ts +64 -64
  11. package/lib/browser/editable-widget/memory-editable-table-widget.js +324 -324
  12. package/lib/browser/memory-inspector-frontend-contribution.d.ts +46 -46
  13. package/lib/browser/memory-inspector-frontend-contribution.js +291 -291
  14. package/lib/browser/memory-inspector-frontend-module.d.ts +21 -21
  15. package/lib/browser/memory-inspector-frontend-module.js +87 -87
  16. package/lib/browser/memory-provider/cdt-gdb-memory-provider.d.ts +35 -35
  17. package/lib/browser/memory-provider/cdt-gdb-memory-provider.js +136 -136
  18. package/lib/browser/memory-provider/memory-provider-service.d.ts +36 -36
  19. package/lib/browser/memory-provider/memory-provider-service.js +101 -101
  20. package/lib/browser/memory-provider/memory-provider.d.ts +65 -65
  21. package/lib/browser/memory-provider/memory-provider.js +96 -96
  22. package/lib/browser/memory-provider/memory-provider.spec.d.ts +16 -16
  23. package/lib/browser/memory-provider/memory-provider.spec.js +23 -23
  24. package/lib/browser/memory-widget/memory-options-widget.d.ts +136 -136
  25. package/lib/browser/memory-widget/memory-options-widget.js +564 -564
  26. package/lib/browser/memory-widget/memory-table-widget.d.ts +146 -146
  27. package/lib/browser/memory-widget/memory-table-widget.js +487 -487
  28. package/lib/browser/memory-widget/memory-widget.d.ts +33 -33
  29. package/lib/browser/memory-widget/memory-widget.js +112 -112
  30. package/lib/browser/register-widget/register-filter-service.d.ts +42 -42
  31. package/lib/browser/register-widget/register-filter-service.js +81 -81
  32. package/lib/browser/register-widget/register-options-widget.d.ts +81 -81
  33. package/lib/browser/register-widget/register-options-widget.js +338 -338
  34. package/lib/browser/register-widget/register-table-widget.d.ts +77 -77
  35. package/lib/browser/register-widget/register-table-widget.js +230 -230
  36. package/lib/browser/register-widget/register-widget-types.d.ts +29 -29
  37. package/lib/browser/register-widget/register-widget-types.js +35 -35
  38. package/lib/browser/utils/memory-commands.d.ts +27 -27
  39. package/lib/browser/utils/memory-commands.js +67 -67
  40. package/lib/browser/utils/memory-hover-renderer.d.ts +33 -33
  41. package/lib/browser/utils/memory-hover-renderer.js +111 -111
  42. package/lib/browser/utils/memory-recents.d.ts +27 -27
  43. package/lib/browser/utils/memory-recents.js +52 -52
  44. package/lib/browser/utils/memory-widget-components.d.ts +58 -58
  45. package/lib/browser/utils/memory-widget-components.js +69 -69
  46. package/lib/browser/utils/memory-widget-manager.d.ts +50 -50
  47. package/lib/browser/utils/memory-widget-manager.js +182 -182
  48. package/lib/browser/utils/memory-widget-utils.d.ts +113 -113
  49. package/lib/browser/utils/memory-widget-utils.js +42 -42
  50. package/lib/browser/utils/memory-widget-variable-utils.d.ts +57 -57
  51. package/lib/browser/utils/memory-widget-variable-utils.js +143 -143
  52. package/lib/browser/utils/multi-select-bar.d.ts +31 -31
  53. package/lib/browser/utils/multi-select-bar.js +34 -34
  54. package/lib/browser/wrapper-widgets/memory-dock-panel.d.ts +24 -24
  55. package/lib/browser/wrapper-widgets/memory-dock-panel.js +48 -48
  56. package/lib/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.d.ts +23 -23
  57. package/lib/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.js +57 -57
  58. package/lib/browser/wrapper-widgets/memory-layout-widget.d.ts +51 -51
  59. package/lib/browser/wrapper-widgets/memory-layout-widget.js +191 -191
  60. package/lib/common/util.d.ts +21 -21
  61. package/lib/common/util.js +30 -30
  62. package/lib/common/utils.spec.d.ts +16 -16
  63. package/lib/common/utils.spec.js +45 -45
  64. package/package.json +4 -4
  65. package/src/browser/diff-widget/memory-diff-options-widget.tsx +152 -152
  66. package/src/browser/diff-widget/memory-diff-select-widget.tsx +163 -163
  67. package/src/browser/diff-widget/memory-diff-table-widget.tsx +366 -366
  68. package/src/browser/diff-widget/memory-diff-widget-types.ts +45 -45
  69. package/src/browser/editable-widget/memory-editable-table-widget.tsx +359 -359
  70. package/src/browser/memory-inspector-frontend-contribution.ts +301 -301
  71. package/src/browser/memory-inspector-frontend-module.ts +118 -118
  72. package/src/browser/memory-provider/cdt-gdb-memory-provider.ts +132 -132
  73. package/src/browser/memory-provider/memory-provider-service.ts +86 -86
  74. package/src/browser/memory-provider/memory-provider.spec.ts +23 -23
  75. package/src/browser/memory-provider/memory-provider.ts +119 -119
  76. package/src/browser/memory-widget/memory-options-widget.tsx +738 -738
  77. package/src/browser/memory-widget/memory-table-widget.tsx +625 -625
  78. package/src/browser/memory-widget/memory-widget.ts +114 -114
  79. package/src/browser/register-widget/register-filter-service.ts +76 -76
  80. package/src/browser/register-widget/register-options-widget.tsx +393 -393
  81. package/src/browser/register-widget/register-table-widget.tsx +276 -276
  82. package/src/browser/register-widget/register-widget-types.ts +45 -45
  83. package/src/browser/register-widget/register-widget.css +34 -34
  84. package/src/browser/style/index.css +746 -746
  85. package/src/browser/style/memory-lock.svg +21 -21
  86. package/src/browser/style/memory-view.svg +20 -20
  87. package/src/browser/style/register-lock.svg +29 -29
  88. package/src/browser/style/register-view.svg +28 -28
  89. package/src/browser/utils/memory-commands.ts +76 -76
  90. package/src/browser/utils/memory-hover-renderer.ts +113 -113
  91. package/src/browser/utils/memory-recents.ts +58 -58
  92. package/src/browser/utils/memory-widget-components.tsx +193 -193
  93. package/src/browser/utils/memory-widget-manager.ts +179 -179
  94. package/src/browser/utils/memory-widget-utils.tsx +132 -132
  95. package/src/browser/utils/memory-widget-variable-utils.ts +170 -170
  96. package/src/browser/utils/multi-select-bar.css +61 -61
  97. package/src/browser/utils/multi-select-bar.tsx +75 -75
  98. package/src/browser/wrapper-widgets/memory-dock-panel.ts +51 -51
  99. package/src/browser/wrapper-widgets/memory-dockpanel-placeholder-widget.tsx +38 -38
  100. package/src/browser/wrapper-widgets/memory-layout-widget.tsx +167 -167
  101. package/src/common/util.ts +28 -28
  102. package/src/common/utils.spec.ts +52 -52
@@ -1,359 +1,359 @@
1
- /********************************************************************************
2
- * Copyright (C) 2021 Ericsson and others.
3
- *
4
- * This program and the accompanying materials are made available under the
5
- * terms of the Eclipse Public License v. 2.0 which is available at
6
- * http://www.eclipse.org/legal/epl-2.0.
7
- *
8
- * This Source Code may also be made available under the following Secondary
9
- * Licenses when the conditions for such availability set forth in the Eclipse
10
- * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- * with the GNU Classpath Exception which is available at
12
- * https://www.gnu.org/software/classpath/license.html.
13
- *
14
- * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- ********************************************************************************/
16
-
17
- import { Key, KeyCode } from '@theia/core/lib/browser';
18
- import { Deferred } from '@theia/core/lib/common/promise-util';
19
- import { injectable } from '@theia/core/shared/inversify';
20
- import * as React from '@theia/core/shared/react';
21
- import * as Long from 'long';
22
- import { DebugProtocol } from '@vscode/debugprotocol';
23
- import { hexStrToUnsignedLong } from '../../common/util';
24
- import { MemoryOptionsWidget } from '../memory-widget/memory-options-widget';
25
- import { MemoryTable, MemoryTableWidget } from '../memory-widget/memory-table-widget';
26
- import { MemoryWidget } from '../memory-widget/memory-widget';
27
- import { EasilyMappedObject } from '../utils/memory-hover-renderer';
28
- import { Constants, Interfaces } from '../utils/memory-widget-utils';
29
- import { nls } from '@theia/core/lib/common/nls';
30
-
31
- export type EditableMemoryWidget = MemoryWidget<MemoryOptionsWidget, MemoryEditableTableWidget>;
32
- export namespace EditableMemoryWidget {
33
- export const ID = 'editable.memory.widget';
34
- }
35
-
36
- @injectable()
37
- export class MemoryEditableTableWidget extends MemoryTableWidget {
38
- protected pendingMemoryEdits = new Map<string, string>();
39
- protected override previousBytes: Interfaces.LabeledUint8Array | undefined;
40
- protected memoryEditsCompleted = new Deferred<void>();
41
- protected highlightedField: Long = Long.fromInt(-1);
42
- protected writeErrorInfo: { location: string, error: string } | undefined;
43
- protected currentErrorTimeout: number | undefined;
44
-
45
- protected doShowMoreMemoryBefore = false;
46
- protected doShowMoreMemoryAfter = false;
47
-
48
- protected override async doInit(): Promise<void> {
49
- this.memoryEditsCompleted.resolve();
50
- await super.doInit();
51
- this.addClass('editable');
52
- }
53
-
54
- resetModifiedValue(valueAddress: Long): void {
55
- const didChange = this.pendingMemoryEdits.delete(valueAddress.toString());
56
- if (didChange) {
57
- this.update();
58
- }
59
- }
60
-
61
- protected override getState(): void {
62
- super.getState();
63
- if (!this.isInBounds(this.highlightedField)) {
64
- this.highlightedField = this.memory.address;
65
- }
66
- }
67
-
68
- protected override async handleMemoryChange(newMemory: Interfaces.MemoryReadResult): Promise<void> {
69
- await this.memoryEditsCompleted.promise;
70
- if (newMemory.bytes.length === 0) {
71
- this.pendingMemoryEdits.clear();
72
- }
73
- super.handleMemoryChange(newMemory);
74
- }
75
-
76
- protected override areSameRegion(a: Interfaces.MemoryReadResult, b?: Interfaces.MemoryReadResult): boolean {
77
- return b !== undefined && a.address.equals(b.address) && a.bytes.length === b.bytes.length;
78
- }
79
-
80
- protected override getTableFooter(): React.ReactNode {
81
- const showButtons = !!this.pendingMemoryEdits.size && !this.writeErrorInfo;
82
- return (
83
- (showButtons || this.writeErrorInfo) && (
84
- <div className='memory-edit-button-container'>
85
- {showButtons && <button
86
- className='theia-button secondary'
87
- onClick={this.handleClearEditClick}
88
- type='reset'
89
- title={nls.localize('theia/memory-inspector/editable/clear', 'Clear Changes')}
90
- >
91
- {nls.localize('theia/memory-inspector/editable/clear', 'Clear Changes')}
92
- </button>}
93
- {showButtons && <button
94
- className='theia-button main'
95
- onClick={this.submitMemoryEdits}
96
- type='submit'
97
- title={nls.localize('theia/memory-inspector/editable/apply', 'Apply Changes')}
98
- >
99
- {nls.localize('theia/memory-inspector/editable/apply', 'Apply Changes')}
100
- </button>}
101
- {!!this.writeErrorInfo && <div className='memory-edit-error'>
102
- <div className='memory-edit-error-location'>{`Error writing to 0x${Long.fromString(this.writeErrorInfo?.location).toString(16)}`}</div>
103
- <div className='memory-edit-error-details'>{this.writeErrorInfo?.error}</div>
104
- </div>}
105
- </div>)
106
- );
107
- }
108
-
109
- protected override getBitAttributes(arrayOffset: number, iteratee: Interfaces.LabeledUint8Array): Partial<Interfaces.FullNodeAttributes> {
110
- const attributes = super.getBitAttributes(arrayOffset, iteratee);
111
- const classNames = attributes.className?.split(' ') ?? [];
112
- const itemID = this.memory.address.add(arrayOffset);
113
- const isHighlight = itemID.equals(this.highlightedField);
114
- const isEditPending = this.pendingMemoryEdits.has(itemID.toString());
115
- const padder = isHighlight && isEditPending ? '\xa0' : '0'; // non-breaking space so it doesn't look like plain whitespace.
116
- const stringValue = (this.pendingMemoryEdits.get(itemID.toString()) ?? this.memory.bytes[arrayOffset].toString(16)).padStart(2, padder);
117
- if (!this.options.isFrozen) {
118
- if (isHighlight) {
119
- classNames.push('highlight');
120
- }
121
- if (isEditPending) {
122
- classNames.push('modified');
123
- }
124
- }
125
- return {
126
- ...attributes,
127
- className: classNames.join(' '),
128
- content: stringValue
129
- };
130
- }
131
-
132
- protected override getHoverForChunk(span: HTMLElement): EasilyMappedObject | undefined {
133
- const addressAsString = span.getAttribute('data-id');
134
- if (addressAsString) {
135
- const address = hexStrToUnsignedLong(addressAsString);
136
- const { value } = this.composeByte(address, true);
137
- const { value: inMemory } = this.composeByte(address, false);
138
- const oldValue = this.previousBytes && this.composeByte(address, false, this.previousBytes).value;
139
- const decimal = parseInt(value, 16);
140
- const octal = decimal.toString(8).padStart(this.options.byteSize / 8, '0');
141
- const UTF8 = String.fromCharCode(decimal);
142
- const binary = this.getPaddedBinary(decimal);
143
- const toSend: EasilyMappedObject = { hex: value, octal, binary, decimal };
144
- if (UTF8) {
145
- toSend.UTF8 = UTF8;
146
- }
147
- if (inMemory !== value) {
148
- toSend['Current Value'] = inMemory;
149
- }
150
- if (oldValue !== undefined && oldValue !== value) {
151
- toSend['Previous Value'] = oldValue;
152
- }
153
- return toSend;
154
- }
155
- return undefined;
156
- }
157
-
158
- protected composeByte(
159
- addressPlusArrayOffset: Long,
160
- usePendingEdits: boolean,
161
- dataSource: Uint8Array = this.memory.bytes,
162
- ): Interfaces.ByteFromChunkData {
163
- let value = '';
164
-
165
- const offset = addressPlusArrayOffset.subtract(this.memory.address);
166
- const chunksPerByte = this.options.byteSize / 8;
167
- const startingChunkIndex = offset.subtract(offset.modulo(chunksPerByte));
168
- const address = this.memory.address.add(startingChunkIndex.divide(chunksPerByte));
169
- for (let i = 0; i < chunksPerByte; i += 1) {
170
- const targetOffset = startingChunkIndex.add(i);
171
- const targetChunk = this.getFromMapOrArray(targetOffset, usePendingEdits, dataSource);
172
- value += targetChunk.padStart(2, '0');
173
- }
174
-
175
- return { address, value };
176
- }
177
-
178
- protected getFromMapOrArray(arrayOffset: Long, usePendingEdits: boolean, dataSource: Uint8Array = this.memory.bytes): string {
179
- let value = usePendingEdits ? this.pendingMemoryEdits.get(arrayOffset.add(this.memory.address).toString()) : undefined;
180
- if (value === undefined) {
181
- value = dataSource[arrayOffset.toInt()]?.toString(16) ?? '';
182
- }
183
- return value;
184
- }
185
-
186
- protected handleClearEditClick = (): void => this.clearEdits();
187
-
188
- protected clearEdits(address?: Long): void {
189
- if (typeof address === 'number') {
190
- this.pendingMemoryEdits.delete(address);
191
- } else {
192
- this.pendingMemoryEdits.clear();
193
- }
194
- this.update();
195
- }
196
-
197
- protected submitMemoryEdits = async (): Promise<void> => {
198
- this.memoryEditsCompleted = new Deferred();
199
- let didUpdateMemory = false;
200
- for (const [key, edit] of this.createUniqueEdits()) {
201
- try {
202
- await this.doWriteMemory(edit);
203
- didUpdateMemory = true;
204
- this.pendingMemoryEdits.delete(key);
205
- } catch (e) {
206
- console.warn('Problem writing memory with arguments', edit, '\n', e);
207
- const text = e instanceof Error ? e.message : 'Unknown error';
208
- this.showWriteError(key, text);
209
- break;
210
- }
211
- }
212
- this.memoryEditsCompleted.resolve();
213
- if (didUpdateMemory) {
214
- this.optionsWidget.fetchNewMemory();
215
- }
216
- };
217
-
218
- protected createUniqueEdits(): Array<[string, DebugProtocol.WriteMemoryArguments]> {
219
- const addressesSubmitted = new Set<string>();
220
- const edits: Array<[string, DebugProtocol.WriteMemoryArguments]> = [];
221
- for (const k of this.pendingMemoryEdits.keys()) {
222
- const address = Long.fromString(k);
223
- const { address: addressToSend, value: valueToSend } = this.composeByte(address, true);
224
- const memoryReference = '0x' + addressToSend.toString(16);
225
- if (!addressesSubmitted.has(memoryReference)) {
226
- const data = Buffer.from(valueToSend, 'hex').toString('base64');
227
- edits.push([k, { memoryReference, data }]);
228
- addressesSubmitted.add(memoryReference);
229
- }
230
- }
231
- return edits;
232
- }
233
-
234
- protected doWriteMemory(writeMemoryArgs: DebugProtocol.WriteMemoryArguments): Promise<DebugProtocol.WriteMemoryResponse> {
235
- return this.memoryProvider.writeMemory(writeMemoryArgs);
236
- }
237
-
238
- protected showWriteError(location: string, error: string): void {
239
- if (this.currentErrorTimeout !== undefined) {
240
- clearTimeout(this.currentErrorTimeout);
241
- }
242
- this.writeErrorInfo = { location, error };
243
- this.update();
244
- this.currentErrorTimeout = window.setTimeout(() => this.hideWriteError(), Constants.ERROR_TIMEOUT);
245
- }
246
-
247
- protected hideWriteError(): void {
248
- this.currentErrorTimeout = undefined;
249
- this.writeErrorInfo = undefined;
250
- this.update();
251
- }
252
-
253
- protected override getWrapperHandlers(): MemoryTable.WrapperHandlers {
254
- return this.options.isFrozen
255
- ? super.getWrapperHandlers()
256
- : {
257
- onClick: this.handleTableClick,
258
- onContextMenu: this.handleTableRightClick,
259
- onKeyDown: this.handleTableInput,
260
- onMouseMove: this.handleTableMouseMove,
261
- };
262
- }
263
-
264
- protected handleTableClick = (event: React.MouseEvent): void => {
265
- const target = event.target as HTMLElement;
266
- if (target.classList?.contains('eight-bits')) {
267
- this.highlightedField = hexStrToUnsignedLong(target.getAttribute('data-id') ?? '-0x1');
268
- this.update();
269
- event.stopPropagation();
270
- }
271
- };
272
-
273
- protected override doHandleTableRightClick(event: React.MouseEvent): void {
274
- const target = event.target as HTMLElement;
275
- if (target.classList?.contains('eight-bits')) {
276
- this.highlightedField = hexStrToUnsignedLong(target.getAttribute('data-id') ?? '-0x1');
277
- }
278
- super.doHandleTableRightClick(event);
279
- }
280
-
281
- protected handleTableInput = (event: React.KeyboardEvent): void => {
282
- if (this.highlightedField.lessThan(0)) {
283
- return;
284
- }
285
- const keyCode = KeyCode.createKeyCode(event.nativeEvent).key?.keyCode;
286
- const initialHighlight = this.highlightedField;
287
- const initialHighlightIndex = initialHighlight.subtract(this.memory.address);
288
- if (keyCode === Key.TAB.keyCode) {
289
- return;
290
- }
291
- const arrayElementsPerRow = (this.options.byteSize / 8) * this.options.bytesPerGroup * this.options.groupsPerRow;
292
- const isAlreadyEdited = this.pendingMemoryEdits.has(this.highlightedField.toString());
293
- const oldValue = this.pendingMemoryEdits.get(initialHighlight.toString()) ??
294
- this.memory.bytes[initialHighlightIndex.toInt()].toString(16).padStart(2, '0');
295
- let possibleNewHighlight = new Long(-1);
296
- let newValue = oldValue;
297
- switch (keyCode) {
298
- case Key.ARROW_DOWN.keyCode:
299
- possibleNewHighlight = initialHighlight.add(arrayElementsPerRow);
300
- event.preventDefault();
301
- event.stopPropagation();
302
- break;
303
- case Key.ARROW_UP.keyCode:
304
- possibleNewHighlight = initialHighlight.greaterThan(arrayElementsPerRow) ? initialHighlight.subtract(arrayElementsPerRow) : possibleNewHighlight;
305
- event.preventDefault();
306
- event.stopPropagation();
307
- break;
308
- case Key.ARROW_RIGHT.keyCode:
309
- possibleNewHighlight = initialHighlight.add(1);
310
- event.preventDefault();
311
- event.stopPropagation();
312
- break;
313
- case Key.ARROW_LEFT.keyCode:
314
- possibleNewHighlight = initialHighlight.greaterThan(0) ? initialHighlight.subtract(1) : possibleNewHighlight;
315
- break;
316
- case Key.BACKSPACE.keyCode:
317
- newValue = oldValue.slice(0, oldValue.length - 1);
318
- break;
319
- case Key.DELETE.keyCode:
320
- newValue = '';
321
- break;
322
- case Key.ENTER.keyCode:
323
- this.submitMemoryEdits();
324
- break;
325
- case Key.ESCAPE.keyCode:
326
- if (isAlreadyEdited) {
327
- this.clearEdits(this.highlightedField);
328
- } else {
329
- this.clearEdits();
330
- }
331
- break;
332
- default: {
333
- const keyValue = parseInt(KeyCode.createKeyCode(event.nativeEvent).toString(), 16);
334
- if (!Number.isNaN(keyValue)) {
335
- newValue = isAlreadyEdited ? oldValue : '';
336
- if (newValue.length < 2) {
337
- newValue += keyValue.toString(16);
338
- }
339
- }
340
- }
341
- }
342
- if (this.isInBounds(possibleNewHighlight)) {
343
- this.highlightedField = possibleNewHighlight;
344
- }
345
- const valueWasChanged = newValue !== oldValue;
346
- if (valueWasChanged) {
347
- this.pendingMemoryEdits.set(this.highlightedField.toString(), newValue);
348
- }
349
- if (valueWasChanged || !this.highlightedField.equals(initialHighlight)) {
350
- this.update();
351
- }
352
- };
353
-
354
- protected isInBounds(candidateAddress: Long): boolean {
355
- const { address, bytes } = this.memory;
356
- return candidateAddress.greaterThanOrEqual(address) &&
357
- candidateAddress.lessThan(address.add(bytes.length));
358
- }
359
- }
1
+ /********************************************************************************
2
+ * Copyright (C) 2021 Ericsson and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import { Key, KeyCode } from '@theia/core/lib/browser';
18
+ import { Deferred } from '@theia/core/lib/common/promise-util';
19
+ import { injectable } from '@theia/core/shared/inversify';
20
+ import * as React from '@theia/core/shared/react';
21
+ import * as Long from 'long';
22
+ import { DebugProtocol } from '@vscode/debugprotocol';
23
+ import { hexStrToUnsignedLong } from '../../common/util';
24
+ import { MemoryOptionsWidget } from '../memory-widget/memory-options-widget';
25
+ import { MemoryTable, MemoryTableWidget } from '../memory-widget/memory-table-widget';
26
+ import { MemoryWidget } from '../memory-widget/memory-widget';
27
+ import { EasilyMappedObject } from '../utils/memory-hover-renderer';
28
+ import { Constants, Interfaces } from '../utils/memory-widget-utils';
29
+ import { nls } from '@theia/core/lib/common/nls';
30
+
31
+ export type EditableMemoryWidget = MemoryWidget<MemoryOptionsWidget, MemoryEditableTableWidget>;
32
+ export namespace EditableMemoryWidget {
33
+ export const ID = 'editable.memory.widget';
34
+ }
35
+
36
+ @injectable()
37
+ export class MemoryEditableTableWidget extends MemoryTableWidget {
38
+ protected pendingMemoryEdits = new Map<string, string>();
39
+ protected override previousBytes: Interfaces.LabeledUint8Array | undefined;
40
+ protected memoryEditsCompleted = new Deferred<void>();
41
+ protected highlightedField: Long = Long.fromInt(-1);
42
+ protected writeErrorInfo: { location: string, error: string } | undefined;
43
+ protected currentErrorTimeout: number | undefined;
44
+
45
+ protected doShowMoreMemoryBefore = false;
46
+ protected doShowMoreMemoryAfter = false;
47
+
48
+ protected override async doInit(): Promise<void> {
49
+ this.memoryEditsCompleted.resolve();
50
+ await super.doInit();
51
+ this.addClass('editable');
52
+ }
53
+
54
+ resetModifiedValue(valueAddress: Long): void {
55
+ const didChange = this.pendingMemoryEdits.delete(valueAddress.toString());
56
+ if (didChange) {
57
+ this.update();
58
+ }
59
+ }
60
+
61
+ protected override getState(): void {
62
+ super.getState();
63
+ if (!this.isInBounds(this.highlightedField)) {
64
+ this.highlightedField = this.memory.address;
65
+ }
66
+ }
67
+
68
+ protected override async handleMemoryChange(newMemory: Interfaces.MemoryReadResult): Promise<void> {
69
+ await this.memoryEditsCompleted.promise;
70
+ if (newMemory.bytes.length === 0) {
71
+ this.pendingMemoryEdits.clear();
72
+ }
73
+ super.handleMemoryChange(newMemory);
74
+ }
75
+
76
+ protected override areSameRegion(a: Interfaces.MemoryReadResult, b?: Interfaces.MemoryReadResult): boolean {
77
+ return b !== undefined && a.address.equals(b.address) && a.bytes.length === b.bytes.length;
78
+ }
79
+
80
+ protected override getTableFooter(): React.ReactNode {
81
+ const showButtons = !!this.pendingMemoryEdits.size && !this.writeErrorInfo;
82
+ return (
83
+ (showButtons || this.writeErrorInfo) && (
84
+ <div className='memory-edit-button-container'>
85
+ {showButtons && <button
86
+ className='theia-button secondary'
87
+ onClick={this.handleClearEditClick}
88
+ type='reset'
89
+ title={nls.localize('theia/memory-inspector/editable/clear', 'Clear Changes')}
90
+ >
91
+ {nls.localize('theia/memory-inspector/editable/clear', 'Clear Changes')}
92
+ </button>}
93
+ {showButtons && <button
94
+ className='theia-button main'
95
+ onClick={this.submitMemoryEdits}
96
+ type='submit'
97
+ title={nls.localize('theia/memory-inspector/editable/apply', 'Apply Changes')}
98
+ >
99
+ {nls.localize('theia/memory-inspector/editable/apply', 'Apply Changes')}
100
+ </button>}
101
+ {!!this.writeErrorInfo && <div className='memory-edit-error'>
102
+ <div className='memory-edit-error-location'>{`Error writing to 0x${Long.fromString(this.writeErrorInfo?.location).toString(16)}`}</div>
103
+ <div className='memory-edit-error-details'>{this.writeErrorInfo?.error}</div>
104
+ </div>}
105
+ </div>)
106
+ );
107
+ }
108
+
109
+ protected override getBitAttributes(arrayOffset: number, iteratee: Interfaces.LabeledUint8Array): Partial<Interfaces.FullNodeAttributes> {
110
+ const attributes = super.getBitAttributes(arrayOffset, iteratee);
111
+ const classNames = attributes.className?.split(' ') ?? [];
112
+ const itemID = this.memory.address.add(arrayOffset);
113
+ const isHighlight = itemID.equals(this.highlightedField);
114
+ const isEditPending = this.pendingMemoryEdits.has(itemID.toString());
115
+ const padder = isHighlight && isEditPending ? '\xa0' : '0'; // non-breaking space so it doesn't look like plain whitespace.
116
+ const stringValue = (this.pendingMemoryEdits.get(itemID.toString()) ?? this.memory.bytes[arrayOffset].toString(16)).padStart(2, padder);
117
+ if (!this.options.isFrozen) {
118
+ if (isHighlight) {
119
+ classNames.push('highlight');
120
+ }
121
+ if (isEditPending) {
122
+ classNames.push('modified');
123
+ }
124
+ }
125
+ return {
126
+ ...attributes,
127
+ className: classNames.join(' '),
128
+ content: stringValue
129
+ };
130
+ }
131
+
132
+ protected override getHoverForChunk(span: HTMLElement): EasilyMappedObject | undefined {
133
+ const addressAsString = span.getAttribute('data-id');
134
+ if (addressAsString) {
135
+ const address = hexStrToUnsignedLong(addressAsString);
136
+ const { value } = this.composeByte(address, true);
137
+ const { value: inMemory } = this.composeByte(address, false);
138
+ const oldValue = this.previousBytes && this.composeByte(address, false, this.previousBytes).value;
139
+ const decimal = parseInt(value, 16);
140
+ const octal = decimal.toString(8).padStart(this.options.byteSize / 8, '0');
141
+ const UTF8 = String.fromCharCode(decimal);
142
+ const binary = this.getPaddedBinary(decimal);
143
+ const toSend: EasilyMappedObject = { hex: value, octal, binary, decimal };
144
+ if (UTF8) {
145
+ toSend.UTF8 = UTF8;
146
+ }
147
+ if (inMemory !== value) {
148
+ toSend['Current Value'] = inMemory;
149
+ }
150
+ if (oldValue !== undefined && oldValue !== value) {
151
+ toSend['Previous Value'] = oldValue;
152
+ }
153
+ return toSend;
154
+ }
155
+ return undefined;
156
+ }
157
+
158
+ protected composeByte(
159
+ addressPlusArrayOffset: Long,
160
+ usePendingEdits: boolean,
161
+ dataSource: Uint8Array = this.memory.bytes,
162
+ ): Interfaces.ByteFromChunkData {
163
+ let value = '';
164
+
165
+ const offset = addressPlusArrayOffset.subtract(this.memory.address);
166
+ const chunksPerByte = this.options.byteSize / 8;
167
+ const startingChunkIndex = offset.subtract(offset.modulo(chunksPerByte));
168
+ const address = this.memory.address.add(startingChunkIndex.divide(chunksPerByte));
169
+ for (let i = 0; i < chunksPerByte; i += 1) {
170
+ const targetOffset = startingChunkIndex.add(i);
171
+ const targetChunk = this.getFromMapOrArray(targetOffset, usePendingEdits, dataSource);
172
+ value += targetChunk.padStart(2, '0');
173
+ }
174
+
175
+ return { address, value };
176
+ }
177
+
178
+ protected getFromMapOrArray(arrayOffset: Long, usePendingEdits: boolean, dataSource: Uint8Array = this.memory.bytes): string {
179
+ let value = usePendingEdits ? this.pendingMemoryEdits.get(arrayOffset.add(this.memory.address).toString()) : undefined;
180
+ if (value === undefined) {
181
+ value = dataSource[arrayOffset.toInt()]?.toString(16) ?? '';
182
+ }
183
+ return value;
184
+ }
185
+
186
+ protected handleClearEditClick = (): void => this.clearEdits();
187
+
188
+ protected clearEdits(address?: Long): void {
189
+ if (typeof address === 'number') {
190
+ this.pendingMemoryEdits.delete(address);
191
+ } else {
192
+ this.pendingMemoryEdits.clear();
193
+ }
194
+ this.update();
195
+ }
196
+
197
+ protected submitMemoryEdits = async (): Promise<void> => {
198
+ this.memoryEditsCompleted = new Deferred();
199
+ let didUpdateMemory = false;
200
+ for (const [key, edit] of this.createUniqueEdits()) {
201
+ try {
202
+ await this.doWriteMemory(edit);
203
+ didUpdateMemory = true;
204
+ this.pendingMemoryEdits.delete(key);
205
+ } catch (e) {
206
+ console.warn('Problem writing memory with arguments', edit, '\n', e);
207
+ const text = e instanceof Error ? e.message : 'Unknown error';
208
+ this.showWriteError(key, text);
209
+ break;
210
+ }
211
+ }
212
+ this.memoryEditsCompleted.resolve();
213
+ if (didUpdateMemory) {
214
+ this.optionsWidget.fetchNewMemory();
215
+ }
216
+ };
217
+
218
+ protected createUniqueEdits(): Array<[string, DebugProtocol.WriteMemoryArguments]> {
219
+ const addressesSubmitted = new Set<string>();
220
+ const edits: Array<[string, DebugProtocol.WriteMemoryArguments]> = [];
221
+ for (const k of this.pendingMemoryEdits.keys()) {
222
+ const address = Long.fromString(k);
223
+ const { address: addressToSend, value: valueToSend } = this.composeByte(address, true);
224
+ const memoryReference = '0x' + addressToSend.toString(16);
225
+ if (!addressesSubmitted.has(memoryReference)) {
226
+ const data = Buffer.from(valueToSend, 'hex').toString('base64');
227
+ edits.push([k, { memoryReference, data }]);
228
+ addressesSubmitted.add(memoryReference);
229
+ }
230
+ }
231
+ return edits;
232
+ }
233
+
234
+ protected doWriteMemory(writeMemoryArgs: DebugProtocol.WriteMemoryArguments): Promise<DebugProtocol.WriteMemoryResponse> {
235
+ return this.memoryProvider.writeMemory(writeMemoryArgs);
236
+ }
237
+
238
+ protected showWriteError(location: string, error: string): void {
239
+ if (this.currentErrorTimeout !== undefined) {
240
+ clearTimeout(this.currentErrorTimeout);
241
+ }
242
+ this.writeErrorInfo = { location, error };
243
+ this.update();
244
+ this.currentErrorTimeout = window.setTimeout(() => this.hideWriteError(), Constants.ERROR_TIMEOUT);
245
+ }
246
+
247
+ protected hideWriteError(): void {
248
+ this.currentErrorTimeout = undefined;
249
+ this.writeErrorInfo = undefined;
250
+ this.update();
251
+ }
252
+
253
+ protected override getWrapperHandlers(): MemoryTable.WrapperHandlers {
254
+ return this.options.isFrozen
255
+ ? super.getWrapperHandlers()
256
+ : {
257
+ onClick: this.handleTableClick,
258
+ onContextMenu: this.handleTableRightClick,
259
+ onKeyDown: this.handleTableInput,
260
+ onMouseMove: this.handleTableMouseMove,
261
+ };
262
+ }
263
+
264
+ protected handleTableClick = (event: React.MouseEvent): void => {
265
+ const target = event.target as HTMLElement;
266
+ if (target.classList?.contains('eight-bits')) {
267
+ this.highlightedField = hexStrToUnsignedLong(target.getAttribute('data-id') ?? '-0x1');
268
+ this.update();
269
+ event.stopPropagation();
270
+ }
271
+ };
272
+
273
+ protected override doHandleTableRightClick(event: React.MouseEvent): void {
274
+ const target = event.target as HTMLElement;
275
+ if (target.classList?.contains('eight-bits')) {
276
+ this.highlightedField = hexStrToUnsignedLong(target.getAttribute('data-id') ?? '-0x1');
277
+ }
278
+ super.doHandleTableRightClick(event);
279
+ }
280
+
281
+ protected handleTableInput = (event: React.KeyboardEvent): void => {
282
+ if (this.highlightedField.lessThan(0)) {
283
+ return;
284
+ }
285
+ const keyCode = KeyCode.createKeyCode(event.nativeEvent).key?.keyCode;
286
+ const initialHighlight = this.highlightedField;
287
+ const initialHighlightIndex = initialHighlight.subtract(this.memory.address);
288
+ if (keyCode === Key.TAB.keyCode) {
289
+ return;
290
+ }
291
+ const arrayElementsPerRow = (this.options.byteSize / 8) * this.options.bytesPerGroup * this.options.groupsPerRow;
292
+ const isAlreadyEdited = this.pendingMemoryEdits.has(this.highlightedField.toString());
293
+ const oldValue = this.pendingMemoryEdits.get(initialHighlight.toString()) ??
294
+ this.memory.bytes[initialHighlightIndex.toInt()].toString(16).padStart(2, '0');
295
+ let possibleNewHighlight = new Long(-1);
296
+ let newValue = oldValue;
297
+ switch (keyCode) {
298
+ case Key.ARROW_DOWN.keyCode:
299
+ possibleNewHighlight = initialHighlight.add(arrayElementsPerRow);
300
+ event.preventDefault();
301
+ event.stopPropagation();
302
+ break;
303
+ case Key.ARROW_UP.keyCode:
304
+ possibleNewHighlight = initialHighlight.greaterThan(arrayElementsPerRow) ? initialHighlight.subtract(arrayElementsPerRow) : possibleNewHighlight;
305
+ event.preventDefault();
306
+ event.stopPropagation();
307
+ break;
308
+ case Key.ARROW_RIGHT.keyCode:
309
+ possibleNewHighlight = initialHighlight.add(1);
310
+ event.preventDefault();
311
+ event.stopPropagation();
312
+ break;
313
+ case Key.ARROW_LEFT.keyCode:
314
+ possibleNewHighlight = initialHighlight.greaterThan(0) ? initialHighlight.subtract(1) : possibleNewHighlight;
315
+ break;
316
+ case Key.BACKSPACE.keyCode:
317
+ newValue = oldValue.slice(0, oldValue.length - 1);
318
+ break;
319
+ case Key.DELETE.keyCode:
320
+ newValue = '';
321
+ break;
322
+ case Key.ENTER.keyCode:
323
+ this.submitMemoryEdits();
324
+ break;
325
+ case Key.ESCAPE.keyCode:
326
+ if (isAlreadyEdited) {
327
+ this.clearEdits(this.highlightedField);
328
+ } else {
329
+ this.clearEdits();
330
+ }
331
+ break;
332
+ default: {
333
+ const keyValue = parseInt(KeyCode.createKeyCode(event.nativeEvent).toString(), 16);
334
+ if (!Number.isNaN(keyValue)) {
335
+ newValue = isAlreadyEdited ? oldValue : '';
336
+ if (newValue.length < 2) {
337
+ newValue += keyValue.toString(16);
338
+ }
339
+ }
340
+ }
341
+ }
342
+ if (this.isInBounds(possibleNewHighlight)) {
343
+ this.highlightedField = possibleNewHighlight;
344
+ }
345
+ const valueWasChanged = newValue !== oldValue;
346
+ if (valueWasChanged) {
347
+ this.pendingMemoryEdits.set(this.highlightedField.toString(), newValue);
348
+ }
349
+ if (valueWasChanged || !this.highlightedField.equals(initialHighlight)) {
350
+ this.update();
351
+ }
352
+ };
353
+
354
+ protected isInBounds(candidateAddress: Long): boolean {
355
+ const { address, bytes } = this.memory;
356
+ return candidateAddress.greaterThanOrEqual(address) &&
357
+ candidateAddress.lessThan(address.add(bytes.length));
358
+ }
359
+ }