Skip to content

Commit

Permalink
Rebuild all examples by clang 18
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jun 28, 2024
1 parent 05669f4 commit 2cd8829
Show file tree
Hide file tree
Showing 26 changed files with 186 additions and 109 deletions.
48 changes: 48 additions & 0 deletions ckb-debugger/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
CC := clang-18 --target=riscv64 -march=rv64imac_zba_zbb_zbc_zbs -g -nostdinc -nostdlib -Ickb-c-stdlib -Ickb-c-stdlib/libc

all: ckb-c-stdlib \
always_failure \
exec_callee \
exec_caller \
fib \
out_of_memory \
print_log \
spawn_callee_strcat \
spawn_caller_strcat \
timenow

ckb-c-stdlib:
git clone https://github.com/nervosnetwork/ckb-c-stdlib
cd ckb-c-stdlib
git checkout d7dc1e3
cd ..

always_failure:
$(CC) -o $@ $@.c

exec_callee:
$(CC) -o $@ $@.c

exec_caller:
$(CC) -o $@ $@.c

fib:
$(CC) -o $@ $@.c

out_of_memory:
$(CC) -o $@ $@.c

print_log:
$(CC) -o $@ $@.c

spawn_callee_strcat:
$(CC) -o $@ $@.c

spawn_caller_strcat:
$(CC) -o $@ $@.c

timenow:
$(CC) -o $@ $@.c

test:
bash MakefileTest.sh
33 changes: 33 additions & 0 deletions ckb-debugger/examples/MakefileTest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set -ex

ckb-debugger --mode decode-instruction 0x00054363

ckb-debugger --mode fast --bin always_failure 2>&1 | grep "ValidationFailure"
ckb-debugger --mode full --bin always_failure 2>&1 | grep "Run result: 1"

ckb-debugger --mode fast --tx-file exec.json --cell-index 0 --cell-type input --script-group-type lock
ckb-debugger --mode full --tx-file exec.json --cell-index 0 --cell-type input --script-group-type lock

ckb-debugger --mode fast --bin fib
ckb-debugger --mode full --bin fib --pprof /tmp/fib.pprof
ckb-debugger --mode full --bin fib --enable-overlapping-detection
ckb-debugger --mode full --bin fib --enable-steplog
ckb-debugger --mode fast --bin fib --max-cycles 100 2>&1 | grep "ExceededMaximumCycles"
ckb-debugger --mode full --bin fib --max-cycles 100 2>&1 | grep "CyclesExceeded"

ckb-debugger --mode fast --tx-file mock_tx.json --cell-index 0 --cell-type input --script-group-type lock
ckb-debugger --mode full --tx-file mock_tx.json --cell-index 0 --cell-type input --script-group-type lock
ckb-debugger --mode full --tx-file mock_tx.json --cell-index 0 --cell-type input --script-group-type lock --bin always_failure 2>&1 | grep "Run result: 1"

ckb-debugger --mode fast --bin out_of_memory 2>&1 | grep "MemOutOfBound"
ckb-debugger --mode full --bin out_of_memory 2>&1 | grep "MemOutOfBound"

ckb-debugger --mode fast --bin print_log
ckb-debugger --mode full --bin print_log

ckb-debugger --mode fast --tx-file spawn.json --cell-index 0 --cell-type input --script-group-type lock
ckb-debugger --mode full --tx-file spawn.json --cell-index 0 --cell-type input --script-group-type lock
ckb-debugger --mode full --tx-file spawn.json --cell-index 0 --cell-type input --script-group-type lock --pid 0 --pprof /tmp/spawn.pprof
ckb-debugger --mode full --tx-file spawn.json --cell-index 0 --cell-type input --script-group-type lock --pid 1 --pprof /tmp/spawn.pprof

ckb-debugger --mode full --bin timenow
1 change: 0 additions & 1 deletion ckb-debugger/examples/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Example

- [fib.md](./fib.md): This project profiling data in the format expected by the flamegraph visualization tool.
- [gdb.md](./gdb.md): This article introduces debugging programs by using gdb.
- [mock_tx.md](./mock_tx.md): We can download a transaction from the network and execute it locally.
- [out_of_memory.md](./out_of_memory.md): If your code triggers OOM, then ckb-debugger will print out the function call stack.
- [print_log.md](./print_log.md): Tracing program execution by printing logs is a common operation.
Expand Down
Binary file modified ckb-debugger/examples/always_failure
Binary file not shown.
2 changes: 2 additions & 0 deletions ckb-debugger/examples/always_failure.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "entry.h"

int main() {
return 1;
}
30 changes: 0 additions & 30 deletions ckb-debugger/examples/check.sh

This file was deleted.

10 changes: 5 additions & 5 deletions ckb-debugger/examples/exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
Execution of Exec scripts is no different from normal scripts:

```sh
$ ckb-debugger --tx-file examples/exec.json --cell-index 0 --cell-type input --script-group-type lock
$ ckb-debugger --tx-file exec.json --cell-index 0 --cell-type input --script-group-type lock
```

If you want to use gdb to debug it, the way will be slightly more complicated:

```sh
$ ckb-debugger --tx-file examples/exec.json --cell-index 0 --cell-type input --script-group-type lock --mode gdb --gdb-listen 127.0.0.1:9999
$ riscv64-unknown-elf-gdb --command=examples/exec_gdb_cmd.txt
$ ckb-debugger --tx-file exec.json --cell-index 0 --cell-type input --script-group-type lock --mode gdb --gdb-listen 127.0.0.1:9999
$ riscv64-unknown-elf-gdb --command=exec_gdb_cmd.txt
```

Let's see what `exec_gdb_cmd.txt` says:

```text
file examples/exec_caller # Import symbols from exec_caller
file exec_caller # Import symbols from exec_caller
target remote 127.0.0.1:9999 # Link to ckb-debugger
b __internal_syscall # Set a breakpoint at __internal_syscall
c # Fetch the 1st breakpoint: ckb_debug
c # Fetch the 2nd breakpoint: ckb_exec
file examples/exec_callee # Import symbols from exec_callee
file exec_callee # Import symbols from exec_callee
b _start # Set a breakpoint at exec_callee's _start
c # Continue
l # List raw codes
Expand Down
Binary file modified ckb-debugger/examples/exec_callee
Binary file not shown.
Binary file modified ckb-debugger/examples/exec_caller
Binary file not shown.
4 changes: 2 additions & 2 deletions ckb-debugger/examples/exec_gdb_cmd.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
file examples/exec_caller
file exec_caller
target remote 127.0.0.1:9999
b __internal_syscall
c
c
file examples/exec_callee
file exec_callee
b _start
c
l
Binary file modified ckb-debugger/examples/fib
Binary file not shown.
2 changes: 2 additions & 0 deletions ckb-debugger/examples/fib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "entry.h"

int fib(int n) {
if (n == 0 || n == 1) {
return n;
Expand Down
38 changes: 19 additions & 19 deletions ckb-debugger/examples/fib.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

This project profiling data in the format expected by the flamegraph visualization tool.

# Usage

Suppose the program to be tested is:

```c
int fib(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}

int main() {
if (fib(10) != 55) {
return 1;
}
return 0;
}
# Gdb debugging

This article introduces debugging programs by using gdb.

For example, we want to know the parameters passed in to a function at runtime:

```sh
$ ckb-debugger --mode gdb --gdb-listen 127.0.0.1:9999 --bin fib
$ riscv64-unknown-elf-gdb fib

$ (gdb) target remote 127.0.0.1:9999
$ (gdb) b fib
$ (gdb) c
Breakpoint 1, fib (n=5) at fib.c:2
```

At the Breakpoint 1, we learn that fib (n=5) at fib.c:2.


# Get flamegraph

We should take the `-g` option on compiling for saving the debugging information:

```sh
Expand Down
17 changes: 0 additions & 17 deletions ckb-debugger/examples/gdb.md

This file was deleted.

Binary file modified ckb-debugger/examples/out_of_memory
Binary file not shown.
2 changes: 2 additions & 0 deletions ckb-debugger/examples/out_of_memory.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "entry.h"

const int n = 2;

int a() {
Expand Down
30 changes: 19 additions & 11 deletions ckb-debugger/examples/out_of_memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
If your code triggers OOM, then ckb-debugger will print out the function call stack:

```sh
$ riscv64-unknown-elf-gcc -g -o out_of_memory out_of_memory.c

$ make out_of_memory
$ ckb-debugger --bin out_of_memory

Trace:
??:??:??
??:??:??
/code/ckb-debugger/examples/out_of_memory.c:23:main
/code/ckb-debugger/examples/out_of_memory.c:19:c
/code/ckb-debugger/examples/out_of_memory.c:15:b
/code/ckb-debugger/examples/out_of_memory.c:5:a
Error:
MemOutOfBound
??:??:??
/home/ubuntu/src/ckb-standalone-debugger/ckb-debugger/examples/ckb-c-stdlib/libc/entry.h:9:_start
/home/ubuntu/src/ckb-standalone-debugger/ckb-debugger/examples/out_of_memory.c:25:main
/home/ubuntu/src/ckb-standalone-debugger/ckb-debugger/examples/out_of_memory.c:21:c
/home/ubuntu/src/ckb-standalone-debugger/ckb-debugger/examples/out_of_memory.c:17:b
/home/ubuntu/src/ckb-standalone-debugger/ckb-debugger/examples/out_of_memory.c:7:a

pc : 0x 12D28
zero: 0x 0 ra : 0x 12D3E sp : 0x 3FFFA0 gp : 0x 146E8
tp : 0x 0 t0 : 0x 0 t1 : 0x 0 t2 : 0x 0
s0 : 0x 3FFFB0 s1 : 0x 0 a0 : 0x 0 a1 : 0x 400000
a2 : 0x 0 a3 : 0x 0 a4 : 0x 0 a5 : 0x 0
a6 : 0x 0 a7 : 0x 0 s2 : 0x 0 s3 : 0x 0
s4 : 0x 0 s5 : 0x 0 s6 : 0x 0 s7 : 0x 0
s8 : 0x 0 s9 : 0x 0 s10 : 0x 0 s11 : 0x 0
t3 : 0x 0 t4 : 0x 0 t5 : 0x 0 t6 : 0x 0

Error: MemOutOfBound
```
Binary file modified ckb-debugger/examples/print_log
Binary file not shown.
2 changes: 1 addition & 1 deletion ckb-debugger/examples/spawn.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
syscall spawn is a new interface added since ckb2023.

```sh
$ ckb-debugger --mode fast --tx-file examples/spawn.json --cell-index 0 --cell-type input --script-group-type lock
$ ckb-debugger --mode fast --tx-file spawn.json --cell-index 0 --cell-type input --script-group-type lock
```

In order to simplify the writing of tx.json, we designed a simple DSL to assist you in writing tx.json.
Expand Down
Binary file modified ckb-debugger/examples/spawn_callee_strcat
Binary file not shown.
27 changes: 16 additions & 11 deletions ckb-debugger/examples/spawn_callee_strcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <string.h>

#include "ckb_syscalls.h"
#include "spawn_utils.h"

char *strcat(char *restrict dest, const char *restrict src) {
strcpy(dest + strlen(dest), src);
Expand All @@ -17,16 +16,22 @@ int main(int argc, char *argv[]) {
}
size_t content_size = (uint64_t)strlen(content);
uint64_t fds[2] = {0};
uint64_t length = countof(fds);
uint64_t length = 2;
err = ckb_inherited_file_descriptors(fds, &length);
CHECK(err);
CHECK2(length == 2, ErrorCommon);
if (err != 0) {
return err;
}
if (length != 2) {
return 1;
}
size_t content_size2 = content_size;
printf("fds[CKB_STDOUT] = %d", fds[CKB_STDOUT]);
err = ckb_write(fds[CKB_STDOUT], content, &content_size);
CHECK(err);
CHECK2(content_size2 == content_size, ErrorWrite);

exit:
return err;
printf("fds[CKB_STDOUT] = %lu", fds[1]);
err = ckb_write(fds[1], content, &content_size);
if (err != 0) {
return err;
}
if (content_size2 != content_size) {
return 1;
}
return 0;
}
Binary file modified ckb-debugger/examples/spawn_caller_strcat
Binary file not shown.
Loading

0 comments on commit 2cd8829

Please sign in to comment.