*** empty log message ***
[arla.git] / nnpfs / readtrace.py
blob33066e04408b2d1c04fe419e470ff30197f85125
1 #!/usr/bin/python
3 import struct
4 import sys
5 import time
7 byteorder = "="
8 no_offset = 2**64-1
10 def print_offset(offset):
11 if offset == no_offset:
12 return "NNPFS_NO_OFFSET"
13 return "%u" % offset
15 def print_handle(handle):
16 (a, b, c, d) = struct.unpack(byteorder + "LLLL", handle)
17 return "(%d, %d, %d, %d)" % (a, b, c, d)
19 def print_block_handle(handle):
20 (a, b, c, d) = struct.unpack(byteorder + "LLLL", handle[0:16])
21 (offset, ) = struct.unpack(byteorder + "Q", handle[16:24])
22 return "(%d, %d, %d, %d) @%s" % (a, b, c, d, print_offset(offset))
24 def print_asciz(s):
25 return "\"" + s.split("\0", 1)[0] + "\""
27 def print_time(sec):
28 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sec))
30 def print_valid(s, valid, shift):
31 mask = 1 << shift
32 if valid & mask == mask:
33 return s
34 else:
35 return "(" + s + ")"
37 def print_attr(data):
38 (valid,mode,nlink,type,uid,gid,atime,mtime,ctime,fileid,size) = struct.unpack(byteorder + "LLLLLLLLLLQ", data)
39 l1 = []
40 l2 = []
41 l1.append(print_valid("mode %o" % mode, valid, 0))
42 l1.append(print_valid("lnks %d" % nlink, valid, 1))
43 l1.append(print_valid("type %d" % type, valid, 2))
44 l1.append(print_valid("uid %d" % uid, valid, 3))
45 l1.append(print_valid("gid %d" % gid, valid, 4))
46 l1.append(print_valid("fid %d" % fileid, valid, 8))
47 l1.append(print_valid("size %d" % size, valid, 9))
49 l2.append(print_valid("atime %s" % print_time(atime), valid, 5))
50 l2.append(print_valid("mtime %s" % print_time(mtime), valid, 6))
51 l2.append(print_valid("ctime %s" % print_time(ctime), valid, 7))
52 if len(l1) > 0:
53 return (" ".join(l1), " ".join(l2))
54 else:
55 return (" ".join(l2), "")
57 def print_right(right):
58 s = ""
59 if (right & 0x1) == 0x1:
60 s = s + "r"
61 if (right & 0x2) == 0x2:
62 s = s + "w"
63 if (right & 0x4) == 0x4:
64 s = s + "x"
65 if s == "":
66 s = "none"
67 return s
69 def print_rights(iddata, rightsdata):
70 ids = struct.unpack(byteorder + "8L", iddata)
71 rights = struct.unpack(byteorder + "8H", rightsdata)
72 ret = []
73 for id, right in zip(ids, rights):
74 if (id != 4):
75 ret.append("%d:%s" % (id, print_right(right)))
76 return " ".join(ret)
78 def bitfield_print(field, template):
79 s = []
80 for (bit, t) in template:
81 if (field & bit) == bit:
82 s.append(t)
83 return s
85 def print_tokens(tokens):
86 template = [
87 (0x0001, "NR"),
88 (0x0002, "SR"),
89 (0x0004, "NW"),
90 (0x0008, "EW"),
91 (0x0010, "A_R"),
92 (0x0020, "A_W"),
93 (0x0040, "D_R"),
94 (0x0080, "D_W"),
95 (0x0100, "L_R"),
96 (0x0200, "L_W")
98 return "|".join(bitfield_print(tokens, template))
100 def print_node(data):
101 handle = print_handle(data[0:16])
102 (tokens,pad1) = struct.unpack(byteorder + "LL", data[16:24])
103 (attr1, attr2) = print_attr(data[24:72])
104 rights = print_rights(data[72:104], data[104:120])
105 (anonrights,) = struct.unpack(byteorder + "H", data[120:122])
106 anonrights_str = print_right(anonrights)
107 return ("handle %s tokens %s anonrights %s rights %s" % (handle, print_tokens(tokens), anonrights_str, rights), attr1, attr2)
109 def process_wakeup(data):
110 (seqno, error, len) = struct.unpack(byteorder + "LLL", data[0:12])
111 print "wakeup seqno", seqno, "error", error
113 def process_getroot(data):
114 (uid, pag) = struct.unpack(byteorder + "LL", data[0:8])
115 print "getroot uid", uid, "pag", pag
117 def process_getnode(data):
118 (uid, pag) = struct.unpack(byteorder + "LL", data[0:8])
119 handle = print_handle(data[8:24])
120 name = data[24:]
121 print "getnode uid", uid, "pag", pag, "parenthandle", handle, "name", print_asciz(name)
123 def process_inactivenode(data):
124 handle = print_handle(data[0:16])
125 (flag,) = struct.unpack(byteorder + "L", data[16:20])
126 flagtexts = []
127 if (flag & 0x1):
128 flagtexts.append("norefs")
129 if (flag & 0x2):
130 flagtexts.append("delete")
131 print "inactivenode handle", handle, "flags", "|".join(flagtexts)
133 def process_open(data):
134 (uid, pag) = struct.unpack(byteorder + "LL", data[0:8])
135 handle = print_handle(data[8:24])
136 (tokens, ) = struct.unpack(byteorder + "L", data[24:28])
137 print "open uid", uid, "pag", pag, "handle", handle, "tokens", print_tokens(tokens)
139 def process_version(data):
140 print "version"
142 def process_installroot(data):
143 (line1, line2, line3) = print_node(data[0:128])
144 print "installroot " + line1
145 print " " + line2
146 print " " + line3
148 def process_installnode(data):
149 handle = print_handle(data[0:16])
150 name = print_asciz(data[16:272])
151 (line1, line2, line3) = print_node(data[272:400])
152 print "installnode parenthandle %s name %s" % (handle, name)
153 print " " + line1
154 print " " + line2
155 print " " + line3
157 def process_getdata(data):
158 (uid, pag) = struct.unpack(byteorder + "LL", data[0:8])
159 handle = print_handle(data[8:24])
160 (tokens, pad1) = struct.unpack(byteorder + "LL", data[24:32])
161 (offset, len) = struct.unpack(byteorder + "QQ", data[32:48])
162 print "getdata uid", uid, "pag", pag, "handle", handle, "tokens", print_tokens(tokens)
163 print "offset", offset, "len", len
165 def process_installdata(data):
166 # don't print cache handle
167 (line1, line2, line3) = print_node(data[0:128])
168 (flag, id) = struct.unpack(byteorder + "LL", data[128:136])
169 (offset,) = struct.unpack(byteorder + "Q", data[136:144])
170 off = print_offset(offset)
171 template = [
172 (0x0001, "INVALID_DNLC"),
173 (0x0002, "AFSDIR"),
174 (0x0004, "HANDLE_VALID")
176 print "installdata " + line1
177 print " " + line2
178 print " " + line3
179 print " flag", "|".join(bitfield_print(flag, template)), "offset", off, "id", id
181 def process_deletenode(data):
182 handle = print_handle(data[0:16])
183 print "deletenode handle", handle
185 def process_putdata(data):
186 handle = print_handle(data[0:16])
187 (attr1, attr2) = print_attr(data[16:64])
188 (uid, pag) = struct.unpack(byteorder + "LL", data[64:72])
189 (flag, pad1) = struct.unpack(byteorder + "LL", data[72:80])
190 (offset, len) = struct.unpack(byteorder + "QQ", data[80:96])
191 print "putdata handle", handle, "offset", offset, "len", len
192 print "uid", uid, "pag", pag, "flag", flag
193 print " " + attr1
194 print " " + attr2
196 def process_create(data):
197 handle = print_handle(data[0:16])
198 name = print_asciz(data[16:272])
199 (attr1, attr2) = print_attr(data[272:320])
200 (mode, pad1) = struct.unpack(byteorder + "LL", data[320:328])
201 (uid, pag) = struct.unpack(byteorder + "LL", data[328:336])
202 print "create parent", handle, "mode", mode, "uid", uid, "pag", pag, "name", name
203 print " " + attr1
204 print " " + attr2
206 def process_putattr(data):
207 handle = print_handle(data[0:16])
208 (attr1, attr2) = print_attr(data[16:64])
209 (uid, pag) = struct.unpack(byteorder + "LL", data[64:72])
210 print "putattr handle", handle, "uid", uid, "pag", pag
211 print " " + attr1
212 print " " + attr2
214 def process_installattr(data):
215 (line1, line2, line3) = print_node(data[0:128])
216 (flag, pad1) = struct.unpack(byteorder + "LL", data[128:136])
217 print "installattr " + line1
218 print " " + line2
219 print " " + line3
220 print " flag", flag
222 def process_getattr(data):
223 (uid, pag) = struct.unpack(byteorder + "LL", data[0:8])
224 handle = print_handle(data[8:24])
225 print "getattr handle", handle, "uid", uid, "pag", pag
227 def process_invalidnode(data):
228 handle = print_handle(data[0:16])
229 print "invalidnode handle", handle
231 def process_mkdir(data):
232 handle = print_handle(data[0:16])
233 name = print_asciz(data[16:272])
234 (attr1, attr2) = print_attr(data[272:320])
235 (uid, pag) = struct.unpack(byteorder + "LL", data[320:328])
236 print "mkdir parent", handle, "uid", uid, "pag", pag, "name", name
237 print " " + attr1
238 print " " + attr2
240 def process_link(data):
241 parenthandle = print_handle(data[0:16])
242 name = print_asciz(data[16:272])
243 fromhandle = print_handle(data[272:288])
244 (uid, pag) = struct.unpack(byteorder + "LL", data[288:296])
245 print "link parent", parenthandle, "fromhandle", fromhandle, "name", name
247 def process_symlink(data):
248 parenthandle = print_handle(data[0:16])
249 name = print_asciz(data[16:272])
250 content = print_asciz(data[272:2320])
251 (attr1, attr2) = print_attr(data[2320:2368])
252 (uid, pag) = struct.unpack(byteorder + "LL", data[2368:2376])
253 print "symlink parent", parenthandle, "uid", uid, "pag", pag
254 print " " + name + "->" + content
255 print " " + attr1
256 print " " + attr2
258 def process_remove(data):
259 parenthandle = print_handle(data[0:16])
260 name = print_asciz(data[16:272])
261 (uid, pag) = struct.unpack(byteorder + "LL", data[272:280])
262 print "remove parent", parenthandle, "uid", uid, "pag", pag, "name", name
264 def process_rmdir(data):
265 parenthandle = print_handle(data[0:16])
266 name = print_asciz(data[16:272])
267 (uid, pag) = struct.unpack(byteorder + "LL", data[272:280])
268 print "rmdir parent", parenthandle, "uid", uid, "pag", pag, "name", name
270 def process_rename(data):
271 oldparenthandle = print_handle(data[0:16])
272 oldname = print_asciz(data[16:272])
273 newparenthandle = print_handle(data[272:288])
274 newname = print_asciz(data[288:544])
275 (uid, pag) = struct.unpack(byteorder + "LL", data[544:552])
276 print "rename oldparent", oldparenthandle, "oldname", oldname
277 print " newparent", newparenthandle, "newname", newname
278 print " uid", uid, "pag", pag
280 def ioctl_decode(opcode):
281 nr = opcode & 0xff
282 type = (opcode >> 8) & 0xff
283 size = (opcode >> 16) & 0x3fff
284 dir = (opcode >> 30) & 0x3
285 if (type > 64 and type < 91):
286 typeprint = "%d(%s)" % (type, chr(type))
287 else:
288 typeprint = "%d" % type
289 return (nr, type, typeprint, size, dir)
291 def pioctl_settok(data):
292 return "settok"
294 def pioctl_flush(data):
295 return "flush"
297 def pioctl_afs_delete_mt_pt(data):
298 return "afs_delete_mt_pt"
300 def pioctl_getfid(data):
301 return "getfid"
303 def pioctl_file_cell_name(data):
304 return "file_cell_name"
306 def print_pioctl(opcode, data):
307 try:
308 return {(3, 86, 12, 1): pioctl_settok,
309 (6, 86, 12, 1): pioctl_flush,
310 (22, 86, 12, 1): pioctl_getfid,
311 (30, 86, 12, 1): pioctl_file_cell_name,
312 (28, 86, 12, 1): pioctl_afs_delete_mt_pt} [opcode] (data)
313 except KeyError:
314 return " Unknown opcode" + str(opcode)
316 def process_pioctl(data):
317 (opcode, pad1) = struct.unpack(byteorder + "LL", data[0:8])
318 (uid, pag) = struct.unpack(byteorder + "LL", data[8:16])
319 (insize, outsize) = struct.unpack(byteorder + "LL", data[16:24])
320 msg = data[24:2072]
321 handle = print_handle(data[2072:2088])
322 print "pioctl opcode %08x uid %d pag %d insize %d outsize %d" % (opcode, uid, pag, insize, outsize)
323 print " handle", handle
324 if 0:
325 # this is linux specific now
326 (nr, type, typeprint, size, dir) = ioctl_decode(opcode)
327 print " type", typeprint, "nr", nr, "size", size, "dir", dir
328 print " " + print_pioctl((nr, type, size, dir), msg)
329 s = []
330 for i in msg[0:insize]:
331 s.append("%02x" % ord(i))
332 while len(s) > 8:
333 print " " + " ".join(s[0:8])
334 s = s[8:]
335 print " " + " ".join(s)
337 def process_updatefid(data):
338 oldhandle = print_handle(data[0:16])
339 newhandle = print_handle(data[16:32])
340 print "updatefid oldhandle %s newhandle %s" % (oldhandle, newhandle)
342 def process_advlock(data):
343 print "advlock"
345 def process_gc(data):
346 (len, pad) = struct.unpack(byteorder + "LL", data[0:8])
347 print "gc, len", len
348 offset = 8
349 count = 0
350 while count < len:
351 print print_block_handle(data[offset:offset+24])
352 offset = offset + 24
353 count = count + 1
355 def process_appenddata(data):
356 handle = print_block_handle(data[0:24])
357 print "appenddata", handle
359 def process_deletedata(data):
360 handle = print_block_handle(data[0:24])
361 print "deletedata", handle
363 def process_accesses(data):
364 print "accesses"
366 def process_installquota(data):
367 (appendquota,) = struct.unpack(byteorder + "Q", data[0:8])
368 print "installquota", "append", appendquota
370 def process_message(size, opcode, seqno, data):
371 sys.stdout.write (" %4d:" % seqno)
372 try:
373 { 0: process_version,
374 1: process_wakeup,
375 2: process_getroot,
376 3: process_installroot,
377 4: process_getnode,
378 5: process_installnode,
379 6: process_getattr,
380 7: process_installattr,
381 8: process_getdata,
382 9: process_installdata,
383 10: process_inactivenode,
384 11: process_invalidnode,
385 12: process_open,
386 13: process_putdata,
387 14: process_putattr,
388 15: process_create,
389 16: process_mkdir,
390 17: process_link,
391 18: process_symlink,
392 19: process_remove,
393 20: process_rmdir,
394 21: process_rename,
395 22: process_pioctl,
396 23: process_updatefid,
397 24: process_advlock,
398 25: process_gc,
399 26: process_deletenode,
400 27: process_appenddata,
401 28: process_deletedata,
402 29: process_accesses,
403 30: process_installquota} [opcode] (data)
404 except KeyError:
405 print " Unknown opcode", opcode
407 while 1:
408 data = sys.stdin.read(16)
409 if len(data) != 16:
410 break
411 (type, sec, usec, length) = struct.unpack('!LLLL', data)
412 if type == 0x00000001:
413 typetext = "nnpfs"
414 byteorder = ">"
415 elif type == 0x00000002:
416 typetext = "arlad"
417 byteorder = ">"
418 elif type == 0x01000000:
419 typetext = "nnpfs"
420 byteorder = "<"
421 elif type == 0x02000000:
422 typetext = "arlad"
423 byteorder = "<"
424 else:
425 sys.exit(1)
427 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(sec)) + ".%06d" % usec, typetext, "len", length
428 messages = sys.stdin.read(length)
429 if len(messages) != length:
430 break
431 while messages:
432 header = messages[0:16]
433 (size, opcode, seqno, pad1) = struct.unpack(byteorder + "LLLL", header)
434 message = messages[0:size]
435 process_message(size, opcode, seqno, message[16:])
436 print
437 messages = messages[size:]