473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

'C/C++' UDF problem -

Having a hard time with a UDF. Wonder if folk might care to take a look
and tell me what's up.

All of this kicked off from trying to build a set of C++ iostream
derived classes that can make
DB2's gimped BLOB API (sqludf_append() etc) look--to the extent that
this is possible--like
something C++ libraries can use to serialize stuff to (via
iostream::read(), iostream::write() and
what-not).

The problem is that the code below, which is about as simple as you
can make this stuff,
crashes in the constructor for the ostream(). And not bleedin' far in,
neither - mebbe 10
instructions in.

Anyway - if folk want to have a bash at this. I'd like to get these
classes available for
anyone to use. I tried on the c++ forums, but they spent 6 responses
criticising my
C++ syntactical style, which was .... unhelpful.

//
// File: db2_tst.cpp
//
// about:
//
//
// g++ -DDB2_UDF -I../incl -I/home/db2inst1/sqllib/include
-I/usr/include -w -g -fno-inline -c db2_tst.cpp
// g++ -shared -L/home/db2inst1/sqllib/lib
-Wl,-rpath,/home/db2inst1/sqllib/lib -ldb2 -o db2_tst.so db2_tst.o
//
// Copy the db2_tst.so into the appropriate ~sqllib/function
directory.
//
/*
CREATE FUNCTION MyTst ( In_Text varchar (1024) )
RETURNS INTEGER
LANGUAGE C PARAMETER STYLE DB2SQL
EXTERNAL NAME 'db2_tst.so!Tst'
SCRATCHPAD NO FINAL CALL NOT FENCED
NO SQL NO EXTERNAL ACTION DETERMINISTIC;

VALUES MyTst('Do Re Mi Fa So La Te Do');
*/
//
#include <stdlib.h>
#include <ios>
#include <fstream>
#include <iostream>
//
// DB2 includes
//
#include <sqludf.h>
#include <sql.h>

using namespace std;
extern "C"
void SQL_API_FN Tst ( SQLUDF_VARCHAR * inText,
SQLUDF_INTEGER * nOut,
SQLUDF_NULLIND * inText_ind,
SQLUDF_NULLIND * nOut_ind,

SQLUDF_TRAIL_ARGS )
{
filebuf fb;
fb.open("/tmp/Foo", ios::out);

//
// Crashes here -
// VVVVVVVVVVVVVVVVVVVV
iostream pos( &fb );

*nOut = 1;
}

Jan 12 '07 #1
7 2187
DBMS_Plumber wrote:
Having a hard time with a UDF. Wonder if folk might care to take a look
and tell me what's up.

All of this kicked off from trying to build a set of C++ iostream
derived classes that can make
DB2's gimped BLOB API (sqludf_append() etc) look--to the extent that
this is possible--like
something C++ libraries can use to serialize stuff to (via
iostream::read(), iostream::write() and
what-not).

The problem is that the code below, which is about as simple as you
can make this stuff,
crashes in the constructor for the ostream(). And not bleedin' far in,
neither - mebbe 10
instructions in.

Anyway - if folk want to have a bash at this. I'd like to get these
classes available for
anyone to use. I tried on the c++ forums, but they spent 6 responses
criticising my
C++ syntactical style, which was .... unhelpful.

//
// File: db2_tst.cpp
//
// about:
//
//
// g++ -DDB2_UDF -I../incl -I/home/db2inst1/sqllib/include
-I/usr/include -w -g -fno-inline -c db2_tst.cpp
// g++ -shared -L/home/db2inst1/sqllib/lib
-Wl,-rpath,/home/db2inst1/sqllib/lib -ldb2 -o db2_tst.so db2_tst.o
//
// Copy the db2_tst.so into the appropriate ~sqllib/function
directory.
//
/*
CREATE FUNCTION MyTst ( In_Text varchar (1024) )
RETURNS INTEGER
LANGUAGE C PARAMETER STYLE DB2SQL
EXTERNAL NAME 'db2_tst.so!Tst'
SCRATCHPAD NO FINAL CALL NOT FENCED
NO SQL NO EXTERNAL ACTION DETERMINISTIC;

VALUES MyTst('Do Re Mi Fa So La Te Do');
*/
//
#include <stdlib.h>
#include <ios>
#include <fstream>
#include <iostream>
//
// DB2 includes
//
#include <sqludf.h>
#include <sql.h>

using namespace std;
extern "C"
void SQL_API_FN Tst ( SQLUDF_VARCHAR * inText,
SQLUDF_INTEGER * nOut,
SQLUDF_NULLIND * inText_ind,
SQLUDF_NULLIND * nOut_ind,

SQLUDF_TRAIL_ARGS )
{
filebuf fb;
fb.open("/tmp/Foo", ios::out);

//
// Crashes here -
// VVVVVVVVVVVVVVVVVVVV
iostream pos( &fb );

*nOut = 1;
}
"Crashes here" means what? Could you be a bit more specific and provide
things like:
- how did you call the code? within a DB2 process or stand-alone?
- which signal did you get?
- what does the stack traceback show you?
- what have you seen in your debugger?

Essentially, I would suggest that you take the usual steps to debug your
code.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 14 '07 #2
Knut Stolze wrote:
"Crashes here" means what? Could you be a bit more specific and provide
things like:
- how did you call the code? within a DB2 process or stand-alone?
- which signal did you get?
- what does the stack traceback show you?
- what have you seen in your debugger?

Essentially, I would suggest that you take the usual steps to debug your
code.
Oh, and please don't forget to let us know about your environment: DB2,
platform, C/C++ compiler and libraries, ...

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 15 '07 #3

Knut Stolze wrote:
"Crashes here" means what? Could you be a bit more specific and provide
things like:
- how did you call the code? within a DB2 process or stand-alone?
- which signal did you get?
- what does the stack traceback show you?
- what have you seen in your debugger?

Essentially, I would suggest that you take the usual steps to debug your
code.

Oh, and please don't forget to let us know about your environment: DB2,
platform, C/C++ compiler and libraries, ...
Last first:

uname -a; g++ -v; db2level
Linux pebbles 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686
i686 i386 GNU/Linux
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-47)
DB21085I Instance "xram" uses "32" bits and DB2 code release
"SQL08026" with
level identifier "03070106".
Informational tokens are "DB2 v8.1.0.120", "s060801", "MI00162", and
FixPak
"13".
Product is installed at "/opt/IBM/db2/V8.1".

Q1: How did I call the code?

Just as the source snippet says: in a DB2 process, invoked in the
orthodox fashion from the command line.

db2 =VALUES MyTst('Do Re Mi Fa So La Te Do');
VALUES MyTst('Do Re Mi Fa So La Te Do')

1
-----------
SQL0430N User defined function "XRAM.MYTST" (specific name
"SQL070112150328000") has abnormally terminated. SQLSTATE=38503

db2 =>

Q2, Q3, Q4:

What follows is the gdb session. WARNING: Wall of Text inbound!

gdb -p 896 ~xram/sqllib/adm/db2sysc
GNU gdb Red Hat Linux (6.1post-1.20040607.52rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host
libthread_db library "/lib/tls/libthread_db.so.1".

Attaching to program: /home/xram/sqllib/adm/db2sysc, process 896
Reading symbols from /lib/tls/libpthread.so.0...done.
[Thread debugging using libthread_db enabled]
[New Thread -1289360896 (LWP 896)]
Loaded symbols for /lib/tls/libpthread.so.0
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2e.so.1...Error while
reading shared library symbols:
DW_FORM_strp pointing outside of .debug_str section [in module
/opt/IBM/db2/V8.1/lib/libdb2e.so.1]
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2osse.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2osse.so.1
Reading symbols from /lib/tls/libm.so.6...done.
Loaded symbols for /lib/tls/libm.so.6
Reading symbols from /opt/IBM/db2/V8.1/lib/libcxa.so.3...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libcxa.so.3
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Reading symbols from /lib/libcrypt.so.1...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libdl.so.2...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/tls/librt.so.1...done.
Loaded symbols for /lib/tls/librt.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2install.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2install.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2locale.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2locale.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2g11n.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2g11n.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2icuglue.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2icuglue.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2genreg.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2genreg.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2trcapi.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2trcapi.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2dstf.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2dstf.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2dascmn.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2dascmn.so.1
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2osse_db2.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2osse_db2.so.1
Reading symbols from /lib/libnss_files.so.2...done.
Loaded symbols for /lib/libnss_files.so.2
Reading symbols from
/opt/IBM/db2/V8.1/lib/icc/icclib/libicclib.so...done.
Loaded symbols for /home/xram/sqllib/lib32/icc/icclib/libicclib.so
Reading symbols from
/opt/IBM/db2/V8.1/lib/icc/osslib/libcrypto.so.0.9.7...done.
Loaded symbols for
/home/xram/sqllib/lib32/icc/osslib/libcrypto.so.0.9.7
Reading symbols from
/opt/IBM/db2/V8.1/security/plugin/IBM/group/IBMOSgroups.so...done.
Loaded symbols for
/home/xram/sqllib/security32/plugin/IBM/group/IBMOSgroups.so
Reading symbols from
/opt/IBM/db2/V8.1/security/plugin/IBM/server/IBMOSauthserver.so...done.
Loaded symbols for
/home/xram/sqllib/security32/plugin/IBM/server/IBMOSauthserver.so
Reading symbols from /home/xram/sqllib/function/db2_tst.so...done.
Loaded symbols for /home/xram/sqllib/function/db2_tst.so
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2.so.1...Error while
reading shared library symbols:
DW_FORM_strp pointing outside of .debug_str section [in module
/opt/IBM/db2/V8.1/lib/libdb2.so.1]
Reading symbols from /usr/lib/libstdc++.so.5...done.
Loaded symbols for /usr/lib/libstdc++.so.5
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /home/xram/sqllib/function/grphudfs...done.
Loaded symbols for /home/xram/sqllib/function/grphudfs
Reading symbols from /opt/IBM/db2/V8.1/lib/libdb2apie.so.1...done.
Loaded symbols for /opt/IBM/db2/V8.1/lib/libdb2apie.so.1
0xb3e91fb6 in semop () from /lib/tls/libc.so.6
(gdb) break Tst
Breakpoint 1 at 0xb2609186: file db2_tst.cpp, line 41.
(gdb) c
Continuing.
[Switching to Thread -1289360896 (LWP 896)]

Breakpoint 1, Tst (inText=0xb3216388 "Do Re Mi Fa So La Te Do",
nOut=0xb3216798,
inText_ind=0xb3216386, nOut_ind=0xb3216792,
sqludf_sqlstate=0xb3216228 "00000", sqludf_fname=0xb323bbc8
"XRAM.MYTST",
sqludf_fspecname=0xb323bcca "SQL070112150328000",
sqludf_msgtext=0xb3216238 "")
at db2_tst.cpp:41
41 filebuf fb;
Current language: auto; currently c++
(gdb) where
#0 Tst (inText=0xb3216388 "Do Re Mi Fa So La Te Do", nOut=0xb3216798,
inText_ind=0xb3216386, nOut_ind=0xb3216792,
sqludf_sqlstate=0xb3216228 "00000", sqludf_fname=0xb323bbc8
"XRAM.MYTST",
sqludf_fspecname=0xb323bcca "SQL070112150328000",
sqludf_msgtext=0xb3216238 "")
at db2_tst.cpp:41
#1 0xb5dd02ec in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#2 0xb3216388 in ?? ()
#3 0xb3216798 in ?? ()
#4 0xb3216386 in ?? ()
#5 0xb3216792 in ?? ()
#6 0xb3216228 in ?? ()
#7 0xb323bbc8 in ?? ()
#8 0xb323bcca in ?? ()
#9 0xb3216238 in ?? ()
#10 0xb3216310 in ?? ()
#11 0x00000000 in ?? ()
(gdb) list 35,50
35 SQLUDF_INTEGER *
nOut,
36 SQLUDF_NULLIND * inText_ind,
37 SQLUDF_NULLIND * nOut_ind,
38
39 SQLUDF_TRAIL_ARGS )
40 {
41 filebuf fb;
42 fb.open("/tmp/Foo", ios::out);
43
44 //
45 // Crashes here -
46 // VVVVVVVVVVVVVVVVVVVV
47 iostream pos( &fb );
48
49 *nOut = 1;
50 }
(gdb) n
42 fb.open("/tmp/Foo", ios::out);
(gdb) n
47 iostream pos( &fb );
(gdb) nexti
0xb26091b4 47 iostream pos( &fb );
(gdb) nexti
0xb26091ba 47 iostream pos( &fb );
(gdb) nexti
0xb26091bb 47 iostream pos( &fb );
(gdb) nexti
0xb26091c1 47 iostream pos( &fb );
(gdb) nexti
0xb26091c2 47 iostream pos( &fb );
(gdb) where
#0 0xb26091c2 in Tst (inText=0xb3216388 "Do Re Mi Fa So La Te Do",
nOut=0xb3216798, inText_ind=0xb3216386, nOut_ind=0xb3216792,
sqludf_sqlstate=0xb3216228 "00000", sqludf_fname=0xb323bbc8
"XRAM.MYTST",
sqludf_fspecname=0xb323bcca "SQL070112150328000",
sqludf_msgtext=0xb3216238 "")
at db2_tst.cpp:47
#1 0xb5dd02ec in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#2 0xb3216388 in ?? ()
#3 0xb3216798 in ?? ()
#4 0xb3216386 in ?? ()
#5 0xb3216792 in ?? ()
#6 0xb3216228 in ?? ()
#7 0xb323bbc8 in ?? ()
#8 0xb323bcca in ?? ()
#9 0xb3216238 in ?? ()
#10 0xb3216310 in ?? ()
#11 0x00000000 in ?? ()
(gdb) nexti

Program received signal SIGSEGV, Segmentation fault.
0xb5f4ce2b in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
(gdb) where
#0 0xb5f4ce2b in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#1 0xbffef228 in ?? ()
#2 0xb5fede98 in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#3 0x00000004 in ?? ()
#4 0xb75916b0 in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#5 0xbffef380 in ?? ()
#6 0xbffef240 in ?? ()
#7 0xb5fec379 in ?? () from /opt/IBM/db2/V8.1/lib/libdb2e.so.1
#8 0x00000000 in ?? ()
(gdb) Quit
(gdb) quit
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: /home/xram/sqllib/adm/db2sysc, process 896
Script done on Mon 15 Jan 2007 09:12:57 AM PST
[root@pebbles root]#
Notes: I do some fairly sophisticated UDT/UDF development in DB2, and
although I'm more familiar with the INFORMIX IDS product, this rather
mystifies me. It also mystifies me from a C++ development point of
view. I'm not even getting into the meat of the ostream() constructor.

I was rather hoping that someone would be able to say something
categorical, like "Oh. You can't do that in DB2." or "You moron! You
need to use compile-time-tweak-X!" (The later, I trust, more likely
than the former.)

Jan 15 '07 #4
DBMS_Plumber wrote:
>
Knut Stolze wrote:
"Crashes here" means what? Could you be a bit more specific and
provide things like:
- how did you call the code? within a DB2 process or stand-alone?
- which signal did you get?
- what does the stack traceback show you?
- what have you seen in your debugger?

Essentially, I would suggest that you take the usual steps to debug
your code.

Oh, and please don't forget to let us know about your environment: DB2,
platform, C/C++ compiler and libraries, ...

Last first:

uname -a; g++ -v; db2level
Linux pebbles 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686
i686 i386 GNU/Linux
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-47)
DB21085I Instance "xram" uses "32" bits and DB2 code release
"SQL08026" with
level identifier "03070106".
Informational tokens are "DB2 v8.1.0.120", "s060801", "MI00162", and
FixPak
"13".
Product is installed at "/opt/IBM/db2/V8.1".

Q1: How did I call the code?

Just as the source snippet says: in a DB2 process, invoked in the
orthodox fashion from the command line.

db2 =VALUES MyTst('Do Re Mi Fa So La Te Do');
VALUES MyTst('Do Re Mi Fa So La Te Do')

1
-----------
SQL0430N User defined function "XRAM.MYTST" (specific name
"SQL070112150328000") has abnormally terminated. SQLSTATE=38503
You should try to call your code directly from a "int main" function.
Taking DB2 out of the picture can help to verify that it is not your code
causing the problem.
I was rather hoping that someone would be able to say something
categorical, like "Oh. You can't do that in DB2." or "You moron! You
need to use compile-time-tweak-X!" (The later, I trust, more likely
than the former.)
One thing you could try is to link "libstdc++" and/or "libc" before you link
with "libdb2" et al. There are some functions/operators that are
overloaded by DB2 (for example "throw"), which cause an abort(). I don't
know the reason for that overloading - others may be able to comment on
that.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 15 '07 #5

Knut Stolze wrote:
You should try to call your code directly from a "int main" function.
Taking DB2 out of the picture can help to verify that it is not your code
causing the problem.
The following code compiles and works just fine. Doesn't do anything,
of course.

#include <stdlib.h>
#include <ios>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{

filebuf fb;
fb.open("/tmp/Foo", ios::out);
iostream * pos = new iostream ( &fb );
}
One thing you could try is to link "libstdc++" and/or "libc" before you link
with "libdb2" et al. There are some functions/operators that are
overloaded by DB2 (for example "throw"), which cause an abort(). I don't
know the reason for that overloading - others may be able to comment on
that.
Will give that a shot.

Jan 15 '07 #6

DBMS_Plumber wrote:
Knut Stolze wrote:
One thing you could try is to link "libstdc++" and/or "libc" before you link
with "libdb2" et al. There are some functions/operators that are
overloaded by DB2 (for example "throw"), which cause an abort(). I don't
know the reason for that overloading - others may be able to comment on
that.

Will give that a shot.
Made no difference.

I will abandon the idea, and re-write the whole thing in good ol'
fashioned C.

Jan 15 '07 #7
DBMS_Plumber wrote:
>
Knut Stolze wrote:
>You should try to call your code directly from a "int main" function.
Taking DB2 out of the picture can help to verify that it is not your code
causing the problem.

The following code compiles and works just fine. Doesn't do anything,
of course.

#include <stdlib.h>
#include <ios>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{

filebuf fb;
fb.open("/tmp/Foo", ios::out);
iostream * pos = new iostream ( &fb );
}
For debugging purposes, your "main" function should not reimplement the UDF
but rather call the UDF with the same parameters as DB2 would.
>One thing you could try is to link "libstdc++" and/or "libc" before you
link
with "libdb2" et al. There are some functions/operators that are
overloaded by DB2 (for example "throw"), which cause an abort(). I don't
know the reason for that overloading - others may be able to comment on
that.

Will give that a shot.
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.