我如何在gdb中调用gdb.parse_and_eval()编辑的变量的StdDequePrinter 手动?为什么StdDequePrinter等不具有仅查询容器的[[size的功能?
#include <stack>
int
main(
int,
char**)
{ std::stack<int> data;
data.push(1);
data.push(2);
data.push(3);
data.push(4);
data.pop();
return 0;
}
测试漂亮打印机:
import re class Printer: "Print a std::deque" def __init__(self, val): self.typename = val.type.template_argument(0) self.val = val self.elttype = val.type.template_argument(0) size = self.elttype.sizeof if size < 512: self.buffer_size = int (512 / size) else: self.buffer_size = 1 start = self.val['_M_impl']['_M_start'] end = self.val['_M_impl']['_M_finish'] delta_n = end['_M_node'] - start['_M_node'] - 1 delta_s = start['_M_last'] - start['_M_cur'] delta_e = end['_M_cur'] - end['_M_first'] self.size = self.buffer_size * delta_n + delta_s + delta_e def calcSize(self): return int(self.size) # use modified version of STL pretty printer to get the size() value = gdb.parse_and_eval('data.c'); printer = Printer(value) print (str(printer.calcSize())) # how to get the original printer - but it can't print size value only printer = gdb.default_visualizer(value) print (str(printer)) printer.__init__(value.__class__.__name__,value) print (str(printer.to_string())) # get the size only sizeOnly = re.search('.* with ([0-9]+) elements', printer.to_string(), re.IGNORECASE) if sizeOnly: print(sizeOnly.group(1)) # no need to know stack is implemented on top of deque value = gdb.parse_and_eval('data') printer = gdb.default_visualizer(value) print (str(printer)) printer.__init__(value.__class__.__name__,value) print (printer.to_string()) sizeOnly = re.search('.* with ([0-9]+) elements', printer.to_string(), re.IGNORECASE) if sizeOnly: print(sizeOnly.group(1))
输出:
Breakpoint 1, main () at ../main.cpp:19 19 data.push(4); (gdb) source Debug/test.py 3 <libstdcxx.v6.printers.StdDequePrinter object at 0x7f543e0aee80> Value with 3 elements 3 <libstdcxx.v6.printers.StdStackOrQueuePrinter object at 0x7f1f00bc7208> Value wrapping: std::deque with 3 elements 3