Skip to content

Commit b369675

Browse files
author
Your Name
committed
Refactor command parameter handling
1 parent 3ffe6bd commit b369675

31 files changed

+964
-1179
lines changed

DEVELOPMENT.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,26 @@ export class Var {
109109
Similarly, the `run` method of each commandlet is expected to return a `Var` type which can subsequently be referenced by the user using the `ret` keyword in the execution of the next command.
110110

111111
# Parsing
112-
Below is snippet of the `dump` commandlet showing how it parses it's two arguments. The first is the address which to dump and the second is the length. Note that the first argument is also used as the return value for this commandlet. Note also that the second argument is also converted from a `UInt64` to a `number` type, since this is what is reqired by the `hexdump` function called by `this.dump`. This is used in many commands where the parameter is likely to be a small number and therefore loss of precision is not a concern.
112+
Below is snippet of the `dump` commandlet showing how it parses it's three arguments. The first is the address which to dump and the second is the length, and the fourth the width. Note that the first argument is also used as the return value for this commandlet. Note also that the second argument is also converted from a `UInt64` to a `number` type, since this is what is reqired by the `hexdump` function called by `this.dump`. This is used in many commands where the parameter is likely to be a small number and therefore loss of precision is not a concern.
113113
```js
114-
private runWithLength(tokens: Token[]): Var | null {
115-
if (tokens.length !== 2) return null;
116-
117-
const [a0, a1] = tokens;
118-
const [t0, t1] = [a0 as Token, a1 as Token];
119-
const [v0, v1] = [t0.toVar(), t1.toVar()];
120-
121-
if (v0 === null) return null;
122-
if (v1 === null) return null;
114+
public runSync(tokens: Token[]): Var {
115+
const vars = this.transformOptional(
116+
tokens,
117+
[this.parseVar],
118+
[this.parseVar, this.parseWidth],
119+
);
120+
if (vars === null) return this.usage();
121+
const [[v0], [v1, v2]] = vars as [[Var], [Var | null, number | null]];
123122

124123
const address = v0.toPointer();
125-
const length = v1.toU64().toNumber();
126-
this.dump(address, length);
124+
const count = v1 === null ? DEFAULT_COUNT : v1.toU64().toNumber();
125+
const width = v2 === null ? 1 : v2;
126+
this.dump(address, count, width);
127127
return v0;
128128
}
129129

130-
public run(tokens: Token[]): Var {
131-
const retWithLength = this.runWithLength(tokens);
132-
if (retWithLength !== null) return retWithLength;
133-
134-
...
135-
136-
return this.usage();
130+
private dump(address: NativePointer, count: number, width: number = 1) {
131+
...
137132
}
138133
```
139134
# Scripts

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frida-cshell",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Frida's CShell",
55
"scripts": {
66
"prepare": "npm run build && npm run version && npm run package && npm run copy",

src/breakpoints/bp.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class Bp {
3838

3939
private _hits: number;
4040
private _addr: Var | null;
41-
private _literal: string | null;
4241
private _length: number;
4342
private _depth: number;
4443

@@ -52,15 +51,13 @@ export class Bp {
5251
idx: number,
5352
hits: number,
5453
addr: Var | null,
55-
literal: string | null,
5654
length: number = 0,
5755
depth: number = 0,
5856
) {
5957
this._type = type;
6058
this._idx = idx;
6159
this._hits = hits;
6260
this._addr = addr;
63-
this._literal = literal;
6461
this._length = length;
6562
this._depth = depth;
6663
this._listener = null;
@@ -196,7 +193,7 @@ export class Bp {
196193
Output.write(`${Output.yellow('|')} Start Trace `);
197194
Output.write(`${Output.green(`#${this._idx}`)} `);
198195
Output.write(`[${this._type}] `);
199-
Output.write(`${Output.yellow(this._literal ?? '')} `);
196+
Output.write(`${Output.yellow(this.literal)} `);
200197
Output.write(`@ $pc=${Output.blue(Format.toHexString(ctx.pc))} `);
201198
Output.write(`$tid=${threadId}, depth=${this._depth}`);
202199
Output.writeln();
@@ -218,7 +215,7 @@ export class Bp {
218215
Output.write(`${Output.yellow('|')} Stop Trace `);
219216
Output.write(`${Output.green(`#${this._idx}`)} `);
220217
Output.write(`[${this._type}] `);
221-
Output.write(`${Output.yellow(this._literal ?? '')} `);
218+
Output.write(`${Output.yellow(this.literal)} `);
222219
Output.write(`@ $pc=${Output.blue(Format.toHexString(ctx.pc))} `);
223220
Output.write(`$tid=${threadId}`);
224221
Output.writeln();
@@ -244,7 +241,7 @@ export class Bp {
244241
Output.write(`${Output.yellow('|')} Break `);
245242
Output.write(`${Output.green(`#${this._idx}`)} `);
246243
Output.write(`[${this._type}] `);
247-
Output.write(`${Output.yellow(this._literal ?? '')} `);
244+
Output.write(`${Output.yellow(this.literal)} `);
248245
Output.write(`@ $pc=${Output.blue(Format.toHexString(ctx.pc))} `);
249246
Output.write(`$tid=${threadId}`);
250247
Output.writeln();
@@ -330,7 +327,7 @@ export class Bp {
330327
Output.write(`${Output.yellow('|')} Break `);
331328
Output.write(`${Output.green(`#${this._idx}`)} `);
332329
Output.write(`[${this._type}] `);
333-
Output.write(`${Output.yellow(this._literal ?? '')} `);
330+
Output.write(`${Output.yellow(this.literal)} `);
334331
Output.write(`@ $pc=${Output.blue(Format.toHexString(details.from))} `);
335332
Output.write(`$addr=${Output.blue(Format.toHexString(details.address))}`);
336333
Output.writeln();
@@ -364,7 +361,7 @@ export class Bp {
364361
public toString(short: boolean = false): string {
365362
const idxString = Output.green(`#${this._idx.toString()}.`.padEnd(4, ' '));
366363
const typeString = `[${this._type.toString()}]`;
367-
const literalString = Output.yellow(this._literal ?? '');
364+
const literalString = Output.yellow(this.literal);
368365
const addString = `@ $pc=${Output.blue(this.addrString)}`;
369366
const hitsString = `[hits:${this.hitsString}]`;
370367
const lengthString = this.lengthString;
@@ -416,6 +413,10 @@ export class Bp {
416413
return this._addr;
417414
}
418415

416+
public get literal(): string {
417+
return this._addr?.getLiteral() ?? '';
418+
}
419+
419420
public get length(): number | null {
420421
return this._length;
421422
}
@@ -433,11 +434,6 @@ export class Bp {
433434
this._addr = addr;
434435
}
435436

436-
public set literal(literal: string | null) {
437-
if (literal === null) return;
438-
this._literal = literal;
439-
}
440-
441437
public set length(length: number | null) {
442438
if (length === null) return;
443439
this._length = length;

src/breakpoints/bps.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export class Bps {
1212
type: BpType,
1313
hits: number = -1,
1414
addr: Var | null = null,
15-
literal: string | null = null,
1615
length: number = 0,
1716
depth: number = 0,
1817
): Bp {
@@ -23,7 +22,7 @@ export class Bps {
2322
this.checkOverlaps(type, addr.toPointer(), length);
2423
}
2524

26-
const bp = new Bp(type, idx, hits, addr, literal, length, depth);
25+
const bp = new Bp(type, idx, hits, addr, length, depth);
2726
this.byIndex.set(key, bp);
2827
this.last = bp;
2928
this.lines = [];
@@ -70,7 +69,6 @@ export class Bps {
7069
idx: number,
7170
hits: number,
7271
addr: Var | null = null,
73-
literal: string | null = null,
7472
length: number = 0,
7573
depth: number = 0,
7674
): Bp {
@@ -88,7 +86,6 @@ export class Bps {
8886
bp.disable();
8987
bp.hits = hits;
9088
bp.address = addr;
91-
bp.literal = literal;
9289
bp.length = length;
9390
bp.depth = depth;
9491
this.last = bp;

0 commit comments

Comments
 (0)