mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-02-10 23:20:54 +00:00
Don't compile process.[ch] anymore
This commit is contained in:
@ -17,8 +17,6 @@ SYSPROF_CORE = \
|
|||||||
demangle.c \
|
demangle.c \
|
||||||
elfparser.c \
|
elfparser.c \
|
||||||
elfparser.h \
|
elfparser.h \
|
||||||
process.h \
|
|
||||||
process.c \
|
|
||||||
profile.h \
|
profile.h \
|
||||||
profile.c \
|
profile.c \
|
||||||
sfile.h \
|
sfile.h \
|
||||||
|
|||||||
6
TODO
6
TODO
@ -23,6 +23,12 @@ Before 1.0.4:
|
|||||||
|
|
||||||
Before 1.2:
|
Before 1.2:
|
||||||
|
|
||||||
|
* Get rid of remaining gulongs (use uint64_t instead)
|
||||||
|
|
||||||
|
* Move binfile hash table to state_t.
|
||||||
|
|
||||||
|
* Get rid of process.c
|
||||||
|
|
||||||
* On 32 bit, NMI stackframes are not filtered out which leads to
|
* On 32 bit, NMI stackframes are not filtered out which leads to
|
||||||
wrong kernel traces
|
wrong kernel traces
|
||||||
|
|
||||||
|
|||||||
153
binfile.c
153
binfile.c
@ -36,7 +36,6 @@
|
|||||||
|
|
||||||
#include "binfile.h"
|
#include "binfile.h"
|
||||||
#include "elfparser.h"
|
#include "elfparser.h"
|
||||||
#include "process.h"
|
|
||||||
|
|
||||||
struct BinFile
|
struct BinFile
|
||||||
{
|
{
|
||||||
@ -258,8 +257,158 @@ get_debug_binaries (GList *files,
|
|||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static GHashTable *bin_files;
|
static GHashTable *bin_files;
|
||||||
|
|
||||||
|
typedef struct Map Map;
|
||||||
|
struct Map
|
||||||
|
{
|
||||||
|
char * filename;
|
||||||
|
gulong start;
|
||||||
|
gulong end;
|
||||||
|
gulong offset;
|
||||||
|
gulong inode;
|
||||||
|
|
||||||
|
BinFile * bin_file;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Map *
|
||||||
|
read_maps (int pid, int *n_maps)
|
||||||
|
{
|
||||||
|
char *name = g_strdup_printf ("/proc/%d/maps", pid);
|
||||||
|
char buffer[1024];
|
||||||
|
FILE *in;
|
||||||
|
GArray *result;
|
||||||
|
|
||||||
|
in = fopen (name, "r");
|
||||||
|
if (!in)
|
||||||
|
{
|
||||||
|
g_free (name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = g_array_new (FALSE, FALSE, sizeof (Map));
|
||||||
|
|
||||||
|
while (fgets (buffer, sizeof (buffer) - 1, in))
|
||||||
|
{
|
||||||
|
char file[256];
|
||||||
|
int count;
|
||||||
|
gulong start;
|
||||||
|
gulong end;
|
||||||
|
gulong offset;
|
||||||
|
gulong inode;
|
||||||
|
|
||||||
|
count = sscanf (
|
||||||
|
buffer, "%lx-%lx %*15s %lx %*x:%*x %lu %255s",
|
||||||
|
&start, &end, &offset, &inode, file);
|
||||||
|
|
||||||
|
if (count == 5)
|
||||||
|
{
|
||||||
|
Map map;
|
||||||
|
|
||||||
|
map.filename = g_strdup (file);
|
||||||
|
map.start = start;
|
||||||
|
map.end = end;
|
||||||
|
|
||||||
|
if (strcmp (map.filename, "[vdso]") == 0)
|
||||||
|
{
|
||||||
|
/* For the vdso, the kernel reports 'offset' as the
|
||||||
|
* the same as the mapping addres. This doesn't make
|
||||||
|
* any sense to me, so we just zero it here. There
|
||||||
|
* is code in binfile.c (read_inode) that returns 0
|
||||||
|
* for [vdso].
|
||||||
|
*/
|
||||||
|
map.offset = 0;
|
||||||
|
map.inode = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
map.offset = offset;
|
||||||
|
map.inode = inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
map.bin_file = NULL;
|
||||||
|
|
||||||
|
g_array_append_val (result, map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free (name);
|
||||||
|
fclose (in);
|
||||||
|
|
||||||
|
if (n_maps)
|
||||||
|
*n_maps = result->len;
|
||||||
|
|
||||||
|
return (Map *)g_array_free (result, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
free_maps (int *n_maps,
|
||||||
|
Map *maps)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < *n_maps; ++i)
|
||||||
|
{
|
||||||
|
Map *map = &(maps[i]);
|
||||||
|
|
||||||
|
if (map->filename)
|
||||||
|
g_free (map->filename);
|
||||||
|
|
||||||
|
if (map->bin_file)
|
||||||
|
bin_file_free (map->bin_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free (maps);
|
||||||
|
*n_maps = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const guint8 *
|
||||||
|
get_vdso_bytes (gsize *length)
|
||||||
|
{
|
||||||
|
static gboolean has_data;
|
||||||
|
static const guint8 *bytes = NULL;
|
||||||
|
static gsize n_bytes = 0;
|
||||||
|
|
||||||
|
if (!has_data)
|
||||||
|
{
|
||||||
|
Map *maps;
|
||||||
|
int n_maps, i;
|
||||||
|
|
||||||
|
maps = read_maps (getpid(), &n_maps);
|
||||||
|
|
||||||
|
for (i = 0; i < n_maps; ++i)
|
||||||
|
{
|
||||||
|
Map *map = &(maps[i]);
|
||||||
|
|
||||||
|
if (strcmp (map->filename, "[vdso]") == 0)
|
||||||
|
{
|
||||||
|
n_bytes = map->end - map->start;
|
||||||
|
|
||||||
|
/* Dup the memory here so that valgrind will only
|
||||||
|
* report one 1 byte invalid read instead of
|
||||||
|
* a ton when the elf parser scans the vdso
|
||||||
|
*
|
||||||
|
* The reason we get a spurious invalid read from
|
||||||
|
* valgrind is that we are getting the address directly
|
||||||
|
* from /proc/maps, and valgrind knows that its mmap()
|
||||||
|
* wrapper never returned that address. But since it
|
||||||
|
* is a legal mapping, it is legal to read it.
|
||||||
|
*/
|
||||||
|
bytes = g_memdup ((guint8 *)map->start, n_bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
has_data = TRUE;
|
||||||
|
free_maps (&n_maps, maps);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length)
|
||||||
|
*length = n_bytes;
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
BinFile *
|
BinFile *
|
||||||
bin_file_new (const char *filename)
|
bin_file_new (const char *filename)
|
||||||
{
|
{
|
||||||
@ -294,7 +443,7 @@ bin_file_new (const char *filename)
|
|||||||
const guint8 *vdso_bytes;
|
const guint8 *vdso_bytes;
|
||||||
gsize length;
|
gsize length;
|
||||||
|
|
||||||
vdso_bytes = process_get_vdso_bytes (&length);
|
vdso_bytes = get_vdso_bytes (&length);
|
||||||
|
|
||||||
if (vdso_bytes)
|
if (vdso_bytes)
|
||||||
elf = elf_parser_new_from_data (vdso_bytes, length);
|
elf = elf_parser_new_from_data (vdso_bytes, length);
|
||||||
|
|||||||
31
collector.c
31
collector.c
@ -165,11 +165,26 @@ in_dead_period (Collector *collector)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_page_size (void)
|
||||||
|
{
|
||||||
|
static int page_size;
|
||||||
|
static gboolean has_page_size = FALSE;
|
||||||
|
|
||||||
|
if (!has_page_size)
|
||||||
|
{
|
||||||
|
page_size = getpagesize();
|
||||||
|
has_page_size = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return page_size;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_read (gpointer data)
|
on_read (gpointer data)
|
||||||
{
|
{
|
||||||
counter_t *counter = data;
|
counter_t *counter = data;
|
||||||
int mask = (N_PAGES * process_get_page_size() - 1);
|
int mask = (N_PAGES * get_page_size() - 1);
|
||||||
gboolean skip_samples;
|
gboolean skip_samples;
|
||||||
Collector *collector;
|
Collector *collector;
|
||||||
uint64_t head, tail;
|
uint64_t head, tail;
|
||||||
@ -245,27 +260,27 @@ on_read (gpointer data)
|
|||||||
static void *
|
static void *
|
||||||
map_buffer (counter_t *counter)
|
map_buffer (counter_t *counter)
|
||||||
{
|
{
|
||||||
int n_bytes = N_PAGES * process_get_page_size();
|
int n_bytes = N_PAGES * get_page_size();
|
||||||
void *address, *a;
|
void *address, *a;
|
||||||
|
|
||||||
/* We use the old trick of mapping the ring buffer twice
|
/* We use the old trick of mapping the ring buffer twice
|
||||||
* consecutively, so that we don't need special-case code
|
* consecutively, so that we don't need special-case code
|
||||||
* to deal with wrapping.
|
* to deal with wrapping.
|
||||||
*/
|
*/
|
||||||
address = mmap (NULL, n_bytes * 2 + process_get_page_size(), PROT_NONE,
|
address = mmap (NULL, n_bytes * 2 + get_page_size(), PROT_NONE,
|
||||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||||
|
|
||||||
if (address == MAP_FAILED)
|
if (address == MAP_FAILED)
|
||||||
fail ("mmap");
|
fail ("mmap");
|
||||||
|
|
||||||
a = mmap (address + n_bytes, n_bytes + process_get_page_size(),
|
a = mmap (address + n_bytes, n_bytes + get_page_size(),
|
||||||
PROT_READ | PROT_WRITE,
|
PROT_READ | PROT_WRITE,
|
||||||
MAP_SHARED | MAP_FIXED, counter->fd, 0);
|
MAP_SHARED | MAP_FIXED, counter->fd, 0);
|
||||||
|
|
||||||
if (a != address + n_bytes)
|
if (a != address + n_bytes)
|
||||||
fail ("mmap");
|
fail ("mmap");
|
||||||
|
|
||||||
a = mmap (address, n_bytes + process_get_page_size(),
|
a = mmap (address, n_bytes + get_page_size(),
|
||||||
PROT_READ | PROT_WRITE,
|
PROT_READ | PROT_WRITE,
|
||||||
MAP_SHARED | MAP_FIXED, counter->fd, 0);
|
MAP_SHARED | MAP_FIXED, counter->fd, 0);
|
||||||
|
|
||||||
@ -320,7 +335,7 @@ counter_new (Collector *collector,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
counter->data = (uint8_t *)counter->mmap_page + process_get_page_size ();
|
counter->data = (uint8_t *)counter->mmap_page + get_page_size ();
|
||||||
counter->tail = 0;
|
counter->tail = 0;
|
||||||
counter->cpu = cpu;
|
counter->cpu = cpu;
|
||||||
counter->partial = g_string_new (NULL);
|
counter->partial = g_string_new (NULL);
|
||||||
@ -340,7 +355,7 @@ counter_enable (counter_t *counter)
|
|||||||
static void
|
static void
|
||||||
counter_free (counter_t *counter)
|
counter_free (counter_t *counter)
|
||||||
{
|
{
|
||||||
munmap (counter->mmap_page, (N_PAGES + 1) * process_get_page_size());
|
munmap (counter->mmap_page, (N_PAGES + 1) * get_page_size());
|
||||||
fd_remove_watch (counter->fd);
|
fd_remove_watch (counter->fd);
|
||||||
|
|
||||||
close (counter->fd);
|
close (counter->fd);
|
||||||
@ -375,8 +390,6 @@ collector_reset (Collector *collector)
|
|||||||
collector->tracker = NULL;
|
collector->tracker = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
process_flush_caches();
|
|
||||||
|
|
||||||
collector->n_samples = 0;
|
collector->n_samples = 0;
|
||||||
|
|
||||||
g_get_current_time (&collector->latest_reset);
|
g_get_current_time (&collector->latest_reset);
|
||||||
|
|||||||
163
process.c
163
process.c
@ -35,18 +35,6 @@
|
|||||||
|
|
||||||
static GHashTable *processes_by_pid;
|
static GHashTable *processes_by_pid;
|
||||||
|
|
||||||
typedef struct Map Map;
|
|
||||||
struct Map
|
|
||||||
{
|
|
||||||
char * filename;
|
|
||||||
gulong start;
|
|
||||||
gulong end;
|
|
||||||
gulong offset;
|
|
||||||
gulong inode;
|
|
||||||
|
|
||||||
BinFile * bin_file;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Process
|
struct Process
|
||||||
{
|
{
|
||||||
char * cmdline;
|
char * cmdline;
|
||||||
@ -68,142 +56,6 @@ initialize (void)
|
|||||||
processes_by_pid = g_hash_table_new (g_direct_hash, g_direct_equal);
|
processes_by_pid = g_hash_table_new (g_direct_hash, g_direct_equal);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map *
|
|
||||||
read_maps (int pid, int *n_maps)
|
|
||||||
{
|
|
||||||
char *name = g_strdup_printf ("/proc/%d/maps", pid);
|
|
||||||
char buffer[1024];
|
|
||||||
FILE *in;
|
|
||||||
GArray *result;
|
|
||||||
|
|
||||||
in = fopen (name, "r");
|
|
||||||
if (!in)
|
|
||||||
{
|
|
||||||
g_free (name);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = g_array_new (FALSE, FALSE, sizeof (Map));
|
|
||||||
|
|
||||||
while (fgets (buffer, sizeof (buffer) - 1, in))
|
|
||||||
{
|
|
||||||
char file[256];
|
|
||||||
int count;
|
|
||||||
gulong start;
|
|
||||||
gulong end;
|
|
||||||
gulong offset;
|
|
||||||
gulong inode;
|
|
||||||
|
|
||||||
count = sscanf (
|
|
||||||
buffer, "%lx-%lx %*15s %lx %*x:%*x %lu %255s",
|
|
||||||
&start, &end, &offset, &inode, file);
|
|
||||||
if (count == 5)
|
|
||||||
{
|
|
||||||
Map map;
|
|
||||||
|
|
||||||
map.filename = g_strdup (file);
|
|
||||||
map.start = start;
|
|
||||||
map.end = end;
|
|
||||||
|
|
||||||
if (strcmp (map.filename, "[vdso]") == 0)
|
|
||||||
{
|
|
||||||
/* For the vdso, the kernel reports 'offset' as the
|
|
||||||
* the same as the mapping addres. This doesn't make
|
|
||||||
* any sense to me, so we just zero it here. There
|
|
||||||
* is code in binfile.c (read_inode) that returns 0
|
|
||||||
* for [vdso].
|
|
||||||
*/
|
|
||||||
map.offset = 0;
|
|
||||||
map.inode = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
map.offset = offset;
|
|
||||||
map.inode = inode;
|
|
||||||
}
|
|
||||||
|
|
||||||
map.bin_file = NULL;
|
|
||||||
|
|
||||||
g_array_append_val (result, map);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free (name);
|
|
||||||
fclose (in);
|
|
||||||
|
|
||||||
if (n_maps)
|
|
||||||
*n_maps = result->len;
|
|
||||||
|
|
||||||
return (Map *)g_array_free (result, FALSE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
free_maps (int *n_maps,
|
|
||||||
Map *maps)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < *n_maps; ++i)
|
|
||||||
{
|
|
||||||
Map *map = &(maps[i]);
|
|
||||||
|
|
||||||
if (map->filename)
|
|
||||||
g_free (map->filename);
|
|
||||||
|
|
||||||
if (map->bin_file)
|
|
||||||
bin_file_free (map->bin_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free (maps);
|
|
||||||
*n_maps = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const guint8 *
|
|
||||||
process_get_vdso_bytes (gsize *length)
|
|
||||||
{
|
|
||||||
static gboolean has_data;
|
|
||||||
static const guint8 *bytes = NULL;
|
|
||||||
static gsize n_bytes = 0;
|
|
||||||
|
|
||||||
if (!has_data)
|
|
||||||
{
|
|
||||||
Map *maps;
|
|
||||||
int n_maps, i;
|
|
||||||
|
|
||||||
maps = read_maps (getpid(), &n_maps);
|
|
||||||
|
|
||||||
for (i = 0; i < n_maps; ++i)
|
|
||||||
{
|
|
||||||
Map *map = &(maps[i]);
|
|
||||||
|
|
||||||
if (strcmp (map->filename, "[vdso]") == 0)
|
|
||||||
{
|
|
||||||
n_bytes = map->end - map->start;
|
|
||||||
|
|
||||||
/* Dup the memory here so that valgrind will only
|
|
||||||
* report one 1 byte invalid read instead of
|
|
||||||
* a ton when the elf parser scans the vdso
|
|
||||||
*
|
|
||||||
* The reason we get a spurious invalid read from
|
|
||||||
* valgrind is that we are getting the address directly
|
|
||||||
* from /proc/maps, and valgrind knows that its mmap()
|
|
||||||
* wrapper never returned that address. But since it
|
|
||||||
* is a legal mapping, it is legal to read it.
|
|
||||||
*/
|
|
||||||
bytes = g_memdup ((guint8 *)map->start, n_bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
has_data = TRUE;
|
|
||||||
free_maps (&n_maps, maps);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length)
|
|
||||||
*length = n_bytes;
|
|
||||||
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Process *
|
static Process *
|
||||||
create_process (const char *cmdline, int pid)
|
create_process (const char *cmdline, int pid)
|
||||||
{
|
{
|
||||||
@ -296,21 +148,6 @@ process_has_page (Process *process, gulong addr)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
process_get_page_size (void)
|
|
||||||
{
|
|
||||||
static int page_size;
|
|
||||||
static gboolean has_page_size = FALSE;
|
|
||||||
|
|
||||||
if (!has_page_size)
|
|
||||||
{
|
|
||||||
page_size = getpagesize();
|
|
||||||
has_page_size = TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
return page_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
process_ensure_map (Process *process, int pid, gulong addr)
|
process_ensure_map (Process *process, int pid, gulong addr)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -91,6 +91,7 @@ fake_new_process (tracker_t *tracker, pid_t pid)
|
|||||||
{
|
{
|
||||||
tracker_add_process (
|
tracker_add_process (
|
||||||
tracker, pid, g_strstrip (strchr (lines[i], ':') + 1));
|
tracker, pid, g_strstrip (strchr (lines[i], ':') + 1));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -314,7 +315,6 @@ struct process_t
|
|||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
char * comm;
|
char * comm;
|
||||||
char * undefined;
|
|
||||||
|
|
||||||
GPtrArray * maps;
|
GPtrArray * maps;
|
||||||
};
|
};
|
||||||
@ -389,7 +389,6 @@ destroy_process (process_t *process)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
g_free (process->comm);
|
g_free (process->comm);
|
||||||
g_free (process->undefined);
|
|
||||||
|
|
||||||
for (i = 0; i < process->maps->len; ++i)
|
for (i = 0; i < process->maps->len; ++i)
|
||||||
{
|
{
|
||||||
@ -409,7 +408,6 @@ create_process (state_t *state, new_process_t *new_process)
|
|||||||
|
|
||||||
process->pid = new_process->pid;
|
process->pid = new_process->pid;
|
||||||
process->comm = g_strdup (new_process->command_line);
|
process->comm = g_strdup (new_process->command_line);
|
||||||
process->undefined = NULL;
|
|
||||||
process->maps = g_ptr_array_new ();
|
process->maps = g_ptr_array_new ();
|
||||||
|
|
||||||
g_hash_table_insert (
|
g_hash_table_insert (
|
||||||
@ -441,13 +439,12 @@ state_new (void)
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
gulong address;
|
gulong address;
|
||||||
char *name;
|
char *name;
|
||||||
} kernel_symbol_t;
|
} kernel_symbol_t;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
parse_kallsym_line (const char *line,
|
parse_kallsym_line (const char *line, GArray *table)
|
||||||
GArray *table)
|
|
||||||
{
|
{
|
||||||
char **tokens = g_strsplit_set (line, " \t", -1);
|
char **tokens = g_strsplit_set (line, " \t", -1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user