Skip to content

Commit

Permalink
Minor trace improvements and remove all global constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 20, 2024
1 parent ea0ecf2 commit a1c8472
Show file tree
Hide file tree
Showing 35 changed files with 430 additions and 378 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frida-cshell",
"version": "1.5.0",
"version": "1.5.1",
"description": "Frida's CShell",
"scripts": {
"prepare": "npm run build && npm run version && npm run package && npm run copy",
Expand Down
14 changes: 8 additions & 6 deletions src/breakpoints/bp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { Vars } from '../vars/vars.js';
import { CallTrace } from '../traces/call.js';
import { CoverageTrace } from '../traces/coverage/trace.js';

export const BP_LENGTH: number = 16;

export enum BpKind {
Code = 'code',
Memory = 'memory',
Expand All @@ -33,6 +31,7 @@ export enum BpType {
}

export class Bp {
public static readonly BP_LENGTH: number = 16;
private readonly _type: BpType;
private readonly _idx: number;

Expand Down Expand Up @@ -97,7 +96,7 @@ export class Bp {
private enableCode() {
if (this._addr === null) return;
if (this._listener !== null) return;
this._overlay = Overlay.add(this._addr.toPointer(), BP_LENGTH);
this._overlay = Overlay.add(this._addr.toPointer(), Bp.BP_LENGTH);
const addr = this._addr;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const bp = this;
Expand Down Expand Up @@ -243,9 +242,12 @@ export class Bp {
Output.setIndent(true);
Output.writeln();
try {
trace.lines().forEach(l => {
Output.writeln(l);
});
trace
.lines()
.slice(0, TraceData.MAX_LINES)
.forEach(l => {
Output.writeln(l);
});
Output.writeln();
} finally {
Output.setIndent(false);
Expand Down
66 changes: 36 additions & 30 deletions src/breakpoints/regs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Var } from '../vars/var.js';

const THREAD_ID_NAME: string = 'tid';
const RETURN_ADDRESS_NAME: string = 'ra';
const ADDR_NAME: string = 'addr';
const PC_NAME: string = 'pc';
const RETVAL_NAME: string = 'ret';

export class Regs {
private static readonly THREAD_ID_NAME: string = 'tid';
private static readonly RETURN_ADDRESS_NAME: string = 'ra';
private static readonly ADDR_NAME: string = 'addr';
private static readonly PC_NAME: string = 'pc';
private static readonly RETVAL_NAME: string = 'ret';

private static threadId: ThreadId | null = null;
private static ctx: CpuContext | null = null;
private static returnAddress: NativePointer | null = null;
Expand All @@ -17,33 +17,33 @@ export class Regs {
private constructor() {}

public static get(name: string): Var {
if (name === THREAD_ID_NAME) {
if (name === Regs.THREAD_ID_NAME) {
if (this.threadId === null)
throw new Error('thread ID not available outside of a breakpoint');
return new Var(uint64(this.threadId), THREAD_ID_NAME);
} else if (name === RETURN_ADDRESS_NAME) {
return new Var(uint64(this.threadId), Regs.THREAD_ID_NAME);
} else if (name === Regs.RETURN_ADDRESS_NAME) {
if (this.returnAddress === null)
throw new Error('return address not available outside of a breakpoint');
return new Var(
uint64(this.returnAddress.toString()),
RETURN_ADDRESS_NAME,
Regs.RETURN_ADDRESS_NAME,
);
} else if (name === ADDR_NAME) {
} else if (name === Regs.ADDR_NAME) {
if (this.addr === null)
throw new Error('addr not available outside of a breakpoint');
return new Var(uint64(this.addr.toString()), ADDR_NAME);
} else if (name === RETVAL_NAME) {
return new Var(uint64(this.addr.toString()), Regs.ADDR_NAME);
} else if (name === Regs.RETVAL_NAME) {
if (this.retVal === null)
throw new Error(
'return Value not available outside of a function exit breakpoint',
);
return new Var(uint64(this.retVal.toString()), RETVAL_NAME);
return new Var(uint64(this.retVal.toString()), Regs.RETVAL_NAME);
} else if (this.ctx === null) {
if (name === PC_NAME) {
if (name === Regs.PC_NAME) {
if (this.pc === null) {
throw new Error('pc not available outside of a breakpoint');
}
return new Var(uint64(this.pc.toString()), PC_NAME);
return new Var(uint64(this.pc.toString()), Regs.PC_NAME);
} else {
throw new Error('registers not available outside of a breakpoint');
}
Expand Down Expand Up @@ -163,21 +163,21 @@ export class Regs {
}

public static set(name: string, value: Var) {
if (name === THREAD_ID_NAME) {
if (name === Regs.THREAD_ID_NAME) {
throw new Error('thread ID cannot be set');
} else if (name === RETURN_ADDRESS_NAME) {
} else if (name === Regs.RETURN_ADDRESS_NAME) {
throw new Error('return address cannot be set');
} else if (name === ADDR_NAME) {
} else if (name === Regs.ADDR_NAME) {
throw new Error('addr cannot be set');
} else if (name === RETVAL_NAME) {
} else if (name === Regs.RETVAL_NAME) {
if (this.retVal === null)
throw new Error(
'return Value not available outside of a function exit breakpoint',
);
const ptr = value.toPointer();
this.retVal.replace(ptr);
} else if (this.ctx === null) {
if (name === PC_NAME) {
if (name === Regs.PC_NAME) {
throw new Error('pc cannot be set');
} else {
throw new Error('registers not available outside of a breakpoint');
Expand Down Expand Up @@ -300,7 +300,10 @@ export class Regs {

if (this.ctx === null) {
if (this.pc !== null) {
result.push([PC_NAME, new Var(uint64(this.pc.toString()), PC_NAME)]);
result.push([
Regs.PC_NAME,
new Var(uint64(this.pc.toString()), Regs.PC_NAME),
]);
}
} else {
const regs = Array.from(this.getRegs(this.ctx).entries());
Expand All @@ -309,29 +312,32 @@ export class Regs {

if (this.threadId !== null) {
result.push([
THREAD_ID_NAME,
new Var(uint64(this.threadId), THREAD_ID_NAME),
Regs.THREAD_ID_NAME,
new Var(uint64(this.threadId), Regs.THREAD_ID_NAME),
]);
}

if (this.returnAddress !== null) {
result.push([
RETURN_ADDRESS_NAME,
new Var(uint64(this.returnAddress.toString()), RETURN_ADDRESS_NAME),
Regs.RETURN_ADDRESS_NAME,
new Var(
uint64(this.returnAddress.toString()),
Regs.RETURN_ADDRESS_NAME,
),
]);
}

if (this.addr !== null) {
result.push([
ADDR_NAME,
new Var(uint64(this.addr.toString()), ADDR_NAME),
Regs.ADDR_NAME,
new Var(uint64(this.addr.toString()), Regs.ADDR_NAME),
]);
}

if (this.retVal !== null) {
result.push([
RETVAL_NAME,
new Var(uint64(this.retVal.toString()), RETVAL_NAME),
Regs.RETVAL_NAME,
new Var(uint64(this.retVal.toString()), Regs.RETVAL_NAME),
]);
}

Expand Down
46 changes: 23 additions & 23 deletions src/cmdlets/breakpoints/bp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BP_LENGTH, BpType } from '../../breakpoints/bp.js';
import { Bp, BpType } from '../../breakpoints/bp.js';
import { Bps } from '../../breakpoints/bps.js';
import { CmdLet } from '../../commands/cmdlet.js';
import { Input, InputInterceptLine } from '../../io/input.js';
Expand Down Expand Up @@ -66,7 +66,7 @@ abstract class TypedBpCmdLet extends CmdLet implements InputInterceptLine {
public usage(): Var {
const create = this.usageCreate();
const modify = this.usageModify();
const USAGE: string = `Usage: ${this.name}
const usage: string = `Usage: ${this.name}
${Output.bold('show:')}
${this.name} - show all ${this.bpType} breakpoints
Expand All @@ -87,7 +87,7 @@ ${this.name} ${CmdLet.NUM_CHAR}n # - delete a ${this.bpType} breakpoint
${Output.bold('NOTE:')} Set hits to '*' for unlimited breakpoint.`;

Output.writeln(USAGE);
Output.writeln(usage);
return Var.ZERO;
}

Expand Down Expand Up @@ -125,10 +125,10 @@ abstract class CodeBpCmdLet
const bp = Bps.create(this.bpType, 0, null, 0);
Output.writeln(`Created ${bp.toString()}`);
} else if (hits === null) {
const bp = Bps.create(this.bpType, -1, addr, BP_LENGTH);
const bp = Bps.create(this.bpType, -1, addr, Bp.BP_LENGTH);
Output.writeln(`Created ${bp.toString()}`);
} else {
const bp = Bps.create(this.bpType, hits, addr, BP_LENGTH);
const bp = Bps.create(this.bpType, hits, addr, Bp.BP_LENGTH);
Output.writeln(`Created ${bp.toString()}`);
}

Expand All @@ -149,10 +149,10 @@ abstract class CodeBpCmdLet
const bp = Bps.modify(this.bpType, index, 0, null, 0);
Output.writeln(`Modified ${bp.toString()}`);
} else if (hits === null) {
const bp = Bps.modify(this.bpType, index, -1, addr, BP_LENGTH);
const bp = Bps.modify(this.bpType, index, -1, addr, Bp.BP_LENGTH);
Output.writeln(`Modified ${bp.toString()}`);
} else {
const bp = Bps.modify(this.bpType, index, hits, addr, BP_LENGTH);
const bp = Bps.modify(this.bpType, index, hits, addr, Bp.BP_LENGTH);
Output.writeln(`Modified ${bp.toString()}`);
}

Expand All @@ -165,7 +165,7 @@ abstract class CodeBpCmdLet
}

protected override usageCreate(): string {
const USAGE: string = `
const usage: string = `
${this.name} 0 - create ${this.bpType} breakpoint without assigning an address
${this.name} addr - create ${this.bpType} breakpoint without a hit limit
Expand All @@ -175,11 +175,11 @@ ${this.name} addr hits - create ${this.bpType} breakpoint
addr the address to create the breakpoint
hits the number of times the breakpoint should fire`;

return USAGE;
return usage;
}

protected override usageModify(): string {
const USAGE: string = `
const usage: string = `
${this.name} ${CmdLet.NUM_CHAR}n addr - modify a ${this.bpType} breakpoint without a hit limit
${CmdLet.NUM_CHAR}n the number of the breakpoint to modify
addr the address to move the breakpoint
Expand All @@ -188,7 +188,7 @@ ${this.name} ${CmdLet.NUM_CHAR}n addr hits - modify a ${this.bpType} breakpoint
${CmdLet.NUM_CHAR}n the number of the breakpoint to modify
addr the address to move the breakpoint
hits the number of times the breakpoint should fire`;
return USAGE;
return usage;
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ abstract class MemoryBpCmdLet
}

protected override usageCreate(): string {
const USAGE: string = `
const usage: string = `
${this.name} 0 0 - create ${this.bpType} breakpoint without assigning an address
${this.name} addr len - create ${this.bpType} breakpoint without a hit limit
Expand All @@ -274,11 +274,11 @@ ${this.name} addr len hits - create ${this.bpType} breakpoint without a hit limi
addr the address to create the breakpoint
len the length of the memory region to watch
hits the number of times the breakpoint should fire`;
return USAGE;
return usage;
}

protected override usageModify(): string {
const USAGE: string = `
const usage: string = `
${this.name} ${CmdLet.NUM_CHAR}n addr len - modify a ${this.bpType} breakpoint without a hit limit
${CmdLet.NUM_CHAR}n the number of the breakpoint to modify
addr the address to move the breakpoint
Expand All @@ -289,7 +289,7 @@ ${this.name} ${CmdLet.NUM_CHAR}n addr len hits - modify a ${this.bpType} breakpo
addr the address to move the breakpoint
len the length of the memory region to watch
hits the number of times the breakpoint should fire`;
return USAGE;
return usage;
}
}

Expand All @@ -313,7 +313,7 @@ abstract class TraceBpCmdLet
this.bpType,
-1,
addr,
BP_LENGTH,
Bp.BP_LENGTH,
depth.toU64().toNumber(),
);
Output.writeln(`Created ${bp.toString()}`);
Expand All @@ -322,7 +322,7 @@ abstract class TraceBpCmdLet
this.bpType,
hits,
addr,
BP_LENGTH,
Bp.BP_LENGTH,
depth.toU64().toNumber(),
);
Output.writeln(`Created ${bp.toString()}`);
Expand Down Expand Up @@ -352,7 +352,7 @@ abstract class TraceBpCmdLet
index,
-1,
addr,
BP_LENGTH,
Bp.BP_LENGTH,
depth.toU64().toNumber(),
);

Expand All @@ -363,7 +363,7 @@ abstract class TraceBpCmdLet
index,
hits,
addr,
BP_LENGTH,
Bp.BP_LENGTH,
depth.toU64().toNumber(),
);

Expand All @@ -375,7 +375,7 @@ abstract class TraceBpCmdLet
}

protected override usageCreate(): string {
const USAGE: string = `
const usage: string = `
${this.name} addr depth - create ${this.bpType} breakpoint without a hit limit
addr the address to create the breakpoint
depth the maximum depth of callstack to follow
Expand All @@ -384,11 +384,11 @@ ${this.name} addr depth hits - create ${this.bpType} breakpoint
hits the number of times the breakpoint should fire
addr the address to create the breakpoint
depth the maximum depth of callstack to follow`;
return USAGE;
return usage;
}

protected override usageModify(): string {
const USAGE: string = `
const usage: string = `
${this.name} ${CmdLet.NUM_CHAR}n addr depth - modify a ${this.bpType} breakpoint without a hit limit
${CmdLet.NUM_CHAR}n the number of the breakpoint to modify
addr the address to move the breakpoint
Expand All @@ -399,7 +399,7 @@ ${this.name} ${CmdLet.NUM_CHAR}n addr depth hits - modify a ${this.bpType} break
addr the address to move the breakpoint
depth the maximum depth of callstack to follow
hits the number of times the breakpoint should fire`;
return USAGE;
return usage;
}
}

Expand Down
Loading

0 comments on commit a1c8472

Please sign in to comment.