Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*******************************************************************************/
package com.espressif.idf.core.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import com.espressif.idf.core.logging.Logger;
Expand All @@ -24,13 +27,15 @@ private PortChecker()

public static boolean isPortAvailable(int port)
{
try (Socket ignored = new Socket("localhost", port)) //$NON-NLS-1$
try (ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getByName("127.0.0.1"))) //$NON-NLS-1$
{
return false;
serverSocket.setReuseAddress(true);
return true;
}
catch (Exception e)
{
return true;
Logger.log("Port: " + port + " is not available"); //$NON-NLS-1$ //$NON-NLS-2$
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import java.util.List;

import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.debug.gdbjtag.core.IGDBJtagConstants;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.embedcdt.core.EclipseUtils;
import org.eclipse.embedcdt.core.StringUtils;
import org.eclipse.embedcdt.debug.gdbjtag.core.DebugUtils;
Expand Down Expand Up @@ -93,9 +95,12 @@ public static String[] getGdbServerCommandLineArray(ILaunchConfiguration configu
lst.add(executable);

int port = PortChecker
.getAvailablePort(configuration.getAttribute(ConfigurationAttributes.GDB_SERVER_GDB_PORT_NUMBER,
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));

.getAvailablePort(DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);

ILaunchConfigurationWorkingCopy configurationWorkingCopy = configuration.getWorkingCopy();
configurationWorkingCopy.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, port);
configurationWorkingCopy.doSave();

lst.add("-c"); //$NON-NLS-1$
lst.add("gdb_port " + port); //$NON-NLS-1$

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ protected void provideDefaults(ILaunchConfigurationWorkingCopy config) throws Co
fDefaultPreferences.getGdbClientExecutable());
}

int availableRemotePort = PortChecker.getAvailablePort(config.getAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER,
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, availableRemotePort);
if (Configuration.getDoStartGdbServer(config))
{
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
}
Comment on lines +166 to +169
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Port handling logic needs improvement

The current implementation sets a fixed default port when the GDB server starts, but according to PR feedback, this doesn't handle port conflicts effectively. When the default port (3333) is occupied, the system should retry with alternative ports.

Consider implementing a more robust port selection mechanism:

 if (Configuration.getDoStartGdbServer(config))
 {
-    config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
+    int port = PortChecker.findAvailablePort(DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
+    if (port == -1) {
+        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+            "Unable to find available port for GDB server"));
+    }
+    config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, port);
 }

Committable suggestion skipped: line range outside the PR's diff.


config.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, CustomIdfProcessFactory.ID);
}
Expand All @@ -172,8 +173,6 @@ public void initializeServerConsole(IProgressMonitor monitor) throws CoreExcepti

// Add the GDB server process to the launch tree
newProcess = addServerProcess(Configuration.getGdbServerCommandName(fConfig));
newProcess.setAttribute(IProcess.ATTR_CMDLINE, Configuration.getGdbServerCommandLine(fConfig));

monitor.worked(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,19 @@ protected void handleCompleted()
}
// --------------------------------------------------------------------
}

@Override
/**
* This method takes care of cleaning up any resources allocated by the launch, as early as
* the call to getLaunch(), whenever the launch is cancelled or does not complete properly.
* @since 5.0 */
protected void cleanupLaunch(ILaunch launch) throws DebugException
{
if (launch instanceof GdbLaunch)
{
launch.terminate();
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Potential race condition in cleanupLaunch method.
The cleanupLaunch method lacks synchronization, which may lead to race conditions when multiple debug sessions run concurrently.

🔗 Analysis chain

Verify cleanup behavior during concurrent debug sessions.

The cleanup implementation should be tested for race conditions when multiple debug sessions are running simultaneously.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for potential race conditions in cleanup handling
rg -A 5 "terminate|cleanup" --type java

Length of output: 21202


Script:

#!/bin/bash
# Search for synchronization mechanisms in LaunchConfigurationDelegate.java related to cleanup
fd "LaunchConfigurationDelegate.java" | xargs ast-grep --pattern '
class LaunchConfigurationDelegate {
  $$$
  cleanupLaunch($_) {
    $$$
    synchronized($_) {
      $$$
    }
  }
}'

Length of output: 184


/**
* Perform some local validations before starting the debug session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class TabDebugger extends AbstractLaunchConfigurationTab

private Text fTargetIpAddress;
private Text fTargetPortNumber;
private Group gdbClientGroup;

protected Button fUpdateThreadlistOnSuspend;

Expand Down Expand Up @@ -470,6 +471,7 @@ public void widgetSelected(SelectionEvent e)
public void widgetSelected(SelectionEvent e)
{
doStartGdbServerChanged();
gdbClientGroup.setEnabled(!fDoStartGdbServer.getSelection());
if (fDoStartGdbServer.getSelection())
{
fTargetIpAddress.setText(DefaultPreferences.REMOTE_IP_ADDRESS_LOCALHOST);
Expand Down Expand Up @@ -572,16 +574,16 @@ public void modifyText(ModifyEvent e)
private void createGdbClientControls(Composite parent)
{

Group group = new Group(parent, SWT.NONE);
gdbClientGroup = new Group(parent, SWT.NONE);
{
GridLayout layout = new GridLayout();
group.setLayout(layout);
gdbClientGroup.setLayout(layout);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
group.setLayoutData(gd);
group.setText(Messages.getString("DebuggerTab.gdbSetupGroup_Text")); //$NON-NLS-1$
gdbClientGroup.setLayoutData(gd);
gdbClientGroup.setText(Messages.getString("DebuggerTab.gdbSetupGroup_Text")); //$NON-NLS-1$
}

Composite comp = new Composite(group, SWT.NONE);
Composite comp = new Composite(gdbClientGroup, SWT.NONE);
{
GridLayout layout = new GridLayout();
layout.numColumns = 5;
Expand Down
Loading