Skip to content

Commit

Permalink
apex launcher done
Browse files Browse the repository at this point in the history
  • Loading branch information
examon committed Dec 5, 2018
1 parent 098b57b commit 64bb573
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 462 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
build:
# Created by Tomas Meszaros (exo at tty dot com, tmeszaro at redhat dot com)
#
# Published under Apache 2.0 license.
# See LICENSE for details.

# Makefile for building APEXPass and generating documentation
# run: $ make build, to build APEXPass

build: clean
cd src; \
./build.sh;

Expand Down
74 changes: 58 additions & 16 deletions apex.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,85 @@
#!/usr/bin/env python3

# Created by Tomas Meszaros (exo at tty dot com, tmeszaro at redhat dot com)
#
# Published under Apache 2.0 license.
# See LICENSE for details.

"""
python3 apex.py examples/mod1/example_mod1.bc example_mod1.c 11 --export=true
"""

import argparse
import shlex
import subprocess
import sys

def execute(cmd, print_output=True):
def execute(cmd, print_output=False):
try:
out = subprocess.check_output(cmd, shell=True)
print(out.decode("utf-8"))
if print_output:
print(out.decode("utf-8"))
except subprocess.CalledProcessError as e:
sys.stderr.write(str(e) + '\n')
return None

# Config cmd line args.
parser = argparse.ArgumentParser()
parser.add_argument("code", type=str, help="C source code compiled into LLVM bytecode.")
parser.add_argument("file", type=str, help="Target file name (NOT FULL PATH).")
parser.add_argument("line", type=int, help="Target line number.")
parser.add_argument("--export", type=str, help="true/false for exporting call graphs.")

parser.add_argument('--code', type=str, help='input C source code compiled into LLVM bytecode')
parser.add_argument('--file', type=str, help='file where is the target (NOT FULL PATH)')
parser.add_argument('--line', type=int, help='target line number in file')


# Parse cmd line args.
args = parser.parse_args()
code = args.code
target_file = args.file
line = args.line
export = args.export

print("code", code)
print("target_file", target_file)
print("line", line)
execute("rm -rf build; mkdir build")

# Compile apexlib to the bytecode.
execute("clang -O0 -g -c -emit-llvm src/apex/apexlib.c -o build/apexlib.bc")

execute("rm -rf build; mkdir build")
execute("clang -O0 -g -c -emit-llvm src/apex/apexlib.c -o build/bytecode_from_apexlib.bc")
execute("llvm-link build/bytecode_from_apexlib.bc {INPUT} -S -o=build/linked_ir.ll".format(INPUT=code))
execute("llvm-as build/linked_ir.ll -o build/bytecode_from_linked_input.bc")
# Link @code with apexlib bytecode.
execute("llvm-link build/apexlib.bc {INPUT} -S -o=build/linked.ll".format(INPUT=code))

# Procedure bytecode from linked input and apexlib.
execute("llvm-as build/linked.ll -o build/linked.bc")

opt = """opt -o build/bytecode_from_apex.bc -load src/build/apex/libAPEXPass.so -apex -file={FILE} -line={LINE} < build/bytecode_from_linked_input.bc 2> build/apex.out
# Run APEXPass on the linked bytecode we produced above.
# Save log to the build/apex.log.
opt = """opt -o build/apex.bc -load src/build/apex/libAPEXPass.so -apex -file={FILE} -line={LINE} < build/linked.bc 2> build/apex.log
""".format(FILE=target_file, LINE=line)
print(opt)
execute(opt)

# Disassembly apexlib and final extracted bytecode for dbg & logging purposes.
execute("llvm-dis build/apexlib.bc -o build/apexlib.ll")
execute("llvm-dis build/apex.bc -o build/apex.ll")

# Run final extracted bytecode and save the result to the build/apex.out.
execute("lli build/apex.bc > build/apex.out")
execute("cat build/apex.out", print_output=True)


# Optional call graphs export
if export and export == "true":
execute("rm -rf build/callgraphs; mkdir build/callgraphs")
execute("opt -dot-callgraph {INPUT} > /dev/null".format(INPUT=code))
execute("mv callgraph.dot callgraph_no_opt.dot")
execute("dot callgraph_no_opt.dot -Tsvg -O")
execute("rm -rf callgraph_no_opt.dot")
execute("mv callgraph_no_opt.dot.svg build/callgraphs")

execute("opt -dot-callgraph build/linked.bc > /dev/null")
execute("mv callgraph.dot callgraph_linked.dot")
execute("dot callgraph_linked.dot -Tsvg -O")
execute("rm -rf callgraph_linked.dot")
execute("mv callgraph_linked.dot.svg build/callgraphs")

execute("opt -dot-callgraph build/apex.bc > /dev/null")
execute("mv callgraph.dot callgraph_apex.dot")
execute("dot callgraph_apex.dot -Tsvg -O")
execute("rm -rf callgraph_apex.dot")
execute("mv callgraph_apex.dot.svg build/callgraphs")
2 changes: 1 addition & 1 deletion examples/mod1/example_mod1.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ int foo(int n) {
}

int qux(void) {
return 42;
return 42;
}

int bar(void) {
Expand Down
1 change: 1 addition & 0 deletions src/apex/apex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// - target only non global int

// TODO: user I.getprevnode() or I.getnextnode() to for injecting
// TODO: remove unused includes

#include "apex.h"

Expand Down
20 changes: 5 additions & 15 deletions src/apex/apex.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#pragma once

#include <list>
#include <map>
#include <string>
#include <vector>
//#include <list>
//#include <map>
//#include <string>
//#include <vector>

#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
Expand Down Expand Up @@ -56,19 +56,9 @@ std::vector<std::string> PROTECTED_FCNS = {

// These belong to the _apex_extract_int function.
"_apex_extract_int",
"abs",
"log10",
"floor",
"fopen",
"fputs",
"fclose",
"exit",
"sprintf",

// Keep printf calls.
"printf",

// Introduced with debug symbols.
// LLVM stuff
"llvm.dbg.declare",
"llvm.stackrestore",
"llvm.stacksave",
Expand Down
9 changes: 6 additions & 3 deletions src/apex/apexlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#include <stdlib.h>
#include <math.h>

#define APEX_OUTPUT "apex.out"

/// Function used for getting out of the program, usually after calling @_apex_extract_int().
void _apex_exit(int exit_code) {
exit(exit_code);
}

/// Function used for dumping value @i to file @APEX_OUTPUT.
/// Function used for extracting value %i.
void _apex_extract_int(int i) {
printf("%d", i);
}
/*
void _apex_extract_int(int i) {
// Figure out number of digits @i has,
// so we can allocate big enough buffer.
Expand All @@ -36,3 +38,4 @@ void _apex_extract_int(int i) {
printf("Error: Could not save to file!\n");
}
}
*/
17 changes: 0 additions & 17 deletions src/c-code/example.c

This file was deleted.

21 changes: 0 additions & 21 deletions src/c-code/example_mod1.c

This file was deleted.

23 changes: 0 additions & 23 deletions src/c-code/example_mod2.c

This file was deleted.

32 changes: 0 additions & 32 deletions src/c-code/test.c

This file was deleted.

42 changes: 0 additions & 42 deletions src/c-code/test_dependencies_minimal.c

This file was deleted.

Loading

0 comments on commit 64bb573

Please sign in to comment.