Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 88b1abb

Browse files
added task 1
1 parent 81fe026 commit 88b1abb

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed

‎0x0b-strace/EYNTK/ex_2‎

-11.1 KB
Binary file not shown.

‎0x0b-strace/Makefile‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ SRC = $(patsubst %,$(SDIR)/%,$(_SRC))
88
_OBJ = $(_SRC:.c=.o)
99
OBJECTS = $(patsubst %,$(ODIR)/%,$(_OBJ))
1010

11+
_SRC1 = strace_1.c
12+
13+
SRC1 = $(patsubst %,$(SDIR)/%,$(_SRC1))
14+
15+
_OBJ1 = $(_SRC1:.c=.o)
16+
OBJECTS1 = $(patsubst %,$(ODIR)/%,$(_OBJ1))
17+
1118
_DEPS = strace.h
1219
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
1320

@@ -16,18 +23,22 @@ SDIR = ./src
1623
ODIR = .
1724

1825
OUTPUT = strace_0
26+
OUTPUT1 = strace_1
1927

2028
$(ODIR)/%.o : $(SDIR)/%.c
2129
$(CC) $(CFLAGS) -c -o $@ $< -I$(IDIR)
2230

23-
all : $(OUTPUT)
31+
all : $(OUTPUT)$(OUTPUT1)
2432

2533
$(OUTPUT) : $(OBJECTS)
2634
$(CC) -o $@ $^ $(LINKS)
2735

36+
$(OUTPUT1) : $(OBJECTS1)
37+
$(CC) -o $@ $^ $(LINKS)
38+
2839
.PHONY : clean
2940

3041
clean :
31-
rm -f $(OUTPUT) $(OBJECTS)
42+
rm -f $(OUTPUT) $(OBJECTS)$(OUTPUT1)$(OBJECTS1)
3243

33-
re: $(OBJ)
44+
re: $(OBJ)$(OBJ1)

‎0x0b-strace/README.md‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 0x0B. C - Strace
22
### System programming & Algorithm ― Linux programming
33

4-
* strace_0.c - A program that executes and traces a given command.
4+
* strace_0.c - A program that executes and traces a given command. Each time a syscall is intercepted, it prints its number, followed by a new line
55

66
Usage: ./strace_0 command [args...]
77

@@ -77,5 +77,58 @@ Holberton
7777
3
7878
231
7979
$
80+
```
81+
82+
* strace_1.c - A program that executes and traces a given command. Each time a syscall is intercepted, it prints its name, followed by a new line
83+
84+
Usage: ./strace_1 command [args...]
8085

8186
```
87+
$make strace_1
88+
[...]
89+
$./strace_1 /bin/echo Holberton
90+
execve
91+
brk
92+
access
93+
mmap
94+
access
95+
open
96+
fstat
97+
mmap
98+
close
99+
access
100+
open
101+
read
102+
fstat
103+
mmap
104+
mprotect
105+
mmap
106+
mmap
107+
close
108+
mmap
109+
mmap
110+
arch_prctl
111+
mprotect
112+
mprotect
113+
mprotect
114+
munmap
115+
brk
116+
brk
117+
open
118+
fstat
119+
mmap
120+
close
121+
fstat
122+
mmap
123+
write
124+
Holberton
125+
close
126+
munmap
127+
close
128+
exit_group
129+
$
130+
```
131+
132+
## Resources:
133+
134+
* [syscalls](https://filippo.io/linux-syscall-table/)

‎0x0b-strace/src/strace_1.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "strace.h"
2+
3+
/**
4+
* run_tracee - A function that runs a tracee
5+
* @argv: Has path and arguments to be executed
6+
* @envp: environment variables
7+
*/
8+
void run_tracee(char *const argv[], char *const envp[])
9+
{
10+
if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0)
11+
{
12+
perror("Error setting TRACEME");
13+
exit(EXIT_FAILURE);
14+
}
15+
kill(getpid(), SIGSTOP);
16+
execve(argv[0], argv, envp);
17+
}
18+
19+
/**
20+
* wait_for_syscall - A function that traps only syscalls
21+
* @child_pid: pid of the tracee to be traced
22+
* Return: 1 if syscall is trapped else 0
23+
*/
24+
int wait_for_syscall(pid_t child_pid)
25+
{
26+
int status;
27+
28+
while (1)
29+
{
30+
ptrace(PTRACE_SYSCALL, child_pid, 0, 0);
31+
waitpid(child_pid, &status, 0);
32+
if (WIFSTOPPED(status) && WSTOPSIG(status) & 0x80)
33+
return (0);
34+
if (WIFEXITED(status))
35+
return (1);
36+
}
37+
}
38+
39+
/**
40+
* run_tracer - A function that runs a tracer
41+
* @child_pid: pid of the tracee to be traced
42+
*/
43+
void run_tracer(pid_t child_pid)
44+
{
45+
int status;
46+
struct user_regs_struct regs;
47+
48+
waitpid(child_pid, &status, 0);
49+
ptrace(PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACESYSGOOD);
50+
while (1)
51+
{
52+
if (wait_for_syscall(child_pid) != 0)
53+
break;
54+
ptrace(PTRACE_GETREGS, child_pid, 0, &regs);
55+
printf("%s", syscalls_64_g[(size_t) regs.orig_rax].name);
56+
fflush(stdout);
57+
printf("\n");
58+
if (wait_for_syscall(child_pid) != 0)
59+
break;
60+
}
61+
}
62+
63+
/**
64+
* main - Starts the program
65+
* @argc : no of command line arguments
66+
* @argv: command line arguments
67+
* @envp: environment variables
68+
* Return: on success - EXIT_SUCCESS, on failure - EXIT_FAILURE
69+
*/
70+
int main(int argc, char *const argv[], char *const envp[])
71+
{
72+
pid_t child_pid;
73+
74+
if (argc < 2)
75+
printf("Usage: %s command [args...]\n", argv[0]);
76+
else
77+
{
78+
child_pid = fork();
79+
if (child_pid == 0)
80+
{
81+
run_tracee(argv + 1, envp);
82+
} else if (child_pid > 0)
83+
{
84+
run_tracer(child_pid);
85+
} else
86+
{
87+
perror("fork failed");
88+
exit(EXIT_FAILURE);
89+
}
90+
}
91+
return (EXIT_SUCCESS);
92+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /