Skip to content

Commit efcb3fb

Browse files
committed
Support listing control sockets on linux
1 parent 1d2cf21 commit efcb3fb

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@
567567
{
568568
"command": "extension.php-debug.runEditorContents",
569569
"when": "resourceLangId == php && !inDiffEditor && resourceScheme == file"
570+
},
571+
{
572+
"command": "extension.php-debug.pauseXdebugControlSocket",
573+
"when": "isLinux || isWindows"
570574
}
571575
],
572576
"debug/variables/context": [

src/controlSocket.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import * as CP from 'child_process'
44
import { promisify } from 'util'
55
import { supportedEngine } from './xdebugUtils'
66
import { DOMParser } from '@xmldom/xmldom'
7+
import * as fs from 'fs'
8+
import { decode } from 'iconv-lite'
9+
import { ENCODING } from './dbgp'
710

811
export class ControlSocket {
912
/**
@@ -58,7 +61,11 @@ export class ControlSocket {
5861
})
5962
s.on('data', data => {
6063
s.destroy()
61-
resolve(data.toString())
64+
if (data.length > 0 && data.at(data.length - 1) == 0) {
65+
resolve(decode(data.subarray(0, data.length - 1), ENCODING))
66+
} else {
67+
resolve(decode(data, ENCODING))
68+
}
6269
})
6370
s.on('error', error => {
6471
reject(
@@ -74,10 +81,9 @@ export class ControlSocket {
7481
}
7582

7683
async listControlSockets(): Promise<XdebugRunningProcess[]> {
77-
let retval:XdebugRunningProcess[]
84+
let retval: XdebugRunningProcess[]
7885
if (process.platform === 'linux') {
79-
// TODO
80-
throw new Error('Invalid platform for Xdebug control socket')
86+
retval = await this.listControlSocketsLinux()
8187
} else if (process.platform === 'win32') {
8288
retval = await this.listControlSocketsWin()
8389
} else {
@@ -97,6 +103,23 @@ export class ControlSocket {
97103
return retval2
98104
}
99105

106+
private async listControlSocketsLinux(): Promise<XdebugRunningProcess[]> {
107+
const re = /@(xdebug-ctrl\.\d+)$/
108+
109+
const data = await fs.promises.readFile('/proc/net/unix')
110+
const lines = data.toString().split('\n')
111+
const sockets: XdebugRunningProcess[] = []
112+
113+
for (const line of lines) {
114+
const matches = line.match(re)
115+
if (matches && matches.length > 0) {
116+
sockets.push({ ctrlSocket: matches[1] })
117+
}
118+
}
119+
120+
return sockets
121+
}
122+
100123
private async listControlSocketsWin(): Promise<XdebugRunningProcess[]> {
101124
const exec = promisify(CP.exec)
102125
try {
@@ -108,7 +131,6 @@ export class ControlSocket {
108131
.map<XdebugRunningProcess>(v => <XdebugRunningProcess>{ ctrlSocket: v })
109132

110133
return retval
111-
112134
} catch (err) {
113135
if (err instanceof Error && (<ExecError>err).stderr == 'File Not Found\r\n') {
114136
return []
@@ -124,7 +146,7 @@ interface ExecError extends Error {
124146

125147
export interface XdebugRunningProcess {
126148
readonly ctrlSocket: string
127-
ps: ControlPS
149+
ps?: ControlPS
128150
// todo
129151
}
130152

0 commit comments

Comments
 (0)