about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2016-02-16 08:41:50 +0100
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2016-02-19 15:50:59 +0100
commit34039011459c729a93a79deea1d3c5000bc03032 (patch)
tree5e568adc0d73822984cb05664fb976d2863db5ff
parent91e9abd906c0a9f5b43bad5b9789ffa1b054f6fe (diff)
downloadmiasm-34039011459c729a93a79deea1d3c5000bc03032.tar.gz
miasm-34039011459c729a93a79deea1d3c5000bc03032.zip
Core/utils: sanitize whoami/get_caller_name
-rw-r--r--miasm2/core/utils.py16
-rw-r--r--miasm2/jitter/jitload.py4
-rw-r--r--miasm2/os_dep/win_api_x86_32.py7
3 files changed, 16 insertions, 11 deletions
diff --git a/miasm2/core/utils.py b/miasm2/core/utils.py
index 70520c1b..0512f4aa 100644
--- a/miasm2/core/utils.py
+++ b/miasm2/core/utils.py
@@ -17,6 +17,19 @@ pck = {8:pck8,
        16:pck16,
        32:pck32,
        64:pck64}
+def get_caller_name(caller_num=0):
+    """Get the nth caller's name
+    @caller_num: 0 = the caller of get_caller_name, 1 = next parent, ..."""
+    pystk = inspect.stack()
+    if len(pystk) > 1 + caller_num:
+        return pystk[1 + caller_num][3]
+    else:
+        return "Bad caller num"
+
+
+def whoami():
+    """Returns the caller's name"""
+    return get_caller_name(1)
 
 
 class Disasm_Exception(Exception):
@@ -48,9 +61,6 @@ class keydefaultdict(collections.defaultdict):
         value = self[key] = self.default_factory(key)
         return value
 
-def whoami():
-    return inspect.stack()[2][3]
-
 
 class BoundedDict(UserDict.DictMixin):
     """Limited in size dictionary.
diff --git a/miasm2/jitter/jitload.py b/miasm2/jitter/jitload.py
index aeb917d8..86c7b07a 100644
--- a/miasm2/jitter/jitload.py
+++ b/miasm2/jitter/jitload.py
@@ -58,7 +58,7 @@ def named_arguments(func):
             arg_vals = namedtuple("args", args)(*arg_vals)
             # func_name(arguments) return address
             log_func.info('%s(%s) ret addr: %s',
-                whoami(),
+                get_caller_name(1),
                 ', '.join("%s=0x%x" % (field, value)
                           for field, value in arg_vals._asdict().iteritems()),
                 hex(ret_ad))
@@ -67,7 +67,7 @@ def named_arguments(func):
             ret_ad, arg_vals = func(self, args)
             # func_name(arguments) return address
             log_func.info('%s(%s) ret addr: %s',
-                whoami(),
+                get_caller_name(1),
                 ', '.join(hex(arg) for arg in arg_vals),
                 hex(ret_ad))
             return ret_ad, arg_vals
diff --git a/miasm2/os_dep/win_api_x86_32.py b/miasm2/os_dep/win_api_x86_32.py
index ef2cfc38..9b536ec7 100644
--- a/miasm2/os_dep/win_api_x86_32.py
+++ b/miasm2/os_dep/win_api_x86_32.py
@@ -16,7 +16,6 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 import struct
-import inspect
 import os
 import stat
 import time
@@ -31,7 +30,7 @@ except ImportError:
     print "cannot find crypto, skipping"
 
 from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE, PAGE_EXEC
-from miasm2.core.utils import pck16, pck32, upck32, hexdump
+from miasm2.core.utils import pck16, pck32, upck32, hexdump, whoami
 from miasm2.os_dep.common import \
     heap, set_str_ansi, set_str_unic, get_str_ansi, get_str_unic, \
     windows_to_sbpath
@@ -231,10 +230,6 @@ process_list = [
 ]
 
 
-def whoami():
-    return inspect.stack()[1][3]
-
-
 class hobj:
     pass