first commit of cda, my open source code explorer code, imported

This commit is contained in:
George Hotz 2014-07-29 17:03:38 -07:00
parent 2ae1809c6e
commit 0297cab7ec
12 changed files with 9705 additions and 3 deletions

1
.gitignore vendored
View File

@ -16,4 +16,5 @@ qemu/qemu-2*
distrib/
libs/
qiradb/build
cda/clang

119
cda/cachegen.py Normal file
View File

@ -0,0 +1,119 @@
#!/usr/bin/env python2
import os
import sys
basedir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(basedir+"/clang/llvm/tools/clang/bindings/python")
import clang.cindex as ci
ci.Config.set_library_file(basedir+"/clang/build/Release+Asserts/lib/libclang.so")
import pickle
from clang.cindex import CursorKind
# debug
DEBUG = 0
# cache generated
object_cache = {}
xref_cache = {}
# a single index for the runtime of the server
index = ci.Index.create()
def parse_node(node, d, filename, care):
#print node.location.file
if node.location.file != None and str(node.location.file) != filename:
return
ref = node.referenced
if type(ref) != type(None):
usr = ref.get_usr()
#print " "*d, node.kind, node.spelling, node.displayname, node.location, node.extent.start.offset, node.extent.end.offset, node.get_usr(), "****", ref.spelling, ref.location, ref.get_usr()
else:
usr = None
if DEBUG == 1:
print " "*d, node.kind, node.spelling, node.displayname, node.location, node.location.offset, node.extent.start.offset, node.extent.end.offset, usr
"""
if DEBUG == 1:
print " "*d, node.kind, node.spelling, node.displayname, node.location, node.location.offset, node.extent.start.offset, node.extent.end.offset, usr
"""
#print dir(node)
"""
print ref, node.get_usr()
print ref.location
for i in deff:
print i
"""
klass = str(node.kind).split('.')[-1]
(start, end) = (None, None)
if node.kind in [CursorKind.STRING_LITERAL, CursorKind.INTEGER_LITERAL, CursorKind.TYPE_REF, CursorKind.TEMPLATE_REF]:
#if node.kind in [CursorKind.STRING_LITERAL, CursorKind.TYPE_REF, CursorKind.TEMPLATE_REF]:
start = node.extent.start.offset
end = node.extent.end.offset
elif node.kind in [CursorKind.FUNCTION_DECL, CursorKind.FUNCTION_TEMPLATE, CursorKind.VAR_DECL, CursorKind.CLASS_DECL, CursorKind.CXX_METHOD, CursorKind.CLASS_TEMPLATE, CursorKind.PARM_DECL]:
start = node.location.offset
end = node.location.offset + len(node.spelling)
elif node.kind in [CursorKind.MEMBER_REF_EXPR]:
#print node.location.offset, node.extent.start.offset, node.extent.end.offset
if node.location.offset != 0:
start = node.location.offset
else:
start = node.extent.start.offset
end = node.extent.end.offset
#end = node.location.offset + len(node.displayname)
elif node.kind in [CursorKind.DECL_REF_EXPR]:
start = node.location.offset
end = node.extent.end.offset
if end != None:
care.append((start, end, klass, usr))
"""
if end != None and usr != None and node.location.line > 0 and filename[0:len(directory)] == directory:
newval = filename[len(directory):].strip("/")+"#"+str(node.location.line)
if node.is_definition():
# defining the object
if usr in object_cache:
object_cache[usr].append(newval)
else:
object_cache[usr] = [newval]
else:
# xref
if usr in xref_cache:
xref_cache[usr].append(newval)
else:
xref_cache[usr] = [newval]
# link here is good
"""
for child in node.get_children():
parse_node(child, d+1, filename, care)
def parse_file(filename):
# traversal attack
tu = index.parse(filename, args=sys.argv[3:])
# bad shit happened
bad = False
for m in tu.diagnostics:
if m.severity >= 3:
print m
bad = True
if bad == True:
#raise Exception("parsing issue")
print "parsing issue"
# extract the things we care about
care = []
parse_node(tu.cursor, 0, filename, care)
care = sorted(care)
# get file data
rdat = open(filename).read()
return (care, rdat)

29
cda/cda Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python2
import os
import sys
import cachegen
import json
file_cache = {}
if __name__ == "__main__":
directory = os.path.realpath(sys.argv[2])
print "root directory is",directory
if os.path.isfile(directory):
#DEBUG = 1
filename = directory.split("/")[-1]
directory = '/'.join(directory.split("/")[:-1])
print "single file",filename
file_cache[filename] = cachegen.parse_file(directory+"/"+filename)
else:
for root, dir, files in os.walk(directory):
for file in filter(lambda x: x.split(".")[-1] in ["c", "cc", "cpp", "h", "hpp"], files):
fn = root+"/"+file
print "parsing",fn
filename = fn[len(directory):].strip("/")
file_cache[filename] = cachegen.parse_file(fn)
dat = (cachegen.object_cache, file_cache, cachegen.xref_cache)
f = open(sys.argv[1], "wb")
json.dump(dat, f)
f.close()

28
cda/clang_build.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash -e
sudo pip install html
mkdir -p clang
cd clang
if [ ! -f .downloaded_clang ]; then
echo "downloading"
wget http://llvm.org/releases/3.4.2/cfe-3.4.2.src.tar.gz
wget http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz
wget http://llvm.org/releases/3.4.2/llvm-3.4.2.src.tar.gz
touch .downloaded_clang
fi
echo "extracting"
tar xf llvm-3.4.2.src.tar.gz
tar xf cfe-3.4.2.src.tar.gz
tar xf compiler-rt-3.4.src.tar.gz
echo "making symlinks"
ln -sf llvm-3.4.2.src llvm
ln -sf ../../cfe-3.4.2.src llvm/tools/clang
ln -sf ../../compiler-rt-3.4 llvm/projects/compiler-rt
mkdir -p build
cd build
../llvm/configure
make -j32

108
cda/static/cda.css Normal file
View File

@ -0,0 +1,108 @@
body {
font-family: monospace;
}
a {
color: inherit;
}
span {
white-space: nowrap;
}
.link {
/*background-color: #F0F0F0;*/
text-decoration: underline;
/*font-style: italic;*/
}
.xref {
background-color: #EEEEEE;
padding: 5px;
}
.xrefstitle {
color: blue;
}
#ln {
display: inline-block;
float: left;
color: gray;
}
.line_highlighted {
background-color: #EE8888;
}
.highlighted {
background-color: #EEEE88;
}
#code {
overflow: hidden;
}
.dirlink {
color: blue;
}
.filelink {
color: black;
text-decoration: underline;
}
/* declaring a function in the global scope */
.FUNCTION_DECL {
color: red;
}
/* C++ method in class */
.CXX_METHOD {
color: #AAAAAA;
}
/* a variable or function reference */
.DECL_REF_EXPR {
color: #00AA00;
}
/* calling a C++ member function */
.MEMBER_REF_EXPR {
color: #AAAA55;
}
/* string in quotes */
.STRING_LITERAL {
color: blue;
}
/* integer */
.INTEGER_LITERAL {
color: purple;
}
.VAR_DECL {
color: orange;
}
.PARM_DECL {
color: orange;
}
.CLASS_DECL {
color: #00AAAA;
}
.CLASS_TEMPLATE {
color: #00AAAA;
}
.TYPE_REF {
color: #AAAA00;
}
.TEMPLATE_REF {
color: #AA4400;
}

70
cda/static/cda.js Normal file
View File

@ -0,0 +1,70 @@
function p(s) {
console.log(s);
}
var highlighted = $();
$(window).on('hashchange', function() {
if (window.location.hash == "") return;
highlighted.removeClass("line_highlighted")
highlighted = $("#l" + window.location.hash.substr(1))
highlighted.addClass("line_highlighted");
$(window).scrollTo(highlighted, {offset: -150})
});
var selected = $();
function link_click_handler(e) {
//p(e.target.getAttribute('name'));
selected.removeClass('highlighted');
selected = $(document.getElementsByName(e.target.getAttribute('name')));
selected.addClass('highlighted');
/*xdiv.empty();
xdiv.append($('<div class="xrefstitle">'+selected[0].getAttribute('usr')+'</div>'));
var xrefs_ele = selected[0].getAttribute('xrefs');
if (xrefs_ele == null) return;
var xrefs = xrefs_ele.split(" ");
p("xref "+xrefs);
for (var i = 0; i < xrefs.length; i++) {
xdiv.append($('<a href="/f/'+xrefs[i]+'">'+xrefs[i]+'</a></br>'));
}*/
}
function link_dblclick_handler(e) {
var targets = e.target.getAttribute('targets').split(" ");
p(targets);
window.location = "/f/"+targets[0];
}
function xref_key_handler(e) {
var usr = selected[0].getAttribute('name');
window.open('/x/'+btoa(usr), "xrefs", "resizable=no,scrollbars=no,menubar=no,width=300,height=500,left=1000,top=50");
}
// no selection
window.onmousedown = function() { return false; };
// when the page loads we need to check the hash
window.onload = function() {
window.dispatchEvent(new HashChangeEvent("hashchange"))
$('.link').bind('click', link_click_handler);
$('.link').bind('dblclick', link_dblclick_handler);
xdiv = $('<div id="xref"></div>');
$(document.body).prepend(xdiv);
var keys = {88: xref_key_handler};
document.onkeydown = function(e) {
//p(e.which);
if (keys[e.which] !== undefined) {
keys[e.which](e);
}
};
};

11
cda/static/index.html Normal file
View File

@ -0,0 +1,11 @@
<html>
<head>
<link ref="stylesheet" href="/static/cda.css" />
<script src="/static/jquery-2.1.0.js"></script>
<script src="/static/cda.js"></script>
</head>
<body>
</body>
</html>

9111
cda/static/jquery-2.1.0.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,218 @@
/*!
* jQuery.ScrollTo
* Copyright (c) 2007-2012 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Dual licensed under MIT and GPL.
* Date: 4/09/2012
*
* @projectDescription Easy element scrolling using jQuery.
* http://flesler.blogspot.com/2007/10/jqueryscrollto.html
* @author Ariel Flesler
* @version 1.4.3.1
*
* @id jQuery.scrollTo
* @id jQuery.fn.scrollTo
* @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
* The different options for target are:
* - A number position (will be applied to all axes).
* - A string position ('44', '100px', '+=90', etc ) will be applied to all axes
* - A jQuery/DOM element ( logically, child of the element to scroll )
* - A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
* - A hash { top:x, left:y }, x and y can be any kind of number/string like above.
* - A percentage of the container's dimension/s, for example: 50% to go to the middle.
* - The string 'max' for go-to-end.
* @param {Number, Function} duration The OVERALL length of the animation, this argument can be the settings object instead.
* @param {Object,Function} settings Optional set of settings or the onAfter callback.
* @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
* @option {Number, Function} duration The OVERALL length of the animation.
* @option {String} easing The easing method for the animation.
* @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
* @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
* @option {Object, Number} over Add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes.
* @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
* @option {Function} onAfter Function to be called after the scrolling ends.
* @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
* @return {jQuery} Returns the same jQuery object, for chaining.
*
* @desc Scroll to a fixed position
* @example $('div').scrollTo( 340 );
*
* @desc Scroll relatively to the actual position
* @example $('div').scrollTo( '+=340px', { axis:'y' } );
*
* @desc Scroll using a selector (relative to the scrolled element)
* @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
*
* @desc Scroll to a DOM element (same for jQuery object)
* @example var second_child = document.getElementById('container').firstChild.nextSibling;
* $('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
* alert('scrolled!!');
* }});
*
* @desc Scroll on both axes, to different values
* @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
*/
;(function( $ ){
var $scrollTo = $.scrollTo = function( target, duration, settings ){
$(window).scrollTo( target, duration, settings );
};
$scrollTo.defaults = {
axis:'xy',
duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1,
limit:true
};
// Returns the element that needs to be animated to scroll the window.
// Kept for backwards compatibility (specially for localScroll & serialScroll)
$scrollTo.window = function( scope ){
return $(window)._scrollable();
};
// Hack, hack, hack :)
// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
$.fn._scrollable = function(){
return this.map(function(){
var elem = this,
isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
if( !isWin )
return elem;
var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
return /webkit/i.test(navigator.userAgent) || doc.compatMode == 'BackCompat' ?
doc.body :
doc.documentElement;
});
};
$.fn.scrollTo = function( target, duration, settings ){
if( typeof duration == 'object' ){
settings = duration;
duration = 0;
}
if( typeof settings == 'function' )
settings = { onAfter:settings };
if( target == 'max' )
target = 9e9;
settings = $.extend( {}, $scrollTo.defaults, settings );
// Speed is still recognized for backwards compatibility
duration = duration || settings.duration;
// Make sure the settings are given right
settings.queue = settings.queue && settings.axis.length > 1;
if( settings.queue )
// Let's keep the overall duration
duration /= 2;
settings.offset = both( settings.offset );
settings.over = both( settings.over );
return this._scrollable().each(function(){
// Null target yields nothing, just like jQuery does
if (target == null) return;
var elem = this,
$elem = $(elem),
targ = target, toff, attr = {},
win = $elem.is('html,body');
switch( typeof targ ){
// A number will pass the regex
case 'number':
case 'string':
if( /^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
targ = both( targ );
// We are done
break;
}
// Relative selector, no break!
targ = $(targ,this);
if (!targ.length) return;
case 'object':
// DOMElement / jQuery
if( targ.is || targ.style )
// Get the real position of the target
toff = (targ = $(targ)).offset();
}
$.each( settings.axis.split(''), function( i, axis ){
var Pos = axis == 'x' ? 'Left' : 'Top',
pos = Pos.toLowerCase(),
key = 'scroll' + Pos,
old = elem[key],
max = $scrollTo.max(elem, axis);
if( toff ){// jQuery / DOMElement
attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
// If it's a dom element, reduce the margin
if( settings.margin ){
attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
}
attr[key] += settings.offset[pos] || 0;
if( settings.over[pos] )
// Scroll to a fraction of its width/height
attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
}else{
var val = targ[pos];
// Handle percentage values
attr[key] = val.slice && val.slice(-1) == '%' ?
parseFloat(val) / 100 * max
: val;
}
// Number or 'number'
if( settings.limit && /^\d+$/.test(attr[key]) )
// Check the limits
attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );
// Queueing axes
if( !i && settings.queue ){
// Don't waste time animating, if there's no need.
if( old != attr[key] )
// Intermediate animation
animate( settings.onAfterFirst );
// Don't animate this axis again in the next iteration.
delete attr[key];
}
});
animate( settings.onAfter );
function animate( callback ){
$elem.animate( attr, duration, settings.easing, callback && function(){
callback.call(this, target, settings);
});
};
}).end();
};
// Max scrolling position, works on quirks mode
// It only fails (not too badly) on IE, quirks mode.
$scrollTo.max = function( elem, axis ){
var Dim = axis == 'x' ? 'Width' : 'Height',
scroll = 'scroll'+Dim;
if( !$(elem).is('html,body') )
return elem[scroll] - $(elem)[Dim.toLowerCase()]();
var size = 'client' + Dim,
html = elem.ownerDocument.documentElement,
body = elem.ownerDocument.body;
return Math.max( html[scroll], body[scroll] )
- Math.min( html[size] , body[size] );
};
function both( val ){
return typeof val == 'object' ? val : { top:val, left:val };
};
})( jQuery );

View File

@ -19,4 +19,5 @@ sudo $PIP install flask-socketio pillow pyelftools ./qiradb
echo "making symlink"
sudo ln -sf $(pwd)/qira /usr/local/bin/qira
sudo ln -sf $(pwd)/cda/cda /usr/local/bin/cda

View File

@ -176,7 +176,7 @@ def getinstructions(forknum, clstart, clend):
if rret['address'] in program.instructions:
rret['instruction'] = program.instructions[rret['address']]
if rret['address'] in program.dwarves:
rret['comment'] = program.dwarves[rret['address']]
rret['comment'] = program.dwarves[rret['address']][1]
ret.append(rret)
emit('instructions', ret)

View File

@ -127,9 +127,15 @@ Template.__define__("idump", (function() {
} ]
}, function() {
return Spacebars.mustache(self.lookup("hexaddress"));
}), "\n ", function() {
}), "\n ", HTML.DIV({
"class": "instructiondesc"
}, function() {
return Spacebars.mustache(self.lookup("instruction"));
}, "\n "), "\n" ];
}), "\n ", HTML.SPAN({
"class": "comment"
}, function() {
return Spacebars.mustache(self.lookup("comment"));
}), "\n "), "\n" ];
}));
}));