c++ - Qt:使用脚本从二进制文件运行时无法找到应用程序 pid

标签 c++ linux qt shellcode

我的目标是在应用程序运行时获取应用程序的 PID。 我写了一个简单的函数调用 pgrep 命令:

QString Scriptlauncher::getAppProcessId() {
    QProcess p;
    QString programme("pgrep");
    QStringList args = QStringList() << "app_name";
    p.start(programme, args);
    p.waitForReadyRead();
    QByteArray rdata = p.readAllStandardOutput();
    qDebug() << "------------- script output rawdata is:" << rdata;
    if (!rdata.isEmpty()) {
        QString pid(rdata);
        pid = pid.left(pid.length() -1); // cut '\n' symbol
        qWarning() << "APPLICATION pid is" << pid;
        return pid;
    }
    qWarning() << "failed to find out PID";
    return ("-1");
}

当我直接从 Qt 或使用简单脚本运行程序时(将其命名为 execute.sh;它导出所有需要的共享库,然后运行应用程序二进制文件,- 从终端运行应用程序) ,上面的代码块返回正确的值:

user@host:/standalone_package/ execute.sh
------------- script output rawdata is: "21094\n"
APPLICATION pid is "21094"

但是当我从 valgrind 堆分析器命令运行 execute.sh 时,该函数返回:

user@host:/standalone_package/ valgrind --tool=massif --trace-children=yes ./execute.sh
------------- script output rawdata is: ""
failed to find out PID

最佳答案

感谢Hayt对于链接!我找到了解决方案!

second link offered 需要 QProcess 对象的实例。我不知道如何获得它。

但使用 first link ,我直接从应用程序和 valgrind 下获取代码:

QString Scriptlauncher::getAppProcessId() {
    long pid = (long)getpid();
    qDebug ("------------- pid is %d", pid);
    QString pidstr = QString::number(pid);
    return pidstr;
}

关于c++ - Qt:使用脚本从二进制文件运行时无法找到应用程序 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235903/

相关文章:

c++ - 获取从 QTableWidget 中的单元格小部件发出的信号发送器的行索引

c++ - 为用户定义类型重载 QDataStream << 和 >> 运算符

c++ - 为什么常量有类型修饰符?

php - 确定 PHP 在 OOM 终止期间正在做什么?

linux - 进程因 -9 中止而终止?

linux - emacs23 到 emacs24 : imap shell "Opening nnimap server...failed: Pseudo-terminal will not be allocated ..."

c++ - 仅在单击 "open"时关闭 QFileDialog

C++ 模板。如何让这段代码更漂亮?

c++ - 避免 "local variable as default parameter"的最佳设计?

c++ - OpenMP:并行 for(i;...) 和 i 值