diff --git a/python/tools/recorded-call-graph-metrics/.gitignore b/python/tools/recorded-call-graph-metrics/.gitignore index cbdb6353fad..5d5e32913d8 100644 --- a/python/tools/recorded-call-graph-metrics/.gitignore +++ b/python/tools/recorded-call-graph-metrics/.gitignore @@ -1,8 +1,9 @@ # Example DB cg-trace-example-db/ -# Examples traces should be ignored in general -example-traces/ +# Tests artifacts +tests/python-traces/ +tests/cg-trace-test-db # Artifact from building `pip install -e .` src/cg_trace.egg-info/ diff --git a/python/tools/recorded-call-graph-metrics/README.md b/python/tools/recorded-call-graph-metrics/README.md index dc121091c19..646416b309d 100644 --- a/python/tools/recorded-call-graph-metrics/README.md +++ b/python/tools/recorded-call-graph-metrics/README.md @@ -4,17 +4,15 @@ also known as _call graph tracing_. Execute a python program and for each call being made, record the call and callee. This allows us to compare call graph resolution from static analysis with actual data -- that is, can we statically determine the target of each actual call correctly. -This is still in the early stages, and currently only supports a very minimal working example (to show that this approach might work). - -The next hurdle is being able to handle multiple calls on the same line, such as - -- `foo(); bar()` -- `foo(bar())` -- `foo().bar()` - ## How do I give it a spin? -After following setup instructions below, run the `recreate-db.sh` script to create the database `cg-trace-example-db`. Then run the queries inside the `ql/` directory. +After following setup instructions below, you should be able to reproduce the example trace by running + +``` +cg-trace --xml example/simple.xml example/simple.py +``` + +You can also run traces for all tests and build a database by running `tests/create-test-db.sh`. Then run the queries inside the `ql/` directory. ## Setup diff --git a/python/tools/recorded-call-graph-metrics/example-traces/simple.xml b/python/tools/recorded-call-graph-metrics/example/simple.xml similarity index 97% rename from python/tools/recorded-call-graph-metrics/example-traces/simple.xml rename to python/tools/recorded-call-graph-metrics/example/simple.xml index b6e21496f97..a4562cce038 100644 --- a/python/tools/recorded-call-graph-metrics/example-traces/simple.xml +++ b/python/tools/recorded-call-graph-metrics/example/simple.xml @@ -1,10 +1,10 @@ 0.0.2 - --xml example-traces/simple.xml example/simple.py + --xml example/simple.xml example/simple.py completed 0.00 seconds - 2020-07-20T12:02:56 + 2020-07-22T12:14:02 diff --git a/python/tools/recorded-call-graph-metrics/recreate-db.sh b/python/tools/recorded-call-graph-metrics/tests/create-test-db.sh similarity index 52% rename from python/tools/recorded-call-graph-metrics/recreate-db.sh rename to python/tools/recorded-call-graph-metrics/tests/create-test-db.sh index 07f7fe7fcf1..18988e5e5f2 100755 --- a/python/tools/recorded-call-graph-metrics/recreate-db.sh +++ b/python/tools/recorded-call-graph-metrics/tests/create-test-db.sh @@ -1,31 +1,32 @@ #!/bin/bash -set -e -set -x +set -Eeuo pipefail # see https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ -if ! pip show cg_trace; then +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +if ! pip show cg_trace &>/dev/null; then echo "You need to follow setup instructions in README" exit 1 fi -DB="cg-trace-example-db" -SRC="example/" -XMLDIR="example-traces" +DB="$SCRIPTDIR/cg-trace-test-db" +SRC="$SCRIPTDIR/python-src/" +XMLDIR="$SCRIPTDIR/python-traces/" PYTHON_EXTRACTOR=$(codeql resolve extractor --language=python) - -cg-trace --xml "$XMLDIR"/simple.xml example/simple.py -cg-trace --xml "$XMLDIR"/builtins.xml example/builtins.py -cg-trace --xml "$XMLDIR"/multiple-on-one-line.xml example/multiple-on-one-line.py -cg-trace --xml "$XMLDIR"/class-simple.xml example/class-simple.py - - rm -rf "$DB" +rm -rf "$XMLDIR" + +mkdir -p "$XMLDIR" + +for f in $(ls $SRC); do + echo "Tracing $f" + cg-trace --xml "$XMLDIR/${f%.py}.xml" "$SRC/$f" +done codeql database init --source-root="$SRC" --language=python "$DB" codeql database trace-command --working-dir="$SRC" "$DB" "$PYTHON_EXTRACTOR/tools/autobuild.sh" codeql database index-files --language xml --include-extension .xml --working-dir="$XMLDIR" "$DB" codeql database finalize "$DB" -set +x echo "Created database '$DB'" diff --git a/python/tools/recorded-call-graph-metrics/example/BUILD_LIST.py b/python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_LIST.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/BUILD_LIST.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_LIST.py diff --git a/python/tools/recorded-call-graph-metrics/example/BUILD_TUPLE.py b/python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_TUPLE.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/BUILD_TUPLE.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/BUILD_TUPLE.py diff --git a/python/tools/recorded-call-graph-metrics/example/CALL_FUNCTION_EX.py b/python/tools/recorded-call-graph-metrics/tests/python-src/CALL_FUNCTION_EX.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/CALL_FUNCTION_EX.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/CALL_FUNCTION_EX.py diff --git a/python/tools/recorded-call-graph-metrics/example/__getitem__.py b/python/tools/recorded-call-graph-metrics/tests/python-src/__getitem__.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/__getitem__.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/__getitem__.py diff --git a/python/tools/recorded-call-graph-metrics/example/builtins.py b/python/tools/recorded-call-graph-metrics/tests/python-src/builtins.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/builtins.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/builtins.py diff --git a/python/tools/recorded-call-graph-metrics/example/class-simple.py b/python/tools/recorded-call-graph-metrics/tests/python-src/class-simple.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/class-simple.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/class-simple.py diff --git a/python/tools/recorded-call-graph-metrics/example/iteration.py b/python/tools/recorded-call-graph-metrics/tests/python-src/iteration.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/iteration.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/iteration.py diff --git a/python/tools/recorded-call-graph-metrics/example/multiple-on-one-line.py b/python/tools/recorded-call-graph-metrics/tests/python-src/multiple-on-one-line.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/multiple-on-one-line.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/multiple-on-one-line.py diff --git a/python/tools/recorded-call-graph-metrics/example/problem-1.py b/python/tools/recorded-call-graph-metrics/tests/python-src/problem-1.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/problem-1.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/problem-1.py diff --git a/python/tools/recorded-call-graph-metrics/example/problem-2.py b/python/tools/recorded-call-graph-metrics/tests/python-src/problem-2.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/problem-2.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/problem-2.py diff --git a/python/tools/recorded-call-graph-metrics/tests/python-src/simple.py b/python/tools/recorded-call-graph-metrics/tests/python-src/simple.py new file mode 100644 index 00000000000..626d402cb20 --- /dev/null +++ b/python/tools/recorded-call-graph-metrics/tests/python-src/simple.py @@ -0,0 +1,10 @@ +def foo(): + print('foo') + +def bar(): + print('bar') + +foo() +bar() + +foo(); bar() diff --git a/python/tools/recorded-call-graph-metrics/example/with-exit.py b/python/tools/recorded-call-graph-metrics/tests/python-src/with-exit.py similarity index 100% rename from python/tools/recorded-call-graph-metrics/example/with-exit.py rename to python/tools/recorded-call-graph-metrics/tests/python-src/with-exit.py