dwyl / dwyl/learn-zig

Debugging Zig with LLDB

Open
#20 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
4
Forks
1
PR merge metrics
No merged PRs in 30d

Description

# Focus on 'embedded Zig code in Elixir'

## Example

TLTR;

You use `Zigler` to add embedded `Zig` code into `Elixir`.
You run an IEX session in a terminal, and `lldb` in another attached to the IEX pid.

### Zig breakpoint

>[!TIP]
> In your `Zig`, put `@breakpoint();`

For example, I have a function in `Zig` that is exported as a NIF via `Zigler`:

```zig
pub fn from_tuple(term: beam.term) !beam.term {
std.debug.print("{any}\n", .{@TypeOf(beam.get_tuple(term, .{}))});
@breakpoint();
return beam.get_tuple(term, .{});
}
```

This function takes a tuple and returns null or a list made of the elements of the tuple:

- I am printing the expected output Type of the function `get_tuple`
- I put a `@breakpoint()`

### Run the embedded Zig code (via Zigler) in Elixir

In a terminal, I start a session that uses `Zig` code embedded in `Elixir` with `Zigler`:

```sh
iex -S mix
```

>[!TIP]
> get the IEX session `PID` with `System.pid`

```elixir
iex> System.pid
"1234"
```

>[!TIP]
> In another terminal, attach it to `lldb`: `lldb --attach-pid 12345`

```sh
lldb --attach-pid 12987
```

and I can use the commands `continue` or `next` to step:

```
c(ontinue) | n(ext) | s(tep) | p(rint) | exit
```

I pressed __n__ and you see how I navigate step by step into nested functions:

Image

Then I pressed __c__ to resume and I see the results in the IEX session:

- I see that I am passing an optional type (meaning error or result),
- and the result of my function `from_tuple` parses a tuple `{1,2,3}` into a list `[1,2,3]` via the `enif_get_tuple` (what I expect):

Image

This might give some clues of what is going on.

## Knowledge

[Image](https://ziggit.dev/t/zig-debugging-with-lldb/3931)


and


[Image](https://dockyard.com/blog/2024/05/22/debugging-elixir-nifs-with-lldb)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.