Debugging C functions... - Mailing list pgsql-general

From Dmitry Tkach
Subject Debugging C functions...
Date
Msg-id 3C92A827.6050409@openratings.com
Whole thread Raw
List pgsql-general
Hi, everybody!

Here is me again, with my bitwise indexing troubles :-)
(Refer to btree index extension topic from yesterday)...

I have, pretty much, given up the idea to make btrees (or rtrees) do
what I need... now I am trying to make it work with a GiST index (as Tom
Lane suggested earlier)... Because I am too stupid and too lazy to
figure out how to implement GiST from scratch, I am trying to use the
intarray code from contrib - it seems to do exactly what I need, except
it deals with _int4 arrays instead of bitsets.

So, I wrote a C function called pack (should be unpack really, but never
mind :-), that converts my bits into an array, and try to index my table:

create index bset_idx on bset using gist  (pack (flags));

this runs for a few minutes, and then the backend crashes. All I can see
in the log is:

----
Server process (pid 29372) exited with status 11 at Fri Mar 15 18:33:50 2002
Terminating any active server processes...
Server processes were terminated at Fri Mar 15 18:33:50 2002
Reinitializing shared memory and semaphores
The Data Base System is starting up
----

I assume, there must be some bug in the func. I wrote, that's causing
the crash... The question is - how do I debug it?
Are there some techniques to do that (like attaching to the running
backend with a debugger, or something else, perhaps, somehow turning
more debug output on)?

Below is the entire C code I wrote (I am attaching it, hoping, that,
perhaps, somebody would be able to just spot the problem by looking at
it)... It creates one function, that takes an int4 (the bits) and a
second arg, being the maximum bit, that can be set (this is to be able
to work with both int2 and int4)...
And then I have sql wrappers:

create function pack(int4) returns _int4 as 'select pack($1, 1<<31);'
language 'sql';
create function pack(int2) returns _int4 as 'select pack($1::int4,
1<<15);' language 'sql';

Here is the C source:


#include "postgres.h"
#include "utils/array.h"

ArrayType *pack(int,long);
ArrayType *wrap_array(void *, int, int);

ArrayType *pack (int bits, long max)
{
  static int4 buff [sizeof (long)];
  long b;
  int count;

  for (b = 1, count = 0;  b <= max; b <<= 1)
    if (bits & b)
      buff [count++] = b;
  return wrap_array (buff, count, sizeof(int4));
}

ArrayType *wrap_array (void *ptr, int alen, int elen)
{
  int numbytes = alen*elen;
  int sz = numbytes + ARR_OVERHEAD (1);
  ArrayType *result = (ArrayType *) palloc (sz);
  result -> size = sz;
  result -> ndim = 1;
  result -> flags = 0;
  ARR_DIMS(result)[0] = alen;
  ARR_LBOUND(result)[0] = 1;
  memmove (ARR_DATA_PTR(result), ptr, numbytes);
  return result;
}

I compile it with:
gcc -Wall -Wmissing-prototypes -Wmissing-declarations -fpic -I.
-I../../src/include  -DPGSQL71  -c _bits.c -o _bits.o

I will greatly appreciate your responses...

Thanks a lot!

Dima




pgsql-general by date:

Previous
From: Dmitry Tkach
Date:
Subject: Re: Btree index extension question
Next
From: Jeff Davis
Date:
Subject: Re: How to catch NOTIFY messages with PHP