【AI 使用案例】如何使用 AI 进行代码调试

背景说明

在编译器中端构图开发中,需要处理字节码之间的控制流关系。当遇到 fall through(顺序执行到下一个字节码)或跳转时,需要正确设置当前基本块(CurrentBlock)。

如果 CurrentBlock 未被正确设置,后续 fall through 进行 merge 的时候,会出现崩溃。(错误进行 BB 块的相连)


案例一:else 分支缺失导致变量未设置

案例背景

测试代码(switch 表达式)

typescript 复制代码
function switch_expr_test(x) {
    let result = 0;
    switch (true) {
        case x > 0 && x < 3:
            result += 100;
            break;
        case x >= 3 && x < 5:
            result += 200;
            break;
        default:
            result += 0;
            break;
    }
    return result;
}

崩溃信息

复制代码
ASSERTION FAILED: index < GetInputCount()
IN ../../arkcompiler/ets_runtime/ecmascript/arksteed/arksteed_vertex.h:736: SetInput

崩溃栈:
#14 BuildBody at arksteed_graph_builder.cpp:191
  -> mergeStates_[index]->Merge(*currentFrameState_, prevBlock);

含义:在设置 Vertex 输入时,index 超出了该 Vertex 的输入数量

问题代码

arksteed_graph_builder.cpp:167-177

cpp 复制代码
// ========== 第一部分:设置 CurrentBlock ==========
// already fall through and new block
if (blockForBytecode_[index] != nullptr &&
    !IsBlockFinished(blockForBytecode_[index])) {
    SetCurrentBlock(blockForBytecode_[index]);
} else if (index > 0) { // fall through, but not new block
    uint32_t predIndex = iterator_.PredIndex();
    if (blockForBytecode_[predIndex] != nullptr &&
        !IsBlockFinished(blockForBytecode_[predIndex])) {
        SetCurrentBlock(blockForBytecode_[predIndex]);
    }
    // ❌ 如果上面条件不满足,CurrentBlock() 仍然是 nullptr!
}

// ========== 第二部分:使用 CurrentBlock ==========
MergePointInterpreterFrameState* mergeState = mergeStates_[index];
if (mergeState != nullptr) {
    if (CurrentBlock() != nullptr) {
        // Previous bytecode was NOT finished (fallthrough), need to end the block first
        auto *prevBlock = FinishBlock<JumpVertex>({}, &jumpTargets_[index]);
        // Then merges interpreter states to the current
        mergeStates_[index]->Merge(*currentFrameState_, prevBlock);
        // ← 崩溃点:prevBlock 参数与 mergeStates_[index] 期望不匹配
    }
    StartNewBlock(nullptr, index);
} else {
    ASSERT(CurrentBlock() != nullptr);  // ← 潜在崩溃点
    blockForBytecode_[index] = CurrentBlock();
}

问题根因深度分析

问题 1:else 分支缺失
cpp 复制代码
} else if (index > 0) {
    uint32_t predIndex = iterator_.PredIndex();
    if (blockForBytecode_[predIndex] != nullptr &&
        !IsBlockFinished(blockForBytecode_[predIndex])) {
        SetCurrentBlock(blockForBytecode_[predIndex]);
    }
    // ❌ 缺失 else:当上述条件不满足时,CurrentBlock() 未被设置
}
场景 条件 结果
前驱块未创建 blockForBytecode_[predIndex] == nullptr CurrentBlock() 未设置
前驱块已完成 IsBlockFinished(blockForBytecode_[predIndex]) == true CurrentBlock() 未设置

CurrentBlock 设置错了,会导致错误判断了 fall-through 块,导致用例崩溃。

崩溃影响链路

复制代码
┌──────────────────────────────┐
│ 内层 if 条件不满足      │
│ (前驱块 nullptr 或已finished)│
└──────────────────────────────┘
              ↓
    SetCurrentBlock() 未被调用
              ↓
    CurrentBlock() == nullptr
              ↓
    ┌────────────────────────────────┐
    │     mergeState != nullptr?    │
    └────────────────────────────────┘
              ↓           ↓
         YES           NO
          ↓             ↓
    ┌─────────────┐   else {
    │ CurrentBlock()│   ASSERT(CurrentBlock()
    │ != nullptr?  │   != nullptr);
    │ 为 false      │   blockForBytecode_[index] = CurrentBlock();
    ↓             ↓           ↓
跳过 Merge      崩溃!       (潜在)
    →             ↓
StartNewBlock()   (路径 B)

路径 A(实际发生)

  • CurrentBlock() != nullptr(使用的是错误的 Block[0])
  • Merge 时参数不匹配 → 崩溃

路径 B(潜在)

  • mergeState == nullptr
  • ASSERT 失败 → 崩溃

AI 辅助调试过程

第一步:准备上下文信息

需要提供给 AI 的信息:

  1. 崩溃信息

    ASSERTION FAILED: index < GetInputCount()
    位置:arksteed_graph_builder.cpp:191
    函数:mergeStates_[index]->Merge(*currentFrameState_, prevBlock)

  2. 关键代码片段

cpp 复制代码
// 问题代码:else if 分支缺少 else 处理
} else if (index > 0) {
    uint32_t predIndex = iterator_.PredIndex();
    if (blockForBytecode_[predIndex] != nullptr &&
        !IsBlockFinished(blockForBytecode_[predIndex])) {
        SetCurrentBlock(blockForBytecode_[predIndex]);
    }
    // 缺少 else:当上述条件不满足时,CurrentBlock() 未被设置
}

// 后续依赖 CurrentBlock() 的代码
if (mergeState != nullptr) {
    if (CurrentBlock() != nullptr) {
        // 使用 CurrentBlock() 进行操作
    }
}
第二步:向 AI 提问

提问模板:

复制代码
我在调试一个崩溃代码

问题代码:
[粘贴代码]

修改的逻辑:
if (blockForBytecode_[index] != nullptr &&
    !IsBlockFinished(blockForBytecode_[index])) {
    SetCurrentBlock(blockForBytecode_[index]);
} else if (index > 0) { // fall through, but not new block
    uint32_t predIndex = iterator_.PredIndex();
    if (blockForBytecode_[predIndex] != nullptr &&
        !IsBlockFinished(blockForBytecode_[predIndex])) {
        SetCurrentBlock(blockForBytecode_[predIndex]);
    }
    // ❌ 如果上面条件不满足,CurrentBlock() 仍然是 nullptr!
}

请帮我分析:
1. 为什么会有崩溃?
2. 帮我重新编译,并且加入调试日志进行分析。
第三步:AI 可能的分析方向

AI 可能会给出以下分析:

逻辑完整性问题

  • if-else 链只处理了成功情况
  • 失败情况没有对应的处理
  • 变量在所有路径下未被正确设置
第四步:问题定位与修复

问题模式:

问题 现象 解决方案
缺失 else 分支 条件不满足时变量未设置 添加 else 分支处理所有未覆盖情况
变量未初始化 后续使用时崩溃/行为异常 在 else 中明确设置变量或创建新对象
逻辑不完整 只考虑了正常流程 添加异常处理和边界条件

修复建议:

cpp 复制代码
// 添加 else 分支
} else if (index > 0) {
    uint32_t predIndex = iterator_.PredIndex();
    if (blockForBytecode_[predIndex] != nullptr &&
        !IsBlockFinished(blockForBytecode_[predIndex])) {
        SetCurrentBlock(blockForBytecode_[predIndex]);
    } else {
        // ✅ 添加此分支:前驱块不可用时
        // 保持 nullptr 让后续代码创建新 block
        SetCurrentBlock(nullptr);
    }
}

效率对比

调试方式 传统调试 AI 辅助调试
理解问题 需要手动分析每个分支 AI 快速识别逻辑缺陷
分析原因 逐步排查代码路径 AI 指出缺失的分支
解决方案 自己考虑边界情况 AI 提供多种方案对比
验证思路 需要经验积累 AI 给出验证方法建议

时间节省估算:

  • 问题理解:从 2 小时 → 30 分钟
  • 原因分析:从 4 小时 → 1 小时
  • 方案验证:从 3 小时 → 1.5 小时

思考

AI 辅助调试,分5个步骤:

  1. 先给 AI 编译命令和调试命令
    • python ark.py x64.debug
    • ./debug_jit.sh test.ts
  2. AI 添加调试日志
  3. 找到问题,修改代码
  4. AI 帮忙检查修改的代码
  5. 删除调试日志
相关推荐
Cosolar6 分钟前
别再羡慕 Python 了!Java 开发者的 AI Agent 全指南:四大框架从选型到实战
java·人工智能·后端
YA88888888888910 分钟前
B端拓客号码核验:行业困局突围与技术赋能路径探析,氪迹科技法人股东核验系统,阶梯式价格
大数据·人工智能
LaughingZhu10 分钟前
Product Hunt 每日热榜 | 2026-03-29
数据库·人工智能·经验分享·神经网络·chatgpt
AI浩13 分钟前
第 2 章 项目初始化与结构深度解析
人工智能
穆利堂-movno113 分钟前
2026年爆火OpenClaw龙虾在物业行业的应用场景解析,物业openclaw-物业龙虾
人工智能·开源·自动化·新网物业收费软件·新网物业软件系统·物业openclaw·物业龙虾
Light6020 分钟前
财务系统进销存账套设计指南:从传统记账到智能决策的进化之路
人工智能·数字化转型·ai智能·财务系统·业财一体化·进销存管理·账套设计
踩着两条虫21 分钟前
AI驱动的Vue3应用开发平台 深入探究(十六):扩展与定制之自定义组件与设计器面板
前端·vue.js·人工智能·开源·ai编程
老陈测评22 分钟前
AI医院定义:《国际AI医院智联体共识》发布,推动AI医院认知体系重构
人工智能·重构·健康医疗
Cosolar23 分钟前
解锁LLM能力:14种Prompt策略全解析与实践指南
人工智能·后端·面试
云智慧AIOps社区23 分钟前
ITIL 5 已来:轻帆云IT服务管理平台以“AI 原生 + 全生命周期”重构数智化服务管理
运维·人工智能·aiops·it服务管理·itsm平台·cmdb配置管理·it服务管理系统