死锁检测工具调整支持容器

This commit is contained in:
machunyu 2022-11-29 10:17:51 +08:00
parent c8eec9825f
commit 7d1518a15d
4 changed files with 130 additions and 80 deletions

Binary file not shown.

View File

@ -0,0 +1,9 @@
#!/bin/bash
set -e
USER_DIR=$(cd $(dirname $0); cd ..; pwd)
cat $(dirname $0)/.user.pwd | sudo -S ls /proc/$1 > /dev/null
cat $(dirname $0)/.user.pwd | sudo -S $(dirname $0)/detect_deadlock $1

View File

@ -35,85 +35,126 @@ export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "deadlock-detect" is now active!');
let newFn={
local_detect_deadlock:function (message: any ,panel: vscode.WebviewPanel){
let form=message.form;
if(form.CmdType===1){
testPwd(form.pwd, (ok)=>{
if(!ok){
vscode.window.showErrorMessage("用户密码不匹配");
return;
}
if(fs.existsSync(`/proc/${form.pid}`))
{
exec(`echo '${form.pwd}' | sudo -S ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.pid}`, (e: any, x: string, c: string) => {
// console.log(`echo '${form.pwd}' | sudo ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.pid}`);
if(!e){
form.data =x;
form.success='success';
panel.webview.postMessage({
res: form});
}
else{
form.success='error';
panel.webview.postMessage({
res: form});
}
});
}else{
vscode.window.showErrorMessage(`当前不存在${form.pid}进程`);
}
});
}
},
let arch = process.arch;
if (arch == 'x64'){
arch = 'x86_64';
}
remote_detect_deadlock:function (message: any ,panel: vscode.WebviewPanel){
let form=message.form;
if(form.CmdType===3){
// sudo apt install sshpass
exec(`sshpass -p '${form.pwd}' ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} 'echo "${form.pwd}" | sudo -S ls /proc/${form.pid}'`, (e: any, x: string, c: string) => {
if(e){
if(c.includes('Permission denied')){
vscode.window.showErrorMessage("远程主机用户名或密码错误");
}else if(c.includes('ls:')){
vscode.window.showErrorMessage(`远程主机不存在${form.pid}进程`);
}else{
vscode.window.showErrorMessage(`远程主机无法路由,或其他未知错误`);
}
return;
}
child.exec(`sshpass -p '${form.pwd}' ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} '[ ! -d .deadlock-detect ] && mkdir .deadlock-detect || ls .deadlock-detect'`, (e: any, x: string, c: any) => {
// console.log(`echo '${form.pwd}' | sudo ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.pid}`);
if(e){
vscode.window.showErrorMessage("远程主机无法创建目录, 请确认登录用户是否拥有权限.");
return;
}
child.exec(`sshpass -p '${form.pwd}' scp -o StrictHostKeyChecking=no ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.user}@${form.ip}:./.deadlock-detect`, (e: any, x: string, c: any) => {
// console.log(`echo '${form.pwd}' | sudo ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.pid}`);
if(e){
vscode.window.showErrorMessage("远程主机无法拷贝文件, 请确认登录用户是否拥有权限.");
return;
}
child.exec(`sshpass -p '${form.pwd}' ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} 'echo "${form.pwd}" | sudo -S ./.deadlock-detect/detect_deadlock ${form.pid}'`, (e: any, x: string, c: any) => {
// console.log(`echo '${form.pwd}' | sudo ${__dirname}/../detect-tools/x86_64/detect_deadlock ${form.pid}`);
if(!e){
form.data =x;
form.success='success';
panel.webview.postMessage({
res: form});
}
else{
form.success='error';
panel.webview.postMessage({
res: form});
}
});
});
});
});
}
function testPwd1(cb: (ok: boolean)=>void): void {
try{
execSync(`cat ${__dirname}/../detect-tools/.user.pwd | sudo -S ls /root/`);
cb(true);
}catch (error: any) {
cb(false);
}
};
return;
}
function local_detect_deadlock (message: any ,panel: vscode.WebviewPanel){
let form=message.form;
let cmdout = '';
fs.writeFileSync(`${__dirname}/../detect-tools/.user.pwd`, `${form.pwd}`, { encoding: 'utf8', flag: 'w' });
if(form.CmdType===1){
testPwd1((ok)=>{
if(!ok){
vscode.window.showErrorMessage("用户密码不匹配");
return;
}
if(fs.existsSync(`/proc/${form.pid}`))
{
try{
cmdout = execSync(`cat ${__dirname}/../detect-tools/.user.pwd | sudo -S ${__dirname}/../detect-tools/${arch}/detect_deadlock ${form.pid}`).toString();
form.data =cmdout;
form.success='success';
panel.webview.postMessage({
res: form});
}catch (error: any) {
form.success='error';
panel.webview.postMessage({
res: form});
}
}else{
vscode.window.showErrorMessage(`当前不存在${form.pid}进程`);
}
});
}
fs.unlinkSync(`${__dirname}/../detect-tools/.user.pwd`);
}
function remote_clean(form: any){
execSync(`sshpass -f ${__dirname}/../detect-tools/.user.pwd ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} 'rm -rf .user.pwd detect.sh detect_deadlock'`);
fs.unlinkSync(`${__dirname}/../detect-tools/.user.pwd`);
}
function remote_detect_deadlock(message: any, panel: vscode.WebviewPanel){
let form=message.form;
let cmdout = '';
let remote_arch = '';
// sudo apt install sshpass
fs.writeFileSync(`${__dirname}/../detect-tools/.user.pwd`, `${form.pwd}`, { encoding: 'utf8', flag: 'w' });
try{
remote_arch = execSync(`sshpass -f ${__dirname}/../detect-tools/.user.pwd ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} 'uname -m'`).toString();
}catch (error: any) {
vscode.window.showErrorMessage(`${error.message}`);
fs.unlinkSync(`${__dirname}/../detect-tools/.user.pwd`);
return;
}
if (remote_arch.includes('aarch64')){
remote_arch = 'arm64';
}else if (remote_arch.includes('x64') || remote_arch.includes('x86_64')){
remote_arch = 'x86_64';
}
try{
cmdout = execSync(`sshpass -f ${__dirname}/../detect-tools/.user.pwd scp -o StrictHostKeyChecking=no ${__dirname}/../detect-tools/.user.pwd ${__dirname}/../detect-tools/detect.sh ${__dirname}/../detect-tools/${remote_arch}/detect_deadlock ${form.user}@${form.ip}:./`).toString();
}catch (error: any) {
let msg = `${error.message}`;
// console.log(`sshpass -f ${__dirname}/.user.pwd scp -o StrictHostKeyChecking=no ${__dirname}/.user.pwd ${__dirname}/../detect-tools/detect.sh ${__dirname}/../detect-tools/${arch}/detect_deadlock ${form.user}@${form.ip}:/tmp`);
if(msg.includes('Permission denied, please try again')){
vscode.window.showErrorMessage("远程主机用户名或密码错误");
}else if(msg.includes('scp:')){
vscode.window.showErrorMessage("远程主机无法拷贝文件, 请确认登录用户是否拥有权限.");
}else {
vscode.window.showErrorMessage(msg);
}
fs.unlinkSync(`${__dirname}/../detect-tools/.user.pwd`);
return;
}
cmdout = '';
try{
cmdout = execSync(`sshpass -f ${__dirname}/../detect-tools/.user.pwd ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} 'bash detect.sh ${form.pid};'`).toString();
}catch (error: any) {
let msg = `${error.message}`;
// console.log(`sshpass -f ${__dirname}/.user.pwd ssh -o StrictHostKeyChecking=no ${form.user}@${form.ip} '[ ! -d .deadlock-detect ] && mkdir .deadlock-detect || ls .deadlock-detect > /dev/null; mv /tmp/detect.sh .deadlock-detect; bash .deadlock-detect/detect.sh ${form.pid}'`);
if(msg.includes('ls:')){
vscode.window.showErrorMessage(`远程主机不存在${form.pid}进程`);
}else if(msg.includes('Permission denied, please try again')){
vscode.window.showErrorMessage("远程主机用户名或密码错误");
}else {
console.log(msg);
vscode.window.showErrorMessage(`远程主机无法路由,或其他未知错误`);
form.success='error';
panel.webview.postMessage({
res: form});
}
remote_clean(form);
return;
}
{
form.data = cmdout;
form.success='success';
panel.webview.postMessage({
res: form});
}
remote_clean(form);
}
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
@ -129,14 +170,14 @@ export function activate(context: vscode.ExtensionContext) {
retainContextWhenHidden: true,
}
);
panel.webview.onDidReceiveMessage(message => {
switch(message.form.CmdType) {
case 1://local pid dead-detect
newFn.local_detect_deadlock(message,panel);
local_detect_deadlock(message,panel);
break;
case 3://remote pid dead-detect
newFn.remote_detect_deadlock(message,panel);
remote_detect_deadlock(message,panel);
break;
}
});