shared: remove use of libshared

This moves everything into other places and simple includes the
files in the cases that it is necessary. In the future, we can
rewrite sysprofd to use GDBus and add GetProcFile() to allow
for client-side processing of kallsyms.
This commit is contained in:
Christian Hergert
2019-05-08 09:57:30 -07:00
parent 1c6741bdc6
commit 6ba408f073
26 changed files with 33 additions and 122 deletions

View File

@ -6,6 +6,7 @@ libsysprof_public_sources = [
'sp-elf-symbol-resolver.c',
'sp-hostinfo-source.c',
'sp-jitmap-symbol-resolver.c',
'sp-kallsyms.c',
'sp-kernel-symbol.c',
'sp-kernel-symbol-resolver.c',
'sp-local-profiler.c',
@ -27,6 +28,7 @@ libsysprof_public_headers = [
'sp-elf-symbol-resolver.h',
'sp-hostinfo-source.h',
'sp-jitmap-symbol-resolver.h',
'sp-kallsyms.h',
'sp-kernel-symbol.h',
'sp-kernel-symbol-resolver.h',
'sp-local-profiler.h',
@ -48,6 +50,7 @@ libsysprof_private_sources = [
'elfparser.c',
'stackstash.c',
'sp-source-util.c',
'sp-line-reader.c',
]
libsysprof_deps = [

View File

@ -0,0 +1,156 @@
/* sp-kallsyms.c
*
* Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define G_LOG_DOMAIN "sp-kallsyms"
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "sp-kallsyms.h"
struct _SpKallsyms
{
gchar *buf;
gsize buflen;
gchar *endptr;
gchar *iter;
};
void
sp_kallsyms_free (SpKallsyms *self)
{
if (self != NULL)
{
g_clear_pointer (&self->buf, g_free);
g_slice_free (SpKallsyms, self);
}
}
SpKallsyms *
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 (path, &self->buf, &self->buflen, NULL))
return NULL;
self->iter = self->buf;
self->endptr = self->buf + self->buflen;
return g_steal_pointer (&self);
}
gboolean
sp_kallsyms_next (SpKallsyms *self,
const gchar **name,
guint64 *address,
guint8 *type)
{
guint64 addr;
char *tok;
char *pptr;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (self->buf != NULL, FALSE);
g_return_val_if_fail (self->buflen > 0, FALSE);
g_return_val_if_fail (self->iter != NULL, FALSE);
g_return_val_if_fail (self->endptr != NULL, FALSE);
try_next:
if (self->iter >= self->endptr)
return FALSE;
tok = strtok_r (self->iter, " \t\n", &self->iter);
if (!tok || *tok == 0)
return FALSE;
if (*tok == '[')
{
tok = strtok_r (self->iter, " \t\n", &self->iter);
if (!tok || *tok == 0)
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;
if (self->iter >= self->endptr)
return FALSE;
tok = strtok_r (self->iter, " \t\n", &self->iter);
if (!tok || *tok == 0)
return FALSE;
switch (*tok)
{
case 'A':
case 'B':
case 'D':
case 'R':
case 'T':
case 'V':
case 'W':
case 'a':
case 'b':
case 'd':
case 'r':
case 't':
case 'w':
*type = *tok;
break;
default:
return FALSE;
}
if (self->iter >= self->endptr)
return FALSE;
tok = strtok_r (self->iter, " \t\n", &self->iter);
if (!tok || *tok == 0)
return FALSE;
if (self->iter >= self->endptr)
return FALSE;
if (addr == 0)
goto try_next;
*name = tok;
return TRUE;
}

View File

@ -0,0 +1,41 @@
/* sp-kallsyms.h
*
* Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include "sysprof-version-macros.h"
G_BEGIN_DECLS
typedef struct _SpKallsyms SpKallsyms;
SYSPROF_AVAILABLE_IN_ALL
SpKallsyms *sp_kallsyms_new (const gchar *path);
SYSPROF_AVAILABLE_IN_ALL
gboolean sp_kallsyms_next (SpKallsyms *self,
const gchar **name,
guint64 *address,
guint8 *type);
SYSPROF_AVAILABLE_IN_ALL
void sp_kallsyms_free (SpKallsyms *self);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SpKallsyms, sp_kallsyms_free)
G_END_DECLS

View File

@ -27,7 +27,6 @@
#include <sysprof-capture.h>
#include "sp-kallsyms.h"
#include "sp-line-reader.h"
#include "sp-kernel-symbol.h"
static GArray *kernel_symbols;

View File

@ -0,0 +1,120 @@
/* sp-line-reader.c
*
* Copyright 2015-2019 Christian Hergert <christian@hergert.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <string.h>
#include "sp-line-reader.h"
struct _SpLineReader
{
const gchar *contents;
gsize length;
gsize pos;
};
void
sp_line_reader_free (SpLineReader *self)
{
g_slice_free (SpLineReader, self);
}
/**
* sp_line_reader_new:
* @contents: The buffer to read lines from
* @length: the length of @buffer in bytes
*
* Creates a new #SpLineReader for the contents provided. @contents are not
* copied and therefore it is a programming error to free contents before
* freeing the #SpLineReader structure.
*
* Use sp_line_reader_next() to read through the lines of the buffer.
*
* Returns: (transfer full): A new #SpLineReader that should be freed with
* sp_line_reader_free() when no longer in use.
*/
SpLineReader *
sp_line_reader_new (const gchar *contents,
gssize length)
{
SpLineReader *self = g_slice_new (SpLineReader);
if (contents == NULL)
{
contents = "";
length = 0;
}
else if (length < 0)
{
length = strlen (contents);
}
self->contents = contents;
self->length = length;
self->pos = 0;
return self;
}
/**
* sp_line_reader_next:
* @self: the #SpLineReader
* @length: a location for the length of the line in bytes
*
* Moves forward to the beginning of the next line in the buffer. No changes to
* the buffer are made, and the result is a pointer within the string passed as
* @contents in sp_line_reader_init(). Since the line most likely will not be
* terminated with a NULL byte, you must provide @length to determine the
* length of the line.
*
* Using "line[length]" will place you on the \n that was found for the line.
* However, to perform this safely, you need to know that your string was
* either \0 terminated to begin with, or that your buffer provides enough space
* to guarantee you can dereference past the last "textual content" of the
* buffer.
*
* Returns: (nullable) (transfer none): The beginning of the line within the buffer
*/
const gchar *
sp_line_reader_next (SpLineReader *self,
gsize *length)
{
const gchar *ret;
const gchar *endptr;
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (length != NULL, NULL);
if ((self->contents == NULL) || (self->pos >= self->length))
{
*length = 0;
return NULL;
}
ret = &self->contents [self->pos];
endptr = memchr (ret, '\n', self->length - self->pos);
if (G_UNLIKELY (endptr == NULL))
endptr = &self->contents [self->length];
*length = (endptr - ret);
self->pos += *length + 1;
return ret;
}

View File

@ -0,0 +1,40 @@
/* sp-line-reader.h
*
* Copyright 2015-2019 Christian Hergert <christian@hergert.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <glib.h>
G_BEGIN_DECLS
typedef struct _SpLineReader SpLineReader;
G_GNUC_INTERNAL
SpLineReader *sp_line_reader_new (const gchar *contents,
gssize length);
G_GNUC_INTERNAL
void sp_line_reader_free (SpLineReader *self);
G_GNUC_INTERNAL
const gchar *sp_line_reader_next (SpLineReader *self,
gsize *length);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SpLineReader, sp_line_reader_free)
G_END_DECLS

View File

@ -27,6 +27,7 @@ G_BEGIN_DECLS
# include "sp-callgraph-profile.h"
# include "sp-capture-gobject.h"
# include "sp-local-profiler.h"
# include "sp-kallsyms.h"
# include "sp-profile.h"
# include "sp-profiler.h"
# include "sp-map-lookaside.h"