about summary refs log tree commit diff stats
path: root/dev
diff options
context:
space:
mode:
authorSilvino <silvino@bk.ru>2021-03-03 03:07:44 +0000
committerpunk <punk@libernaut>2021-04-21 15:11:33 +0100
commit452477a2635d85ecf772a5242ce97d9479503bb3 (patch)
treea2e34d995cef5ac8068ec7047e93b1125c80d175 /dev
parent4de5ad72311e351792c251eaf807885a493149a4 (diff)
downloaddoc-452477a2635d85ecf772a5242ce97d9479503bb3.tar.gz
OpenBSD documentation
dev/c system dev rev
Diffstat (limited to 'dev')
-rw-r--r--dev/c/index.html15
-rw-r--r--dev/c/src/basic/Makefile4
-rw-r--r--dev/c/src/basic/shell.c57
-rw-r--r--dev/c/src/fork/Makefile11
-rw-r--r--dev/c/src/fork/fork.c38
-rw-r--r--dev/c/src/fork/fork_exec.c40
-rw-r--r--dev/c/src/hello/Makefile3
-rw-r--r--dev/c/src/hello/hello.c6
-rw-r--r--dev/c/src/linux/Makefile13
-rw-r--r--dev/c/src/linux/init.c8
-rw-r--r--dev/c/src/linux/rungdb.sh12
-rw-r--r--dev/c/system.html101
12 files changed, 279 insertions, 29 deletions
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 @@
 		<ul>
 		    <li><a href="hello.html#makefile">Makefile</a></li>
 		    <li><a href="hello.html#debug">Debug</a></li>
+		    <li><a href="basic.html#sources">Multiple sources</a></li>
 		</ul>
 	    </li>
 	    <li><a href="elements.html">Elements</a>
@@ -48,12 +49,7 @@
 	    <li><a href="">Control Flow</a></li>
 	    <li><a href="">Functions</a></li>
 	    <li><a href="">Input &amp; Output</a></li>
-            <li><a href="basic.html">Basic</a>
-		<ul>
-		    <li><a href="basic.html#sources">Multiple sources</a></li>
-		</ul>
-	    </li>
-	    <li><a href="lib.html">Libraries</a>
+  	    <li><a href="lib.html">Libraries</a>
 		<ul>
 		    <li><a href="lib.html#basic">Basic libraries</a></li>
 		    <li><a href="lib.html#advanced">Advanced libraries</a></li>
@@ -70,8 +66,11 @@
 
 
 	    </li>
-	    <li><a href="debugging.html">Debugging</a></li>
-	    <li><a href="system.html">System Development</a></li>
+            <li><a href="debugging.html">Debugging</a>
+	        <ul>
+	            <li><a href="system.html">System development</a></li>
+                </ul>
+            </li>
 	</ul>
 
 	<ul>
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 <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+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 <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+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 <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+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 <stdio.h>
+#include <unistd.h>
 
 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 <stdio.h>
+#include <unistd.h>
+
+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.</p>
 
-        <h2>Kernel Build</h2>
+        <h2>Build Kernel</h2>
 
         <pre>
-        $ 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
         </pre>
 
         <p>Default configuration disable some security
         configurations that allow us to debug (random memory
-        layout).</p>
+        layout KALSR),  CONFIG_COMPAT_BRK don't randomize
+        position of the programs (randomize_va_space) useful when
+        debugging a program (<a href="tracing.html">tracing</a>).
+        Configuration flags to enable;</p>
+
+        <ul>
+            <li>CONFIG_64BIT</li>
+            <li>CONFIG_DEBUG_KERNEL</li>
+            <li>CONFIG_HAVE_ARCH_KGDB</li>
+            <li>CONFIG_FTRACE</li>
+            <li>CONFIG_PRINTK</li>
+            <li>CONFIG_BLK_DEV_INITRD</li>
+            <li>CONFIG_BINFMT_ELF</li>
+            <li>CONFIG_TTY</li>
+            <li>CONFIG_DEBUG_INFO</li>
+            <li>CONFIG_DEBUG_INFO_DWARF4</li>
+            <li>CONFIG_GDB_SCRIPTS</li>
+            <li>CONFIG_READABLE_ASM</li>
+            <li>CONFIG_FRAME_POINTER</li>
+            <li>CONFIG_KGDB</li>
+            <li>CONFIG_KGDB_LOW_LEVEL_TRAP</li>
+            <li>CONFIG_EARLY_PRINTK</li>
+            <li>CONFIG_COMPAT_BRK</li>
+        </ul>
+
+        <p>And to disable;</p>
+
+        <ul>
+            <li>CONFIG_CC_OPTIMIZE_FOR_SIZE</li>
+        </ul>
+
+
+        <p>This changes can be achieved by creating a config-fragment and then
+        merge it with the configuration.</p>
 
         <pre>
-        $ make x86_64_defconfig
+        $ cat &lt;&lt;EOF &gt;.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
         </pre>
 
-        <p>Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4
-        and CONFIG_GDB_SCRIPTS in the kernel;</p>
+        <p>Create a tiny config;</p>
 
         <pre>
-        make x86_64_defconfig
-        cat &lt;&lt;EOF &gt;.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
         </pre>
 
-        <p>Check or change to your needs the configuration;</p>
+        <p>Merge config  with the following script;</p>
+
+        <pre>
+        $ ./scripts/kconfig/merge_config.sh .config .config-fragment
+        </pre>
+
+        <p>Check or change the configuration according to your needs;</p>
+
         <pre>
         $ make nconfig
         </pre>
@@ -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)
 	</pre>
 
+        <p>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.</p>
+
+        <pre>
+        (gdb) apropos lx
+        (gdb) lx-symbols
+        </pre>
+
+        <p>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;</p>
+
+        <pre>
+        (gdb) br do_exit if $lx_current()->pid == 1
+        </pre>
+
         <a href="index.html">C Index</a>
         <p>
         This is part of the LeetIO System Documentation.