Skip to content
Snippets Groups Projects

Add inspect-library helper

Merged Ludovico de Nittis requested to merge wip/denittis/inspect-library into master
1 file
+ 71
38
Compare changes
  • Side-by-side
  • Inline
+ 71
38
@@ -32,6 +32,8 @@
#include <string.h>
#include <dlfcn.h>
#include <link.h>
#include <stdbool.h>
#include <stdlib.h>
#define BASE "Base"
@@ -46,11 +48,15 @@ main (int argc,
const char *symbol;
void *handle = NULL;
struct link_map *dep_map = NULL;
const int SYMBOL_START = 2;
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t chars;
bool first;
if (argc < 2)
if (argc < 2 || argc > 3)
{
fprintf (stderr, "Expected one SONAME as argument.\n");
fprintf (stderr, "Expected, as argument, one SONAME and optionally a filename for symbols.\n");
return 1;
}
@@ -75,41 +81,68 @@ main (int argc,
printf ("\n \"path\": \"%s\",\n", dep_map->l_name);
printf (" \"missing_symbols\": [");
for (int i = SYMBOL_START; i < argc; i++)
{
version = strsep(&argv[i], "@");
symbol = strsep(&argv[i], "@");
if (symbol == NULL)
{
fprintf (stderr, "Probably the version@symbol pair is mispelled.");
printf ("\n ]");
clean_exit (handle);
return 1;
}
if (strcmp (version, BASE) == 0)
{
if (dlsym (handle, symbol) == NULL)
{
if (i == SYMBOL_START)
printf ("\n \"%s\"", symbol);
else
printf (",\n \"%s\"", symbol);
}
}
else
{
if (dlvsym (handle, symbol, version) == NULL)
{
if (i == SYMBOL_START)
printf ("\n \"%s/%s\"", version, symbol);
else
printf (",\n \"%s/%s\"", version, symbol);
}
}
}
if (argc == 3)
{
fp = fopen(argv[2], "r");
if (fp == NULL)
{
fprintf (stderr, "An error occurred reading the provided text file.\n");
printf ("\n ]");
clean_exit (handle);
return 1;
}
first = true;
while ((chars = getline(&line, &len, fp)) != -1)
{
if (line[chars - 1] == '\n')
line[chars - 1] = '\0';
/* Skip any empty line */
if (chars > 1)
{
version = strsep(&line, "@");
symbol = strsep(&line, "@");
if (symbol == NULL)
{
fprintf (stderr, "Probably the version@symbol pair is mispelled.");
printf ("\n ]");
free(line);
clean_exit (handle);
return 1;
}
if (strcmp (version, BASE) == 0)
{
if (dlsym (handle, symbol) == NULL)
{
if (first)
{
printf ("\n \"%s\"", symbol);
first = false;
}
else
printf (",\n \"%s\"", symbol);
}
}
else
{
if (dlvsym (handle, symbol, version) == NULL)
{
if (first)
{
printf ("\n \"%s@%s\"", version, symbol);
first = false;
}
else
printf (",\n \"%s@%s\"", version, symbol);
}
}
}
}
free(line);
fclose(fp);
}
printf ("\n ]");
/* Some loaded libraries may be before our handle.
Loading