aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Berg <johannes@sipsolutions.net>2010-05-20 04:35:28 -0400
committerJohannes Berg <johannes@sipsolutions.net>2010-05-25 07:17:03 -0400
commitdf4d5ba4cb86f1055160559c9b7024e3dbf76a38 (patch)
treeeb68c693bf72b67699f622f4549f9228c9d85059
parentf45a7087dc1b44d8e2572e4290b18ce00e8c228f (diff)
python: improve string handling
This adds ev.str_field('foo') as a non-raising version of str(ev['foo']) that this patch also enables. A new C function is necessary because str(ev['foo'].data) may contain the NUL bytes that should have terminated the C strings. Suggested-by: Pierre Tardy <tardyp@gmail.com> Acked-by: Darren Hart <dvhltc@us.ibm.com> Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
-rw-r--r--ctracecmd.i26
-rw-r--r--tracecmd.py8
2 files changed, 34 insertions, 0 deletions
diff --git a/ctracecmd.i b/ctracecmd.i
index 9569419..624fb63 100644
--- a/ctracecmd.i
+++ b/ctracecmd.i
@@ -69,6 +69,32 @@ static PyObject *py_field_get_data(struct format_field *f, struct record *r)
69 return PyBuffer_FromMemory((char *)r->data + f->offset, f->size); 69 return PyBuffer_FromMemory((char *)r->data + f->offset, f->size);
70} 70}
71 71
72static PyObject *py_field_get_str(struct format_field *f, struct record *r)
73{
74 if (!strncmp(f->type, "__data_loc ", 11)) {
75 unsigned long long val;
76 int offset;
77
78 if (pevent_read_number_field(f, r->data, &val)) {
79 PyErr_SetString(PyExc_TypeError,
80 "Field is not a valid number");
81 return NULL;
82 }
83
84 /*
85 * The actual length of the dynamic array is stored
86 * in the top half of the field, and the offset
87 * is in the bottom half of the 32 bit field.
88 */
89 offset = val & 0xffff;
90
91 return PyString_FromString((char *)r->data + offset);
92 }
93
94 return PyString_FromStringAndSize((char *)r->data + f->offset,
95 strnlen((char *)r->data + f->offset, f->size));
96}
97
72static PyObject *py_format_get_keys(struct event_format *ef) 98static PyObject *py_format_get_keys(struct event_format *ef)
73{ 99{
74 PyObject *list; 100 PyObject *list;
diff --git a/tracecmd.py b/tracecmd.py
index b14a3b9..ad80ccd 100644
--- a/tracecmd.py
+++ b/tracecmd.py
@@ -110,6 +110,11 @@ class Event(object):
110 return None 110 return None
111 return val 111 return val
112 112
113 def str_field(self, name):
114 f = pevent_find_any_field(self._format, name)
115 if f is None:
116 return None
117 return py_field_get_str(f, self._record)
113 118
114class TraceSeq(object): 119class TraceSeq(object):
115 def __init__(self, trace_seq): 120 def __init__(self, trace_seq):
@@ -138,6 +143,9 @@ class Field(object):
138 return val 143 return val
139 __int__ = __long__ 144 __int__ = __long__
140 145
146 def __str__(self):
147 return py_field_get_str(self._field, self._record)
148
141class PEvent(object): 149class PEvent(object):
142 def __init__(self, pevent): 150 def __init__(self, pevent):
143 self._pevent = pevent 151 self._pevent = pevent