我知道之前有类似的问题,但是:
- 关于gprof产生空输出,我正在使用来自Windows 10的MSYS2的GCC 10.2.0,即MingW64分发。我还添加了
-no-pie
到链接器参数中,但仍然没有结果,GNU Prof版本为2.36.1。 - 关于gprof没有输出,我正在执行一个明确需要时间才能执行的程序。
该程序是从这个教程中复制而来的代码:
//test_gprof.c
#include<stdio.h>
void new_func1(void)
{
printf("\n Inside new_func1()\n");
int i = 0;
for(;i<0xffffffee;i++);
return;
}
void func1(void)
{
printf("\n Inside func1 \n");
int i = 0;
for(;i<0xffffffff;i++);
new_func1();
return;
}
static void func2(void)
{
printf("\n Inside func2 \n");
int i = 0;
for(;i<0xffffffaa;i++);
return;
}
int main(void)
{
printf("\n Inside main()\n");
int i = 0;
for(;i<0xffffff;i++);
func1();
func2();
return 0;
}
我正在使用Code::Blocks进行构建。 我尝试过MSYS2的32位和64位版本的MingW。 我已将所有软件包升级到可用的最新版本。
Code::Blocks构建日志输出:
g++.exe -pg -c C:\Users\david\Documents\GameDev\CPPTESTS\main.cpp -o obj\Debug\CPPTESTS\main.o
g++.exe -o CPPTESTS-d.exe obj\Debug\CPPTESTS\main.o -pg -lgmon -no-pie
程序成功编译和执行。执行后会生成一个gmon.out文件。可以使用gprof来解读这个文件:
gprof CPPTests-d.exe gmon.out > gprofoutput.txt
在
gprofoutput.txt
文件中,会生成以下内容:Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
% the percentage of the total running time of the
time program used by this function.
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
calls the number of times this function was invoked, if
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) no time propagated
index % time self children called name
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function is in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`<spontaneous>' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
Copyright (C) 2012-2021 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Index by function name
您可以看到,它完全是空的。此外,如果我在十六进制编辑器中打开gmon.out
,里面大部分都是空白的,下面是第一部分:
F0 14 C5 00 18 8A C5 00 B4 3A 00 00 79 18 05 00 64 00 00 00 96 1A C5 00 A7 16 C5 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 00 FE 00 5D 00 E2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 00 C6 01 A5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 00 E5 00 77 00 F5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
然后结尾是以下内容:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4 15 C5 00 6C 89 C5 00 01 00 00 00 14 16 C5 00 6C 89 C5 00 01 00 00 00 2C 16 C5 00 DB 15 C5 00 01 00 00 00 48 16 C5 00 6C 89 C5 00 01 00 00 00 84 16 C5 00 6C 89 C5 00 01 00 00 00 A0 16 C5 00 0A 16 C5 00 01 00 00 00 A4 16 C5 00 3C 16 C5 00 01 00 00 00
剩下的是几千字节的00
。
我已经尝试了谷歌中能找到的一切方法来让它工作。最初我在实际项目代码中执行时遇到了麻烦,但由于似乎我正在做某些错误的事情,我无法弄清楚,所以我转而使用上面的代码。
任何帮助都将不胜感激。
编辑
我设法在gmon.out中生成结果,gprof正确解析并为我的测试程序中的所有函数提供时间。没有任何更改,甚至没有关闭MSYS2终端(我放弃了Code :: Blocks以确保它不是问题),我然后使用相同的命令重新编译程序,再次执行它,又回到了空结果。
我不确定它是否是编译期间的问题(很难相信,因为它是使用终端历史记录中的最后一个命令编译的),还是在我执行它时出现的机器问题。或者如果这是gprof转换gmon.out文件的问题。
编辑 2
我在另一台从未用于开发的机器上执行了该程序(这是一台类似的Windows 10 Surface Pro)。它产生了gmon.out,但解析通过gprof时似乎没有结果。
编辑 3
我尝试了使用gcc和g++得到相同的结果。我尝试了分离编译和链接步骤或同时进行两个步骤。我也尝试过链接到-lgmon
或不链接。当gprof被解析时都会得到相同的空结果。
自从它随机工作以来,我就没有再次获得过结果。
编辑 4
我使用-g
和-pg
编译程序,然后通过VTune运行它。在热点分析中,我可以看到与分析器相关的函数调用。
这是整个被调用者堆栈。您可以看到与分析器相关的函数调用profthr_func
和get_thrpc
。
Callees CPU Time: Total CPU Time: Self
BaseThreadInitThunk 100.0% 0s
_tmainCRTStartup 99.1% 0s
main 99.1% 0.016s
func1 65.8% 20.521s
new_func1 32.8% 20.456s
func2 33.2% 20.695s
profthr_func 0.6% 0.009s
WaitForSingleObject 0.4% 0.231s
get_thrpc 0.2% 0s
SwitchToThread 0.2% 0.116s
TlsGetValue 0.0% 0.002s
GetLastError 0.0% 0.001s
SwitchToThread 0.0% 0.001s
GetLastError 0.0% 0.001s
[Outside any known module] 0.0% 0.001s
func@0x4b308730 0.0% 0.000s
TlsGetValue 0.0% 0.000s
get_thrpc 0.4% 0s
GetThreadContext 0.3% 0.214s
GetLastError 0.0% 0.006s
TlsGetValue 0.0% 0.000s
[Outside any known module] 0.0% 0.002s
SuspendThread 0.0% 0.001s
[Unknown stack frame(s)] 0.0% 0s
profthr_func 0.0% 0s
WaitForSingleObject 0.0% 0.000s
然后是所有的函数及其源代码等内容:
Function CPU Time: Total CPU Time: Self Module Function (Full) Source File Start Address
BaseThreadInitThunk 100.0% 0s kernel32.dll BaseThreadInitThunk [Unknown] 0x6b81fa10
func@0x4b2e7a03 100.0% 0s ntdll.dll func@0x4b2e7a03 [Unknown] 0x4b2e7a03
func@0x4b2e7a1f 100.0% 0s ntdll.dll func@0x4b2e7a1f [Unknown] 0x4b2e7a1f
_tmainCRTStartup 99.1% 0s CPPTESTS-d.exe _tmainCRTStartup crtexe.c 0x401170
main 99.1% 0.016s CPPTESTS-d.exe main main.cpp 0x40165c
func1 65.8% 20.521s CPPTESTS-d.exe func1(void) main.cpp 0x4015fd
func2 33.2% 20.695s CPPTESTS-d.exe func2 main.cpp 0x40162f
new_func1 32.8% 20.456s CPPTESTS-d.exe new_func1(void) main.cpp 0x4015d0
profthr_func 0.6% 0.009s CPPTESTS-d.exe profthr_func profil.c 0x408620
get_thrpc 0.5% 0s CPPTESTS-d.exe get_thrpc profil.c 0x4085c0
WaitForSingleObject 0.4% 0.231s KernelBase.dll WaitForSingleObject [Unknown] 0x10110440
GetThreadContext 0.3% 0.214s KernelBase.dll GetThreadContext [Unknown] 0x101b61a0
SwitchToThread 0.2% 0.116s KernelBase.dll SwitchToThread [Unknown] 0x100f5b40
GetLastError 0.0% 0.007s kernel32.dll GetLastError [Unknown] 0x6b81e010
[Outside any known module] 0.0% 0.002s [Unknown] [Outside any known module] [Unknown] 0
TlsGetValue 0.0% 0.002s KernelBase.dll TlsGetValue [Unknown] 0x101193a0
SuspendThread 0.0% 0.001s kernel32.dll SuspendThread [Unknown] 0x6b834e90
SwitchToThread 0.0% 0.001s kernel32.dll SwitchToThread [Unknown] 0x6b817cc0
GetLastError 0.0% 0.001s KernelBase.dll GetLastError [Unknown] 0x10110580
TlsGetValue 0.0% 0.000s kernel32.dll TlsGetValue [Unknown] 0x6b81df20
func@0x4b308730 0.0% 0.000s ntdll.dll func@0x4b308730 [Unknown] 0x4b308730
[Unknown stack frame(s)] 0.0% 0s [Unknown] [Unknown stack frame(s)] [Unknown] 0
我不确定这些信息是否对解决我的问题有帮助。gmon.out 文件再次经由 gprof 解析时没有任何信息。
我阅读了这个问题的答案,链接如下:https://dev59.com/H2445IYBdhLWcg3wNXk_#5046039,所以我预期调用
main
、func1
、func2
和 new_func1
应该包含某种形式的工具函数?我运行了 VTune 并检查了线程选项,并注意到 gprof 似乎通过一个新线程来执行它的任务,这与我上面的理解有所不同。然而,它并不能解释为什么我曾经得到过一些输出,但现在无法重现。
但是,在 Vtune 中,我看不到对
mcount
的任何调用,我原以为那就是 gprof 的工作方式。它确实向我展示了一些碎片(正如你在上面的调用堆栈和函数列表中看到的那样),表明从 -pg
标志中进行性能分析的内容存在。EDIT 5:
我上传了我的编译程序和它的 gmon.out,如果有人能够帮忙回答这个问题的话,请查看链接:http://gamedevforums.com/CPPTESTS-d.exe 和 http://gamedevforums.com/gmon.out。
EDIT 6:
我重新安装了 MSYS2,希望能解决问题。但是没有起作用。
EDIT 7:
我尝试使用
-static -static-libgcc -static-libstdc++
进行链接,但没有任何效果。EDIT 8:
我使用
-d
标志执行了 gprof,并将调试输出放在了这个网站上:https://pastebin.com/yhHMxNLp。但对我来说它基本上只是一些无意义的东西。EDIT 9:
我直接在 MSYS2 外安装了 Mingw-Builds,并测试了版本 8.1.0,在 i686 和 86_64 版本中都使用了 Dwarf 和 SJLJ。在使用此版本的 G++ 进行编译和链接后,会生成一个 gmon.out 文件,但是 gprof 然后会给出以下错误(或者是数字的变化):
BFD: Dwarf Error: Could not find abbrev number 108.
但是生成的输出结果可用。我还检查了一个编译为8.1.0程序的gmon.out,新版本的MSYS2 gprof可以轻松解析,因此问题似乎出现在Mingw64的更新版本中。
Gprof报告版本2.30,G++报告版本8.1.0。
编辑10
我尝试在一个干净的Windows 7虚拟机上编译和运行gprof,并得到了相同的结果。因此,我不认为这是我的操作系统(Windows 10)或我的特定计算机的问题。
看起来这个问题在这里没有得到解答。如果我最终找到答案,我会回答这个问题。在此期间,我已经在MSYS2追踪器和Mingw64追踪器上创建了问题。
编辑11
我现在正在处理以下代码:
#include <iostream>
bool is_prime(const int& number)
{
if(number == 0 || number == 1)
return false;
else
{
for(int i = 2; i <= number / 2; ++i)
{
if(number % i == 0)
{
return false;
}
}
}
return true;
}
int main()
{
int low = 0;
int high = 300000;
while(low < high)
{
if(is_prime(low))
std::cout << low << ", ";
++low;
}
std::cout << std::endl;
return 0;
}
使用与之前相同的编译器标志:g++ -g -pg -O0 main.cpp -o CPPTESTS-d.exe
objdump -d CPPTESTS-d.exe
提供了以下来自 main
和 is_prime
部分的信息:
004015d0 <__Z8is_primeRKi>:
4015d0: 55 push %ebp
4015d1: 89 e5 mov %esp,%ebp
4015d3: 83 ec 10 sub $0x10,%esp
4015d6: e8 a5 15 00 00 call 402b80 <_mcount>
4015db: 8b 45 08 mov 0x8(%ebp),%eax
4015de: 8b 00 mov (%eax),%eax
4015e0: 85 c0 test %eax,%eax
4015e2: 74 0a je 4015ee <__Z8is_primeRKi+0x1e>
4015e4: 8b 45 08 mov 0x8(%ebp),%eax
4015e7: 8b 00 mov (%eax),%eax
4015e9: 83 f8 01 cmp $0x1,%eax
4015ec: 75 07 jne 4015f5 <__Z8is_primeRKi+0x25>
4015ee: b8 00 00 00 00 mov $0x0,%eax
4015f3: eb 3b jmp 401630 <__Z8is_primeRKi+0x60>
4015f5: c7 45 fc 02 00 00 00 movl $0x2,-0x4(%ebp)
4015fc: 8b 45 08 mov 0x8(%ebp),%eax
4015ff: 8b 00 mov (%eax),%eax
401601: 89 c2 mov %eax,%edx
401603: c1 ea 1f shr $0x1f,%edx
401606: 01 d0 add %edx,%eax
401608: d1 f8 sar %eax
40160a: 39 45 fc cmp %eax,-0x4(%ebp)
40160d: 7f 1c jg 40162b <__Z8is_primeRKi+0x5b>
40160f: 8b 45 08 mov 0x8(%ebp),%eax
401612: 8b 00 mov (%eax),%eax
401614: 99 cltd
401615: f7 7d fc idivl -0x4(%ebp)
401618: 89 d0 mov %edx,%eax
40161a: 85 c0 test %eax,%eax
40161c: 75 07 jne 401625 <__Z8is_primeRKi+0x55>
40161e: b8 00 00 00 00 mov $0x0,%eax
401623: eb 0b jmp 401630 <__Z8is_primeRKi+0x60>
401625: 83 45 fc 01 addl $0x1,-0x4(%ebp)
401629: eb d1 jmp 4015fc <__Z8is_primeRKi+0x2c>
40162b: b8 01 00 00 00 mov $0x1,%eax
401630: c9 leave
401631: c3 ret
00401632 <_main>:
401632: 8d 4c 24 04 lea 0x4(%esp),%ecx
401636: 83 e4 f0 and $0xfffffff0,%esp
401639: ff 71 fc push -0x4(%ecx)
40163c: 55 push %ebp
40163d: 89 e5 mov %esp,%ebp
40163f: 51 push %ecx
401640: 83 ec 24 sub $0x24,%esp
401643: e8 38 15 00 00 call 402b80 <_mcount>
401648: e8 73 1a 00 00 call 4030c0 <__monstartup>
40164d: e8 be 01 00 00 call 401810 <___main>
401652: c7 45 f0 00 00 00 00 movl $0x0,-0x10(%ebp)
401659: c7 45 f4 e0 93 04 00 movl $0x493e0,-0xc(%ebp)
401660: 8b 45 f0 mov -0x10(%ebp),%eax
401663: 39 45 f4 cmp %eax,-0xc(%ebp)
401666: 7e 3d jle 4016a5 <__fu0___ZSt4cout+0x27>
401668: 8d 45 f0 lea -0x10(%ebp),%eax
40166b: 89 04 24 mov %eax,(%esp)
40166e: e8 5d ff ff ff call 4015d0 <__Z8is_primeRKi>
401673: 84 c0 test %al,%al
401675: 74 23 je 40169a <__fu0___ZSt4cout+0x1c>
401677: 8b 45 f0 mov -0x10(%ebp),%eax
40167a: 89 04 24 mov %eax,(%esp)
40167d: b9 .byte 0xb9
我发现在两个地方的开头都调用了mcount
。因此看起来编译代码已经进行了适当的插桩,但是gprof中仍然没有输出。
gmon.out
。 - HolyBlackCat