{"record":{"id":"0c0fabb0fe148af1","repo":"NationalSecurityAgency/ghidra","slug":"inferiors-infnum-does-not-exist","errorCode":null,"errorMessage":"Inferiors[{infnum}] does not exist","messagePattern":"Inferiors\\[(.+?)\\] does not exist","errorType":"validation","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"Ghidra/Debug/Debugger-agent-gdb/src/main/py/src/ghidragdb/methods.py","lineNumber":102,"sourceCode":"\ndef find_availpid_by_pattern(pattern: re.Pattern, object: TraceObject,\n                             err_msg: str) -> int:\n    mat = pattern.fullmatch(object.path)\n    if mat is None:\n        raise TypeError(f\"{object} is not {err_msg}\")\n    pid = int(mat['pid'])\n    return pid\n\n\ndef find_availpid_by_obj(object: TraceObject) -> int:\n    return find_availpid_by_pattern(AVAILABLE_PATTERN, object, \"an Available\")\n\n\ndef find_inf_by_num(infnum: int) -> gdb.Inferior:\n    for inf in gdb.inferiors():\n        if inf.num == infnum:\n            return inf\n    raise KeyError(f\"Inferiors[{infnum}] does not exist\")\n\n\ndef find_inf_by_pattern(object: TraceObject, pattern: re.Pattern,\n                        err_msg: str) -> gdb.Inferior:\n    mat = pattern.fullmatch(object.path)\n    if mat is None:\n        raise TypeError(f\"{object} is not {err_msg}\")\n    infnum = int(mat['infnum'])\n    return find_inf_by_num(infnum)\n\n\ndef find_inf_by_obj(object: TraceObject) -> gdb.Inferior:\n    return find_inf_by_pattern(object, INFERIOR_PATTERN, \"an Inferior\")\n\n\ndef find_inf_by_infbreak_obj(object: TraceObject) -> gdb.Inferior:\n    return find_inf_by_pattern(object, INF_BREAKS_PATTERN,\n                               \"a BreakpointLocationContainer\")","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Debug/Debugger-agent-gdb/src/main/py/src/ghidragdb/methods.py#L84-L120","documentation":"Thrown as a KeyError by find_inf_by_num in the gdb agent when no GDB inferior with the requested infnum exists in gdb.inferiors(). The function iterates all inferiors and raises if none matches, indicating the inferior was removed, not yet created, or the number is invalid.","triggerScenarios":"Calling find_inf_by_num(3) when only inferiors 1 and 2 exist; calling after the inferior was killed/detached (gdb removes it from the list); using a stale inferior number cached from an earlier session.","commonSituations":"Inferior was killed or detached between caching its number and using it; referencing an inferior number from a saved path after GDB state changed; off-by-one in inferior numbering; inferior creation failed silently.","solutions":["Verify the inferior exists: check [inf.num for inf in gdb.inferiors()] before calling find_inf_by_num.","If the inferior was removed, refresh the trace objects from the current GDB state.","Handle KeyError gracefully — the inferior may have been killed externally."],"exampleFix":"// before\ninf = find_inf_by_num(stale_num)  # KeyError if gone\n// after\nexisting = {inf.num: inf for inf in gdb.inferiors()}\nif stale_num in existing:\n    inf = existing[stale_num]\nelse:\n    print(f'Inferior {stale_num} no longer exists')","handlingStrategy":"try-catch","validationCode":"def inferior_exists(infnum: int) -> bool:\n    return any(inf.num == infnum for inf in gdb.inferiors())\n\ndef safe_find_inf_by_num(infnum: int) -> gdb.Inferior:\n    for inf in gdb.inferiors():\n        if inf.num == infnum:\n            return inf\n    raise KeyError(f'Inferior {infnum} not found in {len(gdb.inferiors())} inferiors')","typeGuard":null,"tryCatchPattern":"try:\n    inf = find_inf_by_num(infnum)\nexcept KeyError as e:\n    print(f'Inferior {infnum} does not exist. Available: {[i.num for i in gdb.inferiors()]}')\n    raise","preventionTips":["Check gdb.inferiors() for the inferior number before calling find_inf_by_num.","Do not cache inferior numbers across kill/detach operations.","Handle KeyError gracefully in automated scripts that may outlive an inferior."],"tags":["gdb","lifecycle","inferior","stale-reference"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}