Skip to content

Connection port of debugger not changing #1257

@Ahmed-Bilal-01

Description

@Ahmed-Bilal-01

I am trying to make a launch file for GDB Hardware Debugger. This configuration runs on a custom added button in menu. Everything has been setup except connection port to what I want to set.

When try to change it remains default localhost:1234 I want it to be localhost:55000 this thing is not getting set from my java code.

`
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.WorkbenchException;

public class MyBuildHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    MessageConsole console = getBuildConsole();
    ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
    MessageConsoleStream out = console.newMessageStream();

    try {
        IProject activeProject = getActiveProject();
        if (activeProject == null || !activeProject.isOpen()) {
            out.println("No active project found.");
            out.close();
            return null;
        }

        String projectName = activeProject.getName();
        String projectPath = activeProject.getLocation().toOSString();
        out.println("Active Project Name: " + projectName);
        out.println("Active Project Path: " + projectPath);

        switchToDebugPerspective();
        launchDebugConfiguration(projectPath, projectName, out);

    } catch (Exception e) {
        out.println("Exception while executing build handler: " + e.getMessage());
        e.printStackTrace(new PrintStream(out));
        try {
            out.close();
        } catch (IOException e1) {
            System.err.println("Error closing console stream: " + e1.getMessage());
        }
    }

    return null;
}

private void switchToDebugPerspective() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window != null) {
        IWorkbenchPage page = window.getActivePage();
        if (page != null) {
            try {
                page.resetPerspective();
                PlatformUI.getWorkbench().showPerspective(IDebugUIConstants.ID_DEBUG_PERSPECTIVE, window);
            } catch (WorkbenchException e) {
                e.printStackTrace();
            }
        }
    }
}

private void launchDebugConfiguration(String projectPath, String projectName, MessageConsoleStream out) {
    try {
        ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
        synchronized (out) {
            out.println("Launch Manager: " + (launchManager != null ? "Available" : "Null"));
        }
        if (launchManager == null) {
            out.println("Error: Launch Manager is null. Check Eclipse runtime environment.");
            return;
        }

        ILaunchConfigurationType configType = launchManager.getLaunchConfigurationType("org.eclipse.cdt.debug.gdbjtag.launchConfigurationType");
        synchronized (out) {
            out.println("Config Type for org.eclipse.cdt.debug.gdbjtag.launchConfigurationType: " + (configType != null ? "Found" : "Not Found"));
        }
        if (configType == null) {
            out.println("Error: GDB JTAG launch configuration type not found. Check CDT installation or ID.");
            return;
        }

        String elfPath = projectPath + "\\Debug\\" + projectName + ".elf";
        File elfFile = new File(elfPath);
        if (!elfFile.exists()) {
            out.println("Error: .elf file not found at " + elfPath + ". Please ensure the build process completed successfully.");
            return;
        }

        String configName = "RM57 Debug - " + projectName;
        ILaunchConfigurationWorkingCopy workingCopy = configType.newInstance(null, configName);

        // Program and project info
        workingCopy.setAttribute("org.eclipse.cdt.launch.PROGRAM_NAME", elfPath);
        workingCopy.setAttribute("org.eclipse.cdt.launch.PROJECT_ATTR", projectName);

        // Set GDB path and remote connection using IGDBLaunchConfigurationConstants
        String gdbPath = projectPath + "\\newScript.bat";
       cworkingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.useRemoteTarget", true);
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.ipAddress", "localhost");
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.portNumber", "55000");
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.connection", "localhost:55000");


        workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUG_NAME, gdbPath);
        workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_REMOTE_TCP, true);
        workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, "localhost");
        workingCopy.setAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, "55000");

        out.println("GDB debugger path set to: " + gdbPath);

        workingCopy.setAttribute("org.eclipse.cdt.launch.debuggerServer", "Generic TCP/IP");
        workingCopy.setAttribute("org.eclipse.cdt.launch.forceThreadListUpdateOnSuspend", true);
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.loadImage", false);
        workingCopy.setAttribute("org.eclipse.cdt.launch.loadImage", false);
        workingCopy.setAttribute("org.eclipse.cdt.launch.loadSymbols", true);
        workingCopy.setAttribute("org.eclipse.cdt.launch.useProjectBinary", true);
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.setStopAt", true);
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.runCommands", "jump main");
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.stopAt", "main");
        workingCopy.setAttribute("org.eclipse.cdt.debug.gdbjtag.core.setResume", false);

        // Save and launch
        ILaunchConfiguration configuration = workingCopy.doSave();
        DebugUITools.launch(configuration, ILaunchManager.DEBUG_MODE);
        out.println("Debug configuration launched successfully.");

    } catch (Exception e) {
        out.println("Failed to launch debug configuration: " + e.getMessage());
        e.printStackTrace(new PrintStream(out));
    }
}

private IProject getActiveProject() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null) return null;

    IWorkbenchPage page = window.getActivePage();
    if (page == null) return null;

    IEditorPart editor = page.getActiveEditor();
    if (editor != null) {
        IFile file = editor.getEditorInput().getAdapter(IFile.class);
        if (file != null) return file.getProject();
    }

    ISelection selection = window.getSelectionService().getSelection();
    if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        Object firstElement = structuredSelection.getFirstElement();
        if (firstElement instanceof IAdaptable) {
            IResource resource = ((IAdaptable) firstElement).getAdapter(IResource.class);
            if (resource != null) return resource.getProject();
        }
    }

    return null;
}

private MessageConsole getBuildConsole() {
    String name = "Build Output";
    ConsolePlugin plugin = ConsolePlugin.getDefault();
    IConsoleManager conMan = plugin.getConsoleManager();
    for (IConsole c : conMan.getConsoles()) {
        if (c instanceof MessageConsole && c.getName().equals(name)) return (MessageConsole) c;
    }
    MessageConsole newConsole = new MessageConsole(name, null);
    conMan.addConsoles(new IConsole[]{newConsole});
    return newConsole;
}

}`

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions