Skip to content

Commit

Permalink
Merge pull request #2 from compspec/add/dwarf-example
Browse files Browse the repository at this point in the history
adding simple callsite/inline example
  • Loading branch information
vsoch committed May 10, 2022
2 parents fa1c06b + e975357 commit 3e050e4
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/basic-diff/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def main():
c.run()

# We can also provide an example logic program!
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
print(res)


Expand Down
5 changes: 3 additions & 2 deletions examples/basic-graph/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

here = os.path.dirname(os.path.abspath(__file__))


def main():

A = Graph()
Expand Down Expand Up @@ -36,8 +37,8 @@ def main():
c.run()

# We can also provide an example logic program!
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
print(res)


Expand Down
6 changes: 4 additions & 2 deletions examples/combine-graphs/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

here = os.path.dirname(os.path.abspath(__file__))


def main():

# Create graphs A and B
Expand Down Expand Up @@ -55,9 +56,10 @@ def main():
c.run()

# We can also provide an example logic program!
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
lp = os.path.join(here, "show-uniques.lp")
res = c.run(lp)
print(res)


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions examples/dwarf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ all: libmath
libmath:
$(MAKE) -C lib/libmath/v1
$(MAKE) -C lib/libmath/v2

libcallsite:
$(MAKE) -C lib/libcallsite/v1
$(MAKE) -C lib/libcallsite/v2
42 changes: 42 additions & 0 deletions examples/dwarf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,47 @@ And then run the examples!
$ python libmath-example.py
```

Or an inline function:

```bash
$ python callsite-example.py
```
```
{
"is_a": [
[
"libcallsite.v1.so"
]
],
"is_b": [
[
"libcallsite.v2.so"
]
],
"is_different": [
[
"libcallsite.v1.so",
"libcallsite.v2.so"
]
],
"removed_node": [
[
"libcallsite.v1.so",
"libcallsite.v2.so",
"function",
"inline_that"
]
],
"added_node": [
[
"libcallsite.v1.so",
"libcallsite.v2.so",
"function",
"inline_this"
]
]
}
```

**more coming soon!** @vsoch will be adding more simple examples to parse different
DWARF information entries.
31 changes: 31 additions & 0 deletions examples/dwarf/callsite-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2022, Vanessa Sochat"
__license__ = "MPL 2.0"

from model import DwarfDifference

import os
import sys
import json

here = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, here)


def main():
# Hard coded examples, for now
lib1 = os.path.join(here, "lib", "callsite", "v1", "libcallsite.v1.so")
lib2 = os.path.join(here, "lib", "callsite", "v2", "libcallsite.v2.so")

for lib in lib1, lib2:
if not os.path.exists(lib):
sys.exit(f"{lib} does not exist.")

# Run the diff!
runner = DwarfDifference(lib1, lib2)
result = runner.run()
print(json.dumps(result, indent=4))


if __name__ == "__main__":
main()
44 changes: 23 additions & 21 deletions examples/dwarf/dwarf.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ def facts(self, die):
if die.tag == "DW_TAG_base_type":
return self.parse_base_type(die)

if die.tag == "DW_TAG_variable":
return self.parse_variable(die)

if die.tag == "DW_TAG_pointer_type":
return self.parse_pointer_type(die)

# TODO haven't seen these yet
print(die)
import IPython

IPython.embed()

if die.tag == "DW_TAG_variable":
return self.parse_variable(die)

if die.tag == "DW_TAG_union_type":
return self.parse_union_type(die)

Expand Down Expand Up @@ -178,14 +181,27 @@ def parse_formal_parameter(self, die):
return
self.gen("location", loc, parent=self.ids[die])

def parse_subprogram(self, die):
def parse_pointer_type(self, die):
"""
Add a function (subprogram) parsed from DWARF
Parse a pointer.
"""
return self.parse_sized_generic(die, "pointer")

def parse_variable(self, die):
"""
# If has DW_TAG_external, we know it's external outside of this CU
if "DW_AT_external" not in die.attributes:
Parse a formal parameter
"""
self.parse_sized_generic(die, "variable")
self.gen("type", self.get_underlying_type(die), parent=self.ids[die])
loc = self.parse_location(die)
if not loc:
return
self.gen("location", loc, parent=self.ids[die])

def parse_subprogram(self, die):
"""
Add a function (subprogram) parsed from DWARF
"""
self.new_node("function", get_name(die), self.ids[die])
self.generate_parent(die)
self.gen("type", self.get_underlying_type(die), parent=self.ids[die])
Expand Down Expand Up @@ -509,20 +525,6 @@ def parse_subrange_type(self, die):
entry["count"] = "unknown"
return entry

def parse_pointer(self, die):
"""
Parse a pointer.
"""
if "DW_AT_type" not in die.attributes:
print("Cannot parse pointer %s without a type." % die)
return

entry = {"class": "Pointer", "size": self.get_size(die)}

# We already have one pointer indirection
entry["underlying_type"] = self.parse_underlying_type(die, 1)
return entry

def parse_sibling(self, die):
"""
Try parsing a sibling.
Expand Down
2 changes: 2 additions & 0 deletions examples/dwarf/lib/callsite/v1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
g++ -g -Wall -fPIC -shared -o libcallsite.v1.so callsite.cpp
11 changes: 11 additions & 0 deletions examples/dwarf/lib/callsite/v1/callsite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <unistd.h>

static int inline_this()
{
return getpid();
}

int main(int argc, char *argv[])
{
return inline_this();
}
2 changes: 2 additions & 0 deletions examples/dwarf/lib/callsite/v2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
g++ -g -Wall -fPIC -shared -o libcallsite.v2.so callsite.cpp
11 changes: 11 additions & 0 deletions examples/dwarf/lib/callsite/v2/callsite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <unistd.h>

static int inline_that()
{
return getpid();
}

int main(int argc, char *argv[])
{
return inline_that();
}

0 comments on commit 3e050e4

Please sign in to comment.