blob: 1c55abfbf348a306084b77957d7928d60f203b40 [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
28python errorreport_handler () {
29 import json
Patrick Williamsf1e5d692016-03-30 15:21:19 -050030 import codecs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 def nativelsb():
33 nativelsbstr = e.data.getVar("NATIVELSBSTRING")
34 # provide a bit more host info in case of uninative build
35 if e.data.getVar('UNINATIVE_URL') != 'unset':
36 return '/'.join([nativelsbstr, lsb_distro_identifier(e.data)])
37 return nativelsbstr
38
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 logpath = e.data.getVar('ERR_REPORT_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 datafile = os.path.join(logpath, "error-report.txt")
41
42 if isinstance(e, bb.event.BuildStarted):
43 bb.utils.mkdirhier(logpath)
44 data = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045 machine = e.data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 data['machine'] = machine
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 data['build_sys'] = e.data.getVar("BUILD_SYS")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048 data['nativelsb'] = nativelsb()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050049 data['distro'] = e.data.getVar("DISTRO")
50 data['target_sys'] = e.data.getVar("TARGET_SYS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 data['failures'] = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -060052 data['component'] = " ".join(e.getPkgs())
53 data['branch_commit'] = str(base_detect_branch(e.data)) + ": " + str(base_detect_revision(e.data))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 lock = bb.utils.lockfile(datafile + '.lock')
55 errorreport_savedata(e, data, "error-report.txt")
56 bb.utils.unlockfile(lock)
57
58 elif isinstance(e, bb.build.TaskFailed):
59 task = e.task
60 taskdata={}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 log = e.data.getVar('BB_LOGFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 taskdata['package'] = e.data.expand("${PF}")
63 taskdata['task'] = task
64 if log:
65 try:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050066 logFile = codecs.open(log, 'r', 'utf-8')
67 logdata = logFile.read()
Patrick Williamsc0f7c042017-02-23 20:41:17 -060068
69 # Replace host-specific paths so the logs are cleaner
70 for d in ("TOPDIR", "TMPDIR"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050071 s = e.data.getVar(d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 if s:
73 logdata = logdata.replace(s, d)
74
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 logFile.close()
76 except:
77 logdata = "Unable to read log file"
78
79 else:
80 logdata = "No Log"
81
82 # server will refuse failures longer than param specified in project.settings.py
83 # MAX_UPLOAD_SIZE = "5242880"
84 # use lower value, because 650 chars can be spent in task, package, version
85 max_logdata_size = 5242000
86 # upload last max_logdata_size characters
87 if len(logdata) > max_logdata_size:
88 logdata = "..." + logdata[-max_logdata_size:]
89 taskdata['log'] = logdata
90 lock = bb.utils.lockfile(datafile + '.lock')
91 jsondata = json.loads(errorreport_getdata(e))
92 jsondata['failures'].append(taskdata)
93 errorreport_savedata(e, jsondata, "error-report.txt")
94 bb.utils.unlockfile(lock)
95
96 elif isinstance(e, bb.event.BuildCompleted):
97 lock = bb.utils.lockfile(datafile + '.lock')
98 jsondata = json.loads(errorreport_getdata(e))
99 bb.utils.unlockfile(lock)
100 failures = jsondata['failures']
101 if(len(failures) > 0):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 filename = "error_report_" + e.data.getVar("BUILDNAME")+".txt"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 datafile = errorreport_savedata(e, jsondata, filename)
104 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))
105 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.")
106}
107
108addhandler errorreport_handler
109errorreport_handler[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskFailed"