Skip to content

Commit 409642c

Browse files
committed
Adds a simple output redirection example.
1 parent 0458120 commit 409642c

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

python/redirect-dump/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redirect-dump

python/redirect-dump/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
CC = clang
3+
4+
all: redirect-dump
5+
6+
redirect-dump: redirect-dump.c
7+
$(CC) -O0 -ggdb3 redirect-dump.c -o redirect-dump
8+
9+
clean:
10+
$(RM) redirect-dump
11+
12+
.PHONY: all clean

python/redirect-dump/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# redirect-dump
2+
3+
This is a simple example on how to redirect an internal `dump` function and
4+
return the dumped value as a string.
5+
6+
## Usage
7+
8+
Run gdb and either use the redirect-dump.gdb file to load the extension, or do
9+
so manually. Run and break in `break_here` and call the new `$dump_arr` function.
10+
The result should be:
11+
12+
```
13+
$ DEBUGINFOD_URLS= gdb -x ./redirect-dump.gdb -ex run -ex 'printf "%s", $dump_arr(arr, len)' -ex c -ex quit ./redirect-dump
14+
0: 0
15+
1: 1
16+
2: 2
17+
3: 3
18+
4: 4
19+
5: 5
20+
6: 6
21+
7: 7
22+
8: 8
23+
9: 9
24+
```

python/redirect-dump/redirect-dump.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
void fdump_arr(FILE* stream, int* arr, size_t len)
5+
{
6+
for (int i = 0; i < len; ++i) {
7+
fprintf(stream, "%d: %d\n", i, arr[i]);
8+
}
9+
}
10+
11+
void dump_arr(int* arr, size_t len)
12+
{
13+
fdump_arr(stdout, arr, len);
14+
}
15+
16+
void break_here(int* arr, size_t len)
17+
{
18+
// Do nothing, set breakpoint here
19+
}
20+
21+
int main(void)
22+
{
23+
int* arr = calloc(10, sizeof(int));
24+
for (int i = 0; i < 10; ++i) {
25+
arr[i] = i;
26+
}
27+
break_here(arr, 10);
28+
free(arr);
29+
return 0;
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
source redirect-dump.py
3+
break break_here
4+

python/redirect-dump/redirect-dump.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import gdb
2+
import tempfile
3+
4+
5+
class DumpArr(gdb.Function):
6+
"""Dump an array"""
7+
8+
def __init__(self):
9+
super(DumpArr, self).__init__("dump_arr")
10+
11+
def invoke(self, arr, length):
12+
with tempfile.NamedTemporaryFile() as tmp:
13+
name = tmp.name
14+
s = gdb.parse_and_eval('(FILE*)fopen("{}", "w")'.format(name))
15+
gdb.parse_and_eval('fdump_arr({}, {}, {})'.format(s, arr, length))
16+
gdb.parse_and_eval('(int)fclose({})'.format(s))
17+
with open(name, 'r') as f:
18+
return f.read()
19+
20+
21+
DumpArr()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy