blob: 1a12db12060934c18e613fcff3adf102a037ac1f [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
9ERR_REPORT_DIR ?= "${LOG_DIR}/error-report"
10
11def errorreport_getdata(e):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050012 import codecs
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014 datafile = os.path.join(logpath, "error-report.txt")
Patrick Williamsf1e5d692016-03-30 15:21:19 -050015 with codecs.open(datafile, 'r', 'utf-8') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016 data = f.read()
17 return data
18
19def errorreport_savedata(e, newdata, file):
20 import json
Patrick Williamsf1e5d692016-03-30 15:21:19 -050021 import codecs
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 datafile = os.path.join(logpath, file)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050024 with codecs.open(datafile, 'w', 'utf-8') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 json.dump(newdata, f, indent=4, sort_keys=True)
26 return datafile
27
Brad Bishopa34c0302019-09-23 22:34:48 -040028def get_conf_data(e, filename):
29 builddir = e.data.getVar('TOPDIR')
30 filepath = os.path.join(builddir, "conf", filename)
31 jsonstring = ""
32 if os.path.exists(filepath):
33 with open(filepath, 'r') as f:
34 for line in f.readlines():
35 if line.startswith("#") or len(line.strip()) == 0:
36 continue
37 else:
38 jsonstring=jsonstring + line
39 return jsonstring
40
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041python errorreport_handler () {
42 import json
Patrick Williamsf1e5d692016-03-30 15:21:19 -050043 import codecs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
Brad Bishopd7bf8c12018-02-25 22:55:05 -050045 def nativelsb():
46 nativelsbstr = e.data.getVar("NATIVELSBSTRING")
47 # provide a bit more host info in case of uninative build
48 if e.data.getVar('UNINATIVE_URL') != 'unset':
49 return '/'.join([nativelsbstr, lsb_distro_identifier(e.data)])
50 return nativelsbstr
51
Brad Bishop6e60e8b2018-02-01 10:27:11 -050052 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 datafile = os.path.join(logpath, "error-report.txt")
54
55 if isinstance(e, bb.event.BuildStarted):
56 bb.utils.mkdirhier(logpath)
57 data = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 machine = e.data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 data['machine'] = machine
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 data['build_sys'] = e.data.getVar("BUILD_SYS")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 data['nativelsb'] = nativelsb()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 data['distro'] = e.data.getVar("DISTRO")
63 data['target_sys'] = e.data.getVar("TARGET_SYS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 data['failures'] = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -060065 data['component'] = " ".join(e.getPkgs())
66 data['branch_commit'] = str(base_detect_branch(e.data)) + ": " + str(base_detect_revision(e.data))
Brad Bishopa34c0302019-09-23 22:34:48 -040067 data['local_conf'] = get_conf_data(e, 'local.conf')
68 data['auto_conf'] = get_conf_data(e, 'auto.conf')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 lock = bb.utils.lockfile(datafile + '.lock')
70 errorreport_savedata(e, data, "error-report.txt")
71 bb.utils.unlockfile(lock)
72
73 elif isinstance(e, bb.build.TaskFailed):
74 task = e.task
75 taskdata={}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 log = e.data.getVar('BB_LOGFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 taskdata['package'] = e.data.expand("${PF}")
78 taskdata['task'] = task
79 if log:
80 try:
Brad Bishop64c979e2019-11-04 13:55:29 -050081 with codecs.open(log, encoding='utf-8') as logFile:
82 logdata = logFile.read()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083 # Replace host-specific paths so the logs are cleaner
84 for d in ("TOPDIR", "TMPDIR"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 s = e.data.getVar(d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086 if s:
87 logdata = logdata.replace(s, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 except:
89 logdata = "Unable to read log file"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090 else:
91 logdata = "No Log"
92
93 # server will refuse failures longer than param specified in project.settings.py
94 # MAX_UPLOAD_SIZE = "5242880"
95 # use lower value, because 650 chars can be spent in task, package, version
96 max_logdata_size = 5242000
97 # upload last max_logdata_size characters
98 if len(logdata) > max_logdata_size:
99 logdata = "..." + logdata[-max_logdata_size:]
100 taskdata['log'] = logdata
101 lock = bb.utils.lockfile(datafile + '.lock')
102 jsondata = json.loads(errorreport_getdata(e))
103 jsondata['failures'].append(taskdata)
104 errorreport_savedata(e, jsondata, "error-report.txt")
105 bb.utils.unlockfile(lock)
106
107 elif isinstance(e, bb.event.BuildCompleted):
108 lock = bb.utils.lockfile(datafile + '.lock')
109 jsondata = json.loads(errorreport_getdata(e))
110 bb.utils.unlockfile(lock)
111 failures = jsondata['failures']
112 if(len(failures) > 0):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 filename = "error_report_" + e.data.getVar("BUILDNAME")+".txt"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 datafile = errorreport_savedata(e, jsondata, filename)
115 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))
116 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.")
117}
118
119addhandler errorreport_handler
120errorreport_handler[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskFailed"