| 1 | #include "ProcessExplorer.h" |
| 2 | #include "ui_ProcessExplorer.h" |
| 3 | |
| 4 | #include <QFile> |
| 5 | #include <QDir> |
| 6 | #include <QRegExp> |
| 7 | |
| 8 | ProcessExplorer::ProcessExplorer(int pid, QWidget *parent) : |
| 9 | QDialog(parent), |
| 10 | ui(new Ui::ProcessExplorer), |
| 11 | _pid(pid) |
| 12 | { |
| 13 | ui->setupUi(this); |
| 14 | |
| 15 | fillFields(_pid); |
| 16 | |
| 17 | ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents); |
| 18 | ui->tableWidget->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Stretch); |
| 19 | ui->tableWidget->horizontalHeader()->setSectionResizeMode(2,QHeaderView::ResizeToContents); |
| 20 | ui->tableWidget->horizontalHeader()->setSectionResizeMode(3,QHeaderView::ResizeToContents); |
| 21 | |
| 22 | connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(close())); |
| 23 | } |
| 24 | |
| 25 | ProcessExplorer::~ProcessExplorer(){ |
| 26 | delete ui; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | void ProcessExplorer::fillFields(int pid){ |
| 33 | ui->label_7->setText(""); |
| 34 | ui->label_5->setText(""); |
| 35 | ui->label_6->setText(""); |
| 36 | |
| 37 | |
| 38 | // general |
| 39 | QString procPath = "/proc/"+QString::number(pid); |
| 40 | |
| 41 | // path exe |
| 42 | QFile filePathExe(procPath+"/exe"); |
| 43 | ui->label_5->setText(filePathExe.symLinkTarget()); |
| 44 | |
| 45 | // command line |
| 46 | QFile fileCmdLine(procPath+"/cmdline"); |
| 47 | if (fileCmdLine.open(QIODevice::ReadOnly)){ |
| 48 | QByteArray fileContents = fileCmdLine.readAll(); |
| 49 | fileContents.replace(char(0x00)," "); |
| 50 | ui->label_6->setText(fileContents); |
| 51 | fileCmdLine.close(); |
| 52 | } |
| 53 | |
| 54 | // path exe |
| 55 | QFile fileCurrentDir(procPath+"/cwd"); |
| 56 | ui->label_7->setText(fileCurrentDir.symLinkTarget()); |
| 57 | |
| 58 | QMap<QString, QString> mapFileProcStatus = mapFromFile("/proc/"+QString::number(pid)+"/status"); |
| 59 | |
| 60 | ui->label_8->setText(mapFileProcStatus["Pid"]); |
| 61 | ui->label_10->setText(mapFileProcStatus["PPid"]); |
| 62 | ui->label_12->setText(mapFileProcStatus["State"]); |
| 63 | |
| 64 | // Environment |
| 65 | QFile fileEnviron(procPath+"/environ"); |
| 66 | if (fileEnviron.open(QIODevice::ReadOnly)){ |
| 67 | QByteArray fileContents = fileEnviron.readAll(); |
| 68 | fileContents.replace(char(0x00),"\n"); |
| 69 | ui->textEdit->setText(fileContents); |
| 70 | fileEnviron.close(); |
| 71 | } |
| 72 | |
| 73 | // Files |
| 74 | QTableWidgetItem *tmpWidgetItem = nullptr; |
| 75 | QDir dir(procPath+"/fd"); |
| 76 | dir.setFilter(QDir::Files); |
| 77 | dir.setSorting(QDir::Name); |
| 78 | QFileInfoList list = dir.entryInfoList(); |
| 79 | for (int i = 0; i < list.size(); ++i) { |
| 80 | QFileInfo fileInfo = list.at(i); |
| 81 | QString Path = fileInfo.symLinkTarget(); |
| 82 | |
| 83 | QMap<QString, QString> mapFile = mapFromFile("/proc/"+QString::number(pid)+"/fdinfo/"+fileInfo.fileName()); |
| 84 | int pos = mapFile["pos"].toInt(); |
| 85 | QString flags = mapFile["flags"]; |
| 86 | |
| 87 | ui->tableWidget->setRowCount(i+1); |
| 88 | |
| 89 | tmpWidgetItem = new QTableWidgetItem(fileInfo.fileName()); |
| 90 | ui->tableWidget->setItem(i, 0, tmpWidgetItem); |
| 91 | |
| 92 | tmpWidgetItem = new QTableWidgetItem(Path); |
| 93 | tmpWidgetItem->setToolTip(Path); |
| 94 | ui->tableWidget->setItem(i, 1, tmpWidgetItem); |
| 95 | |
| 96 | tmpWidgetItem = new QTableWidgetItem(QString::number(pos)); |
| 97 | ui->tableWidget->setItem(i, 2, tmpWidgetItem); |
| 98 | |
| 99 | tmpWidgetItem = new QTableWidgetItem(flags); |
| 100 | ui->tableWidget->setItem(i, 3, tmpWidgetItem); |
| 101 | |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | QMap<QString, QString> ProcessExplorer::mapFromFile(QString path){ |
| 107 | QMap<QString, QString> result; |
| 108 | |
| 109 | QFile file(path); |
| 110 | if (!file.open(QIODevice::ReadOnly|QIODevice::Text)){ |
| 111 | return result; |
| 112 | } |
| 113 | |
| 114 | QByteArray contents = file.readAll(); |
| 115 | QStringList lines = QString(contents).split(QRegularExpression("[\r|\n]")); |
| 116 | for (QString line:lines){ |
| 117 | QStringList pairs = line.split(":"); |
| 118 | if (pairs.size()==2){ |
| 119 | QString key=pairs.at(0); |
| 120 | QString value = pairs.at(1); |
| 121 | value.replace(QRegularExpression("^\\s+"),""); |
| 122 | value.replace(QRegularExpression("\\s+$"),""); |
| 123 | |
| 124 | result[key]=value; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | file.close(); |
| 129 | |
| 130 | |
| 131 | return result; |
| 132 | } |