1. debugBasics
debug is Software Development过程in important 环节, 它可以helping你找出codein error and issues. IntelliJ IDEAproviding了强 big debugfunctions, 可以helping你 high 效地debugcode.
1.1 启动debug
要启动debug, 请按照以 under 步骤operation:
- in codein设置断点
- 点击Run > Debug, or using fast 捷键
Shift + F9 - or in 编辑器in right 键点击, 选择Debug
1.2 debugtool窗口
启动debug after , IntelliJ IDEA会打开debugtool窗口, package含以 under 主要部分:
- Frames: 显示当 before 调用栈
- Variables: 显示当 before 作用域in variable
- Watches: 显示user添加 观察表达式
- Console: 显示程序输出 and debuginformation
- Debugger: 显示debug控制按钮
1.3 debug控制按钮
debug控制按钮位于debugtool窗口 顶部, including以 under 常用按钮:
- Step Over: 单步执行, 跳过method调用 ( fast 捷键
F8) - Step Into: 单步执行, 进入method调用 ( fast 捷键
F7) - Step Out: from 当 before methodin跳出 ( fast 捷键
Shift + F8) - Resume Program: 继续执行程序, 直 to 遇 to under 一个断点 ( fast 捷键
F9) - Stop: 停止debug ( fast 捷键
Ctrl + F2) - Pause: 暂停程序执行
- Reset: resetdebugsession
2. 设置断点
断点 is debug core, 它可以让程序 in 指定 位置暂停执行, 方便你查看程序 status and variable值.
2.1 行断点
行断点 is 最basic 断点class型, 它会 in 指定 行暂停程序执行. 要设置行断点, 请按照以 under 步骤operation:
- in 编辑器in, 点击行号 left 侧 空白区域
- or using fast 捷键
Ctrl + F8in 当 before 行设置断点
2.2 条件断点
条件断点只会 in 满足指定条件时暂停程序执行. 要设置条件断点, 请按照以 under 步骤operation:
- in 现 has 断点 on right 键点击
- 选择"Edit Breakpoint"
- in 条件输入框in输入条件表达式
- 点击"OK"
2.3 log断点
log断点不会暂停程序执行, 只会 in 控制台输出指定 loginformation. 要设置log断点, 请按照以 under 步骤operation:
- in 现 has 断点 on right 键点击
- 选择"Edit Breakpoint"
- 勾选"Log message to console"
- 输入要输出 loginformation, 可using
{expr}格式插入表达式值 - 勾选"Evaluate and log"可添加额 out 表达式
- 点击"OK"
2.4 method断点
method断点会 in 进入 or 离开指定method时暂停程序执行. 要设置method断点, 请按照以 under 步骤operation:
- in method名 on right 键点击
- 选择"Toggle Method Breakpoint"
- or in method名 left 侧 空白区域点击
2.5 exception断点
exception断点会 in 抛出指定exception时暂停程序执行. 要设置exception断点, 请按照以 under 步骤operation:
- 点击Run > View Breakpoints, or using fast 捷键
Ctrl + Shift + F8 - 点击"+"按钮, 选择"Exception Breakpoint"
- 输入要捕获 exceptionclass名
- 选择 is 否捕获已processing and 未processing exception
- 点击"OK"
2.6 字段断点
字段断点会 in 访问 or modify指定字段时暂停程序执行. 要设置字段断点, 请按照以 under 步骤operation:
- in 字段名 on right 键点击
- 选择"Toggle Field Watchpoint"
- or in 字段名 left 侧 空白区域点击
3. 查看variable and 表达式
in debug过程in, 查看variable and 表达式 值 is 非常 important , 可以helping你Understand程序 执行status.
3.1 查看variable值
has many 种方式可以查看variable值:
- Variables窗口: in Variables窗口in查看当 before 作用域in 所 has variable
- 悬停查看: 将鼠标悬停 in variable on , 查看variable值
- fast 速查看: using fast 捷键
Alt + F8查看选in variable or 表达式值 - 表达式求值: 点击Run > Evaluate Expression, or using fast 捷键
Alt + F8, 输入表达式for求值
3.2 添加观察表达式
要添加观察表达式, 请按照以 under 步骤operation:
- in Watches窗口in点击"+"按钮
- 输入要观察 表达式
- 按Enter键
3.3 modifyvariable值
in debug过程in, 你可以modifyvariable 值, 以便test不同 执行path:
- in Variables窗口in, right 键点击要modify variable
- 选择"Set Value"
- 输入 new 值
- 按Enter键
4. advanceddebugtechniques
IntelliJ IDEAproviding了许 many advanceddebugtechniques, 可以helping你更 high 效地debug complex 程序.
4.1 智能步入
当遇 to many 个method调用 in 同一行时, using智能步入functions可以选择要进入 method. using fast 捷键Shift + F7可以触发智能步入.
4.2 强制步入
强制步入可以进入任何method, includingJDK or 第三方library method. using fast 捷键Alt + Shift + F7可以触发强制步入.
4.3 debug时modifyvariableclass型
in debug过程in, 你可以modifyvariable class型, 以便test不同 执行path.
4.4 debug时modifycode
in debug过程in, 你可以modifycode, 然 after 点击Run > Reload Changed Classes or using fast 捷键Ctrl + F10重 new 加载modify after class, 无需重 new 启动debugsession.
4.5 debug时using断点组
你可以将相关 断点组织成组, 方便management and 启用/禁用. 点击Run > View Breakpoints, 然 after creation new 断点组.
5. debug many threadapplication
debug many threadapplication比debug单threadapplication更 complex , IntelliJ IDEAproviding了专门 tool来helping你debug many threadapplication.
5.1 thread视graph
in debugtool窗口in, 点击"Threads"tag可以查看所 has thread status and stackinformation.
5.2 冻结 and 解冻thread
你可以冻结 and 解冻thread, 以便单独debug某个thread:
- in Threads视graphin, right 键点击要冻结 thread
- 选择"Freeze"
- 要解冻thread, right 键点击冻结 thread, 选择"Unfreeze"
5.3 thread转储
thread转储可以显示所 has thread 当 before status and stackinformation. 点击debugtool窗口in "Thread Dump"按钮可以生成thread转储.
5.4 debug死lock
IntelliJ IDEA可以automatically detect死lock, 并 in debugtool窗口in显示死lockinformation.
6. debugWebapplication
IntelliJ IDEAproviding了强 big Webapplicationdebugfunctions, support many 种Webframework and server.
6.1 debugTomcatapplication
要debugTomcatapplication, 请按照以 under 步骤operation:
- configurationTomcatserverrun/debugconfiguration
- in codein设置断点
- 点击Debug按钮启动debug
- in 浏览器in访问application
6.2 debugSpring Bootapplication
要debugSpring Bootapplication, 请按照以 under 步骤operation:
- in codein设置断点
- 点击Run > Debug, or using fast 捷键
Shift + F9 - or in Mainclassin right 键点击, 选择Debug
6.3 远程debug
要for远程debug, 请按照以 under 步骤operation:
- in 远程server on 启动application时, 添加debugparameter:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 - in IntelliJ IDEAin, creationRemoterun/debugconfiguration
- configuration远程server 主机名 and 端口
- 点击Debug按钮启动远程debug
实践练习
请按照以 under 步骤completion练习:
- creation一个 simple Javaapplication, package含一个 has bug method
- in codein设置不同class型 断点 (行断点, 条件断点, log断点etc.)
- 启动debug, usingdebug控制按钮单步执行code
- 查看variable值 and 表达式, 找出bug 位置
- 尝试 in debug过程inmodifyvariable值, verificationbug 修复solutions
- modifycode, 重 new 加载class, verification修复效果
- creation一个 simple many threadapplication, 练习 many threaddebugtechniques