Skip to content

Commit eaae28f

Browse files
author
Your Name
committed
Added errno cmdlet
1 parent 42c07a0 commit eaae28f

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed

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.7.4",
3+
"version": "1.7.5",
44
"description": "Frida's CShell",
55
"scripts": {
66
"prepare": "npm run build && npm run version && npm run package && npm run copy",

src/cmdlets/development/js.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { MacroCmdLet } from '../misc/macro.js';
8282
import { ReplaceCmdLet } from '../breakpoints/replace.js';
8383
import { EchoCmdLet } from '../misc/echo.js';
8484
import { CorpseCmdLet } from '../misc/corpse/corpse.js';
85+
import { ErrnoCmdLet } from '../misc/errno.js';
8586

8687
export class JsCmdLet extends CmdLetBase {
8788
name = 'js';
@@ -126,6 +127,7 @@ js path - load commandlet JS script
126127
DumpCmdLet: DumpCmdLet,
127128
EchoCmdLet: EchoCmdLet,
128129
EqCmdLet: EqCmdLet,
130+
ErrnoCmdLet: ErrnoCmdLet,
129131
ExitCmdLet: ExitCmdLet,
130132
FalseCmdLet: FalseCmdLet,
131133
FdCmdLet: FdCmdLet,

src/cmdlets/misc/corpse/core_pattern.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export class CorePattern {
99
public static get(): string {
1010
const value = File.readAllText(CorePattern.CORE_PATTERN).trimEnd();
1111
if (value.startsWith('|'))
12-
throw new Error(`core pattern must not start with '|' - value: '${value}'`);
12+
throw new Error(
13+
`core pattern must not start with '|' - value: '${value}'`,
14+
);
1315

1416
if (value.indexOf('%') !== -1)
1517
throw new Error(`core pattern must not contain '%' - value: '${value}'`);

src/cmdlets/misc/errno.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { CmdLetBase } from '../../commands/cmdlet.js';
2+
import { Output } from '../../io/output.js';
3+
import { Token } from '../../io/token.js';
4+
import { Var } from '../../vars/var.js';
5+
6+
export class ErrnoCmdLet extends CmdLetBase {
7+
name = 'errno';
8+
category = 'misc';
9+
help = 'displays the errno value';
10+
private static readonly USAGE: string = `Usage: errno
11+
12+
errno - display the errno value`;
13+
14+
private fnErrnoLocation: SystemFunction<NativePointer, []> | null = null;
15+
16+
public runSync(tokens: Token[]): Var {
17+
const retGetErrno = this.getErrno(tokens);
18+
if (retGetErrno !== null) return retGetErrno;
19+
20+
const retSetErrno = this.setErrno(tokens);
21+
if (retSetErrno !== null) return retSetErrno;
22+
23+
return this.usage();
24+
}
25+
26+
private setErrno(tokens: Token[]): Var | null {
27+
const vars = this.transform(tokens, [this.parseVar]);
28+
if (vars === null) return null;
29+
const [value] = vars as [Var];
30+
31+
const errno = value.toU64().toNumber();
32+
const location = this.getErrnoLocation();
33+
location.writeInt(errno);
34+
return value;
35+
}
36+
37+
private getErrno(tokens: Token[]): Var | null {
38+
if (tokens.length !== 0) return null;
39+
const location = this.getErrnoLocation();
40+
41+
const errno = location.readInt();
42+
Output.writeln(`errno: ${errno}`);
43+
44+
return new Var(uint64(errno), 'errno');
45+
}
46+
47+
private getErrnoLocation(): NativePointer {
48+
const fnErrnoLocation = this.fnErrnoLocation as SystemFunction<
49+
NativePointer,
50+
[]
51+
>;
52+
const location =
53+
fnErrnoLocation() as UnixSystemFunctionResult<NativePointer>;
54+
if (location.value.equals(ptr(0)))
55+
throw new Error('failed to get __errno_location()');
56+
return location.value;
57+
}
58+
59+
public usage(): Var {
60+
Output.writeln(ErrnoCmdLet.USAGE);
61+
return Var.ZERO;
62+
}
63+
64+
public override isSupported(): boolean {
65+
switch (Process.platform) {
66+
case 'linux': {
67+
const pErrnoLocation = Module.findExportByName(
68+
null,
69+
'__errno_location',
70+
);
71+
if (pErrnoLocation === null) return false;
72+
this.fnErrnoLocation = new SystemFunction(
73+
pErrnoLocation,
74+
'pointer',
75+
[],
76+
);
77+
return true;
78+
}
79+
case 'darwin':
80+
case 'freebsd':
81+
case 'qnx':
82+
case 'windows':
83+
case 'barebone':
84+
default:
85+
return false;
86+
}
87+
}
88+
}

src/commands/cmdlets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { MacroCmdLet } from '../cmdlets/misc/macro.js';
6969
import { ReplaceCmdLet } from '../cmdlets/breakpoints/replace.js';
7070
import { EchoCmdLet } from '../cmdlets/misc/echo.js';
7171
import { CorpseCmdLet } from '../cmdlets/misc/corpse/corpse.js';
72+
import { ErrnoCmdLet } from '../cmdlets/misc/errno.js';
7273

7374
export class CmdLets {
7475
private static byName: Map<string, CmdLet> = new Map<string, CmdLet>();
@@ -91,6 +92,7 @@ export class CmdLets {
9192
this.registerCmdletType(EchoCmdLet);
9293
this.registerCmdletType(EndianCmdLet);
9394
this.registerCmdletType(EqCmdLet);
95+
this.registerCmdletType(ErrnoCmdLet);
9496
this.registerCmdletType(ExitCmdLet);
9597
this.registerCmdletType(FalseCmdLet);
9698
this.registerCmdletType(FdCmdLet);

0 commit comments

Comments
 (0)