无法使用 LD_PRELOAD 挂钩 ncurses 函数

Unable to hook ncurses functions with LD_PRELOAD

我正在尝试挂钩一些 ncurses 函数,但它们没有任何效果。 ncurses 不是静态链接的,所以我不明白为什么它不起作用。

test.cpp

#include <cstdio>
#include <cstdlib>
#include <curses.h>

int main() {
  initscr();
  cbreak();
  noecho();

  getch();

  endwin();

  return 0;
}

编译:gcc test.cpp -o test -std=c++11 -lncurses

hook.cpp

#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>

int getch() {
  typedef int getch ();
  getch* old_getch = (getch*) dlsym(RTLD_NEXT, "getch");

  int result = old_getch();

  fprintf(stderr, "getch() = %i\n", result);

  return result;
}

int noecho() {
  typedef int noecho ();
  noecho* old_noecho = (noecho*) dlsym(RTLD_NEXT, "noecho");

  int result = old_noecho();

  fprintf(stderr, "noecho() = %i\n", result);

  return result;
}

int endwin() {
  typedef int endwin ();
  endwin* old_endwin = (endwin*) dlsym(RTLD_NEXT, "endwin");

  int result = old_endwin();

  printf("endwin called");

  return result;
}

编译:gcc hook.cpp -o hook.so -shared -ldl -fPIC -std=c++11

可悲的是它什么也没输出,我完全被难住了。

规范没有规定 getch 必须是函数(不是宏)。实际上,在ncurses-6.1中,getch定义为

#define getch() wgetch(stdscr)

尽管如此,libncurses 中有一个 getch 函数(它只是调用 wgetch(stdscr)),所以 dlsym(libncurses_handle,"getch") 确实有效。