C调试中符号数组的自动导出(教学目的)

发布于 2024-11-04 11:59:28 字数 799 浏览 1 评论 0原文

我需要向孩子们(10-15岁,教学是通过网站)教C语言,我希望能够向他们展示程序的一步一步执行,但我不希望他们直接使用调试器(对他们来说太复杂了,他们完全是初学者)。

我的想法是预先计算所有需要的数据并将其显示给他们(使用很酷的 JavaScript 动画、代码中的当前行、变量的值和标准输出)。

我需要的是一种在 C 代码上运行调试器并在每个可能的步骤导出变量值的方法(没有结构,只有基本变量和数组)。

是否有任何 gdb 或其他调试器的接口可以做到这一点?


For some context : we are training students for the IOI (International Olympiad in Informatics) though a website with courses, exercices (automatically corrected)... The code (in C) can be edited , compiled, tested and submitted online (with a javascript editor). This way no need to install anything (at first) so more people can just "try it".

基本的“一步一步”调试只是向初学者展示如何修改变量,“for”或“while”如何工作。作为老师,您可以在白板上做这种事情。更高级的学生将安装一些 IDE 并且将/或不使用调试器。

因此,对于初学者,我们希望他们能够在网站上使用一些基本代码(情感、数学运算、函数调用、for、while、if)来“看到事物”。

I need to teach C to children (10-15 years old, teaching is through a website) and I want to be able to show them a step by step execution of a program but I don't want them to use a debugger directly (too complex for them, they are total beginners).

My idea was to pre-compute all the needed data and to show it to them (with a cool javascript animation, with the current line in the code, the values of the variables and the standard output).

What I need is a way to run a debugger on a C code and to export the values of the variables at each possible step (no struct, just basic variables and arrays).

Is there any interface to gdb or some other debugger that can to that ?


For some context : we are training students for the IOI (International Olympiad in Informatics) though a website with courses, exercices (automatically corrected)...
The code (in C) can be edited , compiled, tested and submitted online (with a javascript editor). This way no need to install anything (at first) so more people can just "try it".

The basic "step by step" debugging was only to show the beginners how variables are modified, how a "for" or a "while" are working. The kind of stuff you can do on a whiteboard as a teacher. More advanced students will install some IDE and will/or not use the debugger.

So for the beginners we want them to be able to play, on the website, with some basic code (affectations, maths operations, function call,for,while,if) to "see things".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-11-11 11:59:28

如果您仅限于具有特定输入的程序,或者根本没有输入,您可以使用 gdb 脚本,如下所示:

try.c(输入程序):

#include <stdio.h>

int main()
{
        int i;
        for (i = 0; i < 10; i++)
        {
                printf("the number now is %d\n", i);
                i++;
        }
        return 0;
}

trace.gdb(基本 gdb 脚本):

break main
run
while 1
info locals
step
end
quit

结果gdb -x trace.gdb -batch try

Breakpoint 1 at 0x40053c: file try.c, line 6.

Breakpoint 1, main () at try.c:6
6               for (i = 0; i < 10; i++)
i = 0
8                       printf("the number now is %d\n", i);
i = 0
the number now is 0
9                       i++;
i = 0
6               for (i = 0; i < 10; i++)
i = 1
8                       printf("the number now is %d\n", i);
i = 2
the number now is 2
9                       i++;
i = 2
6               for (i = 0; i < 10; i++)
i = 3
8                       printf("the number now is %d\n", i);
i = 4
the number now is 4
9                       i++;
i = 4
6               for (i = 0; i < 10; i++)
i = 5
8                       printf("the number now is %d\n", i);
i = 6
the number now is 6
9                       i++;
i = 6
6               for (i = 0; i < 10; i++)
i = 7
8                       printf("the number now is %d\n", i);
i = 8
the number now is 8
9                       i++;
i = 8
6               for (i = 0; i < 10; i++)
i = 9
11              return 0;
i = 10
12      }
i = 10
0x000000300161ebbd in __libc_start_main () from /lib/libc.so.6
No symbol table info available.
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
trace.gdb:6: Error in sourced command file:
No frame selected.

更改 gdb 输出的方法,这样您也许可以调整脚本,使输出可解析,从而使您可以通过 javascript 播放它。

而且您还需要确保程序不会无限循环,可能通过使用 方便变量来限制脚本中 while 循环的数量。

If you're limited to programs with specific input, or without input at all, you can maybe use gdb scripting, in something like this:

try.c (the input program):

#include <stdio.h>

int main()
{
        int i;
        for (i = 0; i < 10; i++)
        {
                printf("the number now is %d\n", i);
                i++;
        }
        return 0;
}

trace.gdb (a basic gdb script):

break main
run
while 1
info locals
step
end
quit

the results of gdb -x trace.gdb -batch try

Breakpoint 1 at 0x40053c: file try.c, line 6.

Breakpoint 1, main () at try.c:6
6               for (i = 0; i < 10; i++)
i = 0
8                       printf("the number now is %d\n", i);
i = 0
the number now is 0
9                       i++;
i = 0
6               for (i = 0; i < 10; i++)
i = 1
8                       printf("the number now is %d\n", i);
i = 2
the number now is 2
9                       i++;
i = 2
6               for (i = 0; i < 10; i++)
i = 3
8                       printf("the number now is %d\n", i);
i = 4
the number now is 4
9                       i++;
i = 4
6               for (i = 0; i < 10; i++)
i = 5
8                       printf("the number now is %d\n", i);
i = 6
the number now is 6
9                       i++;
i = 6
6               for (i = 0; i < 10; i++)
i = 7
8                       printf("the number now is %d\n", i);
i = 8
the number now is 8
9                       i++;
i = 8
6               for (i = 0; i < 10; i++)
i = 9
11              return 0;
i = 10
12      }
i = 10
0x000000300161ebbd in __libc_start_main () from /lib/libc.so.6
No symbol table info available.
Single stepping until exit from function __libc_start_main,
which has no line number information.

Program exited normally.
trace.gdb:6: Error in sourced command file:
No frame selected.

There are ways to change gdb's output so you can perhaps tune the script to make the output parsable in a way that will allow you to make it something playable by javascript.

And you'll also need to make sure the program does not loop endlessly, probably by using convenience variables to limit number of while loops in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文