{"record":{"id":"c6205207b5890478","repo":"NationalSecurityAgency/ghidra","slug":"could-not-build-data-for-register-value-rv-value","errorCode":null,"errorMessage":"Could not build data for register value {rv.value}","messagePattern":"Could not build data for register value (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/methods.py","lineNumber":761,"sourceCode":"\n@REGISTRY.method()\ndef write_reg(frame: StackFrame, name: str, value: bytes) -> None:\n    \"\"\"Write a register.\"\"\"\n    proc = find_proc_by_frame(frame)\n    util.get_debugger().SetSelectedTarget(proc.target)\n    f = find_frame_by_obj(frame)\n    exec_convert_errors(f'frame select {f.idx}')\n    rv = frame.trace.extra.require_rm().map_value_back(proc, name, value)\n    reg = f.registers[0].GetChildMemberWithName(name)\n    error = lldb.SBError()\n    data = lldb.SBData()\n    tgt = util.get_target()\n    for b in rv.value:\n        bv = tgt.EvaluateExpression(f\"(char){b}\")\n        if bv.error.fail:\n            raise Exception(bv.error.description)\n        if not data.Append(bv.GetData()):\n            raise Exception(f\"Could not build data for register value {rv.value}\")\n    if not reg.SetData(data, error):\n        raise Exception(error.description)\n    with commands.open_tracked_tx(f'Write Register {name}'):\n        exec_convert_errors('ghidra trace putreg')\n","sourceCodeStart":743,"sourceCodeEnd":766,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/methods.py#L743-L766","documentation":"Raised inside the write_reg method when lldb.SBData.Append() returns False while assembling register value bytes. The method converts each byte of the register value via EvaluateExpression('(char){b}'), collects the results into an SBData buffer, and this error fires when a byte cannot be appended to that buffer.","triggerScenarios":"write_reg is called with a frame, register name, and byte value. The mapped-back value rv.value is iterated byte-by-byte; for each byte, tgt.EvaluateExpression('(char){b}') is evaluated and its SBData is appended via data.Append(bv.GetData()). If Append returns False for any byte, the exception is raised.","commonSituations":"The byte value in rv.value is outside the valid range for the expression (e.g. negative or >255 if the expression cast is invalid). The LLDB target cannot evaluate the cast expression (e.g. invalid target state). A register value contains unexpected byte values from a faulty register mapper.","solutions":["Check rv.value byte range before the loop: ensure each byte is 0-255.","Verify the LLDB target is in a valid state for expression evaluation before write_reg.","Log bv.GetData() and bv.error for the failing byte to diagnose the LLDB-side failure.","Consider replacing the byte-by-byte Append approach with a single SBData.SetData or CreateDataFromInt call."],"exampleFix":"# before: byte-by-byte Append\nfor b in rv.value:\n    bv = tgt.EvaluateExpression(f\"(char){b}\")\n    if bv.error.fail:\n        raise Exception(bv.error.description)\n    if not data.Append(bv.GetData()):\n        raise Exception(f\"Could not build data for register value {rv.value}\")\n\n# after: single-shot SBData construction\nfrom struct import pack\nraw = bytes(rv.value)\ndata = lldb.SBData.CreateDataFromInt(\n    int.from_bytes(raw, 'big' if rm.byte_order == 'big' else 'little'),\n    size=len(raw),\n) if hasattr(lldb.SBData, 'CreateDataFromInt') else lldb.SBData()\nif not data and not raw:\n    for b in rv.value:\n        bv = tgt.EvaluateExpression(f\"(char){b}\")\n        data.Append(bv.GetData())","handlingStrategy":"try-catch","validationCode":"for b in rv.value:\n    if not (0 <= b <= 255):\n        raise ValueError(f\"Byte {b} out of range for (char) cast\")\n# also verify target is valid for expression evaluation\nif not util.get_target().IsValid():\n    raise RuntimeError(\"LLDB target is not valid for expression evaluation\")","typeGuard":null,"tryCatchPattern":"try:\n    # ... build data via Append ...\nexcept Exception as e:\n    if 'Could not build data' in str(e):\n        log.error(f\"SBData.Append failed for register {name}, value={rv.value}\")\n        raise RuntimeError(f\"Cannot write register {name}: LLDB data build failed\") from e\n    raise","preventionTips":["Validate each byte in rv.value is 0-255 before the expression-evaluation loop.","Ensure the LLDB target is valid and in a stopped state before write_reg.","Consider replacing the byte-by-byte Append approach with a bulk SBData construction."],"tags":["lldb","debugger-agent","register-write","sbdata","exception"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}