Skip to main content

CFiddle makes it easy to ask and answers questions about the compilation and execution of smallish programs written in C or C++.

Project description

CFiddle: A Tool For Studying Small Compiled rograms

CircleCI Binder Documentation Status

CFiddle is a tool for studying the compilation and execution of smallish programs written in C or C++. If you want to know what the compiler does to your code and why your code is slow, CFiddle can help.

It 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 with Jupyter Notebook/Jupyter Lab to support interactive exploration.

It's features include:

  1. Support for compiled languages like C and C++.
  2. Control Flow Graph (CFG) generation from compiled code.
  3. Easy support for varying build-time and run-time paremeters.
  4. Easy, unified parameter and data gathering across building and running code.
  5. Works great with Pandas and Jupyter Notebook/Lab.

The best way to learn about CFiddle is to try it. You can run the examples (this can take a while to load).

Or run it locally with Docker:

docker run -it --publish published=8888,target=8888 stevenjswanson/cfiddle:latest jupyter lab --LabApp.token=''

and then visit http://localhost:8888/lab/tree/README.ipynb.

You can also read the documentation.

Examples

What Does a for loop look like in assembly?

>>> from cfiddle import * 
>>> 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: +SKIP
loop:
.LFB0:
    .cfi_startproc
    endbr64
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, -8(%rbp)
    movl    $0, -4(%rbp)
.L3:
    cmpl    $9, -4(%rbp)
    jg    .L2
    movl    -4(%rbp), %eax
    addl    %eax, -8(%rbp)
    addl    $1, -4(%rbp)
    jmp    .L3
.L2:
    movl    -8(%rbp), %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc

Or, if you prefer a CFG:

>>> build(sample)[0].cfg("loop", "readme_loop.png") 
'readme_loop.png'

CFG Example

What Does -O3 Do To That Loop?

>>> asm = build(sample, build_parameters=dict(OPTIMIZE="-O3"))[0].asm("loop")
>>> print(asm) # doctest: +SKIP
loop:
.LFB0:
    .cfi_startproc
	endbr64
	movl	$45, %eax
	ret
	.cfi_endproc
	

Local Installation

CFiddle depends on some system packages and python's wheel. Setup a virtual environment:

python -m venv cfiddle-venv

Become root, so you can install the systetm packages with apt-get with

sudo bash
. cfiddle-venv/bin/activate	
make install-prereqs
exit

Install cfiddle:

. cfiddle-venv/bin/activate
pip install .

Run the tests:

make test

Common Problems

CFiddle needs LD_LIBRARY_PATH set properly to work, and it can't set it itself reliably. If you get

OSError: libcfiddle.so: cannot open shared object file: No such file or directory

You can update LD_LIBRARY_PATH with:

$(set-cfiddle-ld-path)

Saving Your Work

If you want to save changes you make to any of the examples, you'll need to run docker something like this:

docker run -it --publish published=8888,target=8888 --mount type=bind,source=$HOME,dst=/home/jovyan -w /home/jovyan/cfiddle_work/cfiddle  stevenjswanson/cfiddle:latest  jupyter lab --LabApp.token=''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cfiddle-0.0.0.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cfiddle-0.0.0-py3-none-any.whl (509.0 kB view details)

Uploaded Python 3

File details

Details for the file cfiddle-0.0.0.tar.gz.

File metadata

  • Download URL: cfiddle-0.0.0.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for cfiddle-0.0.0.tar.gz
Algorithm Hash digest
SHA256 b7878e25016f7a385027dabf90f97b8e0d57e3b2bdcf0be956b4fe95082b4ec0
MD5 d7c6f77607c280dd6943282fde170766
BLAKE2b-256 3096e227e0291b03bc899cff43fccf63f3307b78128d8a37dfe48c83b4769438

See more details on using hashes here.

File details

Details for the file cfiddle-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: cfiddle-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 509.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for cfiddle-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aec574da7b0484abee3d839c17ed5e1b9be2d2ab91aa17727ecae4f396adae98
MD5 445b63506ad44dacb7bb3f4b7511d7f7
BLAKE2b-256 9072b14e3345a956f7b4e242b0d5af6c0eed3685d8e7d634aad03f4eed0dc459

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page