mirror of
https://github.com/TinyCC/tinycc.git
synced 2026-02-05 13:21:37 +00:00
I tried several gdb/lldb options to debug libtcc generated code. But gdb/lldb complained that they could not load symbols or that module does not exist. There was also another problem. The code itself was in memory (string) and gdb/lldb do not have functions to acces it. So I came up with a new api for debugging libtcc code. First you enable debugging with: tcc_set_options(s, "-g") Then compile the code with: tcc_compile_string_file(s, program, "<file>.c") Then call tcc_relocate(). And finaly write the object file to disk: elf_output_obj(s, "<file>.o") Now you can start the debugger and put an breakpoint after the elf_output_obj() code. Then use gdb command add-symbol-file <file>.o and from there on you can set breakpoints in the libtcc generated code. You can also step/print variables/... I could not find a simular function in lldb yet. When debugging is done you remove the tcc_set_options(s, "-g"). All other code can remain because tcc_compile_string_file and elf_output_obj do not output any file any more is debug is not set. See also tests/libtcc_debug.c
59 lines
1.3 KiB
C
59 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "libtcc.h"
|
|
|
|
static const char program[] =
|
|
"#include <stdio.h>\n"
|
|
"int fib(int n)\n"
|
|
"{\n"
|
|
" if (n <= 2)\n"
|
|
" return 1;\n"
|
|
" else\n"
|
|
" return fib(n-1) + fib(n-2);\n"
|
|
"}\n"
|
|
"int tst(void)\n"
|
|
"{\n"
|
|
" int i;\n"
|
|
" for (i = 2; i < 20; i++)\n"
|
|
" printf(\"%d \", fib(i));\n"
|
|
" printf(\"\\n\");\n"
|
|
" return 0;\n"
|
|
"}\n";
|
|
|
|
void handle_error(void *opaque, const char *msg)
|
|
{
|
|
fprintf(opaque, "%s\n", msg);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
int (*func)(void);
|
|
TCCState *s = tcc_new();
|
|
|
|
if (!s) {
|
|
fprintf(stderr, __FILE__ ": could not create tcc state\n");
|
|
return 1;
|
|
}
|
|
#if 1
|
|
/* If -g option is not set the debugging files tst.c en tst.o will
|
|
not be created. */
|
|
tcc_set_options(s, "-g");
|
|
#endif
|
|
tcc_set_error_func(s, stdout, handle_error);
|
|
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
|
|
if (tcc_compile_string_file(s, program, "tst.c") == -1)
|
|
return 1;
|
|
if (tcc_relocate(s) < 0)
|
|
return 1;
|
|
elf_output_obj(s, "tst.o");
|
|
/* set breakpoint on next line. and load symbol file with
|
|
gdb command add-symbol-file.
|
|
Then set breakpoint on tst and continue. */
|
|
if ((func = tcc_get_symbol(s, "tst")))
|
|
func();
|
|
tcc_delete(s);
|
|
return 0;
|
|
}
|