3

While using a gem, I've encountered problem in native extension (which is part of the gem) that I wanted to debug. I tried adding printing methods in C code, but nothing displays when using a gem.

I've tried using various printing C functions (including printf, fprintf to stderr and file, rb_p), but none of them worked. However, any changes to gem's .rb files were printed. Therefore, I assumed the issue is connected with the way native extensions are built.

I tried compiling the gem after adding printing methods but before using the gem, using following (oci8) commands:

ruby setup.rb config && ruby setup.rb setup && ruby setup.rb install 

but it didn't change anything.

Can you please give me a hint, how to make printing methods in the C files work? Am I missing a step, which actually compiles native extension?

The debugging I want to do is performed during testing, and the command I am using is:

bundle exec m my_test.rb 

The gem I am trying to debug is ruby-oci8, but I believe my issue is generic to every gem with native extension.

EDIT:

There is a chance that it might not be a generic issue, so I will describe exactly what I am trying to achieve.

I want to debug ruby-oci8 gem's native extension. In class Cursor, inside class initializer, a method from C extension is called:

def initialize(conn, sql = nil)
  # ...
  __initialize(conn, sql) # Initialize the internal C structure.
end

I want to print something inside the called C function. The __initialize function is located in this file (in C called static VALUE oci8_stmt_initialize(VALUE self, VALUE svc, VALUE sql)). So I want to add, e.g.

static VALUE oci8_stmt_initialize(VALUE self, VALUE svc, VALUE sql)
{
  printf("My debug code);
  oci8_stmt_t *stmt = TO_STMT(self);
  /* ... rest of the function */
}

And then, when invoking tests which use connection to the database and oci8 gem, I want to see:

$ bundle exec m my_test.rb
  My debug code

Currently, even though the printf function is added to the C file, the test's output doesn't print it. However, when I added puts "Before __initialize" in cursor.rb file, it is shown by test's output.

Miron Marczuk
  • 81
  • 1
  • 7
  • Can you please explain what you mean by "printing methods" ? But in the meantime, have you looked at using the pry gem? You have access to see C code from inside of it. https://github.com/pry/pry – lacostenycoder May 31 '19 at 17:22
  • 1
    I tried adding a fprintf(stderr,....) to the C code of one of my home-grown gems and it works just fine, so I think to will need to explain in more detail exactly what you are doing. – varro May 31 '19 at 18:39
  • @lacostenycoder By printing methods I mean functions like e.g. `printf("My debug code");`, `fprintf(stderr, "My debug code");` `FILE* file = fopen("my_debug_file", "w"); fprintf(file, "My debug code");` and `rb_p("My debug code");` (those functions I tried using). – Miron Marczuk May 31 '19 at 23:34
  • @varro I updated the question, explaining what I am trying to achieve. – Miron Marczuk Jun 01 '19 at 00:06
  • @lacostenycoder I binded `pry` in cursor.rb file, before `__initialize(..)` method (see updated question) and it showed me the content of the `oci8_stmt_initialize(..)` function **WITH** added `printf("My debug code");`. That means that the changed function is used during the execution - but unfortunately, printf's text is not present in the test's output. – Miron Marczuk Jun 01 '19 at 00:17
  • Thanks for the extra info. Unfortunately, I don't see anything obviously wrong. I would strongly advise you to use fprintf(stderr,...) rather than printf() to avoid possible buffering problems (but I don't think that is your fundamental problem). I'm inclined to think that this is somehow related to how the oci8 gem is constructed, rather than a generic issue, based on my own experiments with a home-grown gem where everything works as expected. – varro Jun 01 '19 at 01:05

1 Answers1

0

To use a local version of a gem you need to point bundler to it:

gem 'example_gem', path: './example_gem'
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
  • Thanks for the suggestion, but it does not solve an issue. Changes in gem's *.rb files are being recognised, but changes in *.c still aren't. – Miron Marczuk Jun 03 '19 at 09:41