kallsyms: make SpKallsyms more testable

This allows us to pass in the file to read from, and adds a quick test
case to view the parsed output. It also fixes a base-10 vs base-16
mistake in parsing addresses.
This commit is contained in:
Christian Hergert
2018-01-29 01:25:14 -08:00
parent af4eac025a
commit 911d51d447
6 changed files with 55 additions and 10 deletions

View File

@ -47,13 +47,16 @@ sp_kallsyms_free (SpKallsyms *self)
}
SpKallsyms *
sp_kallsyms_new (void)
sp_kallsyms_new (const gchar *path)
{
g_autoptr(SpKallsyms) self = NULL;
if (path == NULL)
path = "/proc/kallsyms";
self = g_slice_new0 (SpKallsyms);
if (!g_file_get_contents ("/proc/kallsyms", &self->buf, &self->buflen, NULL))
if (!g_file_get_contents (path, &self->buf, &self->buflen, NULL))
return NULL;
self->iter = self->buf;
@ -70,6 +73,7 @@ sp_kallsyms_next (SpKallsyms *self,
{
guint64 addr;
char *tok;
char *pptr;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (self->buf != NULL, FALSE);
@ -93,10 +97,12 @@ try_next:
return FALSE;
}
addr = g_ascii_strtoull (tok, NULL, 10);
if ((addr == G_MAXUINT64 && errno == ERANGE) ||
(addr == 0 && errno == EINVAL))
return FALSE;
/* We'll keep going if we fail to parse, (null) usually, so that we
* just skip to the next line.
*/
addr = g_ascii_strtoull (tok, &pptr, 16);
if ((pptr == tok) || (addr == G_MAXUINT64 && errno == ERANGE) || (addr == 0 && errno == EINVAL))
addr = 0;
*address = addr;
@ -139,7 +145,7 @@ try_next:
if (self->iter >= self->endptr)
return FALSE;
if (g_strcmp0 (tok, "(null)") == 0)
if (addr == 0)
goto try_next;
*name = tok;

View File

@ -27,7 +27,7 @@ G_BEGIN_DECLS
typedef struct _SpKallsyms SpKallsyms;
SpKallsyms *sp_kallsyms_new (void);
SpKallsyms *sp_kallsyms_new (const gchar *path);
gboolean sp_kallsyms_next (SpKallsyms *self,
const gchar **name,
guint64 *address,

View File

@ -189,7 +189,7 @@ sp_kernel_symbol_load (void)
kernel_symbol_strs = g_string_chunk_new (4096);
ar = g_array_new (FALSE, TRUE, sizeof (SpKernelSymbol));
if (!(kallsyms = sp_kallsyms_new ()))
if (!(kallsyms = sp_kallsyms_new (NULL)))
goto query_daemon;
while (sp_kallsyms_next (kallsyms, &name, &addr, &type))