blob: 9cb6b0bd31d44ed6a29ca0a535eee3340bbde9b6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
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
Andrew Geissler90fd73c2021-03-05 15:25:55 -06009inherit base
10
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011ERR_REPORT_DIR ?= "${LOG_DIR}/error-report"
12
13def errorreport_getdata(e):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050014 import codecs
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016 datafile = os.path.join(logpath, "error-report.txt")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050017 with codecs.open(datafile, 'r', 'utf-8') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 data = f.read()
19 return data
20
21def errorreport_savedata(e, newdata, file):
22 import json
Patrick Williamsf1e5d692016-03-30 15:21:19 -050023 import codecs
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 datafile = os.path.join(logpath, file)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026 with codecs.open(datafile, 'w', 'utf-8') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 json.dump(newdata, f, indent=4, sort_keys=True)
28 return datafile
29
Brad Bishopa34c0302019-09-23 22:34:48 -040030def 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043python errorreport_handler () {
44 import json
Patrick Williamsf1e5d692016-03-30 15:21:19 -050045 import codecs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 datafile = os.path.join(logpath, "error-report.txt")
56
57 if isinstance(e, bb.event.BuildStarted):
58 bb.utils.mkdirhier(logpath)
59 data = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 machine = e.data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 data['machine'] = machine
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 data['build_sys'] = e.data.getVar("BUILD_SYS")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 data['nativelsb'] = nativelsb()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 data['distro'] = e.data.getVar("DISTRO")
65 data['target_sys'] = e.data.getVar("TARGET_SYS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 data['failures'] = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067 data['component'] = " ".join(e.getPkgs())
68 data['branch_commit'] = str(base_detect_branch(e.data)) + ": " + str(base_detect_revision(e.data))
Andrew Geissler90fd73c2021-03-05 15:25:55 -060069 data['bitbake_version'] = e.data.getVar("BB_VERSION")
70 data['layer_version'] = get_layers_branch_rev(e.data)
Brad Bishopa34c0302019-09-23 22:34:48 -040071 data['local_conf'] = get_conf_data(e, 'local.conf')
72 data['auto_conf'] = get_conf_data(e, 'auto.conf')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 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={}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 log = e.data.getVar('BB_LOGFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 taskdata['package'] = e.data.expand("${PF}")
82 taskdata['task'] = task
83 if log:
84 try:
Brad Bishop64c979e2019-11-04 13:55:29 -050085 with codecs.open(log, encoding='utf-8') as logFile:
86 logdata = logFile.read()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060087 # Replace host-specific paths so the logs are cleaner
88 for d in ("TOPDIR", "TMPDIR"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 s = e.data.getVar(d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060090 if s:
91 logdata = logdata.replace(s, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 except:
93 logdata = "Unable to read log file"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 filename = "error_report_" + e.data.getVar("BUILDNAME")+".txt"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 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"