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 23fd7de

Browse files
finish task 0
1 parent 3a96dec commit 23fd7de

File tree

9 files changed

+1258
-0
lines changed

9 files changed

+1258
-0
lines changed

‎0x0b-strace/EYNTK/ex_0.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <unistd.h>
2+
#include <sys/wait.h>
3+
#include <sys/ptrace.h>
4+
#include <stdio.h>
5+
6+
int main(int __attribute__ ((unused)) argc, char **argv)
7+
{
8+
pid_t child_pid;
9+
int status;
10+
11+
child_pid = fork();
12+
if (child_pid == 0)
13+
{
14+
ptrace(PTRACE_TRACEME, 0, 0, 0);
15+
execve(argv[1], argv + 1, NULL);
16+
} else
17+
{
18+
wait(&status);
19+
while (WIFSTOPPED(status))
20+
{
21+
ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0);
22+
printf("single step\n");
23+
wait(&status);
24+
}
25+
printf("Exit status: %d\n", WEXITSTATUS(status));
26+
}
27+
return (0);
28+
}

‎0x0b-strace/EYNTK/ex_1.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <unistd.h>
2+
#include <sys/wait.h>
3+
#include <sys/ptrace.h>
4+
#include <stdio.h>
5+
6+
int main(int __attribute__ ((unused)) argc, char **argv)
7+
{
8+
pid_t child_pid;
9+
int status;
10+
11+
child_pid = fork();
12+
if (child_pid == 0)
13+
{
14+
ptrace(PTRACE_TRACEME, 0, 0, 0);
15+
execve(argv[1], argv + 1, NULL);
16+
} else
17+
{
18+
wait(&status);
19+
while (WIFSTOPPED(status))
20+
{
21+
printf("syscall ");
22+
ptrace(PTRACE_SYSCALL, child_pid, 0, 0);
23+
wait(&status);
24+
if (WIFEXITED(status))
25+
printf("Exit status: %d\n", WEXITSTATUS(status));
26+
else
27+
printf("return\n");
28+
}
29+
30+
}
31+
return (0);
32+
}

‎0x0b-strace/EYNTK/ex_2‎

11.1 KB
Binary file not shown.

‎0x0b-strace/EYNTK/ex_2.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <unistd.h>
2+
#include <sys/wait.h>
3+
#include <sys/ptrace.h>
4+
#include <stdio.h>
5+
#include <sys/user.h>
6+
7+
int main(int __attribute__ ((unused)) argc, char **argv)
8+
{
9+
pid_t child_pid;
10+
int status;
11+
struct user_regs_struct regs;
12+
13+
child_pid = fork();
14+
if (child_pid == 0)
15+
{
16+
ptrace(PTRACE_TRACEME, 0, 0, 0);
17+
execve(argv[1], argv + 1, NULL);
18+
} else
19+
{
20+
while (wait(&status) && !WIFEXITED(status))
21+
{
22+
ptrace(PTRACE_GETREGS, child_pid, 0, &regs);
23+
printf("%ld\n", (size_t)regs.orig_rax);
24+
ptrace(PTRACE_SYSCALL, child_pid, NULL, NULL);
25+
}
26+
printf("Exit status: %d\n", WEXITSTATUS(status));
27+
28+
}
29+
return (0);
30+
}

‎0x0b-strace/Makefile‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CC = gcc-4.8
2+
# CFLAGS = -Wall -Werror -Wextra -pedantic -g
3+
4+
_SRC = strace_0.c
5+
6+
SRC = $(patsubst %,$(SDIR)/%,$(_SRC))
7+
8+
_OBJ = $(_SRC:.c=.o)
9+
OBJECTS = $(patsubst %,$(ODIR)/%,$(_OBJ))
10+
11+
_DEPS = strace.h
12+
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
13+
14+
IDIR = ./inc
15+
SDIR = ./src
16+
ODIR = .
17+
18+
OUTPUT = strace_0
19+
20+
$(ODIR)/%.o : $(SDIR)/%.c
21+
$(CC) $(CFLAGS) -c -o $@ $< -I$(IDIR)
22+
23+
all : $(OUTPUT)
24+
25+
$(OUTPUT) : $(OBJECTS)
26+
$(CC) -o $@ $^ $(LINKS)
27+
28+
.PHONY : clean
29+
30+
clean :
31+
rm -f $(OUTPUT) $(OBJECTS)
32+
33+
re: $(OBJ)

‎0x0b-strace/README.md‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 0x0B. C - Strace
2+
### System programming & Algorithm ― Linux programming
3+
4+
* strace_0.c - A program that executes and traces a given command.
5+
6+
Usage: ./strace_0 command [args...]
7+
8+
```
9+
$make strace_0
10+
[...]
11+
$./strace_0 /bin/echo Holberton
12+
59
13+
12
14+
12
15+
21
16+
21
17+
9
18+
9
19+
21
20+
21
21+
2
22+
2
23+
5
24+
5
25+
9
26+
9
27+
3
28+
3
29+
21
30+
21
31+
2
32+
2
33+
0
34+
0
35+
5
36+
5
37+
9
38+
9
39+
10
40+
10
41+
9
42+
9
43+
9
44+
9
45+
3
46+
3
47+
9
48+
9
49+
9
50+
9
51+
158
52+
158
53+
10
54+
10
55+
10
56+
10
57+
10
58+
10
59+
11
60+
11
61+
12
62+
12
63+
12
64+
12
65+
5
66+
5
67+
9
68+
9
69+
1
70+
Holberton
71+
1
72+
3
73+
3
74+
11
75+
11
76+
3
77+
3
78+
231
79+
$
80+
81+
```

‎0x0b-strace/inc/strace.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _STRACE_H
2+
#define _STRACE_H
3+
4+
#include <unistd.h>
5+
#include <sys/wait.h>
6+
#include <sys/ptrace.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <sys/user.h>
10+
#include "syscalls.h"
11+
12+
#define EXIT_SUCCESS 0
13+
#define EXIT_FAILURE 1
14+
#endif

0 commit comments

Comments
(0)

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