Debugger Tool In Action

本文介绍了C/C++的GDB调试工具,包括环境配置、代码编译、设置断点、运行及查看变量等操作。此外,还提到了Go语言的Delve调试器,Java的JDB和阿里巴巴开源的诊断工具Arthas,为不同语言的开发者提供了调试工具的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

Hello every Dear friend.I am wsyingang. In this article,I will show some debug tools.I will written this blog with English,because I does not find a suitable software for typewriting.

Debugger tool for c/c++

As we know ,GDB is the most popular debug tool for c++/c . Now, have a try.

Env info

Operating System:Ubuntu

Demo code

#include<bits/stdc++.h>
using namespace std;
int add(int a,int b){
    cout<<"in add"<<endl;
    int c=a+b;
    int d=a-b;
    return c;
}
int main(){
    int a;
    a=10;
    int b;
    b=11;
    int ans=add(a,b);
    cout<<"ans==="<<ans<<endl;
    int sum=1;
    for(int i=1;i<=10;i++){
        sum+=i;
    }
    cout<<"sum=="<<sum<<endl;
}

Compile Source Code

compile code with g++ compiler,if you have not this tool on your os,you can install like this:

sudo apt install g++
g++ -g -o test demo.cpp 

now,we get the target :test

Start gdb

gdb test

if everything work well,you will get this info:

Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...

Attention the last line,Reading symbols from test.

Some action

List source code

gdb) list
1	#include<bits/stdc++.h>
2	using namespace std;
3	int add(int a,int b){
4	    cout<<"in add"<<endl;
5	    int c=a+b;
6	    int d=a-b;
7	    return c;
8	}
9	int main(){
10	    int a;
(gdb) 

list command will list source code.Maybe you want look the spec function,you can exec command:list func_name
press enter will show you last code,which not appear.

run

runcommand will run this program.For Example

(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test 
in add
ans===21
sum==56
[Inferior 1 (process 9399) exited normally]
(gdb) 

breakpoint

For debugging breakpoint is very important.You cant set breakpoint with this format:

b line_number

for example

(gdb) b 6
Breakpoint 1 at 0x55555555520e: file demo.cpp, line 6.

Then,run this program

(gdb) run 
Starting program: /home/yg/code-repo/gdb-learn/test 
in add

Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6	    int d=a-b;
(gdb) 

This process will stop at line 6

continue

continue command let process go to next breakpoint.Now we set two breakpoints

(gdb) b 6
Note: breakpoint 1 also set at pc 0x55555555520e.
Breakpoint 2 at 0x55555555520e: file demo.cpp, line 6.
(gdb) b 20
Breakpoint 3 at 0x5555555552a0: file demo.cpp, line 20.

at line 6 and line 20.Then run this program

(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test 
in add

Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6	    int d=a-b;

Now,It reach first breakpoint,and continue

(gdb) continue
Continuing.
ans===21

Breakpoint 3, main () at demo.cpp:20
20	    cout<<"sum=="<<sum<<endl;

it reach second breakpoint.

print /whatis

When you are debugging,you want watch some variable.You can use print and whatis to detect variable.
For Example

(gdb) run
Starting program: /home/yg/code-repo/gdb-learn/test 
in add

Breakpoint 1, add (a=10, b=11) at demo.cpp:6
6	    int d=a-b;
(gdb) print d
$1 = 21845
(gdb) 

Now,you maybe curious about the value of d.It is a strange value,not equal a-b.In fact at line 6,a break point is alive,so d is not assign value at this time.
step step command will let process exec next step,if next step.
next next command will let process exec next command.

(gdb) next
7	    return c;
(gdb) print d
$4 = -1
(gdb) 

if exec next/step command,then print d ,you will find d is equal to a-b.

backtrace

(gdb) backtrace
#0  add (a=10, b=11) at demo.cpp:7
#1  <function called from gdb>
#2  add (a=10, b=11) at demo.cpp:6
#3  0x0000555555555245 in main () at demo.cpp:14
(gdb) 

this command will show you current stack.

quit

if you want finish debug use quit command.

Summary

gdb is a very useful tool for debugging.This above introduction is only small part.You can find more by yourself

Debugger for Go

If you are go programer,you can use gdb for debugging as well.But there as other options.
delve is a easy-to-use debug tool for golang.

How to Install

you can follow the documentation doc.
If everything work well,you will get the binary executable file,at you $GOPATH/bin
for example

yg@ubuntu:~/go/bin$ ls | grep dlv
dlv
yg@ubuntu:~/go/bin$ 

you can add an soft link for this file like this:

yg@ubuntu:/usr/local/bin$ ln -s ~/go/bin/dlv dlv
yg@ubuntu:/usr/local/bin$ ll
total 8
drwxr-xr-x  2 root root 4096 Nov  7 01:22 ./
drwxr-xr-x 11 root root 4096 Aug 15 00:02 ../
lrwxrwxrwx  1 root root   19 Nov  7 01:22 dlv -> /home/yg/go/bin/dlv*

then you can use dlv at everywhere.

Debugger for Java

JDB

a gdb likely tool for java debug

Arthas

Arthas is a Java Diagnostic tool open sourced by Alibaba.

<think>好的,我现在需要帮助用户解决在IntelliJ IDEA 2023.3中开发插件时,如何通过getExecutionConsole获取ConsoleView,并在窗口未创建时创建并打开一个的例子。首先,我需要理解用户的问题背景。 用户提到使用getExecutionConsole方法获取ConsoleView,但如果这个窗口没有创建,他们希望创建一个并打开。这可能涉及到插件开发中的控制台处理,特别是在处理执行结果或日志时。 首先,我需要回忆IntelliJ插件开发的相关知识。ConsoleView通常用于显示运行输出,可能通过ToolWindow来展示。用户可能需要在自己的插件中创建一个工具窗口,并在其中嵌入ConsoleView。 接下来,可能的步骤包括: 1. 获取或创建ToolWindow实例。 2. 检查ConsoleView是否存在,若不存在则创建。 3. 将ConsoleView添加到ToolWindow的内容中。 4. 确保在需要时显示窗口。 需要确认如何通过代码创建ConsoleView。IntelliJ提供了ConsoleViewFactory之类的工具类吗?可能需要使用TextConsoleBuilderFactory来构建ConsoleView。例如: ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole(); 然后,将consoleView添加到ToolWindow的内容管理中。可能需要使用ContentFactory来创建内容,并将其添加到ToolWindow的ContentManager中。 另外,关于getExecutionConsole的具体用法,可能需要查看是否有现有的示例或文档。如果用户之前尝试过getExecutionConsole但返回null,那么可能需要先创建ConsoleView并关联到相应的工具窗口。 还需要考虑线程安全的问题,因为UI操作需要在EDT(事件分发线程)中执行。因此,使用ApplicationManager.getApplication().invokeLater()来包装UI相关的代码。 可能的代码结构如下: ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("YourToolWindowId"); if (toolWindow == null) { // 创建ToolWindow的逻辑?或者需要先注册? // 可能需要通过plugin.xml注册ToolWindow,然后通过ID获取。 // 假设已经注册,直接获取可能即可。 } ConsoleView consoleView = getExecutionConsole(); // 假设这是用户现有的方法 if (consoleView == null) { consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole(); // 创建内容并添加到ToolWindow Content content = ContentFactory.getInstance().createContent(consoleView.getComponent(), "", false); toolWindow.getContentManager().addContent(content); } toolWindow.show(); 不过,用户可能需要更具体的示例,包括如何注册ToolWindow(如果尚未存在),以及如何确保正确创建和显示ConsoleView。可能需要补充关于ToolWindow注册的部分,比如在plugin.xml中添加扩展点: <extensions defaultExtensionNs="com.intellij"> <toolWindow id="YourConsoleID" anchor="bottom" factoryClass="com.yourplugin.YourToolWindowFactory"/> </extensions> 然后实现YourToolWindowFactory类,在其中创建ConsoleView并添加到内容中。 但用户的问题可能更侧重于动态创建,而不是预先注册。因此,可能需要动态创建ToolWindow,不过IntelliJ通常要求预先注册工具窗口。因此,正确的方法可能是在plugin.xml中注册工具窗口,然后在需要时获取或创建。 总结,用户的示例代码可能需要包括: - 获取或注册ToolWindow - 创建ConsoleView实例 - 将ConsoleView添加到ToolWindow的内容中 - 显示ToolWindow 同时,需要注意线程问题,确保UI操作在EDT中执行。 最后,给用户提供一个完整的示例,包括必要的步骤和代码,并提醒可能需要注意的事项,比如插件配置和线程安全。</think>在IntelliJ IDEA插件开发中动态管理控制台窗口的实现示例: 以下是通过自定义ToolWindow管理ConsoleView的完整方案(基于2023.3 API): 1. 首先在plugin.xml注册工具窗口(必需步骤): ```xml <extensions defaultExtensionNs="com.intellij"> <toolWindow id="MyExecutionConsole" anchor="bottom" icon="AllIcons.Debugger.Console" factoryClass="com.yourpackage.ConsoleWindowFactory"/> </extensions> ``` 2. 创建窗口工厂类: ```java public class ConsoleWindowFactory implements ToolWindowFactory { private static ConsoleView sharedConsole; @Override public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { if (sharedConsole == null) { sharedConsole = createConsole(project); } Content content = ContentFactory.getInstance().createContent( sharedConsole.getComponent(), "Execution Output", false); toolWindow.getContentManager().addContent(content); } private ConsoleView createConsole(Project project) { return TextConsoleBuilderFactory.getInstance() .createBuilder(project) .setViewer(true) // 启用交互模式 .setUsePredefinedMessageFilter(false) .getConsole(); } public static ConsoleView getSharedConsole() { return sharedConsole; } } ``` 3. 在Action中动态管理窗口: ```java public class RunAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent e) { Project project = e.getProject(); if (project == null) return; ToolWindowManager mgr = ToolWindowManager.getInstance(project); ToolWindow window = mgr.getToolWindow("MyExecutionConsole"); ConsoleView console = ConsoleWindowFactory.getSharedConsole(); if (console == null) { // 当窗口未初始化时强制激活 ApplicationManager.getApplication().invokeLater(() -> { mgr.getToolWindow("MyExecutionConsole").activate(() -> { console = ConsoleWindowFactory.getSharedConsole(); executeProcess(console); }); }); } else { executeProcess(console); } } private void executeProcess(ConsoleView console) { // 示例执行逻辑 console.print("Starting process...\n", ConsoleViewContentType.NORMAL_OUTPUT); // TODO: 添加实际执行逻辑 } } ``` 关键点说明: 1. 窗口生命周期管理:通过静态变量共享ConsoleView实例,保持单例模式 2. 线程安全:UI操作通过invokeLater确保在事件线程执行 3. 功能扩展点: - 支持多种内容类型(ERROR_OUTPUT/WARNING_OUTPUT) - 可添加自定义消息过滤器 - 支持交互式输入(通过setViewer配置) 使用建议: 1. 在需要获取控制台时优先使用工厂类的getSharedConsole() 2. 当窗口未激活时,使用activate()方法自动初始化 3. 通过ConsoleViewContentType实现彩色输出: ```java console.print("ERROR: ", ConsoleViewContentType.ERROR_OUTPUT); console.print("Warning message\n", ConsoleViewContentType.LOG_WARNING_OUTPUT); ``` 此方案实现了控制台窗口的动态管理,同时符合IntelliJ平台的最佳实践,能够正确处理窗口生命周期和线程安全问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值