简体   繁体   English

在gdb中使用print命令是否安全?

[英]Is it safe to use print command in gdb?

I know that gdb allows to print expressions via print <exp> command. 我知道gdb允许通过print <exp>命令打印表达式。 As I understand result of such exression is saved to gdb variables (which can be accesed with $N - N - number of variable . Is it safe to print result of execution of functions? Suppose I have functions: 据我所知,这种exression的结果被保存到gdb变量(可以使用$N - N - number of variable 。打印函数执行结果是否安全?假设我有函数:

const char* foo(){ return "foo";}

char* foo2(){static char b[10]; /*change b*/; return b}

char* foo3(){char* b; /*Create string on heap*/ return b;}

How does gdb treat results of print foo(), print foo2(), print foo3() ? gdb如何处理print foo(), print foo2(), print foo3()结果print foo(), print foo2(), print foo3() Will it release memory of variables and can it lead to errors? 它会释放变量的内存吗?它会导致错误吗?

Gdb does nothing different than calling the function and catch the result. Gdb没有什么不同于调用函数并捕获结果。 So if the function have side effects like allocating memory, the memory is still allocated after the function call from gdb. 因此,如果该函数具有分配内存等副作用,则在从gdb调用函数后仍会分配内存。

So for you foo3() function the memory will be allocated on the heap until you will free it ( manually ). 所以对于你的foo3()函数,内存将在堆上分配,直到你释放它(手动)。 But this is not an "unsafe" behavior, it simply produces memory leaks. 但这不是一种“不安全”的行为,它只会产生内存泄漏。 If that is acceptable in your debug environment, there is no problem. 如果在调试环境中可以接受,则没有问题。 If you allocate tons of memory but never free it, you will run in "out of memory" problems. 如果你分配了大量内存但从未释放它,你将遇到“内存不足”的问题。 On an small embedded system this is potentially a problem. 在小型嵌入式系统上,这可能是一个问题。

gdb itself will never release any memory. gdb本身永远不会释放任何内存。 Maybe you want to call two functions where the first allocates memory and the second needs to use it. 也许你想调用两个函数,第一个分配内存,第二个需要使用它。 So gdb is not allowed to decide how to handle allocated storage. 因此不允许gdb决定如何处理分配的存储。 As a result gdb will not release memory! 结果gdb不会释放内存!

If you open files, sockets or use semaphores, you have the same effect. 如果打开文件,套接字或使用信号量,则会产生相同的效果。 The file will not be closed or a semaphore will not released. 该文件不会被关闭或信号量不会被释放。 You as the user have to know what you call and what side effects your call will produce in the given environment, not only with memory but with all other resources and states in your prog. 您作为用户必须知道您呼叫的内容以及您的呼叫在给定环境中将产生的副作用,不仅包括内存,还包括您的编程中的所有其他资源和状态。

gdb cannot know what is in your functions so cannot decide to free a returned pointer. gdb无法知道你的函数是什么,所以无法决定free返回的指针。 If you're returning a literal or a static variable, there's no need to free the memory, since no allocation is performed (and it would be a fault to free it). 如果你要返回一个文字或static变量,则不需要释放内存,因为不执行任何分配(释放它是一个错误)。

The third example, on the other hand, allocates some memory, so it's a kind of memory leak, yes, unless you're able to call another function from gdb like free on the gdb variable. 第三个例子,在另一方面,分配一些内存,所以它是一种内存泄漏的,是的,除非你能够从调用另一个函数gdb喜欢free的gdb的变量。

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

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