Bug: -pg not working
medium severity
### Contact Details
brad@bradhutchings.com
### What happened?
`-pg` is supposed to instruct gcc compiler and linker to generate a `gmon.out` file containing execution timings that can be processed by `gprof` to profile your app.
I have not gotten it to work with `cosmocc` (and platform specific tools) while building [llamafile](https://github.com/Mozilla-Ocho/llamafile) or [Mmojo Server](https://github.com/BradHutchings/Mmojo-Server). After digging into Cosmo source to see if I could figure out what was going on, I made a simple example based on the instructions in the main [README.md](https://github.com/jart/cosmopolitan/blob/master/README.md).
Start by downloading the latest release:
```
mkdir -p cosmocc
cd cosmocc
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
unzip cosmocc.zip
```
Let's make a c++ app that runs long enough to generate a good `gmon.out` file:
```
cat << EOF > hello.cpp
#include <iostream>
#include <thread> // For std::this_thread::sleep_for
#include <chrono> // For std::chrono::seconds, milliseconds, etc.
int main() {
std::cout << "Starting..." << std::endl;
// Sleep for 2 seconds
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Resuming after 2 seconds." << std::endl;
// Sleep for 500 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "Resuming after 500 milliseconds." << std::endl;
return 0;
}
EOF
```
Let's compile it with the `-pg` flag:
```
g++ -o hello_cpp hello.cpp -pg
```
Run it:
```
./hello_cpp
```
Verify that `gmon.out` was generated:
```
ls -al gmon.out
```
Delete everything we built:
```
rm hello_cpp* gmon.out
```
Set the $BUILDLOG variable to current directory and build with `cosmocc`:
```
BUILDLOG=$(pwd)/build.log
bin/cosmocc -o hello_cpp hello.cpp -Iinclude/third_party/libcxx -pg
```
Verify that `-pg` was passed to build commands:
```
grep "\-pg" build.log
```
Run it:
```
./hello_cpp
```
Verify that `gmon.out` **was not** generated:
```
ls -al gmon.out
```
### Version
cosmocc (GCC) 14.1.0
### What operating system are you seeing the problem on?
Linux
### Relevant log output
```shell
`build.log` shows `cosmocc` passing `-pg` along.
mmojo@mmojo:~/cosmocc $ grep "\-pg" build.log
# bin/cosmocc -o hello_cpp hello.cpp -Iinclude/third_party/libcxx -pg
(cd /home/mmojo/cosmocc; bin/x86_64-linux-cosmo-gcc -o/tmp/fatcosmocc.5dz0b8pewwvi3.o -D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__ -include libc/integral/normalize.inc -fportcosmo -fno-semantic-interposition -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer -fno-schedule-insns2 -Wno-implicit-int -mno-tls-direct-seg-refs -fpatchable-function-entry=18,16 -fno-inline-functions-called-once -DFTRACE -DSYSDEBUG -fno-pie -nostdinc -isystem bin/../include -Iinclude/third_party/libcxx -pg -c hello.cpp -fno-omit-frame-pointer)
(cd /home/mmojo/cosmocc; bin/aarch64-linux-cosmo-gcc -o/tmp/fatcosmocc.7cgk59iyxay31.o -D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__ -include libc/integral/normalize.inc -fportcosmo -fno-semantic-interposition -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer -fno-schedule-insns2 -Wno-implicit-int -ffixed-x18 -ffixed-x28 -fpatchable-function-entry=7,6 -fno-inline-functions-called-once -DFTRACE -DSYSDEBUG -fno-pie -nostdinc -isystem bin/../include -fsigned-char -Iinclude/third_party/libcxx -pg -c hello.cpp -fno-omit-frame-pointer)
(cd /home/mmojo/cosmocc; bin/x86_64-linux-cosmo-gcc -o/tmp/fatcosmocc.qj1z2n0q94fj0.com.dbg bin/../x86_64-linux-cosmo/lib/ape.o bin/../x86_64-linux-cosmo/lib/crt.o -static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelro -Wl,--gc-sections -Lbin/../x86_64-linux-cosmo/lib -Lbin/../x86_64-linux-cosmo/lib -Wl,-T,bin/../x86_64-linux-cosmo/lib/ape.lds -Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=16384 /tmp/fatcosmocc.5dz0b8pewwvi3.o -Iinclude/third_party/libcxx -pg -lcosmo)
(cd /home/mmojo/cosmocc; bin/aarch64-linux-cosmo-gcc -o/tmp/fatcosmocc.hdxwmenicjw91.aarch64.elf bin/../aarch64-linux-cosmo/lib/crt.o -static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelro -Wl,--gc-sections -Lbin/../aarch64-linux-cosmo/lib -Lbin/../aarch64-linux-cosmo/lib -Wl,-T,bin/../aarch64-linux-cosmo/lib/aarch64.lds -Wl,-z,common-page-size=16384 -Wl,-z,max-page-size=16384 /tmp/fatcosmocc.7cgk59iyxay31.o -Iinclude/third_party/libcxx -pg -lcosmo)
```
0 条评论