Package: cfiddle
CFiddle: A Tool For Studying Small Compiled ProgramsCFiddle is a tool for studying the compilation, execution, and performance of
smallish programs written in compiled languages like C, C++, or Go.  If you
want to know what the compiler does to your code, how your code interacts with
hardware, and why it's slow, CFiddle can help.CFiddle makes it easy to ask and answer interesting questions about what happens to
programs as they go from source code to running program.  CFiddle can run on
its own, but it is built to work withJupyter Notebook/Jupyter
Labto support interactive exploration.  It provides
first-class support for accessing hardware performance counters.It's features include:Support for compiled languages like C, C++, and Go.Multiple architecture (x86, ARM, PowerPC, etc.) and compiler (gccandclang) support.Easy access to OS and hardware performance counters.Control Flow Graph (CFG)generation from compiled code.Easy support for varying build-time and run-time paremeters.Easy, unified parameter and data gathering across building and running code.Works great withPandasandJupyter Notebook/Lab.Trying CFiddleThe best way to learn about CFiddle is to try it.  There are several ways to do this:Run the
examples on Binder.org(this can take a while to load, and performance counters don't work.).You visithttps://try-cfiddle.nvsl.io(to try CFiddle on x86).  Performance counters are fully functionaly.Run it locally with Docker (--privilegedis required to access the performance counters):docker run -it --privileged --publish published=8889,target=8888 stevenjswanson/cfiddle:latest jupyter lab --LabApp.token='sesame'and then visithttp://localhost:8889/lab/tree/README.ipynb-- the token is "sesame".DocumentationHere'sthe documentation.ExamplesWhat Does aforloop look like in assembly?>>>fromcfiddleimport*>>>sample=code(r"""...    extern "C"...    int loop() {...    int sum = 0;...	   for(int i = 0; i < 10; i++) {... 		sum += i;...    }...	   return sum;... }... """)>>>asm=build(sample)[0].asm("loop")>>>print(asm)# doctest: +SKIPloop:.LFB0:.cfi_startprocendbr64pushq%rbp.cfi_def_cfa_offset16.cfi_offset6,-16movq%rsp,%rbp.cfi_def_cfa_register6movl$0,-8(%rbp)movl$0,-4(%rbp).L3:cmpl$9,-4(%rbp)jg.L2movl-4(%rbp),%eaxaddl%eax,-8(%rbp)addl$1,-4(%rbp)jmp.L3.L2:movl-8(%rbp),%eaxpopq%rbp.cfi_def_cfa7,8ret.cfi_endprocOr, if you prefer a CFG:>>> build(sample)[0].cfg("loop", "readme_loop.png") 
'readme_loop.png'What Does-O3Do To That Loop?>>>asm=build(sample,build_parameters=arg_map(OPTIMIZE="-O3"))[0].asm("loop")>>>print(asm)# doctest: +SKIPloop:.LFB0:.cfi_startprocendbr64movl$45,%eaxret.cfi_endprocLocal InstallationCFiddle depends on some system packages and python'swheel.  Setup a virtual environment:python -m venv cfiddle-venvSo you can install the system packages CFiddle needs.  Checkinstall_prereqs.shto see what this includes.  It usesapt-get.sudo bash
. cfiddle-venv/bin/activate	
# export CFIDDLE_INSTALL_CROSS_COMPILERS=yes # uncomment if you want all the cross compilers
./install_prereqs.sh
exitInstall cfiddle:. cfiddle-venv/bin/activate
pip install .Run the tests:make testCommon ProblemsCFiddle needsLD_LIBRARY_PATHset properly to work, and it can't set it itself reliably.  If you getOSError: libcfiddle.so: cannot open shared object file: No such file or directoryYou can updateLD_LIBRARY_PATHwith:$(set-cfiddle-ld-path)Saving Your WorkIf you want to save changes you make to any of the examples, you'll need to run docker something like this (this assumes you're in the root of yourcfiddledirectory):docker run -it -d --publish published=8888,target=8888 --mount type=bind,source=$PWD,dst=/home/jovyan/cfiddle -w /home/jovyan/  stevenjswanson/cfiddle:devel  jupyter lab --LabApp.token=''