-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have a powerful system for defining custom type, object, symbol, and debug info finders, but those currently require manual setup by users. The next step is a plugin system so that these (and more) can be set up automatically. Plugins are simply Python modules that define hook functions (currently there's only one hook). Plugins are registered as package entry points (shout out to Stephen Brennan for the suggestion) and can be configured further via environment variables. They are still called when using libdrgn directly (assuming libdrgn was compiled with Python support). Signed-off-by: Omar Sandoval <[email protected]>
- Loading branch information
Showing
7 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import fnmatch | ||
import importlib # noqa: F401 | ||
import logging | ||
import os | ||
import runpy | ||
import sys | ||
from types import SimpleNamespace | ||
from typing import List, Tuple | ||
|
||
logger = logging.getLogger("drgn.plugins") | ||
|
||
_plugins = None | ||
|
||
|
||
def _load_plugins() -> List[Tuple[str, object]]: | ||
plugins: List[Tuple[str, object]] = [] | ||
enabled_entry_points = {} | ||
|
||
env = os.getenv("DRGN_PLUGINS") | ||
if env: | ||
for item in env.split(","): | ||
if not item: | ||
# Ignore empty items for convenience. | ||
continue | ||
name, sep, value = item.partition("=") | ||
if sep: | ||
try: | ||
if value.startswith("/") or value.startswith("."): | ||
plugin: object = SimpleNamespace(**runpy.run_path(value)) | ||
else: | ||
plugin = importlib.import_module(value) | ||
except Exception: | ||
logger.warning("failed to load %r:", value, exc_info=True) | ||
else: | ||
plugins.append((name, plugin)) | ||
logger.debug("loaded %r", item) | ||
else: | ||
enabled_entry_points[name] = False | ||
|
||
env = os.getenv("DRGN_DISABLE_PLUGINS") | ||
# If all plugins are disabled, avoid the entry point machinery entirely. | ||
if env != "*" or enabled_entry_points: | ||
group = "drgn.plugins" | ||
|
||
if sys.version_info >= (3, 10): | ||
import importlib.metadata # novermin | ||
|
||
entry_points = importlib.metadata.entry_points(group=group) # novermin | ||
elif sys.version_info >= (3, 8): | ||
import importlib.metadata # novermin | ||
|
||
entry_points = importlib.metadata.entry_points()[group] # novermin | ||
else: | ||
import pkg_resources | ||
|
||
entry_points = pkg_resources.iter_entry_points(group) | ||
|
||
disable_plugins = env.split(",") if env else [] | ||
for entry_point in entry_points: | ||
if entry_point.name in enabled_entry_points: | ||
enabled_entry_points[entry_point.name] = True | ||
elif any( | ||
fnmatch.fnmatch(entry_point.name, disable) | ||
for disable in disable_plugins | ||
): | ||
continue | ||
try: | ||
plugin = entry_point.load() | ||
except Exception: | ||
logger.warning("failed to load %r:", entry_point.value, exc_info=True) | ||
else: | ||
plugins.append((entry_point.name, plugin)) | ||
logger.debug("loaded %r", entry_point.name) | ||
|
||
missing_entry_points = [ | ||
key for key, value in enabled_entry_points.items() if not value | ||
] | ||
if missing_entry_points: | ||
missing_entry_points.sort() | ||
logger.warning( | ||
"not found: %s", | ||
", ".join([repr(name) for name in missing_entry_points]), | ||
) | ||
|
||
plugins.sort( | ||
key=lambda plugin: (getattr(plugin[1], "drgn_priority", 50), plugin[0]) | ||
) | ||
return plugins | ||
|
||
|
||
def call_plugins(hook_name: str, *args: object) -> None: | ||
global _plugins | ||
if _plugins is None: | ||
_plugins = _load_plugins() | ||
|
||
for name, plugin in _plugins: | ||
try: | ||
hook = getattr(plugin, hook_name) | ||
except AttributeError: | ||
continue | ||
|
||
try: | ||
hook(*args) | ||
except Exception: | ||
logger.warning("%r %s failed:", name, hook_name, exc_info=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
#ifndef DRGN_PLUGINS_H | ||
#define DRGN_PLUGINS_H | ||
|
||
#include <stdbool.h> | ||
|
||
struct drgn_program; | ||
|
||
#if ENABLE_PYTHON | ||
void drgn_call_plugins_prog(const char *name, struct drgn_program *prog); | ||
#else | ||
static inline void drgn_call_plugins_prog(const char *name, struct drgn_program *prog) {} | ||
#endif | ||
|
||
#endif /* DRGN_PLUGINS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
#include "drgnpy.h" | ||
#include "../plugins.h" | ||
|
||
void drgn_call_plugins_prog(const char *name, struct drgn_program *prog) | ||
{ | ||
PyGILState_guard(); | ||
|
||
static PyObject *call_plugins; | ||
if (!call_plugins) { | ||
_cleanup_pydecref_ PyObject *_drgn_util_plugins_module = | ||
PyImport_ImportModule("_drgn_util.plugins"); | ||
if (!_drgn_util_plugins_module) { | ||
PyErr_WriteUnraisable(NULL); | ||
return; | ||
} | ||
call_plugins = PyObject_GetAttrString(_drgn_util_plugins_module, | ||
"call_plugins"); | ||
if (!call_plugins) { | ||
PyErr_WriteUnraisable(NULL); | ||
return; | ||
} | ||
} | ||
|
||
Program *prog_obj = container_of(prog, Program, prog); | ||
_cleanup_pydecref_ PyObject *res = | ||
PyObject_CallFunction(call_plugins, "sO", name, prog_obj); | ||
if (!res) | ||
PyErr_WriteUnraisable(call_plugins); | ||
} |