1#
2# Collects debug information in order to create error report files.
3#
4# Copyright (C) 2013 Intel Corporation
5# Author: Andreea Brandusa Proca <andreea.b.proca@intel.com>
6#
7# Licensed under the MIT license, see COPYING.MIT for details
8
9inherit base
10
11ERR_REPORT_DIR ?= "${LOG_DIR}/error-report"
12
13def errorreport_getdata(e):
14    import codecs
15    logpath = e.data.getVar('ERR_REPORT_DIR')
16    datafile = os.path.join(logpath, "error-report.txt")
17    with codecs.open(datafile, 'r', 'utf-8') as f:
18        data = f.read()
19    return data
20
21def errorreport_savedata(e, newdata, file):
22    import json
23    import codecs
24    logpath = e.data.getVar('ERR_REPORT_DIR')
25    datafile = os.path.join(logpath, file)
26    with codecs.open(datafile, 'w', 'utf-8') as f:
27        json.dump(newdata, f, indent=4, sort_keys=True)
28    return datafile
29
30def get_conf_data(e, filename):
31    builddir = e.data.getVar('TOPDIR')
32    filepath = os.path.join(builddir, "conf", filename)
33    jsonstring = ""
34    if os.path.exists(filepath):
35        with open(filepath, 'r') as f:
36            for line in f.readlines():
37                if line.startswith("#") or len(line.strip()) == 0:
38                    continue
39                else:
40                    jsonstring=jsonstring + line
41    return jsonstring
42
43python errorreport_handler () {
44        import json
45        import codecs
46
47        def nativelsb():
48            nativelsbstr = e.data.getVar("NATIVELSBSTRING")
49            # provide a bit more host info in case of uninative build
50            if e.data.getVar('UNINATIVE_URL') != 'unset':
51                return '/'.join([nativelsbstr, lsb_distro_identifier(e.data)])
52            return nativelsbstr
53
54        logpath = e.data.getVar('ERR_REPORT_DIR')
55        datafile = os.path.join(logpath, "error-report.txt")
56
57        if isinstance(e, bb.event.BuildStarted):
58            bb.utils.mkdirhier(logpath)
59            data = {}
60            machine = e.data.getVar("MACHINE")
61            data['machine'] = machine
62            data['build_sys'] = e.data.getVar("BUILD_SYS")
63            data['nativelsb'] = nativelsb()
64            data['distro'] = e.data.getVar("DISTRO")
65            data['target_sys'] = e.data.getVar("TARGET_SYS")
66            data['failures'] = []
67            data['component'] = " ".join(e.getPkgs())
68            data['branch_commit'] = str(base_detect_branch(e.data)) + ": " + str(base_detect_revision(e.data))
69            data['bitbake_version'] = e.data.getVar("BB_VERSION")
70            data['layer_version'] = get_layers_branch_rev(e.data)
71            data['local_conf'] = get_conf_data(e, 'local.conf')
72            data['auto_conf'] = get_conf_data(e, 'auto.conf')
73            lock = bb.utils.lockfile(datafile + '.lock')
74            errorreport_savedata(e, data, "error-report.txt")
75            bb.utils.unlockfile(lock)
76
77        elif isinstance(e, bb.build.TaskFailed):
78            task = e.task
79            taskdata={}
80            log = e.data.getVar('BB_LOGFILE')
81            taskdata['package'] = e.data.expand("${PF}")
82            taskdata['task'] = task
83            if log:
84                try:
85                    with codecs.open(log, encoding='utf-8') as logFile:
86                        logdata = logFile.read()
87                    # Replace host-specific paths so the logs are cleaner
88                    for d in ("TOPDIR", "TMPDIR"):
89                        s = e.data.getVar(d)
90                        if s:
91                            logdata = logdata.replace(s, d)
92                except:
93                    logdata = "Unable to read log file"
94            else:
95                logdata = "No Log"
96
97            # server will refuse failures longer than param specified in project.settings.py
98            # MAX_UPLOAD_SIZE = "5242880"
99            # use lower value, because 650 chars can be spent in task, package, version
100            max_logdata_size = 5242000
101            # upload last max_logdata_size characters
102            if len(logdata) > max_logdata_size:
103                logdata = "..." + logdata[-max_logdata_size:]
104            taskdata['log'] = logdata
105            lock = bb.utils.lockfile(datafile + '.lock')
106            jsondata = json.loads(errorreport_getdata(e))
107            jsondata['failures'].append(taskdata)
108            errorreport_savedata(e, jsondata, "error-report.txt")
109            bb.utils.unlockfile(lock)
110
111        elif isinstance(e, bb.event.BuildCompleted):
112            lock = bb.utils.lockfile(datafile + '.lock')
113            jsondata = json.loads(errorreport_getdata(e))
114            bb.utils.unlockfile(lock)
115            failures = jsondata['failures']
116            if(len(failures) > 0):
117                filename = "error_report_" + e.data.getVar("BUILDNAME")+".txt"
118                datafile = errorreport_savedata(e, jsondata, filename)
119                bb.note("The errors for this build are stored in %s\nYou can send the errors to a reports server by running:\n  send-error-report %s [-s server]" % (datafile, datafile))
120                bb.note("The contents of these logs will be posted in public if you use the above command with the default server. Please ensure you remove any identifying or proprietary information when prompted before sending.")
121}
122
123addhandler errorreport_handler
124errorreport_handler[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskFailed"
125