我在程序中成功地使用了mtrace(),但它只创建了一个16mb的不可读文件,我读到here要创建一个可读文件,我应该使用:

 mtrace ./myexe log

问题是,我有一个服务器需要一些参数,我也必须在它上运行一个应用程序,我试着运行这个:
 mtrace "./Server args" log

但如果打开服务器并等待应用程序,它只输出一个未释放内存的列表。
所以我的问题是如何正确使用mtrace服务器?
编辑:
用无法读取的文件运行mtrace给我这个:
 mtrace "./Server args" log

很多这样的台词:
- 0x0000000003941d40 Free 224404 was never alloc'd 0x2384d8b

而这个:
Memory not freed:
-----------------
           Address     Size     Caller
0x00000000039600f0     0x48  at 0x238766c
0x00000000039739e0     0x38  at 0x37f0c0cbad
0x00007fc398000960     0x75  at 0x7fc3f3f0b2fd
0x00007fc3980009e0     0x30  at 0x7fc3f3f0b2fd
0x00007fc3c0001240     0x26  at 0x7fc3f3f0b2fd
0x00007fc3c00012a0     0x27  at 0x7fc3f3f0b2fd
0x00007fc3c0001ee0     0x29  at 0x7fc3f3f0b2fd
0x00007fc3c0001f20     0x2a  at 0x7fc3f3f0b2fd
0x00007fc3c0001f90     0x2a  at 0x7fc3f3f0b2fd
0x00007fc3c0001fd0     0x22  at 0x7fc3f3f0b2fd
0x00007fc3c0002030     0x2a  at 0x7fc3f3f0b2fd
0x00007fc3c00026c0     0x29  at 0x7fc3f3f0b2fd
0x00007fc3c0002700     0x22  at 0x7fc3f3f0b2fd
0x00007fc3c0002730     0x29  at 0x7fc3f3f0b2fd
0x00007fc3c00032d0     0x26  at 0x7fc3f3f0b2fd
0x00007fc3c00034d0     0x27  at 0x7fc3f3f0b2fd
0x00007fc3c00035b0     0x22  at 0x7fc3f3f0b2fd
0x00007fc3c00035e0     0x29  at 0x7fc3f3f0b2fd
0x00007fc3c00082b0      0x9  at 0x37f1081182
0x00007fc3c00084b0     0x28  at 0x7fc3f3f0b2fd
0x00007fc3c000a2f0      0xb  at 0x37f1081182
0x00007fc3c000a650     0x26  at 0x7fc3f3f0b2fd
0x00007fc3c000ac10     0x1e  at 0x7fc3f3f0b2fd
0x00007fc3c000af90      0xc  at 0x37f1081182
0x00007fc3c000b360     0x29  at 0x7fc3f3f0b2fd
0x00007fc3c000b870     0x38  at 0x7fc3f3f0b2fd
0x00007fc3c000ede0      0xc  at 0x37f1081182
0x00007fc3c00118a0     0x22  at 0x7fc3f3f0b2fd
0x00007fc3c0011f30     0x2c  at 0x7fc3f3f0b2fd
0x00007fc3c0011fd0     0x1e  at 0x7fc3f3f0b2fd
0x00007fc3c0012040     0x2a  at 0x7fc3f3f0b2fd
0x00007fc3c0012c30     0x40  at 0x7fc3f3f0b2fd
0x00007fc3c0014700      0xb  at 0x37f1081182
0x00007fc3c0014f10      0xb  at 0x37f1081182
0x00007fc3c0014fa0      0xb  at 0x37f1081182
0x00007fc3c00151d0     0x88  at 0x7fc3f7cc05c9
0x00007fc3c001d4b0     0x88  at 0x7fc3f7cc05c9
0x00007fc3c0024a00      0xb  at 0x37f1081182
0x00007fc3c003e960     0x40  at 0x7fc3f3f0b2fd
0x00007fc3c003edb0     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003ee40    0x170  at 0x37f0c118a3
0x00007fc3c003efc0     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003f050    0x170  at 0x37f0c118a3
0x00007fc3c003f1d0     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003f260    0x170  at 0x37f0c118a3
0x00007fc3c003f3e0     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003f470    0x170  at 0x37f0c118a3
0x00007fc3c003f5f0     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003f680    0x170  at 0x37f0c118a3
0x00007fc3c003f800     0x88  at 0x7fc3f3f0b2fd
0x00007fc3c003f890    0x170  at 0x37f0c118a3
0x00007fc3c0040890        0  at 0x7fc3efa7ee9d
0x00007fc3c00408b0      0x8  at 0x7fc3efa7ee9d
0x00007fc3c0040fc0    0x3f0  at 0x7fc3f3f0b2fd

最佳答案

我认为你误解了mtrace命令的用法。基于this看来,mtrace只是将日志文件转换成人类可读的东西。要首先创建日志文件,需要运行应用程序。
因此,要创建日志文件,首先将对mtrace()(c函数)等的相关调用放入代码中,并在启用调试的情况下重新编译。接下来,您只需正常运行程序:

./Server args

它会将任何内存问题写入名由MALLOC_TRACE环境变量设置的日志文件。
然后读取运行的日志文件
mtrace Server <logfilename>

编辑:对于内存问题的一般调试,我建议您改为查看valgrind(当然,除非您已经了解valgrind,并且有充分的理由不将其用于本例)。

关于c++ - 如何在服务器上使用mtrace,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24980372/

10-09 03:46