This is the mail archive of the mailing list for the Archer project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] Make it easier to call into inferior from python when "this" pointer is already available?


Consider this source:

/// --- cut ---
#include <string>
#include <vector>
#include <string.h>

using namespace std;

struct Buffer 
  Buffer(const string& name);
  string str() const;
  void *p_;

int main()
  vector<Buffer> v;

  return 0;

/// Unimportant implementation details....
Buffer::Buffer(const string& name) : p_(new string(name)) { }
string Buffer::str() const
  return *(string*)p_;
/// --- cut ---

Let's assume for now that transformation of Buffer.p_ into textual
representation is:
- already available in the inferior, and
- complicated enough that we don't want to redo it in Python.

Given above, I can do a pretty-printer like this:

### cut ###
class BufferPrinter:

  def __init__(self, val):
    self.val = val

  def to_string(self):
    expr = "Buffer::str(%s)" % str(self.val.address())
    return gdb.parse_and_eval(expr)

  def display_hint(self):
    return 'string'

gdb.pretty_printers['^Buffer$'] = lambda val: BufferPrinter(val)
### cut ###

This works, but is somewhat "unnatural": we already have 'self.val',
so a more intuitive expression would be something like:


or some such. It would probably also be less work for GDB, since
Python is already holding a value for part of the evaluation stack.


Paul Pluzhnikov

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]