/* Linux-specific functions to retrieve OS data.
- Copyright (C) 2009-2014 Free Software Foundation, Inc.
+ Copyright (C) 2009-2017 Free Software Foundation, Inc.
This file is part of GDB.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#ifdef GDBSERVER
-#include "server.h"
-#else
-#include "defs.h"
-#endif
-
+#include "common-defs.h"
#include "linux-osdata.h"
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <ctype.h>
-#include <string.h>
#include <utmp.h>
#include <time.h>
#include <unistd.h>
linux_common_core_of_thread (ptid_t ptid)
{
char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
- FILE *f;
char *content = NULL;
char *p;
char *ts = 0;
sprintf (filename, "/proc/%lld/task/%lld/stat",
(PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid));
- f = gdb_fopen_cloexec (filename, "r");
+ gdb_file_up f = gdb_fopen_cloexec (filename, "r");
if (!f)
return -1;
for (;;)
{
int n;
- content = xrealloc (content, content_read + 1024);
- n = fread (content + content_read, 1, 1024, f);
+ content = (char *) xrealloc (content, content_read + 1024);
+ n = fread (content + content_read, 1, 1024, f.get ());
content_read += n;
if (n < 1024)
{
core = -1;
xfree (content);
- fclose (f);
return core;
}
command_from_pid (char *command, int maxlen, PID_T pid)
{
char *stat_path = xstrprintf ("/proc/%lld/stat", pid);
- FILE *fp = gdb_fopen_cloexec (stat_path, "r");
+ gdb_file_up fp = gdb_fopen_cloexec (stat_path, "r");
command[0] = '\0';
(for the brackets). */
char cmd[18];
PID_T stat_pid;
- int items_read = fscanf (fp, "%lld %17s", &stat_pid, cmd);
+ int items_read = fscanf (fp.get (), "%lld %17s", &stat_pid, cmd);
if (items_read == 2 && pid == stat_pid)
{
cmd[strlen (cmd) - 1] = '\0'; /* Remove trailing parenthesis. */
strncpy (command, cmd + 1, maxlen); /* Ignore leading parenthesis. */
}
-
- fclose (fp);
}
else
{
{
char *pathname = xstrprintf ("/proc/%lld/cmdline", pid);
char *commandline = NULL;
- FILE *f = gdb_fopen_cloexec (pathname, "r");
+ gdb_file_up f = gdb_fopen_cloexec (pathname, "r");
if (f)
{
size_t len = 0;
- while (!feof (f))
+ while (!feof (f.get ()))
{
char buf[1024];
- size_t read_bytes = fread (buf, 1, sizeof (buf), f);
+ size_t read_bytes = fread (buf, 1, sizeof (buf), f.get ());
if (read_bytes)
{
}
}
- fclose (f);
-
if (commandline)
{
size_t i;
strcpy (user, "?");
/* Find CPU cores used by the process. */
- cores = (int *) xcalloc (num_cores, sizeof (int));
+ cores = XCNEWVEC (int, num_cores);
task_count = get_cores_used_by_process (pid, cores, num_cores);
cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1);
{
struct dirent *dp;
const size_t list_block_size = 512;
- PID_T *process_list = (PID_T *) xmalloc (list_block_size * 2 * sizeof (PID_T));
+ PID_T *process_list = XNEWVEC (PID_T, list_block_size * 2);
size_t process_count = 0;
size_t i;
|| NAMELEN (dp) > sizeof ("4294967295") - 1)
continue;
- sprintf (procentry, "/proc/%s", dp->d_name);
+ xsnprintf (procentry, sizeof (procentry), "/proc/%s",
+ dp->d_name);
if (stat (procentry, &statbuf) == 0
&& S_ISDIR (statbuf.st_mode))
{
return len;
}
+/* Collect data about the cpus/cores on the system */
+
+static LONGEST
+linux_xfer_osdata_cpus (gdb_byte *readbuf,
+ ULONGEST offset, ULONGEST len)
+{
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct buffer buffer;
+
+ if (offset == 0)
+ {
+ int first_item = 1;
+
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"cpus\">\n");
+
+ gdb_file_up fp = gdb_fopen_cloexec ("/proc/cpuinfo", "r");
+ if (fp != NULL)
+ {
+ char buf[8192];
+
+ do
+ {
+ if (fgets (buf, sizeof (buf), fp.get ()))
+ {
+ char *key, *value;
+ int i = 0;
+
+ key = strtok (buf, ":");
+ if (key == NULL)
+ continue;
+
+ value = strtok (NULL, ":");
+ if (value == NULL)
+ continue;
+
+ while (key[i] != '\t' && key[i] != '\0')
+ i++;
+
+ key[i] = '\0';
+
+ i = 0;
+ while (value[i] != '\t' && value[i] != '\0')
+ i++;
+
+ value[i] = '\0';
+
+ if (strcmp (key, "processor") == 0)
+ {
+ if (first_item)
+ buffer_grow_str (&buffer, "<item>");
+ else
+ buffer_grow_str (&buffer, "</item><item>");
+
+ first_item = 0;
+ }
+
+ buffer_xml_printf (&buffer,
+ "<column name=\"%s\">%s</column>",
+ key,
+ value);
+ }
+ }
+ while (!feof (fp.get ()));
+
+ if (first_item == 0)
+ buffer_grow_str (&buffer, "</item>");
+ }
+
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the buffer. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
/* Collect all the open file descriptors found in /proc and put the details
found about them into READBUF. */
|| NAMELEN (dp) > sizeof ("4294967295") - 1)
continue;
- sprintf (procentry, "/proc/%s", dp->d_name);
+ xsnprintf (procentry, sizeof (procentry), "/proc/%s",
+ dp->d_name);
if (stat (procentry, &statbuf) == 0
&& S_ISDIR (statbuf.st_mode))
{
print_sockets (unsigned short family, int tcp, struct buffer *buffer)
{
const char *proc_file;
- FILE *fp;
if (family == AF_INET)
proc_file = tcp ? "/proc/net/tcp" : "/proc/net/udp";
else
return;
- fp = gdb_fopen_cloexec (proc_file, "r");
+ gdb_file_up fp = gdb_fopen_cloexec (proc_file, "r");
if (fp)
{
char buf[8192];
do
{
- if (fgets (buf, sizeof (buf), fp))
+ if (fgets (buf, sizeof (buf), fp.get ()))
{
uid_t uid;
unsigned int local_port, remote_port, state;
}
}
}
- while (!feof (fp));
-
- fclose (fp);
+ while (!feof (fp.get ()));
}
}
if (offset == 0)
{
- FILE *fp;
-
if (len_avail != -1 && len_avail != 0)
buffer_free (&buffer);
len_avail = 0;
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n");
- fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
+ gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
if (fp)
{
char buf[8192];
do
{
- if (fgets (buf, sizeof (buf), fp))
+ if (fgets (buf, sizeof (buf), fp.get ()))
{
key_t key;
uid_t uid, cuid;
}
}
}
- while (!feof (fp));
-
- fclose (fp);
+ while (!feof (fp.get ()));
}
buffer_grow_str0 (&buffer, "</osdata>\n");
if (offset == 0)
{
- FILE *fp;
-
if (len_avail != -1 && len_avail != 0)
buffer_free (&buffer);
len_avail = 0;
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n");
- fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
+ gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
if (fp)
{
char buf[8192];
do
{
- if (fgets (buf, sizeof (buf), fp))
+ if (fgets (buf, sizeof (buf), fp.get ()))
{
key_t key;
uid_t uid, cuid;
}
}
}
- while (!feof (fp));
-
- fclose (fp);
+ while (!feof (fp.get ()));
}
buffer_grow_str0 (&buffer, "</osdata>\n");
if (offset == 0)
{
- FILE *fp;
-
if (len_avail != -1 && len_avail != 0)
buffer_free (&buffer);
len_avail = 0;
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n");
- fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
+ gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
if (fp)
{
char buf[8192];
do
{
- if (fgets (buf, sizeof (buf), fp))
+ if (fgets (buf, sizeof (buf), fp.get ()))
{
key_t key;
PID_T lspid, lrpid;
}
}
}
- while (!feof (fp));
-
- fclose (fp);
+ while (!feof (fp.get ()));
}
buffer_grow_str0 (&buffer, "</osdata>\n");
if (offset == 0)
{
- FILE *fp;
-
if (len_avail != -1 && len_avail != 0)
buffer_free (&buffer);
len_avail = 0;
buffer_init (&buffer);
buffer_grow_str (&buffer, "<osdata type=\"modules\">\n");
- fp = gdb_fopen_cloexec ("/proc/modules", "r");
+ gdb_file_up fp = gdb_fopen_cloexec ("/proc/modules", "r");
if (fp)
{
char buf[8192];
do
{
- if (fgets (buf, sizeof (buf), fp))
+ if (fgets (buf, sizeof (buf), fp.get ()))
{
char *name, *dependencies, *status, *tmp;
unsigned int size;
address);
}
}
- while (!feof (fp));
-
- fclose (fp);
+ while (!feof (fp.get ()));
}
buffer_grow_str0 (&buffer, "</osdata>\n");
}
struct osdata_type {
- char *type;
- char *title;
- char *description;
+ const char *type;
+ const char *title;
+ const char *description;
LONGEST (*getter) (gdb_byte *readbuf, ULONGEST offset, ULONGEST len);
} osdata_table[] = {
+ { "cpus", "CPUs", "Listing of all cpus/cores on the system",
+ linux_xfer_osdata_cpus },
+ { "files", "File descriptors", "Listing of all file descriptors",
+ linux_xfer_osdata_fds },
+ { "modules", "Kernel modules", "Listing of all loaded kernel modules",
+ linux_xfer_osdata_modules },
+ { "msg", "Message queues", "Listing of all message queues",
+ linux_xfer_osdata_msg },
{ "processes", "Processes", "Listing of all processes",
linux_xfer_osdata_processes },
{ "procgroups", "Process groups", "Listing of all process groups",
linux_xfer_osdata_processgroups },
- { "threads", "Threads", "Listing of all threads",
- linux_xfer_osdata_threads },
- { "files", "File descriptors", "Listing of all file descriptors",
- linux_xfer_osdata_fds },
- { "sockets", "Sockets", "Listing of all internet-domain sockets",
- linux_xfer_osdata_isockets },
- { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
- linux_xfer_osdata_shm },
{ "semaphores", "Semaphores", "Listing of all semaphores",
linux_xfer_osdata_sem },
- { "msg", "Message queues", "Listing of all message queues",
- linux_xfer_osdata_msg },
- { "modules", "Kernel modules", "Listing of all loaded kernel modules",
- linux_xfer_osdata_modules },
+ { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
+ linux_xfer_osdata_shm },
+ { "sockets", "Sockets", "Listing of all internet-domain sockets",
+ linux_xfer_osdata_isockets },
+ { "threads", "Threads", "Listing of all threads",
+ linux_xfer_osdata_threads },
{ NULL, NULL, NULL }
};