diff mbox series

[RFC,v2,1/3] sstate: Replay warnings and errors when restoring from sstate

Message ID 20261115-mathieu-debug-replay-v4-v2-1-8fbfc07058b1@bootlin.com
State New
Headers show
Series sstate: Replay warnings and errors when restoring from sstate | expand

Commit Message

Mathieu Dubois-Briand Nov. 15, 2026, 11:33 a.m. UTC
Save the messages logged by an sstate task into its sstate package
(sstate-log.json, with build-specific paths replaced by @VAR@
placeholders) and replay them when the package is restored from the
sstate cache, substituting the paths of the build doing the replay.
Errors are saved too, since bb.error() can be called without failing the
task, so warnings which previously only appeared on the first build (e.g.
QA messages) are now shown again on sstate reuse.

AI-Generated: Uses opencode (DeepSeek V4 Flash)
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
---
 meta/classes-global/sstate.bbclass | 91 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 88 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-global/sstate.bbclass b/meta/classes-global/sstate.bbclass
index c6d945918232..ed88dae17202 100644
--- a/meta/classes-global/sstate.bbclass
+++ b/meta/classes-global/sstate.bbclass
@@ -101,6 +101,12 @@  SSTATEPREINSTFUNCS = ""
 SSTATEPOSTUNPACKFUNCS = "sstate_hardcode_path_unpack"
 EXTRA_STAGING_FIXMES ?= "HOSTTOOLS_DIR"
 
+# Build-specific paths substituted in replayed task log messages. Keep the
+# order longest-path-first: a shorter value is a prefix of the longer ones
+# (e.g. TOPDIR is a prefix of TMPDIR, HOME of both) and must be substituted
+# after them, or it would match inside their values.
+SSTATE_LOG_VARS ?= "TMPDIR DL_DIR SSTATE_DIR TOPDIR COREBASE HOME"
+
 # Check whether sstate exists for tasks that support sstate and are in the
 # locked signatures file.
 SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK ?= 'error'
@@ -200,6 +206,73 @@  def sstate_state_fromvars(d, task = None):
     ss['fixmedir'] = fixmedir
     return ss
 
+def sstate_log_strip_paths(d, message):
+    import re
+
+    # Replace paths in log messages by placeholders, so we are not leaking
+    # build paths and can show consistent paths on replay.
+    # Escape literal '@' first.
+    message = message.replace('@', r'\@')
+    for var in d.getVar('SSTATE_LOG_VARS').split():
+        value = (d.getVar(var) or '').rstrip('/')
+        if value:
+            message = re.sub(re.escape(value) + r'(?!\w)', '@%s@' % var,
+                             message)
+
+    return message
+
+def sstate_log_expand_paths(d, message):
+    # Add back paths into the replayed log messages
+    for var in d.getVar('SSTATE_LOG_VARS').split():
+        value = (d.getVar(var) or '').rstrip('/')
+        if value:
+            message = message.replace('@%s@' % var, value)
+
+    message = message.replace(r'\@', '@')
+
+    return message
+
+def sstate_log_save(d, record):
+    # Raw logstore entry (a [levelno, message] pair) -> archived record
+    # (dict), with build paths replaced by placeholders. Inverse of
+    # sstate_log_expand.
+    levelno, message = record
+    return {'levelno': levelno, 'message': sstate_log_strip_paths(d, message)}
+
+def sstate_log_emit(d, logentry):
+    levelno = logentry.get('levelno', 0)
+    if levelno < bb.msg.BBLogFormatter.WARNONCE or "message" not in logentry:
+        return
+    message = sstate_log_expand_paths(d, logentry['message'])
+    if levelno >= bb.msg.BBLogFormatter.ERROR:
+        bb.error(message)
+    elif levelno == bb.msg.BBLogFormatter.ERRORONCE:
+        bb.erroronce(message)
+    elif levelno == bb.msg.BBLogFormatter.WARNONCE:
+        bb.warnonce(message)
+    else:
+        bb.warn(message)
+
+python sstate_save_task_log () {
+    import json
+
+    # Save the messages logged by this task, so they can be replayed when the
+    # task is restored from sstate. The records are written by bitbake right
+    # after the task body, before the postfuncs.
+    mytask = d.getVar('BB_RUNTASK')
+    recordsfn = os.path.join(d.getVar('T'), 'logrecords.%s.json' % mytask)
+    if not os.path.exists(recordsfn):
+        return
+    with open(recordsfn) as f:
+        records = json.load(f)
+
+    tasks = {mytask: [sstate_log_save(d, record) for record in records]}
+
+    logfn = os.path.join(d.getVar('SSTATE_BUILDDIR'), "sstate-log.json")
+    with open(logfn, 'w') as f:
+        json.dump({'tasks': tasks}, f)
+}
+
 def sstate_add(ss, source, dest, d):
     if not source.endswith("/"):
          source = source + "/"
@@ -387,11 +460,12 @@  def sstate_installpkg(ss, d):
         # All hooks should run in the SSTATE_INSTDIR
         bb.build.exec_func(f, d, (sstateinst,))
 
-    return sstate_installpkgdir(ss, d)
+    return sstate_installpkgdir(ss, d, replay=True)
 
-def sstate_installpkgdir(ss, d):
+def sstate_installpkgdir(ss, d, replay=False):
     import oe.path
     import subprocess
+    import json
 
     sstateinst = d.getVar("SSTATE_INSTDIR")
     d.setVar('SSTATE_FIXMEDIR', ss['fixmedir'])
@@ -402,6 +476,17 @@  def sstate_installpkgdir(ss, d):
 
     sstate_install(ss, d)
 
+    if replay:
+        logfp = os.path.join(sstateinst, "sstate-log.json")
+        if os.path.exists(logfp):
+            with open(logfp, 'r') as logfile:
+                logdata = json.load(logfile)
+            records = logdata.get('tasks', {}).get(d.getVar('BB_RUNTASK').removesuffix('_setscene'), [])
+            for logentry in records:
+                sstate_log_emit(d, logentry)
+
+        bb.utils.remove(logfp)
+
     return True
 
 python sstate_hardcode_path_unpack () {
@@ -686,7 +771,7 @@  def sstate_package(ss, d):
     if d.getVar('SSTATE_SKIP_CREATION') == '1':
         return
 
-    sstate_create_package = ['sstate_report_unihash', 'sstate_create_and_sign_package']
+    sstate_create_package = ['sstate_report_unihash', 'sstate_save_task_log', 'sstate_create_and_sign_package']
 
     for f in (d.getVar('SSTATECREATEFUNCS') or '').split() + \
              sstate_create_package + \