Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8ce7d62
Move show.py to c_analyzer_common.
ericsnowcurrently Sep 27, 2019
c40c247
Small cleanups around _check_results().
ericsnowcurrently Sep 27, 2019
bf3bd21
Merge c_parser.info into c_analyzer_common.info.
ericsnowcurrently Sep 27, 2019
52113ae
c_symbols.binary -> c_symbols.find.
ericsnowcurrently Sep 27, 2019
dc8333f
Add ID.match().
ericsnowcurrently Sep 30, 2019
9b47596
Add known.look_up_variable().
ericsnowcurrently Sep 30, 2019
67eea78
Add c_parser.find module.
ericsnowcurrently Sep 30, 2019
b61b704
Add c_symbols.find module.
ericsnowcurrently Sep 30, 2019
a041e4a
Avoid variable resolution in _nm.iter_symbols().
ericsnowcurrently Sep 30, 2019
b9b0499
Add c_analyzer_common.find module.
ericsnowcurrently Sep 30, 2019
15b60db
Use the new modules in __main__.
ericsnowcurrently Sep 30, 2019
2f9958f
Drop the old code.
ericsnowcurrently Sep 30, 2019
b05272c
Specify "preprocessed" option as an arg.
ericsnowcurrently Sep 30, 2019
80d299c
Add a TODO.
ericsnowcurrently Sep 30, 2019
15c1226
Use declarations.iter_all() instead of iter_variables().
ericsnowcurrently Sep 30, 2019
e08bad5
Make sure "resolve()" is used properly.
ericsnowcurrently Oct 1, 2019
fa6f1b5
Move extract_storage() to c_parser.declarations.
ericsnowcurrently Oct 4, 2019
6531014
Move the generic code under a top-level c_analyzer package.
ericsnowcurrently Oct 4, 2019
15f010b
Move some code to a new c_analyzer.variables package.
ericsnowcurrently Oct 4, 2019
c399dc0
c_global -> cpython.
ericsnowcurrently Oct 4, 2019
025e8c3
Do not use Variable in symbols or parser packages.
ericsnowcurrently Oct 15, 2019
f60551e
Drop "INCLUDES" from SOURCE_DIRS.
ericsnowcurrently Oct 18, 2019
4beba49
Process files more cleanly.
ericsnowcurrently Oct 18, 2019
9e5c09b
Fix a typo.
ericsnowcurrently Oct 18, 2019
6e14276
Call check_filename() properly.
ericsnowcurrently Oct 18, 2019
0ce0cd1
Fix one last import.
ericsnowcurrently Oct 18, 2019
264d698
Fix whitespace.
ericsnowcurrently Oct 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
c_symbols.binary -> c_symbols.find.
  • Loading branch information
ericsnowcurrently committed Sep 27, 2019
commit 52113aeb4790b7bd3ec7cea59deacdebd25a5e87
5 changes: 5 additions & 0 deletions Tools/c-analyzer/c_analyzer_common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import sys


PKG_ROOT = os.path.dirname(__file__)
Expand All @@ -14,6 +15,10 @@
'Modules',
]]

#PYTHON = os.path.join(REPO_ROOT, 'python')
PYTHON = sys.executable


# Clean up the namespace.
del sys
del os
6 changes: 3 additions & 3 deletions Tools/c-analyzer/c_globals/find.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from c_analyzer_common import SOURCE_DIRS
from c_analyzer_common import SOURCE_DIRS, PYTHON
from c_analyzer_common.info import UNKNOWN, Variable
from c_symbols import (
info as s_info,
binary as b_symbols,
find as b_symbols,
source as s_symbols,
resolve,
)
Expand All @@ -12,7 +12,7 @@
# XXX needs tests:
# * iter_variables

def globals_from_binary(binfile=b_symbols.PYTHON, *,
def globals_from_binary(binfile=PYTHON, *,
knownvars=None,
dirnames=None,
_iter_symbols=b_symbols.iter_symbols,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
import os
import os.path
import shutil
import sys

from c_analyzer_common import util, info
from . import source
from .info import Symbol


#PYTHON = os.path.join(REPO_ROOT, 'python')
PYTHON = sys.executable
from .info import Symbol


def iter_symbols(binary=PYTHON, dirnames=None, *,
# Alternately, use look_up_known_symbol()
# from c_globals.supported.
find_local_symbol=source.find_symbol,
_file_exists=os.path.exists,
_iter_symbols_nm=(lambda b, *a: _iter_symbols_nm(b, *a)),
):
"""Yield a Symbol for each symbol found in the binary."""
if not _file_exists(binary):
raise Exception('executable missing (need to build it first?)')

if find_local_symbol:
cache = {}
def find_local_symbol(name, *, _find=find_local_symbol):
return _find(name, dirnames, _perfilecache=cache)
else:
find_local_symbol = None

if os.name == 'nt':
# XXX Support this.
raise NotImplementedError
else:
yield from _iter_symbols_nm(binary, find_local_symbol)
# XXX need tests:
# * iter_symbols


#############################
# binary format (e.g. ELF)
NM_KINDS = {
'b': Symbol.KIND.VARIABLE, # uninitialized
'd': Symbol.KIND.VARIABLE, # initialized
#'g': Symbol.KIND.VARIABLE, # uninitialized
#'s': Symbol.KIND.VARIABLE, # initialized
't': Symbol.KIND.FUNCTION,
}

SPECIAL_SYMBOLS = {
# binary format (e.g. ELF)
'__bss_start',
'__data_start',
'__dso_handle',
Expand All @@ -63,29 +41,21 @@ def _is_special_symbol(name):
return False


#############################
# "nm"

NM_KINDS = {
'b': Symbol.KIND.VARIABLE, # uninitialized
'd': Symbol.KIND.VARIABLE, # initialized
#'g': Symbol.KIND.VARIABLE, # uninitialized
#'s': Symbol.KIND.VARIABLE, # initialized
't': Symbol.KIND.FUNCTION,
}

def iter_symbols(binfile, find_local_symbol=None,
*,
nm=None,
_which=shutil.which,
_run=util.run_cmd,
):
"""Yield a Symbol for each relevant entry reported by the "nm" command."""
if nm is None:
nm = _which('nm')
if not nm:
raise NotImplementedError

def _iter_symbols_nm(binary, find_local_symbol=None,
*,
_which=shutil.which,
_run=util.run_cmd,
):
nm = _which('nm')
if not nm:
raise NotImplementedError
argv = [nm,
'--line-numbers',
binary,
binfile,
]
try:
output = _run(argv)
Expand Down
42 changes: 42 additions & 0 deletions Tools/c-analyzer/c_symbols/find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import os.path
import shutil

from c_analyzer_common import PYTHON

from . import _nm, source

# XXX need tests:
# * iter_symbols


def _get_platform_tool():
if os.name == 'nt':
# XXX Support this.
raise NotImplementedError
elif nm := shutil.which('nm'):
return lambda b, fls: _nm.iter_symbols(b, fls, nm=nm)
else:
raise NotImplementedError


def iter_symbols(binfile=PYTHON, dirnames=None, *,
# Alternately, use look_up_known_symbol()
# from c_globals.supported.
find_local_symbol=source.find_symbol,
_file_exists=os.path.exists,
_get_platform_tool=_get_platform_tool,
):
"""Yield a Symbol for each symbol found in the binary."""
if not _file_exists(binfile):
raise Exception('executable missing (need to build it first?)')

if find_local_symbol:
cache = {}
def find_local_symbol(name, *, _find=find_local_symbol):
return _find(name, dirnames, _perfilecache=cache)
else:
find_local_symbol = None

_iter_symbols = _get_platform_tool()
yield from _iter_symbols(binfile, find_local_symbol)