Skip to content

Commit

Permalink
Add support for hot command to show hot threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 19, 2024
1 parent 4c1ee9f commit 22e100c
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ RUN gcc \
-Wall \
-Werror \
-no-pie \
-D_GNU_SOURCE \
-lpthread \
-o target \
/root/target.c
RUN mkdir /root/x86
Expand All @@ -283,6 +285,8 @@ RUN gcc \
-Werror \
-no-pie \
-m32 \
-D_GNU_SOURCE \
-lpthread \
-o target \
/root/target.c
RUN mkdir /root/arm
Expand All @@ -302,6 +306,8 @@ RUN arm-none-linux-gnueabihf-gcc \
-Werror \
-no-pie \
-marm \
-D_GNU_SOURCE \
-lpthread \
-o target \
/root/target.c
RUN mkdir /root/arm64
Expand All @@ -321,6 +327,8 @@ RUN aarch64-none-linux-gnu-gcc \
-Werror \
-no-pie \
-march=armv8-a \
-D_GNU_SOURCE \
-lpthread \
-o target \
/root/target.c

Expand Down
49 changes: 49 additions & 0 deletions assets/target/target.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <fcntl.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define NUM_THREADS 5

typedef unsigned int uint;

__attribute__((noinline)) void my_memcpy(void *dest, const void *src, size_t n)
Expand Down Expand Up @@ -106,14 +109,51 @@ void my_a(uint i)
}
}

static void *busy_loop(void *arg)
{
char thread_name[16] = {0};
int index = *(int *)arg;
printf("Thread %d started\n", index);

snprintf(thread_name, sizeof(thread_name), "Child-%d", index);
pthread_setname_np(pthread_self(), thread_name);

long limit = (index + 1) * 10000000L;

while (true)
{
for (volatile long i = 0; i < limit; i++)
;

usleep(500000);
}

return 0;
}

int main(int argc, char **argv, char **envp)
{
pthread_t threads[NUM_THREADS] = {0};
int thread_indices[NUM_THREADS] = {0};

int fd = open("/dev/null", O_RDWR);
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);

pthread_setname_np(pthread_self(), "Parent");

for (int i = 0; i < NUM_THREADS; i++)
{
thread_indices[i] = i;
if (pthread_create(&threads[i], NULL, busy_loop, &thread_indices[i]) != 0)
{
perror("Failed to create thread");
return 1;
}
}

while (true)
{

Expand All @@ -129,6 +169,15 @@ int main(int argc, char **argv, char **envp)
puts(buf);

free(buf);

for (volatile long i = 0; i < 100000000L; i++)
;

usleep(500000);
}

for (int i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
}
14 changes: 7 additions & 7 deletions package
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ echo 'EOF' >> $TEMPDIR/frida-cshell
echo ')' >> $TEMPDIR/frida-cshell

cat <<EOF >> $TEMPDIR/frida-cshell
verbose=
debug=
file=
name=
pid=
Expand All @@ -25,7 +25,7 @@ show_help () {
echo " -f spawn FILE"
echo " -n attach to NAME"
echo " -p attach to PID"
echo " -V enable verbose mode"
echo " -d enable debug mode"
echo
}
Expand All @@ -34,7 +34,7 @@ if [[ "\$1" == "--help" ]]; then
exit 0
fi
while getopts ":f:hn:p:V:" opt; do
while getopts ":f:hn:p:d" opt; do
case \$opt in
f)
file=\$OPTARG
Expand All @@ -48,8 +48,8 @@ while getopts ":f:hn:p:V:" opt; do
p)
pid=\$OPTARG
;;
V)
verbose=true
d)
debug=true
;;
:)
echo "Option - \$OPTARG requires an argument."
Expand All @@ -76,8 +76,8 @@ then
exit 1
fi
if [ \${verbose} ]; then
opt="{\"verbose\":true}"
if [ \${debug} ]; then
opt="{\"debug\":true}"
else
opt="{}"
fi
Expand Down
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.4.4",
"version": "1.4.5",
"description": "Frida's CShell",
"scripts": {
"prepare": "npm run build && npm run version && npm run package && npm run copy",
Expand Down
23 changes: 6 additions & 17 deletions src/cmdlets/breakpoints/bp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Token } from '../../io/token.js';
import { Var } from '../../vars/var.js';

const NUM_CHAR: string = '#';
const UNLIMITED_CHAR: string = '*';

abstract class TypedBpCmdLet extends CmdLet implements InputInterceptLine {
public abstract readonly bpType: BpType;
Expand Down Expand Up @@ -45,16 +44,6 @@ abstract class TypedBpCmdLet extends CmdLet implements InputInterceptLine {
return val;
}

protected parseHits(token: Token): number | null {
if (token.getLiteral() === UNLIMITED_CHAR) return -1;

const v = token.toVar();
if (v === null) return null;

const hits = v.toU64().toNumber();
return hits;
}

private runDelete(tokens: Token[]): Var | null {
const vars = this.transform(tokens, [this.parseIndex, this.parseDelete]);
if (vars === null) return null;
Expand Down Expand Up @@ -140,7 +129,7 @@ abstract class CodeBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[addr], [hits]] = vars as [[Var], [number | null]];
Expand All @@ -164,7 +153,7 @@ abstract class CodeBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseIndex, this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[index, addr], [hits]] = vars as [[number, Var], [number | null]];
Expand Down Expand Up @@ -224,7 +213,7 @@ abstract class MemoryBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseVar, this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[addr, length], [hits]] = vars as [[Var, Var], [number | null]];
Expand All @@ -249,7 +238,7 @@ abstract class MemoryBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseIndex, this.parseVar, this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[index, addr, length], [hits]] = vars as [
Expand Down Expand Up @@ -325,7 +314,7 @@ abstract class TraceBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseVar, this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[addr, depth], [hits]] = vars as [[Var, Var], [number | null]];
Expand Down Expand Up @@ -360,7 +349,7 @@ abstract class TraceBpCmdLet
const vars = this.transformOptional(
tokens,
[this.parseIndex, this.parseVar, this.parseVar],
[this.parseHits],
[this.parseNumberOrAll],
);
if (vars === null) return null;
const [[index, addr, depth], [hits]] = vars as [
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 @@ -58,6 +58,7 @@ import { WriteCmdLet } from '../data/write.js';
import { GrepCmdLet } from '../misc/grep.js';
import { CatCmdLet } from '../files/cat.js';
import { LogCmdLet } from '../misc/log.js';
import { HotCmdLet } from '../thread/hot.js';

const USAGE: string = `Usage: js
Expand Down Expand Up @@ -104,6 +105,7 @@ export class JsCmdLet extends CmdLet {
HelpCmdLet: HelpCmdLet,
History: History,
HistoryCmdLet: HistoryCmdLet,
HotCmdLet: HotCmdLet,
Input: Input,
InsnBpCmdLet: InsnBpCmdLet,
LdCmdLet: LdCmdLet,
Expand Down
1 change: 1 addition & 0 deletions src/cmdlets/misc/grep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class GrepCmdLet extends CmdLet {
public runSync(tokens: Token[]): Var {
const vars = this.transformOptional(tokens, [], [this.parseLiteral]);
if (vars === null) return this.usage();
// eslint-disable-next-line prefer-const
let [_, [filter]] = vars as [[], [string | null]];
if (filter === null) {
Output.clearFilter();
Expand Down
Loading

0 comments on commit 22e100c

Please sign in to comment.