S
Sam (Deleted User)
Guest
ubuntu needs
~/crosspross.cpp
you can pass a parent directory, a exe name, or the path + exe to --pid-from -exe and case sensative it finds all pids that match
i tried adding -g and - ggdb and neither seemed to legitimately add debugging symbols and the backtrack wasn't helpful.
backtrace
what are you do here
sudo apt-get install g++ libprocps-dev
~/crosspross.cpp
C++:
/*
MIT License
Copyright © 2021 Samuel Venable
Copyright © 2021 Lars Nilsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cstdio>
#include <proc/readproc.h>
typedef pid_t PROSSID;
using std::string;
using std::vector;
using std::to_string;
namespace {
string PathFromFile(string file) {
size_t fp = file.find_last_of("/\\");
return file.substr(0, fp);
}
string NameFromFile(string file) {
size_t fp = file.find_last_of("/\\");
return file.substr(fp + 1);
}
} // anonymous namespace
namespace CrossPross {
void ExeFromProssId(PROSSID prossId, char **buffer) {
string symLink = string("/proc/") + to_string(prossId) + string("/exe");
char exe[PATH_MAX]; realpath(symLink.c_str(), exe);
string str(exe); str.resize(str.length() + 1, '\0');
*buffer = str.data();
}
// segfaults after 3 pids printed; backtrace is garbage even with -ggdb
void ProssIdFromExe(const char *exe, PROSSID **prossId, size_t *size) {
proc_t proc_info;
vector<PROSSID> vec; size_t i = 0;
memset(&proc_info, 0, sizeof(proc_info));
PROCTAB *proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS);
while (readproc(proc, &proc_info) != 0) {
char *buffer;
ExeFromProssId(proc_info.tgid, &buffer);
string str1 = exe ? : "";
string str2 = buffer ? : "";
if (str1 == str2) {
vec.push_back(proc_info.tgid); i++;
} else if (str1 == NameFromFile(str2)) {
vec.push_back(proc_info.tgid); i++;
} else if (str1 == PathFromFile(str2)) {
vec.push_back(proc_info.tgid); i++;
}
}
std::copy(vec.begin(), vec.end(), *prossId);
*size = i; closeproc(proc);
}
void CwdFromProssId(PROSSID prossId, char **buffer) {
string symLink = string("/proc/") + to_string(prossId) + string("/cwd");
char cwd[PATH_MAX]; realpath(symLink.c_str(), cwd);
string str(cwd); str.resize(str.length() + 1, '\0');
*buffer = str.data();
}
// segfaults after 3 pids printed; backtrace is garbage even with -ggdb
void ProssIdFromCwd(const char *cwd, PROSSID **prossId, size_t *size) {
proc_t proc_info;
vector<PROSSID> vec; size_t i = 0;
memset(&proc_info, 0, sizeof(proc_info));
PROCTAB *proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS);
while (readproc(proc, &proc_info) != 0) {
char *buffer;
CwdFromProssId(proc_info.tgid, &buffer);
string str1 = cwd ? : "";
string str2 = buffer ? : "";
if (str1 == str2) {
vec.push_back(proc_info.tgid); i++;
}
}
std::copy(vec.begin(), vec.end(), *prossId);
*size = i; closeproc(proc);
}
void CmdlineFromProssId(PROSSID prossId, char ***buffer, size_t *size) {
proc_t proc_info;
memset(&proc_info, 0, sizeof(proc_info));
PROCTAB *proc = openproc(PROC_FILLCOM | PROC_PID, &prossId);
size_t i = 0, pos = 0;
if (readproc(proc, &proc_info) != 0) {
while (proc_info.cmdline[pos] != NULL) {
pos += strlen(proc_info.cmdline[i]);
i++;
}
}
*buffer = proc_info.cmdline;
*size = i; closeproc(proc);
}
void EnvironFromProssId(PROSSID prossId, char ***buffer, size_t *size) {
string env;
proc_t proc_info;
memset(&proc_info, 0, sizeof(proc_info));
PROCTAB *proc = openproc(PROC_FILLENV | PROC_PID, &prossId);
size_t i = 0, pos = 0;
if (readproc(proc, &proc_info) != 0) {
while (proc_info.environ[pos] != NULL) {
pos += strlen(proc_info.environ[i]);
i++;
}
}
*buffer = proc_info.environ;
*size = i; closeproc(proc);
}
} // namespace CrossPross
namespace CrossProssPrint {
void PrintExeFromPid(PROSSID prossId) {
char *buffer;
CrossPross::ExeFromProssId(prossId, &buffer);
std::cout << buffer << std::endl;
}
void PrintPidFromExe(const char *exe) {
PROSSID *prossId; size_t size;
for (size_t i = 0; i < size; i++) {
CrossPross::ProssIdFromExe(exe, &prossId, &size);
std::cout << prossId[i] << std::endl;
}
}
void PrintCwdFromPid(PROSSID prossId) {
char *buffer;
CrossPross::CwdFromProssId(prossId, &buffer);
std::cout << buffer << std::endl;
}
void PrintPidFromCwd(const char *cwd) {
PROSSID *prossId; size_t size;
for (size_t i = 0; i < size; i++) {
CrossPross::ProssIdFromCwd(cwd, &prossId, &size);
std::cout << prossId[i] << std::endl;
}
}
void PrintCmdFromPid(PROSSID prossId) {
char **buffer;
size_t size;
for (size_t i = 0; i <= size; i++) {
CrossPross::CmdlineFromProssId(prossId, &buffer, &size);
std::cout << buffer[i] << std::endl;
}
}
void PrintEnvFromPid(PROSSID prossId) {
char **buffer;
size_t size;
for (size_t i = 0; i <= size; i++) {
CrossPross::EnvironFromProssId(prossId, &buffer, &size);
std::cout << buffer[i] << std::endl;
}
}
} // namespace CrossProssPrint
int main(int argc, char **argv) {
if (argc == 3) {
PROSSID pid = 0;
char *exe = NULL, *cwd = NULL;
if (strcmp(argv[2], "self") == 0) {
pid = getpid();
CrossPross::ExeFromProssId(pid, &exe);
CrossPross::CwdFromProssId(pid, &cwd);
} else pid = strtoul(argv[2], nullptr, 10);
if (strcmp(argv[1], "--exe-from-pid") == 0) {
CrossProssPrint::PrintExeFromPid(pid);
} else if (strcmp(argv[1], "--pid-from-exe") == 0) {
CrossProssPrint::PrintPidFromExe(exe ? : argv[2]);
} else if (strcmp(argv[1], "--cwd-from-pid") == 0) {
CrossProssPrint::PrintCwdFromPid(pid);
} else if (strcmp(argv[1], "--pid-from-cwd") == 0) {
CrossProssPrint::PrintPidFromCwd(cwd ? : argv[2]);
} else if (strcmp(argv[1], "--cmd-from-pid") == 0) {
CrossProssPrint::PrintCmdFromPid(pid);
} else if (strcmp(argv[1], "--env-from-pid") == 0) {
CrossProssPrint::PrintEnvFromPid(pid);
}
}
return 0;
}
~/crosspross --pid-from-exe /usr/bin
you can pass a parent directory, a exe name, or the path + exe to --pid-from -exe and case sensative it finds all pids that match
cd ~/;g++ "crosspross.cpp" -o "crosspross" -std=c++20 -static-libgcc -static-libstdc++ -lprocps -fPIC -ggdb -g
i tried adding -g and - ggdb and neither seemed to legitimately add debugging symbols and the backtrack wasn't helpful.
backtrace
Code:
neon@neon:~$ cd ~/;g++ "crosspross.cpp" -o "crosspross" -std=c++20 -static-libgcc -static-libstdc++ -lprocps -lpthread -fPIC -ggdb -g
neon@neon:~/$ gdb ./crosspross
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./crosspross...
(gdb) run --pid-from-exe /usr/bin
Starting program: /home/neon/crosspross --pid-from-exe /usr/bin
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
1479
1483
1487
1488
1644
1659
1664
1678
1680
1695
1697
1713
1721
1726
1822
2595
27536
31385
62789
62801
63106
63108
63122
Program received signal SIGSEGV, Segmentation fault.
0x000005d0000005cf in ?? ()
(gdb) bt
#0 0x000005d0000005cf in ?? ()
#1 0x0000067b0000066c in ?? ()
#2 0x0000068e00000680 in ?? ()
#3 0x0000069f00000690 in ?? ()
#4 0x000006b1000006a1 in ?? ()
#5 0x000006be000006b9 in ?? ()
#6 0x00000a230000071e in ?? ()
#7 0x00007a9900006b90 in ?? ()
#8 0x0000f5510000f545 in ?? ()
#9 0x0000f6840000f682 in ?? ()
#10 0x000000000000f692 in ?? ()
#11 0xd782b058c64d3307 in ?? ()
#12 0xd782a0147f633307 in ?? ()
#13 0x00007fff00000000 in ?? ()
#14 0x0000000000000000 in ?? ()
(gdb)
Last edited by a moderator: