Esempio n. 1
0
 def main(self):
     if self.options.profiler:
         from ptrace.profiler import runProfiler
         runProfiler(getLogger(), self._main)
     else:
         self._main()
     if self._output is not None:
         self._output.close()
Esempio n. 2
0
async def make_app():
    app = web.Application(logger=logging2.getLogger(__name__),
                          client_max_size=(2**20) * 20)
    aiohttp_jinja2.setup(app,
                         loader=jinja2.FileSystemLoader(
                             path.join(PWD, 'templates')))
    app.add_routes(routes)
    return app
Esempio n. 3
0
 def _setupLog(self, fd):
     logger = getLogger()
     handler = StreamHandler(fd)
     logger.addHandler(handler)
     if self.options.debug:
         level = DEBUG
     elif self.options.verbose:
         level = INFO
     elif self.options.quiet:
         level = ERROR
     else:
         level = WARNING
     logger.setLevel(level)
Esempio n. 4
0
 def _main(self):
     self.debugger = PtraceDebugger()
     try:
         self.runDebugger()
     except ProcessExit as event:
         self.processExited(event)
     except PtraceError as err:
         error("ptrace() error: %s" % err)
     except KeyboardInterrupt:
         error("Interrupted.")
     except PTRACE_ERRORS as err:
         writeError(getLogger(), err, "Debugger error")
     self.debugger.quit()
Esempio n. 5
0
 def getText(self):
     if not self.text:
         try:
             text = self.createText()
             if text is not None:
                 self.text = str(text)
             elif self.type and self.type.endswith("*"):
                 self.text = formatAddress(self.value)
             else:
                 self.text = repr(self.value)
         except PTRACE_ERRORS as err:
             writeError(
                 getLogger(), err,
                 "Format argument %s of function %s() value error" %
                 (self.name, self.function.name))
             self.text = repr(self.value)
     return self.text
Esempio n. 6
0
def writeError(logger, error, title="ERROR", log_level=ERROR):
    """
    Write an error into the logger:
     - logger: the logger (if None, use getLogger())
     - error: the exception object
     - title: error message prefix (e.g. title="Initialization error")
     - log_level: log level of the error

    If the exception is a SystemExit or a KeyboardInterrupt, re-emit
    (raise) the exception and don't write it.
    """
    if not logger:
        logger = getLogger()
    if error.__class__ in (SystemExit, KeyboardInterrupt):
        raise error
    log_func = getLogFunc(logger, log_level)
    log_func("%s: %s" % (title, formatError(error)))
    writeBacktrace(logger, log_level=changeLogLevel(log_level, -1))
Esempio n. 7
0
    def readStruct(self, address, struct):
        address = self.value

        struct_name = struct.__name__
        data = self.function.process.readStruct(address, struct)
        arguments = []
        for name, argtype in struct._fields_:
            value = getattr(data, name)
            try:
                text = self.formatStructValue(struct_name, name, value)
                if text is not None:
                    text = str(text)
                else:
                    text = repr(value)
            except PTRACE_ERRORS as err:
                writeError(getLogger(), err, "Format struct value error")
                text = repr(value)
            arguments.append("%s=%s" % (name, text))

        data = "<%s %s>" % (struct_name, ", ".join(arguments))
        return self.formatPointer(data, address)
Esempio n. 8
0
    def createText(self):
        value = self.value
        argtype = self.type
        name = self.name
        if not argtype or not name:
            return formatWordHex(self.value)

        syscall = self.function.name

        # Special cases
        try:
            return SYSCALL_ARG_DICT[syscall][name][value]
        except KeyError:
            pass
        try:
            callback = ARGUMENT_CALLBACK[syscall][name]
        except KeyError:
            callback = None
        if callback:
            return callback(self)
        if syscall == "execve":
            if name in ("argv", "envp"):
                return self.readCStringArray(value)
        if syscall == "socketcall":
            if name == "call":
                try:
                    return SOCKETCALL[value]
                except KeyError:
                    return str(value)
            if name == "args":
                func_call = FunctionCall("socketcall", self.options)
                setupSocketCall(func_call, self.function.process,
                                self.function[0], self.value)
                text = "<%s>" % func_call.format()
                return self.formatPointer(text, self.value)
        if syscall == "write" and name == "buf":
            fd = self.function[0].value
            if fd < 3:
                length = self.function[2].value
                return self.readString(value, length)
        if name == "signum":
            return signalName(value)
        if name in DIRFD_ARGUMENTS and argtype == "int":
            return formatDirFd(uint2int(value))

        # Remove "const " prefix
        if argtype.startswith("const "):
            argtype = argtype[6:]

        if name in FILENAME_ARGUMENTS and argtype == "char *":
            return self.readCString(value)

        # Format depending on the type
        if argtype.endswith("*"):
            try:
                text = self.formatValuePointer(argtype[:-1])
                if text:
                    return text
            except PTRACE_ERRORS as err:
                writeError(getLogger(),
                           err,
                           "Warning: Format %r value error" % self,
                           log_level=INFO)
            return formatAddress(self.value)

        # Array like "int[2]"
        match = re.match(r"(.*)\[([0-9])+\]", argtype)
        if match:
            basetype = match.group(1)
            count = int(match.group(2))
            if basetype == "int":
                return self.readArray(self.value, c_int, count)

        # Simple types
        if argtype in ("unsigned int", "unsigned long", "u32"):
            return str(self.value)
        if argtype in INTEGER_TYPES:
            return str(uint2int(self.value))

        # Default formatter: hexadecimal
        return formatWordHex(self.value)