简体   繁体   English

C中的程序状态

[英]program state in C

I just started programming in c, and I want to test some code. 我刚刚开始用c编程,我想测试一些代码。 So, I need to know the value of a variable at a specific point in the program which I already know. 因此,我需要在我已经知道的程序中的特定点上知道变量的值。

while searching I saw many people are using gdb and core dump but most of the examples I found they use it to debug the code if there is a crash. 在搜索时,我看到很多人都在使用gdb和core dump,但是我发现,大多数示例在发生崩溃时都使用它来调试代码。 in my case, I don't want to terminate the execution, I just want to save/know the value of a specific variable at a specific point. 就我而言,我不想终止执行,我只想在特定点保存/知道特定变量的值。

for example: 例如:

if I have this piece of code: 如果我有这段代码:

int func(int x){

    x = 3 * x;

    if(x > 0){

        x = x % 4;
        /* I want to know the value of x at this point*/
    }
    else {

        x = x + 1;
        /* I want to know the value of x at this point*/
    }

    return x;

}

if the user enters the value, I want to know what will be the value of x inside the block of (if) after the calculation. 如果用户输入值,我想知道计算后(if)块内x的值是什么。

UPDATE: to clarify my question, I have a big code and I want to test the complete package and I want to write a function that tells me what is the stored value at this program point. 更新:为澄清我的问题,我有一个大代码,我想测试完整的程序包,我想编写一个函数,该函数告诉我此程序点的存储值是多少。

GDB is the best tool for you. GDB是最适合您的工具。 While the program is in execution you can see the values of the variables. 在程序执行过程中,您可以看到变量的值。 Please follow the below steps: 请按照以下步骤操作:

compile your program with -g flag. 用-g标志编译程序。

gcc -g program.c -o output

Now run your program with gdb: 现在使用gdb运行程序:

gdb output

In Gdb command line set a breakpoint at 'main' by using: 在Gdb命令行中,使用以下命令在'main'处设置一个断点:

(gdb) b main

or use below one to set a breakpoint at a particular line. 或使用低于1的值在特定行上设置断点。

(gdb) b line_number

now enter 'r' to run the program. 现在输入“ r”运行程序。

(gdb) r

type 'n' and press enter to go to next line 输入“ n”,然后按Enter键转到下一行

(gdb) n 

type 'step' to step into a function: 输入“ step”以进入功能:

(gdb) step 

Examine the variable value by using 通过使用检查变量值

(gdb) print variable-name

Keep the breakpoint at 'line no' where you want to see the value of a variable and use ' print variable-name ' to view the value. 将断点保留在要查看变量值的“第no行”,并使用“ print variable-name ”查看值。

Please take this as a reference for more GDB commands: http://www.yolinux.com/TUTORIALS/GDB-Commands.html 请将此作为更多GDB命令的参考: http : //www.yolinux.com/TUTORIALS/GDB-Commands.html

Hope this answer will help you to debug your code. 希望这个答案可以帮助您调试代码。

I think You can use something like this 我想你可以用这样的东西

printf("%d\n",x);

after each expression with 在每个表达式之后

x= ...

in Your function. 在您的功能中。 Or You can use fprintf to write values in a file instead printf if You don't want to output values to the console. 或者如果您不想将值输出到控制台,则可以使用fprintf在文件中写入值,而不是printf

Use the debugger (install any C IDE like Eclipse CDT and you will not have to configure anything) 使用调试器(安装任何C IDE(例如Eclipse CDT),您将无需进行任何配置)

You can even do the debugging online https://www.onlinegdb.com/ 您甚至可以在线进行调试https://www.onlinegdb.com/

Another approach expanding on using printf is to use debugging macros or functions. 使用printf扩展的另一种方法是使用调试宏或函数。 Eg see: 例如:

https://github.com/jleffler/soq/blob/master/src/libsoq/debug.c https://github.com/jleffler/soq/blob/master/src/libsoq/debug.c

Something like this can be used to allow dynamically enabling debugging for some use cases of your function and then disable it again so you don't get too much output to have to work through when debugging your code. 这样的事情可用于允许针对某些功能用例动态启用调试,然后再次将其禁用,这样在调试代码时就不会产生过多的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM