Home > Software engineering >  Is it possible to load an extra helper .so during current gdb session?
Is it possible to load an extra helper .so during current gdb session?

Time:10-24

Is it possible to load an extra helper .so during current gdb session?

// point.h
class Point
{
public:
  int x;
  int y;
  Point(int x1, int y1) {x = x1; y = y1;}
};

// main.cpp
#include "point.h"
#include <stdio.h>
int main()
{
  Point p(3, 4);
  printf("%d\n", p.x);
  return 0;
}

g -g -c main.cpp -o main.o g -g main.o -o main

When debugging, I need to add a helper function to dump the Point object. But I don't want to recompile and rerun. (It might take a long time.) So I am trying to build another helper.so.

// helper.cpp
#include "point.h"
#include <stdio.h>
void dump_point(Point *p)
{
  printf("Point(%d, %d)\n", p->x, p->y);
}

g -g -fPIC -shared helper.cpp -o helper.so

Is it possible to load this helper.so in gdb so I can call dump_point() without rerunning?

CodePudding user response:

When debugging, I need to add a helper function to dump the Point object.

The "normal" way to do this to write a custom pretty printer in Python.

One advantage of doing that is that the pretty printer will also work for a core dump, whereas dump_point() solution will not (regardless of whether it's linked in or loaded from a separate .so).

So I am trying to build another helper.so.

If your main was linked against libdl, you could do this:

(gdb) call dlopen("./helper.so", 0)
(gdb) call dlsym($_, "dump_point")

Note: you will want to make dump_point extern "C" to avoid name mangling.

  • Related