@theia/memory-inspector 1.34.3 → 1.34.4

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