From 452477a2635d85ecf772a5242ce97d9479503bb3 Mon Sep 17 00:00:00 2001
From: Silvino
Date: Wed, 3 Mar 2021 03:07:44 +0000
Subject: OpenBSD documentation dev/c system dev rev
---
dev/c/index.html | 15 ++++---
dev/c/src/basic/Makefile | 4 +-
dev/c/src/basic/shell.c | 57 +++++++++++++++++++++++++
dev/c/src/fork/Makefile | 11 +++++
dev/c/src/fork/fork.c | 38 +++++++++++++++++
dev/c/src/fork/fork_exec.c | 40 ++++++++++++++++++
dev/c/src/hello/Makefile | 3 +-
dev/c/src/hello/hello.c | 6 ++-
dev/c/src/linux/Makefile | 13 ++++++
dev/c/src/linux/init.c | 8 ++++
dev/c/src/linux/rungdb.sh | 12 ++++++
dev/c/system.html | 101 ++++++++++++++++++++++++++++++++++++++-------
12 files changed, 279 insertions(+), 29 deletions(-)
create mode 100644 dev/c/src/basic/shell.c
create mode 100644 dev/c/src/fork/Makefile
create mode 100644 dev/c/src/fork/fork.c
create mode 100644 dev/c/src/fork/fork_exec.c
create mode 100644 dev/c/src/linux/Makefile
create mode 100644 dev/c/src/linux/init.c
create mode 100644 dev/c/src/linux/rungdb.sh
(limited to 'dev')
diff --git a/dev/c/index.html b/dev/c/index.html
index 1622cc1..eaf54a8 100644
--- a/dev/c/index.html
+++ b/dev/c/index.html
@@ -14,6 +14,7 @@
Elements
@@ -48,12 +49,7 @@
Control Flow
Functions
Input & Output
- Basic
-
-
- Libraries
+ Libraries
- Debugging
- System Development
+ Debugging
+
+
diff --git a/dev/c/src/basic/Makefile b/dev/c/src/basic/Makefile
index f165c15..88f7890 100644
--- a/dev/c/src/basic/Makefile
+++ b/dev/c/src/basic/Makefile
@@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall
-basic-c: main.o basic.o
+shell: shell.c
clean:
- rm -f *.o basic-c
+ rm -f *.o shell
diff --git a/dev/c/src/basic/shell.c b/dev/c/src/basic/shell.c
new file mode 100644
index 0000000..addc8a7
--- /dev/null
+++ b/dev/c/src/basic/shell.c
@@ -0,0 +1,57 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+char *getinput(char *buffer, size_t buflen) {
+ printf("$$ ");
+ return fgets(buffer, buflen, stdin);
+}
+
+void sig_int(int signo) {
+ printf("\nCaught SIGINT (Signal #%d)!\n$$ ", signo);
+ (void)fflush(stdout);
+}
+
+int main(int argc, char **argv) {
+ char buf[BUFSIZ];
+ pid_t pid;
+ int status;
+
+ /* cast to void to silence compiler warnings */
+ (void)argc;
+ (void)argv;
+
+ if (signal(SIGINT, sig_int) == SIG_ERR) {
+ fprintf(stderr, "signal error: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ while (getinput(buf, sizeof(buf))) {
+ buf[strlen(buf) - 1] = '\0';
+
+ if((pid=fork()) == -1) {
+ fprintf(stderr, "shell: can't fork: %s\n",
+ strerror(errno));
+ continue;
+ } else if (pid == 0) { /* child */
+ execlp(buf, buf, (char *)0);
+ fprintf(stderr, "shell: couldn't exec %s: %s\n", buf,
+ strerror(errno));
+ exit(EX_UNAVAILABLE);
+ }
+
+ /* parent waits */
+ if ((pid=waitpid(pid, &status, 0)) < 0) {
+ fprintf(stderr, "shell: waitpid error: %s\n",
+ strerror(errno));
+ }
+ }
+
+ exit(EX_OK);
+}
diff --git a/dev/c/src/fork/Makefile b/dev/c/src/fork/Makefile
new file mode 100644
index 0000000..a737794
--- /dev/null
+++ b/dev/c/src/fork/Makefile
@@ -0,0 +1,11 @@
+
+progs=fork fork_exec
+all: $(progs)
+
+fork: fork.c
+
+fork_exec: fork_exec.c
+
+
+clean:
+ rm $(progs)
diff --git a/dev/c/src/fork/fork.c b/dev/c/src/fork/fork.c
new file mode 100644
index 0000000..25e4909
--- /dev/null
+++ b/dev/c/src/fork/fork.c
@@ -0,0 +1,38 @@
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+int main(){
+
+ pid_t cpid,ppid;
+ char errbuf[1024];
+
+ cpid = fork();
+ if (cpid == -1) {
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "fork: %s", strerror(errno));
+ printf("%s\n", errbuf);
+ exit(1);
+ }
+
+ if (cpid == 0) {
+ //child
+ ppid = getppid();
+ if(ppid == 1){
+ printf("parent died ?\n");
+ _exit(1);
+ }
+ printf("I'm child with %i, parent %i\n", getpid(), ppid);
+ _exit(0);
+ }
+/* parent */
+ wait(NULL);
+ printf("Child id: %i\n", cpid);
+ return 0;
+}
+
diff --git a/dev/c/src/fork/fork_exec.c b/dev/c/src/fork/fork_exec.c
new file mode 100644
index 0000000..7f87c84
--- /dev/null
+++ b/dev/c/src/fork/fork_exec.c
@@ -0,0 +1,40 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+int main(){
+
+ pid_t cpid,ppid;
+ char errbuf[1024];
+
+ char *prog = "vim";
+ char *const args[3]= {"vim", "fork_exec.c", NULL};
+
+ cpid = fork();
+ if (cpid == -1) {
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "fork: %s", strerror(errno));
+ printf("%s\n", errbuf);
+ exit(1);
+ }
+
+ if (cpid == 0) {
+ //child
+ ppid = getppid();
+ if(ppid == 1){
+ printf("parent died ?\n");
+ _exit(1);
+ }
+ execvp(prog, args);
+ _exit(0);
+ }
+/* parent */
+ wait(NULL);
+ printf("Child id: %i\n", cpid);
+ return 0;
+}
+
diff --git a/dev/c/src/hello/Makefile b/dev/c/src/hello/Makefile
index a6d9f07..2c0ff2d 100644
--- a/dev/c/src/hello/Makefile
+++ b/dev/c/src/hello/Makefile
@@ -1,7 +1,6 @@
-CC=gcc
CFLAGS=-Wall
-hello: hello.o
+hello: hello.c
clean:
rm -f *.o hello
diff --git a/dev/c/src/hello/hello.c b/dev/c/src/hello/hello.c
index df66493..092efa5 100644
--- a/dev/c/src/hello/hello.c
+++ b/dev/c/src/hello/hello.c
@@ -1,6 +1,10 @@
#include
+#include
int main() {
- printf("hello World!");
+ char *name;
+ name = getlogin();
+
+ printf("hello %s!\n", name);
return 0;
}
diff --git a/dev/c/src/linux/Makefile b/dev/c/src/linux/Makefile
new file mode 100644
index 0000000..e026551
--- /dev/null
+++ b/dev/c/src/linux/Makefile
@@ -0,0 +1,13 @@
+CC=gcc
+CFLAGS=-Wall -static
+#LDFLAGS=-lc -lnss_files -lnss_dns -lresolv
+
+all: init ramdisk
+
+init: init.c
+
+ramdisk:
+ find . | cpio -o -H newc | gzip > rootfs.cpio.gz
+
+clean:
+ rm -f *.o init rootfs.cpio.gz
diff --git a/dev/c/src/linux/init.c b/dev/c/src/linux/init.c
new file mode 100644
index 0000000..10af0be
--- /dev/null
+++ b/dev/c/src/linux/init.c
@@ -0,0 +1,8 @@
+#include
+#include
+
+int main() {
+ printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
+ sleep(0xFFFFFFFF);
+ return 0;
+}
diff --git a/dev/c/src/linux/rungdb.sh b/dev/c/src/linux/rungdb.sh
new file mode 100644
index 0000000..9408d7f
--- /dev/null
+++ b/dev/c/src/linux/rungdb.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+gdb \
+ -ex "add-auto-load-safe-path $(pwd)" \
+ -ex "file vmlinux" \
+ -ex 'set arch i386:x86-64:intel' \
+ -ex 'target remote localhost:1234' \
+ -ex 'hbreak start_kernel' \
+ -ex 'continue' \
+ -ex 'disconnect' \
+ -ex 'set arch i386:x86-64' \
+ -ex 'target remote localhost:1234'
diff --git a/dev/c/system.html b/dev/c/system.html
index eedf242..d1d6558 100644
--- a/dev/c/system.html
+++ b/dev/c/system.html
@@ -15,35 +15,87 @@
will run on and GDB will connect to it to help us
understand how things tick.
- Kernel Build
+ Build Kernel
- $ tar xf linux-4.9.48.tar.xz
- $ cd linux-4.9.48
+ $ tar xf linux-4.9.258.tar.xz
+ $ cd linux-4.9.258
Default configuration disable some security
configurations that allow us to debug (random memory
- layout).
+ layout KALSR), CONFIG_COMPAT_BRK don't randomize
+ position of the programs (randomize_va_space) useful when
+ debugging a program (tracing).
+ Configuration flags to enable;
+
+
+ - CONFIG_64BIT
+ - CONFIG_DEBUG_KERNEL
+ - CONFIG_HAVE_ARCH_KGDB
+ - CONFIG_FTRACE
+ - CONFIG_PRINTK
+ - CONFIG_BLK_DEV_INITRD
+ - CONFIG_BINFMT_ELF
+ - CONFIG_TTY
+ - CONFIG_DEBUG_INFO
+ - CONFIG_DEBUG_INFO_DWARF4
+ - CONFIG_GDB_SCRIPTS
+ - CONFIG_READABLE_ASM
+ - CONFIG_FRAME_POINTER
+ - CONFIG_KGDB
+ - CONFIG_KGDB_LOW_LEVEL_TRAP
+ - CONFIG_EARLY_PRINTK
+ - CONFIG_COMPAT_BRK
+
+
+ And to disable;
+
+
+ - CONFIG_CC_OPTIMIZE_FOR_SIZE
+
+
+
+ This changes can be achieved by creating a config-fragment and then
+ merge it with the configuration.
- $ make x86_64_defconfig
+ $ cat <<EOF >.config-fragment
+ CONFIG_64BIT=y
+ CONFIG_DEBUG_KERNEL=y
+ CONFIG_HAVE_ARCH_KGDB=y
+ CONFIG_COMPAT_BRK=y
+ CONFIG_FTRACE=y
+ CONFIG_PRINTK=y
+ CONFIG_BLK_DEV_INITRD=y
+ CONFIG_BINFMT_ELF=y
+ CONFIG_TTY=y
+ CONFIG_DEBUG_INFO=y
+ CONFIG_DEBUG_INFO_DWARF4=y
+ CONFIG_GDB_SCRIPTS=y
+ CONFIG_READABLE_ASM=y
+ CONFIG_FRAME_POINTER=y
+ CONFIG_KGDB=y
+ CONFIG_KGDB_LOW_LEVEL_TRAP=y
+ CONFIG_EARLY_PRINTK=y
+ CONFIG_CC_OPTIMIZE_FOR_SIZE=n
+ EOF
- Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4
- and CONFIG_GDB_SCRIPTS in the kernel;
+ Create a tiny config;
- make x86_64_defconfig
- cat <<EOF >.config-fragment
- CONFIG_DEBUG_INFO=y
- CONFIG_DEBUG_KERNEL=y
- CONFIG_GDB_SCRIPTS=y
- EOF
- ./scripts/kconfig/merge_config.sh .config .config-fragment
+ $ make ARCH=x86_64 tinyconfig
- Check or change to your needs the configuration;
+ Merge config with the following script;
+
+
+ $ ./scripts/kconfig/merge_config.sh .config .config-fragment
+
+
+ Check or change the configuration according to your needs;
+
$ make nconfig
@@ -138,7 +190,7 @@
-ex "file vmlinux" \
-ex 'set arch i386:x86-64:intel' \
-ex 'target remote localhost:1234' \
- -ex 'break start_kernel' \
+ -ex 'hbreak start_kernel' \
-ex 'continue' \
-ex 'disconnect' \
-ex 'set arch i386:x86-64' \
@@ -169,6 +221,23 @@
(gdb)
+ lx-symbols allows to debug kernel modules, after starting the vm and loading
+ the module use lx-symbols to load the symbols from all the modules loaded in
+ the kernel.
+
+
+ (gdb) apropos lx
+ (gdb) lx-symbols
+
+
+ It's useful to set conditional breakpoints or a break point can be trigger
+ by unrelated tasks, example of a break point on do_exit function but only by
+ the process with pid 1;
+
+
+ (gdb) br do_exit if $lx_current()->pid == 1
+
+
C Index
This is part of the LeetIO System Documentation.
--
cgit 1.4.1-2-gfad0