drgn/libdrgn/object_index.c
Omar Sandoval 87b7292aa5 Relicense drgn from GPLv3+ to LGPLv2.1+
drgn is currently licensed as GPLv3+. Part of the long term vision for
drgn is that other projects can use it as a library providing
programmatic interfaces for debugger functionality. A more permissive
license is better suited to this goal. We decided on LGPLv2.1+ as a good
balance between software freedom and permissiveness.

All contributors not employed by Meta were contacted via email and
consented to the license change. The only exception was the author of
commit c4fbf7e589 ("libdrgn: fix for compilation error"), who did not
respond. That commit reverted a single line of code to one originally
written by me in commit 640b1c011d ("libdrgn: embed DWARF index in
DWARF info cache").

Signed-off-by: Omar Sandoval <osandov@osandov.com>
2022-11-01 17:05:16 -07:00

100 lines
2.2 KiB
C

// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <stdlib.h>
#include <string.h>
#include "object_index.h"
void drgn_object_index_init(struct drgn_object_index *oindex)
{
oindex->finders = NULL;
}
void drgn_object_index_deinit(struct drgn_object_index *oindex)
{
struct drgn_object_finder *finder;
finder = oindex->finders;
while (finder) {
struct drgn_object_finder *next = finder->next;
free(finder);
finder = next;
}
}
struct drgn_error *
drgn_object_index_add_finder(struct drgn_object_index *oindex,
drgn_object_find_fn fn, void *arg)
{
struct drgn_object_finder *finder;
finder = malloc(sizeof(*finder));
if (!finder)
return &drgn_enomem;
finder->fn = fn;
finder->arg = arg;
finder->next = oindex->finders;
oindex->finders = finder;
return NULL;
}
void drgn_object_index_remove_finder(struct drgn_object_index *oindex)
{
struct drgn_object_finder *finder = oindex->finders->next;
free(oindex->finders);
oindex->finders = finder;
}
struct drgn_error *drgn_object_index_find(struct drgn_object_index *oindex,
const char *name,
const char *filename,
enum drgn_find_object_flags flags,
struct drgn_object *ret)
{
struct drgn_error *err;
size_t name_len;
struct drgn_object_finder *finder;
const char *kind_str;
if ((flags & ~DRGN_FIND_OBJECT_ANY) || !flags) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"invalid find object flags");
}
name_len = strlen(name);
finder = oindex->finders;
while (finder) {
err = finder->fn(name, name_len, filename, flags, finder->arg,
ret);
if (err != &drgn_not_found)
return err;
finder = finder->next;
}
switch (flags) {
case DRGN_FIND_OBJECT_CONSTANT:
kind_str = "constant ";
break;
case DRGN_FIND_OBJECT_FUNCTION:
kind_str = "function ";
break;
case DRGN_FIND_OBJECT_VARIABLE:
kind_str = "variable ";
break;
default:
kind_str = "";
break;
}
if (filename) {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s' in '%s'",
kind_str, name, filename);
} else {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s'", kind_str,
name);
}
}