Med Ismail Bennani a758c9f720 [lldb/Plugins] Add memory region support in ScriptedProcess
This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.

In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
  such as the memory region name, address range, permissions ...
  This is used when reading memory at some address to compute the offset
  in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
  to simplify the access to a specific memory region.

With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.

To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.

Differential Revision: https://reviews.llvm.org/D108953

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2021-10-08 14:54:07 +02:00

62 lines
2.6 KiB
C++

//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
#include <string>
#include "lldb/Host/Config.h"
#if LLDB_ENABLE_PYTHON
#include "lldb/lldb-forward.h"
#include "lldb/lldb-types.h"
namespace lldb_private {
// GetPythonValueFormatString provides a system independent type safe way to
// convert a variable's type into a python value format. Python value formats
// are defined in terms of builtin C types and could change from system to as
// the underlying typedef for uint* types, size_t, off_t and other values
// change.
template <typename T> const char *GetPythonValueFormatString(T t);
template <> const char *GetPythonValueFormatString(char *);
template <> const char *GetPythonValueFormatString(char);
template <> const char *GetPythonValueFormatString(unsigned char);
template <> const char *GetPythonValueFormatString(short);
template <> const char *GetPythonValueFormatString(unsigned short);
template <> const char *GetPythonValueFormatString(int);
template <> const char *GetPythonValueFormatString(unsigned int);
template <> const char *GetPythonValueFormatString(long);
template <> const char *GetPythonValueFormatString(unsigned long);
template <> const char *GetPythonValueFormatString(long long);
template <> const char *GetPythonValueFormatString(unsigned long long);
template <> const char *GetPythonValueFormatString(float t);
template <> const char *GetPythonValueFormatString(double t);
extern "C" void *LLDBSwigPythonCreateScriptedProcess(
const char *python_class_name, const char *session_dictionary_name,
const lldb::TargetSP &target_sp, StructuredDataImpl *args_impl,
std::string &error_string);
extern "C" void *LLDBSwigPythonCreateScriptedThread(
const char *python_class_name, const char *session_dictionary_name,
const lldb::TargetSP &target_sp, std::string &error_string);
extern "C" void *LLDBSWIGPython_CastPyObjectToSBData(void *data);
extern "C" void *LLDBSWIGPython_CastPyObjectToSBError(void *data);
extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data);
extern "C" void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(void *data);
} // namespace lldb_private
#endif // LLDB_ENABLE_PYTHON
#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H