23 #include <sys/types.h> 24 #include <sys/sysinfo.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 42 #define NAMELEN(dirent) strlen ((dirent)->d_name) 55 #define MAX_PID_T_STRLEN (sizeof ("-9223372036854775808") - 1) 72 sprintf (filename,
"/proc/%lld/task/%lld/stat",
81 content = (
char *)
xrealloc (content, content_read + 1024);
82 n = fread (content + content_read, 1, 1024, f.get ());
86 content[content_read] =
'\0';
92 p = strrchr (content,
')');
99 p = strtok_r (p,
" ", &ts);
100 for (i = 0; p != NULL && i != 36; ++i)
101 p = strtok_r (NULL,
" ", &ts);
103 if (p == NULL || sscanf (p,
"%d", &core) == 0)
130 int items_read = fscanf (fp.get (),
"%lld %17s", &stat_pid, cmd);
132 if (items_read == 2 &&
pid == stat_pid)
134 cmd[strlen (cmd) - 1] =
'\0';
135 strncpy (command, cmd + 1, maxlen);
141 snprintf (command, maxlen,
"%lld",
pid);
144 command[maxlen - 1] =
'\0';
156 char *commandline = NULL;
163 while (!feof (f.get ()))
166 size_t read_bytes = fread (buf, 1,
sizeof (buf), f.get ());
170 commandline = (
char *)
xrealloc (commandline, len + read_bytes + 1);
171 memcpy (commandline + len, buf, read_bytes);
181 for (i = 0; i < len; ++i)
182 if (commandline[i] ==
'\0')
183 commandline[i] =
' ';
185 commandline[len] =
'\0';
191 commandline = (
char *)
xmalloc (32);
192 commandline[0] =
'[';
195 len = strlen (commandline);
197 strcat (commandline,
"]");
212 struct passwd *pwentry = getpwuid (uid);
216 strncpy (user, pwentry->pw_name, maxlen);
218 user[maxlen - 1] =
'\0';
233 sprintf (procentry,
"/proc/%lld",
pid);
235 if (stat (procentry, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
237 *owner = statbuf.st_uid;
255 sprintf (taskdir,
"/proc/%lld/task",
pid);
256 dir = opendir (taskdir);
259 while ((dp = readdir (dir)) != NULL)
264 if (!isdigit (dp->d_name[0])
268 sscanf (dp->d_name,
"%lld", &
tid);
272 if (core >= 0 && core < num_cores)
290 static const char *buf;
298 if (len_avail != -1 && len_avail != 0)
305 dirp = opendir (
"/proc");
308 const int num_cores = sysconf (_SC_NPROCESSORS_ONLN);
311 while ((dp = readdir (dirp)) != NULL)
315 char user[UT_NAMESIZE];
322 if (!isdigit (dp->d_name[0])
326 sscanf (dp->d_name,
"%lld", &
pid);
337 cores_str = (
char *)
xcalloc (task_count,
sizeof (
"4294967295") + 1);
339 for (i = 0; i < num_cores && task_count > 0; ++i)
342 char core_str[
sizeof (
"4294967295")];
344 sprintf (core_str,
"%d", i);
345 strcat (cores_str, core_str);
347 task_count -= cores[i];
349 strcat (cores_str,
",");
357 "<column name=\"pid\">%lld</column>" 358 "<column name=\"user\">%s</column>" 359 "<column name=\"command\">%s</column>" 360 "<column name=\"cores\">%s</column>" 376 len_avail = strlen (buf);
388 if (len > len_avail -
offset)
390 memcpy (readbuf, buf +
offset, len);
424 return this->
pid < other.
pid;
437 static const char *buf;
445 if (len_avail != -1 && len_avail != 0)
452 dirp = opendir (
"/proc");
455 std::vector<pid_pgid_entry> process_list;
458 process_list.reserve (512);
462 while ((dp = readdir (dirp)) != NULL)
466 if (!isdigit (dp->d_name[0])
470 sscanf (dp->d_name,
"%lld", &
pid);
471 pgid = getpgid (
pid);
474 process_list.emplace_back (
pid, pgid);
480 std::sort (process_list.begin (), process_list.end ());
485 PID_T pgid = entry.pgid;
486 char leader_command[32];
495 "<column name=\"pgid\">%lld</column>" 496 "<column name=\"leader command\">%s</column>" 497 "<column name=\"pid\">%lld</column>" 498 "<column name=\"command line\">%s</column>" 511 len_avail = strlen (buf);
523 if (len > len_avail -
offset)
525 memcpy (readbuf, buf +
offset, len);
538 static const char *buf;
546 if (len_avail != -1 && len_avail != 0)
553 dirp = opendir (
"/proc");
558 while ((dp = readdir (dirp)) != NULL)
561 char procentry[
sizeof (
"/proc/4294967295")];
563 if (!isdigit (dp->d_name[0])
564 ||
NAMELEN (dp) >
sizeof (
"4294967295") - 1)
567 xsnprintf (procentry,
sizeof (procentry),
"/proc/%s",
569 if (stat (procentry, &statbuf) == 0
570 && S_ISDIR (statbuf.st_mode))
577 pathname =
xstrprintf (
"/proc/%s/task", dp->d_name);
579 pid = atoi (dp->d_name);
582 dirp2 = opendir (pathname);
588 while ((dp2 = readdir (dirp2)) != NULL)
593 if (!isdigit (dp2->d_name[0])
594 ||
NAMELEN (dp2) >
sizeof (
"4294967295") - 1)
597 tid = atoi (dp2->d_name);
603 "<column name=\"pid\">%lld</column>" 604 "<column name=\"command\">%s</column>" 605 "<column name=\"tid\">%lld</column>" 606 "<column name=\"core\">%d</column>" 626 len_avail = strlen (buf);
638 if (len > len_avail -
offset)
640 memcpy (readbuf, buf +
offset, len);
651 static const char *buf;
659 if (len_avail != -1 && len_avail != 0)
673 if (fgets (buf,
sizeof (buf), fp.get ()))
678 key = strtok (buf,
":");
682 value = strtok (NULL,
":");
686 while (key[i] !=
'\t' && key[i] !=
'\0')
697 if (strcmp (key,
"processor") == 0)
708 "<column name=\"%s\">%s</column>",
713 while (!feof (fp.get ()));
721 len_avail = strlen (buf);
733 if (len > len_avail -
offset)
735 memcpy (readbuf, buf +
offset, len);
748 static const char *buf;
756 if (len_avail != -1 && len_avail != 0)
763 dirp = opendir (
"/proc");
768 while ((dp = readdir (dirp)) != NULL)
771 char procentry[
sizeof (
"/proc/4294967295")];
773 if (!isdigit (dp->d_name[0])
774 ||
NAMELEN (dp) >
sizeof (
"4294967295") - 1)
777 xsnprintf (procentry,
sizeof (procentry),
"/proc/%s",
779 if (stat (procentry, &statbuf) == 0
780 && S_ISDIR (statbuf.st_mode))
787 pid = atoi (dp->d_name);
790 pathname =
xstrprintf (
"/proc/%s/fd", dp->d_name);
791 dirp2 = opendir (pathname);
797 while ((dp2 = readdir (dirp2)) != NULL)
803 if (!isdigit (dp2->d_name[0]))
806 fdname =
xstrprintf (
"%s/%s", pathname, dp2->d_name);
807 rslt = readlink (fdname, buf,
sizeof (buf) - 1);
814 "<column name=\"pid\">%s</column>" 815 "<column name=\"command\">%s</column>" 816 "<column name=\"file descriptor\">%s</column>" 817 "<column name=\"name\">%s</column>" 822 (rslt >= 0 ? buf : dp2->d_name));
837 len_avail = strlen (buf);
849 if (len > len_avail -
offset)
851 memcpy (readbuf, buf +
offset, len);
878 case TCP_ESTABLISHED:
879 return "ESTABLISHED";
920 const char *proc_file;
922 if (family == AF_INET)
923 proc_file = tcp ?
"/proc/net/tcp" :
"/proc/net/udp";
924 else if (family == AF_INET6)
925 proc_file = tcp ?
"/proc/net/tcp6" :
"/proc/net/udp6";
936 if (fgets (buf,
sizeof (buf), fp.get ()))
939 unsigned int local_port, remote_port, state;
940 char local_address[NI_MAXHOST], remote_address[NI_MAXHOST];
944 #error "local_address and remote_address buffers too small" 947 result = sscanf (buf,
948 "%*d: %32[0-9A-F]:%X %32[0-9A-F]:%X %X %*X:%*X %*X:%*X %*X %d %*d %*u %*s\n",
949 local_address, &local_port,
950 remote_address, &remote_port,
958 char user[UT_NAMESIZE];
959 char local_service[NI_MAXSERV], remote_service[NI_MAXSERV];
961 if (family == AF_INET)
963 sscanf (local_address,
"%X",
964 &locaddr.
sin.sin_addr.s_addr);
965 sscanf (remote_address,
"%X",
966 &remaddr.
sin.sin_addr.s_addr);
968 locaddr.
sin.sin_port = htons (local_port);
969 remaddr.
sin.sin_port = htons (remote_port);
971 addr_size =
sizeof (
struct sockaddr_in);
975 sscanf (local_address,
"%8X%8X%8X%8X",
976 locaddr.
sin6.sin6_addr.s6_addr32,
977 locaddr.
sin6.sin6_addr.s6_addr32 + 1,
978 locaddr.
sin6.sin6_addr.s6_addr32 + 2,
979 locaddr.
sin6.sin6_addr.s6_addr32 + 3);
980 sscanf (remote_address,
"%8X%8X%8X%8X",
981 remaddr.
sin6.sin6_addr.s6_addr32,
982 remaddr.
sin6.sin6_addr.s6_addr32 + 1,
983 remaddr.
sin6.sin6_addr.s6_addr32 + 2,
984 remaddr.
sin6.sin6_addr.s6_addr32 + 3);
986 locaddr.
sin6.sin6_port = htons (local_port);
987 remaddr.
sin6.sin6_port = htons (remote_port);
989 locaddr.
sin6.sin6_flowinfo = 0;
990 remaddr.
sin6.sin6_flowinfo = 0;
991 locaddr.
sin6.sin6_scope_id = 0;
992 remaddr.
sin6.sin6_scope_id = 0;
994 addr_size =
sizeof (
struct sockaddr_in6);
997 locaddr.
sa.sa_family = remaddr.
sa.sa_family = family;
999 result = getnameinfo (&locaddr.
sa, addr_size,
1000 local_address, sizeof (local_address),
1001 local_service,
sizeof (local_service),
1002 NI_NUMERICHOST | NI_NUMERICSERV
1003 | (tcp ? 0 : NI_DGRAM));
1007 result = getnameinfo (&remaddr.
sa, addr_size,
1009 sizeof (remote_address),
1011 sizeof (remote_service),
1012 NI_NUMERICHOST | NI_NUMERICSERV
1013 | (tcp ? 0 : NI_DGRAM));
1022 "<column name=\"local address\">%s</column>" 1023 "<column name=\"local port\">%s</column>" 1024 "<column name=\"remote address\">%s</column>" 1025 "<column name=\"remote port\">%s</column>" 1026 "<column name=\"state\">%s</column>" 1027 "<column name=\"user\">%s</column>" 1028 "<column name=\"family\">%s</column>" 1029 "<column name=\"protocol\">%s</column>" 1037 (family == AF_INET) ?
"INET" :
"INET6",
1038 tcp ?
"STREAM" :
"DGRAM");
1042 while (!feof (fp.get ()));
1052 static const char *buf;
1053 static LONGEST len_avail = -1;
1058 if (len_avail != -1 && len_avail != 0)
1072 len_avail = strlen (buf);
1084 if (len > len_avail -
offset)
1085 len = len_avail -
offset;
1086 memcpy (readbuf, buf +
offset, len);
1101 time_t t = (time_t) seconds;
1103 strncpy (time, ctime (&t), maxlen);
1104 time[maxlen - 1] =
'\0';
1114 struct group *grentry = getgrgid (gid);
1118 strncpy (group, grentry->gr_name, maxlen);
1120 group[maxlen - 1] =
'\0';
1133 static const char *buf;
1134 static LONGEST len_avail = -1;
1139 if (len_avail != -1 && len_avail != 0)
1153 if (fgets (buf,
sizeof (buf), fp.get ()))
1159 int shmid,
size, nattch;
1160 TIME_T atime, dtime, ctime;
1164 items_read = sscanf (buf,
1165 "%d %d %o %d %lld %lld %d %u %u %u %u %lld %lld %lld",
1166 &key, &shmid, &perms, &
size,
1169 &uid, &gid, &cuid, &cgid,
1170 &atime, &dtime, &ctime);
1172 if (items_read == 14)
1174 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1175 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1176 char ccmd[32], lcmd[32];
1177 char atime_str[32], dtime_str[32], ctime_str[32];
1194 "<column name=\"key\">%d</column>" 1195 "<column name=\"shmid\">%d</column>" 1196 "<column name=\"permissions\">%o</column>" 1197 "<column name=\"size\">%d</column>" 1198 "<column name=\"creator command\">%s</column>" 1199 "<column name=\"last op. command\">%s</column>" 1200 "<column name=\"num attached\">%d</column>" 1201 "<column name=\"user\">%s</column>" 1202 "<column name=\"group\">%s</column>" 1203 "<column name=\"creator user\">%s</column>" 1204 "<column name=\"creator group\">%s</column>" 1205 "<column name=\"last shmat() time\">%s</column>" 1206 "<column name=\"last shmdt() time\">%s</column>" 1207 "<column name=\"last shmctl() time\">%s</column>" 1226 while (!feof (fp.get ()));
1231 len_avail = strlen (buf);
1243 if (len > len_avail -
offset)
1244 len = len_avail -
offset;
1245 memcpy (readbuf, buf +
offset, len);
1257 static const char *buf;
1258 static LONGEST len_avail = -1;
1263 if (len_avail != -1 && len_avail != 0)
1277 if (fgets (buf,
sizeof (buf), fp.get ()))
1282 unsigned int perms, nsems;
1287 items_read = sscanf (buf,
1288 "%d %d %o %u %d %d %d %d %lld %lld",
1289 &key, &semid, &perms, &nsems,
1290 &uid, &gid, &cuid, &cgid,
1293 if (items_read == 10)
1295 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1296 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1297 char otime_str[32], ctime_str[32];
1310 "<column name=\"key\">%d</column>" 1311 "<column name=\"semid\">%d</column>" 1312 "<column name=\"permissions\">%o</column>" 1313 "<column name=\"num semaphores\">%u</column>" 1314 "<column name=\"user\">%s</column>" 1315 "<column name=\"group\">%s</column>" 1316 "<column name=\"creator user\">%s</column>" 1317 "<column name=\"creator group\">%s</column>" 1318 "<column name=\"last semop() time\">%s</column>" 1319 "<column name=\"last semctl() time\">%s</column>" 1334 while (!feof (fp.get ()));
1339 len_avail = strlen (buf);
1351 if (len > len_avail -
offset)
1352 len = len_avail -
offset;
1353 memcpy (readbuf, buf +
offset, len);
1365 static const char *buf;
1366 static LONGEST len_avail = -1;
1371 if (len_avail != -1 && len_avail != 0)
1385 if (fgets (buf,
sizeof (buf), fp.get ()))
1391 unsigned int perms, cbytes, qnum;
1393 TIME_T stime, rtime, ctime;
1396 items_read = sscanf (buf,
1397 "%d %d %o %u %u %lld %lld %d %d %d %d %lld %lld %lld",
1398 &key, &msqid, &perms, &cbytes, &qnum,
1399 &lspid, &lrpid, &uid, &gid, &cuid, &cgid,
1400 &stime, &rtime, &ctime);
1402 if (items_read == 14)
1404 char user[UT_NAMESIZE], group[UT_NAMESIZE];
1405 char cuser[UT_NAMESIZE], cgroup[UT_NAMESIZE];
1406 char lscmd[32], lrcmd[32];
1407 char stime_str[32], rtime_str[32], ctime_str[32];
1424 "<column name=\"key\">%d</column>" 1425 "<column name=\"msqid\">%d</column>" 1426 "<column name=\"permissions\">%o</column>" 1427 "<column name=\"num used bytes\">%u</column>" 1428 "<column name=\"num messages\">%u</column>" 1429 "<column name=\"last msgsnd() command\">%s</column>" 1430 "<column name=\"last msgrcv() command\">%s</column>" 1431 "<column name=\"user\">%s</column>" 1432 "<column name=\"group\">%s</column>" 1433 "<column name=\"creator user\">%s</column>" 1434 "<column name=\"creator group\">%s</column>" 1435 "<column name=\"last msgsnd() time\">%s</column>" 1436 "<column name=\"last msgrcv() time\">%s</column>" 1437 "<column name=\"last msgctl() time\">%s</column>" 1456 while (!feof (fp.get ()));
1461 len_avail = strlen (buf);
1473 if (len > len_avail -
offset)
1474 len = len_avail -
offset;
1475 memcpy (readbuf, buf +
offset, len);
1487 static const char *buf;
1488 static LONGEST len_avail = -1;
1493 if (len_avail != -1 && len_avail != 0)
1507 if (fgets (buf,
sizeof (buf), fp.get ()))
1511 unsigned long long address;
1514 name = strtok (buf,
" ");
1518 tmp = strtok (NULL,
" ");
1521 if (sscanf (tmp,
"%u", &
size) != 1)
1524 tmp = strtok (NULL,
" ");
1527 if (sscanf (tmp,
"%d", &uses) != 1)
1530 dependencies = strtok (NULL,
" ");
1531 if (dependencies == NULL)
1534 status = strtok (NULL,
" ");
1538 tmp = strtok (NULL,
"\n");
1541 if (sscanf (tmp,
"%llx", &address) != 1)
1547 "<column name=\"name\">%s</column>" 1548 "<column name=\"size\">%u</column>" 1549 "<column name=\"num uses\">%d</column>" 1550 "<column name=\"dependencies\">%s</column>" 1551 "<column name=\"status\">%s</column>" 1552 "<column name=\"address\">%llx</column>" 1562 while (!feof (fp.get ()));
1567 len_avail = strlen (buf);
1579 if (len > len_avail -
offset)
1580 len = len_avail -
offset;
1581 memcpy (readbuf, buf +
offset, len);
1592 {
"cpus",
"CPUs",
"Listing of all cpus/cores on the system",
1594 {
"files",
"File descriptors",
"Listing of all file descriptors",
1596 {
"modules",
"Kernel modules",
"Listing of all loaded kernel modules",
1598 {
"msg",
"Message queues",
"Listing of all message queues",
1600 {
"processes",
"Processes",
"Listing of all processes",
1602 {
"procgroups",
"Process groups",
"Listing of all process groups",
1604 {
"semaphores",
"Semaphores",
"Listing of all semaphores",
1606 {
"shm",
"Shared-memory regions",
"Listing of all shared-memory regions",
1608 {
"sockets",
"Sockets",
"Listing of all internet-domain sockets",
1610 {
"threads",
"Threads",
"Listing of all threads",
1612 { NULL, NULL, NULL }
1619 if (!annex || *annex ==
'\0')
1621 static const char *buf;
1622 static LONGEST len_avail = -1;
1629 if (len_avail != -1 && len_avail != 0)
1640 "<column name=\"Type\">%s</column>" 1641 "<column name=\"Description\">%s</column>" 1642 "<column name=\"Title\">%s</column>" 1650 len_avail = strlen (buf);
1662 if (len > len_avail -
offset)
1663 len = len_avail -
offset;
1664 memcpy (readbuf, buf +
offset, len);
static constexpr ptid_t tid
static void group_from_gid(char *group, int maxlen, gid_t gid)
int ptid_get_pid(const ptid_t &ptid)
static LONGEST linux_xfer_osdata_modules(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_isockets(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
static LONGEST linux_xfer_osdata_shm(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_fds(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
void buffer_free(struct buffer *buffer)
static LONGEST linux_xfer_osdata_cpus(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static void user_from_uid(char *user, int maxlen, uid_t uid)
static LONGEST linux_xfer_osdata_msg(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
ptid_t ptid_build(int pid, long lwp, long tid)
static LONGEST linux_xfer_osdata_processgroups(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
std::unique_ptr< FILE, gdb_file_deleter > gdb_file_up
struct osdata_type osdata_table[]
static void print_sockets(unsigned short family, int tcp, struct buffer *buffer)
void buffer_xml_printf(struct buffer *buffer, const char *format,...)
static int get_process_owner(uid_t *owner, PID_T pid)
static void command_from_pid(char *command, int maxlen, PID_T pid)
pid_pgid_entry(PID_T pid_, PID_T pgid_)
LONGEST(* getter)(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
int linux_common_core_of_thread(ptid_t ptid)
gdb_file_up gdb_fopen_cloexec(const char *filename, const char *opentype)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
char * xstrprintf(const char *format,...)
#define buffer_grow_str(BUFFER, STRING)
long ptid_get_lwp(const ptid_t &ptid)
static char * commandline_from_pid(PID_T pid)
static LONGEST linux_xfer_osdata_processes(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
LONGEST linux_common_xfer_osdata(const char *annex, gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
PTR xrealloc(PTR ptr, size_t size)
#define buffer_grow_str0(BUFFER, STRING)
void buffer_init(struct buffer *buffer)
int xsnprintf(char *str, size_t size, const char *format,...)
static void time_from_time_t(char *time, int maxlen, TIME_T seconds)
unsigned long long ULONGEST
static LONGEST linux_xfer_osdata_threads(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static LONGEST linux_xfer_osdata_sem(gdb_byte *readbuf, ULONGEST offset, ULONGEST len)
static int get_cores_used_by_process(PID_T pid, int *cores, const int num_cores)
PTR xcalloc(size_t number, size_t size)
char * buffer_finish(struct buffer *buffer)
bool operator<(const pid_pgid_entry &other) const
static const char * format_socket_state(unsigned char state)