1# Copyright (C) 2013 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import logging
30
31
32_log = logging.getLogger(__name__)
33
34
35class DumpReader(object):
36    """Base class for breakpad dump readers."""
37
38    def __init__(self, host, build_dir):
39        self._host = host
40        self._build_dir = build_dir
41
42    def check_is_functional(self):
43        """This routine must be implemented by subclasses.
44
45        Returns True if this reader is functional."""
46        raise NotImplementedError()
47
48    def crash_dumps_directory(self):
49        return self._host.filesystem.join(self._build_dir, 'crash-dumps')
50
51    def clobber_old_results(self):
52        if self._host.filesystem.isdir(self.crash_dumps_directory()):
53            self._host.filesystem.rmtree(self.crash_dumps_directory())
54
55    def look_for_new_crash_logs(self, crashed_processes, start_time):
56        if not crashed_processes:
57            return None
58
59        if not self.check_is_functional():
60            return None
61
62        pid_to_minidump = dict()
63        for root, dirs, files in self._host.filesystem.walk(self.crash_dumps_directory()):
64            for dmp in [f for f in files if f.endswith(self._file_extension())]:
65                dmp_file = self._host.filesystem.join(root, dmp)
66                if self._host.filesystem.mtime(dmp_file) < start_time:
67                    continue
68                pid = self._get_pid_from_dump(dmp_file)
69                if pid:
70                    pid_to_minidump[pid] = dmp_file
71
72        result = dict()
73        for test, process_name, pid in crashed_processes:
74            if str(pid) in pid_to_minidump:
75                stack = self._get_stack_from_dump(pid_to_minidump[str(pid)])
76                if stack:
77                    result[test] = stack
78
79        return result
80
81    def _get_pid_from_dump(self, dump_file):
82        """This routine must be implemented by subclasses.
83
84        This routine returns the PID of the crashed process that produced the given dump_file."""
85        raise NotImplementedError()
86
87    def _get_stack_from_dump(self, dump_file):
88        """This routine must be implemented by subclasses.
89
90        Returns the stack stored in the given breakpad dump_file."""
91        raise NotImplementedError()
92
93    def _file_extension(self):
94        """This routine must be implemented by subclasses.
95
96        Returns the file extension of crash dumps written by breakpad."""
97        raise NotImplementedError()
98