Skip to content

Instantly share code, notes, and snippets.

@roelj
Created August 31, 2015 20:19
Show Gist options
  • Save roelj/8b375cbc480105755892 to your computer and use it in GitHub Desktop.
Save roelj/8b375cbc480105755892 to your computer and use it in GitHub Desktop.
Create a graph from a map file
#!/bin/bash
mapFile="$1"
dotFile="$2"
##### DO NOT EDIT BELOW ########
# Write the graph to a file in DOT format.
function write_to_file
{
echo "strict digraph {" > "$1"
echo -e "$GRAPH" | sort | uniq >> "$1"
echo -e "$COLORS" >> "$1"
echo "}" >> "$1"
}
# Add colors to the graph nodes
function color_known_entities
{
COLORS+=" \"libmemory-patient\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libmemory-study\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libmemory-serie\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libmemory-slice\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libmemory-tree\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libmemory-io\" [shape=ellipse, style=filled, fillcolor=goldenrod1]\n"
COLORS+=" \"libcommon-algebra\" [shape=ellipse, style=filled, fillcolor=brown3]\n"
COLORS+=" \"libcommon-debug\" [shape=ellipse, style=filled, fillcolor=brown3]\n"
COLORS+=" \"libcommon-tree\" [shape=ellipse, style=filled, fillcolor=brown3]\n"
COLORS+=" \"libcommon-history\" [shape=ellipse, style=filled, fillcolor=brown3]\n"
COLORS+=" \"libcommon-list\" [shape=ellipse, style=filled, fillcolor=brown3]\n"
COLORS+=" \"libcairo\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"libclutter-1\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"libgtk-3\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"libc\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"libpthread\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"ld-linux-x86-64\" [shape=ellipse, style=filled, fillcolor=black, fontcolor=white]\n"
COLORS+=" \"libconfiguration\" [shape=ellipse, style=filled, fillcolor=dodgerblue4, fontcolor=white]\n"
COLORS+=" \"libpixeldata-plugin\" [shape=ellipse, style=filled, fillcolor=burlywood3]\n"
COLORS+=" \"libpixeldata\" [shape=ellipse, style=filled, fillcolor=burlywood3]\n"
COLORS+=" \"mainwindow\" [shape=ellipse, style=filled, fillcolor=darksalmon]\n"
COLORS+=" \"main\" [shape=ellipse, style=filled, fillcolor=darksalmon]\n"
COLORS+=" \"libviewer\" [shape=ellipse, style=filled, fillcolor=grey84]\n"
#COLORS+=" \"libhistogram\" [shape=ellipse, style=filled, fillcolor=khaki]\n"
COLORS+=" \"libio-nifti\" [shape=ellipse, style=filled, fillcolor=hotpink3]\n"
COLORS+=" \"libio-dicom\" [shape=ellipse, style=filled, fillcolor=hotpink3]\n"
COLORS+=" \"zz\" [shape=ellipse, style=filled, fillcolor=hotpink3]\n"
COLORS+=" \"zzio\" [shape=ellipse, style=filled, fillcolor=hotpink3]\n"
}
# Filter and prettify the graph nodes.
function add_graph_line
{
pretty_first=$(basename $(echo $1 | cut -d "(" -f 2 | cut -d "." -f 1))
# Filter out the first argument
if [[ "$1" = /usr/lib* ]]
then
if [[ "$pretty_first" != "libcairo" &&
"$pretty_first" != "libgtk-3" &&
"$pretty_first" != "libclutter-1" ]];
then
return 1
fi
fi
pretty_second=$(basename $(echo $2 | cut -d "(" -f 2 | cut -d "." -f 1))
# Filter out the second argument
if [[ "$2" = /usr/lib* ]]
then
if [[ "$pretty_second" != "libcairo" &&\
"$pretty_second" != "libgtk-3" &&\
"$pretty_second" != "libclutter-1" ]];
then
return 1
fi
fi
GRAPH+=" \"$pretty_first\" -> \"$pretty_second\"\n" #[label=\"$3\"]
}
## Extract relationships from the map file.
function extract_graph
{
lineNumber=$(grep -n "Symbol" "$mapFile" | awk -F ":" '{ print $1 }')
numberOfLines=$(cat "$mapFile" | wc -l)
numberOfLinesFromTail=$(($numberOfLines - $lineNumber))
while read LINE
do
numberOfWords=$(echo "$LINE" | wc -w)
if [ $numberOfWords -eq 2 ]
then
#lastSymbol=$(echo "$LINE" | awk '{print $1}')
lastModule=$(echo "$LINE" | awk '{print $2}')
elif [ $numberOfWords -eq 1 ]
then
#relationModule=$(echo "$LINE" | awk '{print $1}')
add_graph_line "$LINE" "$lastModule" #"$lastSymbol"
fi
done < <(tail -n "$numberOfLinesFromTail" "$mapFile")
}
if [ ! -f "$mapFile" ]
then
echo "Map file does not exist, please use valid map file"
exit
fi
if [ -z "$dotFile" ]
then
echo "Dot file argument is empty, please parse a argument"
exit
fi
## Check if parsed map file contains a is cross reference table
if [ $(grep -c "Cross Reference Table" "$mapFile") -eq 0 ]
then
echo "Parsed map file is not suitable for this script"
echo "It doesn't contain a cross reference table"
exit
fi
if [ $(grep "Symbol" "$mapFile" | grep -c "File") -eq 0 ]
then
echo "Parsed map file is not suitable for this script"
echo "It doesn't contain a cross reference table"
exit
fi
extract_graph
color_known_entities
write_to_file "$dotFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment