Skip to content

Commit

Permalink
Added errno cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 15, 2024
1 parent 42c07a0 commit eaae28f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 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.7.4",
"version": "1.7.5",
"description": "Frida's CShell",
"scripts": {
"prepare": "npm run build && npm run version && npm run package && npm run copy",
Expand Down
2 changes: 2 additions & 0 deletions src/cmdlets/development/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { MacroCmdLet } from '../misc/macro.js';
import { ReplaceCmdLet } from '../breakpoints/replace.js';
import { EchoCmdLet } from '../misc/echo.js';
import { CorpseCmdLet } from '../misc/corpse/corpse.js';
import { ErrnoCmdLet } from '../misc/errno.js';

export class JsCmdLet extends CmdLetBase {
name = 'js';
Expand Down Expand Up @@ -126,6 +127,7 @@ js path - load commandlet JS script
DumpCmdLet: DumpCmdLet,
EchoCmdLet: EchoCmdLet,
EqCmdLet: EqCmdLet,
ErrnoCmdLet: ErrnoCmdLet,
ExitCmdLet: ExitCmdLet,
FalseCmdLet: FalseCmdLet,
FdCmdLet: FdCmdLet,
Expand Down
4 changes: 3 additions & 1 deletion src/cmdlets/misc/corpse/core_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class CorePattern {
public static get(): string {
const value = File.readAllText(CorePattern.CORE_PATTERN).trimEnd();
if (value.startsWith('|'))
throw new Error(`core pattern must not start with '|' - value: '${value}'`);
throw new Error(
`core pattern must not start with '|' - value: '${value}'`,
);

if (value.indexOf('%') !== -1)
throw new Error(`core pattern must not contain '%' - value: '${value}'`);
Expand Down
88 changes: 88 additions & 0 deletions src/cmdlets/misc/errno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { CmdLetBase } from '../../commands/cmdlet.js';
import { Output } from '../../io/output.js';
import { Token } from '../../io/token.js';
import { Var } from '../../vars/var.js';

export class ErrnoCmdLet extends CmdLetBase {
name = 'errno';
category = 'misc';
help = 'displays the errno value';
private static readonly USAGE: string = `Usage: errno
errno - display the errno value`;

private fnErrnoLocation: SystemFunction<NativePointer, []> | null = null;

public runSync(tokens: Token[]): Var {
const retGetErrno = this.getErrno(tokens);
if (retGetErrno !== null) return retGetErrno;

const retSetErrno = this.setErrno(tokens);
if (retSetErrno !== null) return retSetErrno;

return this.usage();
}

private setErrno(tokens: Token[]): Var | null {
const vars = this.transform(tokens, [this.parseVar]);
if (vars === null) return null;
const [value] = vars as [Var];

const errno = value.toU64().toNumber();
const location = this.getErrnoLocation();
location.writeInt(errno);
return value;
}

private getErrno(tokens: Token[]): Var | null {
if (tokens.length !== 0) return null;
const location = this.getErrnoLocation();

const errno = location.readInt();
Output.writeln(`errno: ${errno}`);

return new Var(uint64(errno), 'errno');
}

private getErrnoLocation(): NativePointer {
const fnErrnoLocation = this.fnErrnoLocation as SystemFunction<
NativePointer,
[]
>;
const location =
fnErrnoLocation() as UnixSystemFunctionResult<NativePointer>;
if (location.value.equals(ptr(0)))
throw new Error('failed to get __errno_location()');
return location.value;
}

public usage(): Var {
Output.writeln(ErrnoCmdLet.USAGE);
return Var.ZERO;
}

public override isSupported(): boolean {
switch (Process.platform) {
case 'linux': {
const pErrnoLocation = Module.findExportByName(
null,
'__errno_location',
);
if (pErrnoLocation === null) return false;
this.fnErrnoLocation = new SystemFunction(
pErrnoLocation,
'pointer',
[],
);
return true;
}
case 'darwin':
case 'freebsd':
case 'qnx':
case 'windows':
case 'barebone':
default:
return false;
}
}
}
2 changes: 2 additions & 0 deletions src/commands/cmdlets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { MacroCmdLet } from '../cmdlets/misc/macro.js';
import { ReplaceCmdLet } from '../cmdlets/breakpoints/replace.js';
import { EchoCmdLet } from '../cmdlets/misc/echo.js';
import { CorpseCmdLet } from '../cmdlets/misc/corpse/corpse.js';
import { ErrnoCmdLet } from '../cmdlets/misc/errno.js';

export class CmdLets {
private static byName: Map<string, CmdLet> = new Map<string, CmdLet>();
Expand All @@ -91,6 +92,7 @@ export class CmdLets {
this.registerCmdletType(EchoCmdLet);
this.registerCmdletType(EndianCmdLet);
this.registerCmdletType(EqCmdLet);
this.registerCmdletType(ErrnoCmdLet);
this.registerCmdletType(ExitCmdLet);
this.registerCmdletType(FalseCmdLet);
this.registerCmdletType(FdCmdLet);
Expand Down

0 comments on commit eaae28f

Please sign in to comment.