This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] Make it easier to call into inferior from python when "this" pointer is already available?
- From: ppluzhnikov at google dot com (Paul Pluzhnikov)
- To: archer at sourceware dot org
- Date: Thu, 18 Dec 2008 12:11:23 -0800 (PST)
- Subject: [python] Make it easier to call into inferior from python when "this" pointer is already available?
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=google.com; s=beta;t=1229631087; bh=qFNRf7eek2FhHW999uQE7BU6taI=;h=DomainKey-Signature:To:Subject:Message-Id:Date:From; b=Fu6vcM+pNtQoosJ0StT+QhlE3AD4tXzWxH1UZHnLz3tAek5qc/jg9yuhLnJCicqua2/2WOfeNSTCtdbpOqLSyQ==
- Domainkey-signature: a=rsa-sha1; s=beta; d=google.com; c=nofws; q=dns;h=to:subject:message-id:date:from;b=cHY4WCcEyaQPl1Kiq4dKMD6ml9EmwttOKGq9l7OQOEEw/tseokuMxi4WDyTyANTu268qlzCn8yLSjFT3q8pjcA==
Greetings,
Consider this source:
/// --- cut ---
#include <string>
#include <vector>
#include <string.h>
using namespace std;
struct Buffer
{
Buffer(const string& name);
string str() const;
private:
void *p_;
};
int main()
{
vector<Buffer> v;
v.push_back(Buffer("abcdefghijklmop"));
v.push_back(Buffer("qrstuvwxyz"));
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:
self.val.parse_and_eval_subexpr("->str()")
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.
Comments?
--
Paul Pluzhnikov