2 # Copyright 2012 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """"Processes a log file and resolves IPC message identifiers.
8 Resolves IPC messages of the form [unknown type NNNNNN] to named IPC messages.
10 e.g. logfile containing
12 I/stderr ( 3915): ipc 3915.3.1370207904 2147483647 S [unknown type 66372]
14 will be transformed to:
16 I/stderr ( 3915): ipc 3915.3.1370207904 2147483647 S ViewMsg_SetCSSColors
18 In order to find the message header files efficiently, it requires that
19 Chromium is checked out using git.
22 from __future__ import print_function
32 """Get chromium's source directory."""
33 return os.path.join(sys.path[0], '..')
37 """Read from file f and generate right-stripped lines."""
42 def _GetMsgStartTable():
43 """Read MsgStart enumeration from ipc/ipc_message_utils.h.
45 Determines the message type identifiers by reading.
46 header file ipc/ipc_message_utils.h and looking for
47 enum IPCMessageStart. Assumes following code format in header file:
48 enum IPCMessageStart {
54 A dictionary mapping StartName to enumeration value.
56 ipc_message_file = _SourceDir() + '/ipc/ipc_message_utils.h'
57 ipc_message_lines = _ReadLines(open(ipc_message_file))
60 msg_start_table = dict()
61 for line in ipc_message_lines:
63 if line.strip() == '};':
65 msgstart_index = line.find('MsgStart')
66 msg_type = line[:msgstart_index] + 'MsgStart'
67 msg_start_table[msg_type.strip()] = count
69 elif line.strip() == 'enum IPCMessageStart {':
72 return msg_start_table
75 def _FindMessageHeaderFiles():
76 """Look through the source directory for *_messages.h."""
77 os.chdir(_SourceDir())
78 pipe = subprocess.Popen(['git', 'ls-files', '--', '*_messages.h'],
79 stdout=subprocess.PIPE)
80 return _ReadLines(pipe.stdout)
83 def _GetMsgId(msg_start, line_number, msg_start_table):
84 """Construct the meessage id given the msg_start and the line number."""
85 hex_str = '%x%04x' % (msg_start_table[msg_start], line_number)
86 return int(hex_str, 16)
89 def _ReadHeaderFile(f, msg_start_table, msg_map):
90 """Read a header file and construct a map from message_id to message name."""
91 msg_def_re = re.compile(
92 '^IPC_(?:SYNC_)?MESSAGE_[A-Z0-9_]+\(([A-Za-z0-9_]+).*')
93 msg_start_re = re.compile(
94 '^\s*#define\s+IPC_MESSAGE_START\s+([a-zA-Z0-9_]+MsgStart).*')
101 match = re.match(msg_start_re, line)
103 msg_start = match.group(1)
104 # print("msg_start = " + msg_start)
105 match = re.match(msg_def_re, line)
107 msg_name = match.group(1)
108 # print("msg_name = " + msg_name)
109 if msg_start and msg_name:
110 msg_id = _GetMsgId(msg_start, line_number, msg_start_table)
111 msg_map[msg_id] = msg_name
115 def _ResolveMsg(msg_type, msg_map):
116 """Fully resolve a message type to a name."""
117 if msg_type in msg_map:
118 return msg_map[msg_type]
120 return '[Unknown message %d (0x%x)]x' % (msg_type, msg_type)
123 def _ProcessLog(f, msg_map):
124 """Read lines from f and resolve the IPC messages according to msg_map."""
125 unknown_msg_re = re.compile('\[unknown type (\d+)\]')
128 match = re.search(unknown_msg_re, line)
130 line = re.sub(unknown_msg_re,
131 _ResolveMsg(int(match.group(1)), msg_map),
137 """Returns a dictionary mapping from message number to message name."""
138 msg_start_table = _GetMsgStartTable()
140 for header_file in _FindMessageHeaderFiles():
141 _ReadHeaderFile(open(header_file),
148 """Processes one or more log files with IPC logging messages.
150 Replaces '[unknown type NNNNNN]' with resolved
153 Reads from standard input if no log files specified on the
156 parser = optparse.OptionParser('usage: %prog [LOGFILE...]')
157 (_, args) = parser.parse_args()
159 msg_map = _GetMsgMap()
163 for log_file in log_files:
164 _ProcessLog(open(log_file), msg_map)
166 _ProcessLog(sys.stdin, msg_map)
169 if __name__ == '__main__':