profiling FPC code under windows? lazarus / 34-Windows ---------------------------------------------------------------------- terryc · 2015-11-04 18:55 How does one profile (or measure test coverage) of FPC code under windows? I've tried: - valgrind (but it doesn't work under windows and valgrind for windows is still only a dream)... - very sleepy profiler, but it doesn't seem to understand the DWARF2 debug information in the exe, - gprof, but -pg doesn't work under FPC 3.x - gprof under FPC 2.6.4, but the exe doesn't  link since there are missing libraries    i downloaded and installed cygwin64, got the libraries, but still there are missing symbols: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Free Pascal Compiler version 2.6.4 [2015/04/18] for i386Copyright (c) 1993-2014 by Florian Klaempfl and othersTarget OS: Win32 for i386Compiling SimpleProgram.lprLinking SimpleProgram.exeSimpleProgram.lpr(5,1) Error: Undefined symbol: __mcleanupSimpleProgram.lpr(5,1) Error: Undefined symbol: _monstartupSimpleProgram.lpr(5,1) Error: Undefined symbol: ___mainSimpleProgram.lpr(5,1) Error: Undefined symbol: _cygwin_crt0SimpleProgram.lpr(5,1) Fatal: There were 4 errors compiling module, stoppingFatal: Compilation aborted I tried a very simple program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program SimpleProgram;begin  Writeln('Hello World!');end. and compiled with the following: fpc -FlCygwin64\lib -FlCygwin64\lib\gcc\x86_64-pc-cygwin\4.9.3 -FlCygwin64\lib\w32api -pg SimpleProgram.lpr Has someone been able to profile FPC code under Windows? Is a special tool needed? Is there a way to remove those missing symbols? Thanks for your help. ---------------------------------------------------------------------- marcov · 2015-11-04 19:37 Quote from: terrycFree Pascal Compiler version 2.6.4 [2015/04/18] for i386 Quote from: terryci downloaded and installed cygwin64 This can't work. The i386 linker will simply skip the 64-bit files, and then symbols are missing. I'm not sure if it would have worked otherwise, but this violates the first rule, namely, make sure the various parts of a program belong together. ---------------------------------------------------------------------- terryc · 2015-11-05 05:17 Thanks for finding the discrepancy between win64 and i386. However, the compiler is loading symbols, see output of FPC with option -va: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [0.841] Opening library C:\Cygwin64\lib\libc.a[0.851] Opening library C:\Cygwin64\lib\libgmon.a[0.854] Opening library C:\Cygwin64\lib\libcygwin.a[0.884] Opening library C:\Cygwin64\lib\w32api\libuser32.a[0.901] Opening library C:\Cygwin64\lib\w32api\libkernel32.a[0.934] Opening library C:\Cygwin64\lib\gcc\x86_64-pc-cygwin\4.9.3\libgcc.a[1.071] Number of unresolved externals in objects 76[1.071] Number of unresolved externals after static libraries 76[1.071] Number of unresolved externals after defining COMMON symbols 76[1.074] Number of unresolved externals after DLL imports 4[1.074] Undefined symbol: __mcleanup[1.074] Undefined symbol: _monstartup[1.074] Undefined symbol: ___main[1.074] Undefined symbol: _cygwin_crt0[1.079] There were 4 errors compiling module, stopping[1.081] Compilation abortedParticularly __main, i would have expected the Pascal program to provide the __main symbol. On the other hand, using the FPC cross-compiler for Win64 leads to the following results: Code: [Select]Free Pascal Compiler version 2.6.4 [2014/03/06] for x86_64 Copyright (c) 1993-2014 by Florian Klaempfl and others ppcrossX64 -pg SimpleProgram.lpr Fatal: Option "-pg" is not, or not yet, supported on the current target platform Fatal: Compilation aborted Any other ideas? ---------------------------------------------------------------------- marcov · 2015-11-05 10:49 I tried using -pg on 3.1.1, but my (old) cygwin install missed gcc and winapi, so I got Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Free Pascal Compiler version 3.1.1 [2015/09/04] for i386Copyright (c) 1993-2015 by Florian Klaempfl and othersNote: Switching assembler to default source writing assemblerTarget OS: Win32 for i386Compiling hw.ppAssembling programLinking hw.exehw.pp(2,22) Error: Import library not found for user32hw.pp(2,22) Error: Import library not found for kernel32hw.pp(2,22) Error: Import library not found for gcchw.pp(2,22) Fatal: There were 3 errors compiling module, stoppingFatal: Compilation abortedError: d:\pp32\bin\i386-win32\ppc386.exe returned an error exitcode  Note, you might also want to experiment with -Xe which calls the external (ld) linker instead of FPC's internal. I can't comment on 2.6.x, I haven't used it in years for something other than bootstrapping 3.x ---------------------------------------------------------------------- terryc · 2015-11-05 16:24 Thanks Marcov, i'll download the FPC 3.x.x 32 bit and try again. The official user manual for FPC states using gprof with MinGW, i'll try that. Cygwin is not possible, since it's only 64 bit now. The problem is that the latest MinGW doesn't seem to have a C library... Have you used gprof with FPC on linux? Does it work there? Best regards, Thierry ---------------------------------------------------------------------- terryc · 2015-11-07 08:14 I've tried 3 differents ways: 1°) With FPC 3.1.1 on MinGW: Code: [Select]fpc -FlMinGW\mingw32\lib\gcc\mingw32\4.8.1 -FlMinGW\lib -pg SimpleProgram.lpr SimpleProgram.lpr(5,1) Error: Import library not found for c SimpleProgram.lpr(5,1) Error: Import library not found for cygwin SimpleProgram.lpr(5,1) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted Error: ppc386.exe returned an error exitcode MinGW does not provide a libC nor libCygwin. This result is in contraduction with FPC's FAQ which states FPC's gprof should work on Windows with MinGW:  http://www.freepascal.org/faq.var#win-profile 2°) FPC 3.1.1 with Cygwin. I downloaded the latest 32-bit cygwin and compiled the simple program with FPC 3.1.1: Code: [Select]fpc -Fle:\cygwin\lib -FlE:\cygwin\lib\w32api -FlE:\cygwin\lib\gcc\i686-pc-cygw in\4.9.3 -pg SimpleProgram.lpr SimpleProgram.lpr(5,1) Error: Failed reading coff file, invalid section index wh ile reading E:\cygwin\lib\libcygwin.a(_cygwin_crt0_common.o) Fatal: Compilation aborted An unhandled exception occurred at $005895C4: EAccessViolation: Access violation   $005895C4   $0044D408   $0044CE56   $004426B4   $00442B02   $004335B7   $00413F39 Error: ppc386.exe returned an error exitcode Apparently, FPC has a problem reading the latest cygwin compiled files to link the program. 3°) So i tried FPC with cygwin and external link (-Xe): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fpc -Xe -Fle:\cygwin\lib -FlE:\cygwin\lib\w32api -FlE:\cygwin\lib\gcc\i686-pc- cygwin\4.9.3 -pg SimpleProgram.lprfpc\fpc32\units\i386- win32\rtl\sysinitgprof.o:sysinitgprof.pp:(.text.n_sysinitgprof_$$_exeg mon_start+0x1a): undefined reference to `_text_start__' fpc\fpc32\unit s\i386- win32\rtl\sysinitgprof.o:sysinitgprof.pp:(.text.n_sysinitgprof_$$_dllg mon_start+0x1a): undefined reference to `_text_start__' fpc\fpc32\bin\i386-win32\ld.exe: C:\codetyphon\fpc\fpc32\units\i386-win32\rtl\sysinitgprof.o: bad reloc address 0x1a in section `.text.n_sysinitgprof_$$_dllgmon_start'fpc\fpc 32\bin\i386-win32\ld.exe: final link failed: Invalid operationSimpleProgram.lpr(5,1) Error: Error while linkingSimpleProgram.lpr(5,1) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: ppc386.exe returned an error exitcode This time, it's the cygwin linker which is unhappy with the structure and contents of FPC's sysinitgprof. Has someone been able to use gprof on windows recently with FPC 3.1.1? Should i report this as a bug? ---------------------------------------------------------------------- terryc · 2015-11-07 08:28 Naturally, i have checked the cygwin install, with this piece of code in c : Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i /* Hello World program */ #include subHello(int i){   printf("Hello World ", i);}; main(){  int i;  for(i = 0; i<1000; i++)  {    subHello(i);  }; }  Compiled and executed: Code: [Select]gcc -Wall -pg HelloWorld.c -o Test_HelloWorld Test_HelloWorld gprof test_HelloWorld gmon.out > analysis.txt I get the following results in analysis.txt: Code: [Select]Flat profile: Each sample counts as 0.01 seconds.  no time accumulated   %   cumulative   self              self     total  time   seconds   seconds    calls  Ts/call  Ts/call  name   0.00      0.00     0.00     1000     0.00     0.00  subHello  %         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. so gcc works, gprof works, so now i just need to get FPC to work with gprof. ---------------------------------------------------------------------- marcov · 2015-11-07 12:39 I don't know what the exact status of gprof on windows is. Most tools used to be cygwin, but converted to mingw over time. Maybe it was done incompletely for gprof. Probably is that both (cygwin and mingw) evolve over time too, and that is noticable in these lesser used areas. So don't expect to find a magic combination to just work, you might need to understand the situation a bit. If you go with mingw (which I would), try to build you own list of libraries (eradicate old cygwin and c $linklibs from the sources that added those dependencies). It seems that mingw links to msvcrt nowadays. ---------------------------------------------------------------------- Thaddy · 2015-11-07 15:31 Can you explain mingw over cygwin? I never understood that choice in general. I am not biased, simply asking. ---------------------------------------------------------------------- marcov · 2015-11-07 16:07 Quote from: Thaddy on November 07, 2015, 04:31:23 pmCan you explain mingw over cygwin? I never understood that choice in general. I am not biased, simply asking. mingw is more Windows then cygwin, and integrates better with other programs. In the past most parts of mingw (and generated files) also were standalone applications, I heard they recently ventured a bit off that path. But in the past cygwin was further along as mingw, so cygwin was used. ---------------------------------------------------------------------- terryc · 2015-12-09 14:15 Haven't been able to get FPC to work with gprof on windows. Until I get a linux box running, this is temporarily stopped. I don't have enough knowledge of low-level C nor enough motivation to go into this. I used fpprofile with the -gw option enabled so that it could read the stackframes. That works. However fpprofile doesn't go down to line coverage, only procedure coverage, which is sufficient for my needs. And of course it modifies the source code.