Skip to content

Commit

Permalink
work towards reading python support to canal
Browse files Browse the repository at this point in the history
  • Loading branch information
peadar committed May 8, 2024
1 parent 32e69c6 commit e804f26
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ if (PYTHON3 AND Python3_Development_FOUND)
set(pysrc ${pysrc} python3.cc pythonc.c)
set_source_files_properties(python3.cc PROPERTIES COMPILE_FLAGS ${pyinc})
set_source_files_properties(pythonc.c PROPERTIES COMPILE_FLAGS ${pyinc})
set_source_files_properties(canal.cc PROPERTIES COMPILE_FLAGS ${pyinc})
else()
message(FATAL_ERROR "Path to CPython source code needed for Python 3 support\nSpecify with -DPYTHON3_SOURCE=<path>")
endif()
Expand Down
23 changes: 16 additions & 7 deletions canal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@
#include "libpstack/fs.h"
#include "libpstack/ioflag.h"
#include "libpstack/flags.h"
#if defined( WITH_PYTHON3 ) || defined( WITH_PYTHON2 )
#define WITH_PYTHON
#endif
#ifdef WITH_PYTHON
#include "libpstack/python.h"
#include <Python.h>
#endif

using namespace std;
using namespace pstack;

using AddressRanges = std::vector<std::pair<Elf::Off, Elf::Off>>;
#ifdef WITH_PYTHON
bool doPython = false;
std::unique_ptr<PythonPrinter<3>> py = nullptr;
#endif

// does "name" match the glob pattern "pattern"?
Expand Down Expand Up @@ -220,9 +224,9 @@ template <typename Matcher, typename Word> inline void search(
<< std::dec << " ... size=" << sym->sym.st_size
<< ", diff=" << p - sym->memaddr() << endl;
#ifdef WITH_PYTHON
if (doPython) {
if (py) {
std::cout << "pyo " << Elf::Addr(loc) << " ";
py.print(Elf::Addr(loc) - sizeof (PyObject) +
py->print(Elf::Addr(loc) - sizeof (PyObject) +
sizeof (struct _typeobject *));
std::cout << "\n";
}
Expand Down Expand Up @@ -264,12 +268,13 @@ mainExcept(int argc, char *argv[])
AddressRanges searchaddrs;
std::string findstr;
int symOffset = -1;
bool doPython = false;

Flags flags;

flags
#ifdef WITH_PYTHON
.add("python", 'P', "try to find python objects", setf(doPython))
.add("python", 'P', "try to find python objects", Flags::setf(doPython))
#endif
.add("show-syms", 'V', "show symbols matching search pattern", Flags::setf(showsyms))
.add("show-addrs", 's', "show adddress of references found in core", Flags::setf(showaddrs))
Expand Down Expand Up @@ -310,12 +315,19 @@ mainExcept(int argc, char *argv[])
optind++;
}


if (argc - optind < 1) {
clog << Usage(flags);
return 0;
}

auto process = Procman::Process::load(exec, argv[optind], PstackOptions(), imageCache);

PyInterpInfo info;
if (doPython) {
info = getPyInterpInfo(*process);
py = make_unique<PythonPrinter<3>>(*process, std::cout, info);
}
if (searchaddrs.size()) {
std::clog << "finding references to " << dec << searchaddrs.size() << " addresses\n";
for (auto &addr : searchaddrs)
Expand Down Expand Up @@ -353,9 +365,6 @@ mainExcept(int argc, char *argv[])
exit(0);

// Now run through the corefile, searching for virtual objects.
#ifdef WITH_PYTHON
PythonPrinter<2> py(*process, std::cout);
#endif
auto as = process->addressSpace();
for (auto &segment : as ) {
if (verbose) {
Expand Down
1 change: 0 additions & 1 deletion libpstack/python.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ struct PythonPrinter {
Elf::Addr interp_head;
Elf::Object::sptr libpython;
Elf::Addr libpythonAddr;
const PyInterpInfo &info;
std::map<const _typeobject *, const PythonTypePrinter<PyV> *> printers;
bool interpFound() const; // returns true if the printer could find the interpreter.
};
Expand Down
1 change: 0 additions & 1 deletion python.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ PythonPrinter<PyV>::PythonPrinter(Procman::Process &proc_, std::ostream &os_, co
, interp_head(info_.interpreterHead)
, libpython(info_.libpython)
, libpythonAddr(info_.libpythonAddr)
, info(info_)
{
if (!interpFound())
return;
Expand Down

0 comments on commit e804f26

Please sign in to comment.