Skip to content
Snippets Groups Projects

Add inspect-library helper

Merged Ludovico de Nittis requested to merge wip/denittis/inspect-library into master
Compare and Show latest version
4 files
+ 78
42
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 56
22
@@ -34,10 +34,12 @@
#include <link.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#define BASE "Base"
void clean_exit (void *handle);
static void print_json_string_content (const char *s);
static void clean_exit (void *handle);
int
main (int argc,
@@ -48,6 +50,7 @@ main (int argc,
const char *symbol;
void *handle = NULL;
struct link_map *dep_map = NULL;
struct link_map *the_library = NULL;
FILE *fp;
char *line = NULL;
size_t len = 0;
@@ -62,7 +65,9 @@ main (int argc,
soname = argv[1];
printf ("{\n");
printf (" \"%s\": {", soname);
printf (" \"");
print_json_string_content (soname);
printf ("\": {");
handle = dlopen (soname, RTLD_NOW);
if (handle == NULL)
{
@@ -72,13 +77,15 @@ main (int argc,
}
/* Using RTLD_DI_LINKMAP insted of RTLD_DI_ORIGIN we don't need to worry
* about allocating a big enough array for the path. */
if (dlinfo (handle, RTLD_DI_LINKMAP, &dep_map) != 0 || dep_map == NULL)
if (dlinfo (handle, RTLD_DI_LINKMAP, &the_library) != 0 || the_library == NULL)
{
fprintf (stderr, "Unable to obtain the path: %s\n", dlerror ());
clean_exit (handle);
return 1;
}
printf ("\n \"path\": \"%s\",\n", dep_map->l_name);
printf ("\n \"path\": \"");
print_json_string_content (the_library->l_name);
printf ("\",\n");
printf (" \"missing_symbols\": [");
if (argc == 3)
@@ -86,7 +93,9 @@ main (int argc,
fp = fopen(argv[2], "r");
if (fp == NULL)
{
fprintf (stderr, "An error occurred reading the provided text file.\n");
int saved_errno = errno;
fprintf (stderr, "Error reading \"%s\": %s\n", argv[2], strerror (saved_errno));
printf ("\n ]");
clean_exit (handle);
return 1;
@@ -118,11 +127,14 @@ main (int argc,
{
if (first)
{
printf ("\n \"%s\"", symbol);
printf ("\n \"");
first = false;
}
else
printf (",\n \"%s\"", symbol);
printf (",\n \"");
print_json_string_content (symbol);
printf ("\"");
}
}
else
@@ -131,11 +143,16 @@ main (int argc,
{
if (first)
{
printf ("\n \"%s@%s\"", version, symbol);
printf ("\n \"");
first = false;
}
else
printf (",\n \"%s@%s\"", version, symbol);
printf (",\n \"");
print_json_string_content (version);
printf ("@");
print_json_string_content (symbol);
printf ("\"");
}
}
}
@@ -145,27 +162,30 @@ main (int argc,
}
printf ("\n ]");
dep_map = the_library;
/* Some loaded libraries may be before our handle.
* To list them all we move the pointer at the beginning. */
while (dep_map != NULL && dep_map->l_prev != NULL)
dep_map = dep_map->l_prev;
printf (",\n \"dependencies\": [");
/* Some entries don't have a name. We move the pointer to the
* first element that has one. */
while (dep_map != NULL && strcmp (dep_map->l_name, "") == 0)
dep_map = dep_map->l_next;
if (dep_map != NULL)
{
printf ("\n \"%s\"", dep_map->l_name);
dep_map = dep_map->l_next;
}
first = true;
for (; dep_map != NULL; dep_map = dep_map->l_next)
{
if (strcmp (dep_map->l_name, "") != 0)
printf (",\n \"%s\"", dep_map->l_name);
if (dep_map == the_library || strcmp (dep_map->l_name, "") == 0)
continue;
if (first)
{
printf ("\n \"");
first = false;
}
else
printf (",\n \"");
print_json_string_content (dep_map->l_name);
printf ("\"");
}
printf ("\n ]\n }");
@@ -174,6 +194,20 @@ main (int argc,
return 0;
}
void
print_json_string_content (const char *s)
{
const char *p;
for (p = s; *p != '\0'; p++)
{
if (*p == '"' || *p == '\\' || *p <= 0x1F)
printf ("\\u%04x", *p);
else
printf ("%c", *p);
}
}
void
clean_exit (void *handle)
{
Loading