Debugging C Extensions and [A bit OT] general C Questions

B

Brian Schröder

Hello Group,

I'm right now learning to write C extensions and it works quite good. The only problem is, that my C was never very profound and I seem to have forgotten all of it in the meantime. So obviously I write a lot of bugs. The question is, how do I debug them.

Right now I'm using printf to output things to the console, but I'd rather like to debug the process in somethink like ddd. Is this possible?
I tried to connect to the running process, but it said to me that the process was "no program".

And now the OT question:

anybody knows of a compact C manual on the web, where I can for example learn how in this code:

/* Structure containing the volume data */
typedef struct {
unsigned long width, height, depth, length;
double *data;
} SRVolume;

/* Create a new SRVolume Datastructure, initialized to size 0x0x0
* Is this allocation method correct, or am I copying the data onto the
* stack and back?
*/
SRVolume rvAlloc() {
SRVolume *result = calloc(1, sizeof(SRVolume));
result->data = calloc(0, sizeof(double));
return(*result);
}

the result data data is returned? I'd think that there is no copying of large amounts of data involved, but I'm not 100% shure because I haven't found a good manual explaining this yet.

Additionally I get under special circumstances (Creating a volume, setting a value and using p(volume)) the message that it tries to free a wrong pointer. If i change p(volume) to puts volume.inspect it works. This is shurely not enough description, but maybe you could point me to some more information on the GC and other things that can bring forth this error message.

Best regards,

Brian
 
S

Shashank Date

Hi Brian,

--- Brian Schröder said:
Hello Group,

I'm right now learning to write C extensions and it
works quite good. The only problem is, that my C was
never very profound and I seem to have forgotten all
of it in the meantime. So obviously I write a lot of
bugs. The question is, how do I debug them.

Right now I'm using printf to output things to the
console,

That is how I debug all my C code :)
but I'd rather like to debug the process in
somethink like ddd. Is this possible?

Yes, I have done that too, although not with
ruby-extensions.
I tried to connect to the running process, but it
said to me that the process was "no program".

And now the OT question:

anybody knows of a compact C manual on the web,
where I can for example learn how in this code:

/* Structure containing the volume data */
typedef struct {
unsigned long width, height, depth, length;
double *data;
} SRVolume;

/* Create a new SRVolume Datastructure, initialized
to size 0x0x0
* Is this allocation method correct, or am I
copying the data onto the
* stack and back?
*/
SRVolume rvAlloc() {
SRVolume *result = calloc(1, sizeof(SRVolume));
result->data = calloc(0, sizeof(double));
return(*result);
}

the result data data is returned?

Yes. I'm in the habit of type casting the calloc, like
so:

SRVolume *result = (SRVolume *)calloc(1,
sizeof(SRVolume));

which, if nothing else makes my compiler happy ;-)
I'd think that
there is no copying of large amounts of data
involved,

Correct. Only the pointer is copied. I would check to
make sure that it did not return a NULL.
but I'm not 100% shure because I haven't
found a good manual explaining this yet.

Have you tried this:

http://www.eskimo.com/~scs/cclass/krnotes/top.html
Additionally I get under special circumstances
(Creating a volume, setting a value and using
p(volume)) the message that it tries to free a wrong
pointer.

What is p(volume) ? Is it in Ruby or C?
If i change p(volume) to puts volume.inspect

... looks like it is in Ruby.
it works. This is shurely not enough
description, but maybe you could point me to some
more information on the GC and other things that can
bring forth this error message.

Can you show us some more code surrounding it?

HTH,
Best regards,

Brian

-- shanko

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
T

Tim Hunter

Hello Group,

I'm right now learning to write C extensions and it works quite good. The only problem is, that my C was never very profound and I seem to have forgotten all of it in the meantime. So obviously I write a lot of bugs. The question is, how do I debug them.

Right now I'm using printf to output things to the console, but I'd rather like to debug the process in somethink like ddd. Is this possible?
I tried to connect to the running process, but it said to me that the process was "no program".

Sure it's possible. I use gdb. Start ruby under gdb:

gdb ruby

When gdb prompts you, set a breakpoint in dln.c at the point just
after the extension gets loaded. That line varies from release to
release but it's around line 1350. Look for the call to your
initialization function: "(*init_fct)();"

b dln.c:1350

After your extension is loaded you can set breakpoints in it.
 
T

Tilman Sauerbeck

Brian Schröder said:
Right now I'm using printf to output things to the console, but I'd rather like to debug the process in somethink like ddd. Is this possible?
I tried to connect to the running process, but it said to me that the process was "no program".

gdb ruby
b Init_my_ext
r

gdb will tell you that it couldn't resolve the breakpoint because your
extension isn't loaded yet, but it will do so as soon as you
require/load your extension.

If the GC is troubling you, get Valgrind (and a suppression file for
Ruby, e.g. http://code-monkey.de/files/ruby.supp) and give it a go.
There will be a lot of false positives though, so it's not that easy to
get the useful information out of Valgrind's output ;)
And now the OT question:

anybody knows of a compact C manual on the web, where I can for example learn how in this code:

[snip]

the result data data is returned? I'd think that there is no copying of large amounts of data involved, but I'm not 100% shure because I haven't found a good manual explaining this yet.

Umm. All it copies is sizeof(SRVolume) bytes. It will only copy the
pointer to the result data, not the contents itself.
Additionally I get under special circumstances (Creating a volume, setting a value and using p(volume)) the message that it tries to free a wrong pointer. If i change p(volume) to puts volume.inspect it works. This is shurely not enough description, but maybe you could point me to some more information on the GC and other things that can bring forth this error message.

Check that your mark callbacks are set up correctly, i.e. make sure
you're not forgetting to mark some of your objects.

btw, your lines are a bit too long ;)
Usually it's set to 74 characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top