drgn/libdrgn/lazy_object.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

50 lines
1.4 KiB
C

// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <assert.h>
#include "lazy_object.h"
static_assert(offsetof(union drgn_lazy_object, obj.type) ==
offsetof(union drgn_lazy_object, thunk.dummy_type),
"drgn_lazy_object layout is invalid");
struct drgn_error *drgn_lazy_object_evaluate(union drgn_lazy_object *lazy_obj)
{
struct drgn_error *err;
if (!drgn_lazy_object_is_evaluated(lazy_obj)) {
struct drgn_program *prog = lazy_obj->thunk.prog;
drgn_object_thunk_fn *fn = lazy_obj->thunk.fn;
void *arg = lazy_obj->thunk.arg;
drgn_object_init(&lazy_obj->obj, prog);
err = fn(&lazy_obj->obj, arg);
if (err) {
/* Oops, revert back to a thunk so it can be retried. */
drgn_lazy_object_init_thunk(lazy_obj, prog, fn, arg);
return err;
}
}
return NULL;
}
void drgn_lazy_object_deinit(union drgn_lazy_object *lazy_obj)
{
if (drgn_lazy_object_is_evaluated(lazy_obj))
drgn_object_deinit(&lazy_obj->obj);
else
lazy_obj->thunk.fn(NULL, lazy_obj->thunk.arg);
}
struct drgn_error *
drgn_lazy_object_check_prog(const union drgn_lazy_object *lazy_obj,
struct drgn_program *prog)
{
if ((drgn_lazy_object_is_evaluated(lazy_obj) ?
drgn_object_program(&lazy_obj->obj) : lazy_obj->thunk.prog)
!= prog) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"object is from different program");
}
return NULL;
}