blob: cc3e97f980323167297634351d261bf9c273ba6f [file] [log] [blame]
Ken Mixter20d9e472010-08-12 10:58:46 -07001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Dale Curtis497c2cb2010-11-16 13:44:33 -08005import logging, os, re, shutil
Ken Mixter20d9e472010-08-12 10:58:46 -07006from autotest_lib.client.bin import site_log_reader, site_utils, test
7from autotest_lib.client.common_lib import error, utils
8
9
10class CrashTest(test.test):
11
12 _CONSENT_FILE = '/home/chronos/Consent To Send Stats'
Ken Mixterddcd92d2010-11-01 19:07:08 -070013 _CORE_PATTERN = '/proc/sys/kernel/core_pattern'
Ken Mixter20d9e472010-08-12 10:58:46 -070014 _CRASH_REPORTER_PATH = '/sbin/crash_reporter'
15 _CRASH_SENDER_PATH = '/sbin/crash_sender'
16 _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender'
17 _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid'
18 _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending'
Ken Mixter38dfe852010-08-18 15:24:00 -070019 _PAUSE_FILE = '/var/lib/crash_sender_paused'
Ken Mixter20d9e472010-08-12 10:58:46 -070020 _SYSTEM_CRASH_DIR = '/var/spool/crash'
21 _USER_CRASH_DIR = '/home/chronos/user/crash'
22
Ken Mixter4f619652010-10-18 12:11:18 -070023 def _set_system_sending(self, is_enabled):
24 """Sets whether or not the system crash_sender is allowed to run.
25
26 crash_sender may still be allowed to run if _set_child_sending is
27 called with true and it is run as a child process."""
Ken Mixter20d9e472010-08-12 10:58:46 -070028 if is_enabled:
29 if os.path.exists(self._PAUSE_FILE):
30 os.remove(self._PAUSE_FILE)
31 else:
32 utils.system('touch ' + self._PAUSE_FILE)
33
34
Ken Mixter4f619652010-10-18 12:11:18 -070035 def _set_child_sending(self, is_enabled):
36 """Overrides crash sending enabling for child processes."""
37 if is_enabled:
38 os.environ['OVERRIDE_PAUSE_SENDING'] = "1"
39 else:
40 del os.environ['OVERRIDE_PAUSE_SENDING']
41
42
Ken Mixter20d9e472010-08-12 10:58:46 -070043 def _reset_rate_limiting(self):
44 utils.system('rm -rf ' + self._CRASH_SENDER_RATE_DIR)
45
46
47 def _clear_spooled_crashes(self):
48 utils.system('rm -rf ' + self._SYSTEM_CRASH_DIR)
49 utils.system('rm -rf ' + self._USER_CRASH_DIR)
50
51
52 def _kill_running_sender(self):
53 if not os.path.exists(self._CRASH_SENDER_RUN_PATH):
54 return
55 running_pid = int(utils.read_file(self._CRASH_SENDER_RUN_PATH))
56 logging.warning('Detected running crash sender (%d), killing' %
57 running_pid)
58 utils.system('kill -9 %d' % running_pid)
59 os.remove(self._CRASH_SENDER_RUN_PATH)
60
61
62 def _set_sending_mock(self, mock_enabled, send_success=True):
63 if mock_enabled:
64 if send_success:
65 data = ''
66 else:
67 data = '1'
68 logging.info('Setting sending mock')
69 utils.open_write_close(self._MOCK_CRASH_SENDING, data)
70 else:
71 utils.system('rm -f ' + self._MOCK_CRASH_SENDING)
72
73
74 def _set_consent(self, has_consent):
75 if has_consent:
76 utils.open_write_close(self._CONSENT_FILE, 'test-consent')
77 logging.info('Created ' + self._CONSENT_FILE)
78 else:
79 utils.system('rm -f "%s"' % (self._CONSENT_FILE))
80
81
82 def _get_pushed_consent_file_path(self):
83 return os.path.join(self.bindir, 'pushed_consent')
84
85
86 def _push_consent(self):
87 if os.path.exists(self._CONSENT_FILE):
Dale Curtis497c2cb2010-11-16 13:44:33 -080088 shutil.move(self._CONSENT_FILE,
89 self._get_pushed_consent_file_path())
Ken Mixter20d9e472010-08-12 10:58:46 -070090
91
92 def _pop_consent(self):
93 self._set_consent(False)
94 if os.path.exists(self._get_pushed_consent_file_path()):
Dale Curtis497c2cb2010-11-16 13:44:33 -080095 shutil.move(self._get_pushed_consent_file_path(),
96 self._CONSENT_FILE)
Ken Mixter20d9e472010-08-12 10:58:46 -070097
98
99 def _get_crash_dir(self, username):
100 if username == 'chronos':
101 return self._USER_CRASH_DIR
102 else:
103 return self._SYSTEM_CRASH_DIR
104
105
106 def _initialize_crash_reporter(self):
107 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH)
Ken Mixterddcd92d2010-11-01 19:07:08 -0700108 # Completely disable crash_reporter from generating crash dumps
109 # while any tests are running, otherwise a crashy system can make
110 # these tests flaky.
111 self.enable_crash_filtering('none')
Ken Mixter20d9e472010-08-12 10:58:46 -0700112
113
Ken Mixter67ff5622010-09-30 15:32:17 -0700114 def write_crash_dir_entry(self, name, contents):
Ken Mixter20d9e472010-08-12 10:58:46 -0700115 entry = os.path.join(self._SYSTEM_CRASH_DIR, name)
116 if not os.path.exists(self._SYSTEM_CRASH_DIR):
117 os.makedirs(self._SYSTEM_CRASH_DIR)
Ken Mixter67ff5622010-09-30 15:32:17 -0700118 utils.open_write_close(entry, contents)
Ken Mixter20d9e472010-08-12 10:58:46 -0700119 return entry
120
121
Ken Mixter1a894e02010-10-28 15:42:52 -0700122 def write_fake_meta(self, name, exec_name, payload, complete=True):
123 last_line = ''
124 if complete:
125 last_line = 'done=1\n'
Ken Mixter67ff5622010-09-30 15:32:17 -0700126 return self.write_crash_dir_entry(name,
127 'exec_name=%s\n'
128 'ver=my_ver\n'
Ken Mixter1a894e02010-10-28 15:42:52 -0700129 'payload=%s\n'
130 '%s' % (exec_name, payload,
131 last_line))
Ken Mixter67ff5622010-09-30 15:32:17 -0700132
133
Ken Mixter20d9e472010-08-12 10:58:46 -0700134 def _prepare_sender_one_crash(self,
135 send_success,
136 reports_enabled,
137 username,
Ken Mixter38dfe852010-08-18 15:24:00 -0700138 report):
Ken Mixter20d9e472010-08-12 10:58:46 -0700139 self._set_sending_mock(mock_enabled=True, send_success=send_success)
140 self._set_consent(reports_enabled)
Ken Mixter38dfe852010-08-18 15:24:00 -0700141 if report is None:
Ken Mixter1a894e02010-10-28 15:42:52 -0700142 payload = self.write_crash_dir_entry('fake.dmp', '')
143 report = self.write_fake_meta('fake.meta', 'fake', payload)
Ken Mixter38dfe852010-08-18 15:24:00 -0700144 return report
Ken Mixter20d9e472010-08-12 10:58:46 -0700145
146
147 def _parse_sender_output(self, output):
148 """Parse the log output from the crash_sender script.
149
150 This script can run on the logs from either a mocked or true
151 crash send.
152
153 Args:
154 output: output from the script
155
156 Returns:
157 A dictionary with these values:
Ken Mixter38dfe852010-08-18 15:24:00 -0700158 exec_name: name of executable which crashed
Ken Mixter67ff5622010-09-30 15:32:17 -0700159 meta_path: path to the report metadata file
160 output: the output from the script, copied
Ken Mixter38dfe852010-08-18 15:24:00 -0700161 report_kind: kind of report sent (minidump vs kernel)
Ken Mixter20d9e472010-08-12 10:58:46 -0700162 send_attempt: did the script attempt to send a crash.
163 send_success: if it attempted, was the crash send successful.
Ken Mixterd79140e2010-10-26 14:45:30 -0700164 sig: signature of the report, if given.
Ken Mixter20d9e472010-08-12 10:58:46 -0700165 sleep_time: if it attempted, how long did it sleep before
166 sending (if mocked, how long would it have slept)
Ken Mixter20d9e472010-08-12 10:58:46 -0700167 """
168 sleep_match = re.search('Scheduled to send in (\d+)s', output)
169 send_attempt = sleep_match is not None
170 if send_attempt:
171 sleep_time = int(sleep_match.group(1))
172 else:
173 sleep_time = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700174 meta_match = re.search('Metadata: (\S+) \((\S+)\)', output)
175 if meta_match:
176 meta_path = meta_match.group(1)
177 report_kind = meta_match.group(2)
Ken Mixter38dfe852010-08-18 15:24:00 -0700178 else:
Ken Mixter67ff5622010-09-30 15:32:17 -0700179 meta_path = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700180 report_kind = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700181 payload_match = re.search('Payload: (\S+)', output)
182 if payload_match:
183 report_payload = payload_match.group(1)
184 else:
185 report_payload = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700186 exec_name_match = re.search('Exec name: (\S+)', output)
187 if exec_name_match:
188 exec_name = exec_name_match.group(1)
189 else:
190 exec_name = None
Ken Mixter10c48672010-11-01 13:37:08 -0700191 sig_match = re.search('sig: (\S+)', output)
Ken Mixterd79140e2010-10-26 14:45:30 -0700192 if sig_match:
193 sig = sig_match.group(1)
194 else:
195 sig = None
Ken Mixter20d9e472010-08-12 10:58:46 -0700196 send_success = 'Mocking successful send' in output
Ken Mixter38dfe852010-08-18 15:24:00 -0700197 return {'exec_name': exec_name,
198 'report_kind': report_kind,
Ken Mixter67ff5622010-09-30 15:32:17 -0700199 'meta_path': meta_path,
200 'report_payload': report_payload,
Ken Mixter38dfe852010-08-18 15:24:00 -0700201 'send_attempt': send_attempt,
Ken Mixter20d9e472010-08-12 10:58:46 -0700202 'send_success': send_success,
Ken Mixterd79140e2010-10-26 14:45:30 -0700203 'sig': sig,
Ken Mixter20d9e472010-08-12 10:58:46 -0700204 'sleep_time': sleep_time,
205 'output': output}
206
207
Ken Mixter654f32e2010-10-20 11:47:31 -0700208 def wait_for_sender_completion(self):
209 """Wait for crash_sender to complete.
210
211 Wait for no crash_sender's last message to be placed in the
212 system log before continuing and for the process to finish.
213 Otherwise we might get only part of the output."""
214 site_utils.poll_for_condition(
215 lambda: self._log_reader.can_find('crash_sender done.'),
216 timeout=60,
217 exception=error.TestError(
218 'Timeout waiting for crash_sender to emit done: ' +
219 self._log_reader.get_logs()))
220 site_utils.poll_for_condition(
221 lambda: utils.system('pgrep crash_sender',
222 ignore_status=True) != 0,
223 timeout=60,
224 exception=error.TestError(
225 'Timeout waiting for crash_sender to finish: ' +
226 self._log_reader.get_logs()))
227
228
Ken Mixter20d9e472010-08-12 10:58:46 -0700229 def _call_sender_one_crash(self,
230 send_success=True,
231 reports_enabled=True,
232 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700233 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700234 """Call the crash sender script to mock upload one crash.
235
236 Args:
237 send_success: Mock a successful send if true
238 reports_enabled: Has the user consented to sending crash reports.
239 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700240 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700241
242 Returns:
243 Returns a dictionary describing the result with the keys
244 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700245 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700246 send script
247 rate_count: how many crashes have been uploaded in the past
248 24 hours.
249 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700250 report = self._prepare_sender_one_crash(send_success,
251 reports_enabled,
252 username,
253 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700254 self._log_reader.set_start_by_current()
255 script_output = utils.system_output(
256 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
257 ignore_status=True)
Ken Mixter654f32e2010-10-20 11:47:31 -0700258 self.wait_for_sender_completion()
Ken Mixter20d9e472010-08-12 10:58:46 -0700259 output = self._log_reader.get_logs()
260 logging.debug('Crash sender message output:\n' + output)
261 if script_output != '':
262 raise error.TestFail(
263 'Unexpected crash_sender stdout/stderr: ' + script_output)
264
Ken Mixter38dfe852010-08-18 15:24:00 -0700265 if os.path.exists(report):
266 report_exists = True
267 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700268 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700269 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700270 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
271 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
272 else:
273 rate_count = 0
274
275 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700276 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700277 result['rate_count'] = rate_count
278
279 # Show the result for debugging but remove 'output' key
280 # since it's large and earlier in debug output.
281 debug_result = dict(result)
282 del debug_result['output']
283 logging.debug('Result of send (besides output): %s' % debug_result)
284
285 return result
286
287
Ken Mixterddcd92d2010-11-01 19:07:08 -0700288 def _replace_crash_reporter_filter_in(self, new_parameter):
289 core_pattern = utils.read_file(self._CORE_PATTERN)[:-1]
290 core_pattern = re.sub('--filter_in=\S*\s*', '',
291 core_pattern).rstrip()
292 if new_parameter:
293 core_pattern += ' ' + new_parameter
294 utils.system('echo "%s" > %s' % (core_pattern, self._CORE_PATTERN))
295
296
297 def enable_crash_filtering(self, name):
298 self._replace_crash_reporter_filter_in('--filter_in=' + name)
299
300
301 def disable_crash_filtering(self):
302 self._replace_crash_reporter_filter_in('')
303
304
Ken Mixter20d9e472010-08-12 10:58:46 -0700305 def initialize(self):
306 test.test.initialize(self)
307 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700308 self._leave_crash_sending = True
Ken Mixter67ff5622010-09-30 15:32:17 -0700309 self._automatic_consent_saving = True
Ken Mixterddcd92d2010-11-01 19:07:08 -0700310 self.enable_crash_filtering('none')
Ken Mixter20d9e472010-08-12 10:58:46 -0700311
312
313 def cleanup(self):
314 self._reset_rate_limiting()
315 self._clear_spooled_crashes()
Ken Mixter4f619652010-10-18 12:11:18 -0700316 self._set_system_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700317 self._set_sending_mock(mock_enabled=False)
Ken Mixter67ff5622010-09-30 15:32:17 -0700318 if self._automatic_consent_saving:
319 self._pop_consent()
Ken Mixterddcd92d2010-11-01 19:07:08 -0700320 self.disable_crash_filtering()
Ken Mixter20d9e472010-08-12 10:58:46 -0700321 test.test.cleanup(self)
322
323
Ken Mixter38dfe852010-08-18 15:24:00 -0700324 def run_crash_tests(self,
325 test_names,
326 initialize_crash_reporter=False,
327 clear_spool_first=True,
328 must_run_all=True):
329 """Run crash tests defined in this class.
330
331 Args:
332 test_names: array of test names
333 initialize_crash_reporter: should set up crash reporter for every run
334 must_run_all: should make sure every test in this class is mentioned
335 in test_names
336 """
Ken Mixter67ff5622010-09-30 15:32:17 -0700337 if self._automatic_consent_saving:
338 self._push_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700339
Ken Mixter38dfe852010-08-18 15:24:00 -0700340 if must_run_all:
341 # Sanity check test_names is complete
342 for attr in dir(self):
343 if attr.find('_test_') == 0:
344 test_name = attr[6:]
345 if not test_name in test_names:
346 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700347
348 for test_name in test_names:
349 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700350 if initialize_crash_reporter:
351 self._initialize_crash_reporter()
Ken Mixter4f619652010-10-18 12:11:18 -0700352 # Disable crash_sender from running, kill off any running ones, but
353 # set environment so crash_sender may run as a child process.
354 self._set_system_sending(False)
355 self._set_child_sending(True)
Ken Mixter20d9e472010-08-12 10:58:46 -0700356 self._kill_running_sender()
357 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700358 if clear_spool_first:
359 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700360 getattr(self, '_test_' + test_name)()