Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Experts on embedding Perl in C wanted: Weird problem on RH7.3/Perl 5.6.1

Reply
Thread Tools

Experts on embedding Perl in C wanted: Weird problem on RH7.3/Perl 5.6.1

 
 
David F. Skoll
Guest
Posts: n/a
 
      11-05-2003
Hi,

I'm tearing my hair out on this one. I'm trying to embed a Perl
interpreter into a C program. I need to be able to create and destroy
the interpreter periodically, but will never actually have two interpreters
at the same time.

On Red Hat Linux 7.3 with Perl 5.6.1, the attached program segfaults. On
Red Hat 9 with Perl 5.8.0, it works perfectly. Save the program code
as "test-embed-perl.c" and the build script as "buildte". Then run:

sh buildte
./te

to see the behaviour. Enlightenment is appreciated!

A gdb stacktrace shows this:

$ gdb ./te
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x080a098b in Perl_sv_upgrade ()
(gdb) where
#0 0x080a098b in Perl_sv_upgrade ()
#1 0x080a4244 in Perl_sv_setpvn ()
#2 0x080a708d in Perl_newSVpvn ()
#3 0x0805abb9 in S_parse_body ()
#4 0x0805a993 in perl_parse ()
#5 0x08059a77 in make_embedded_interpreter () at test-embed-perl.c:30
#6 0x08059aa1 in main () at test-embed-perl.c:39
#7 0x400841c4 in __libc_start_main () from /lib/libc.so.6

Thanks,

David.

========= buildte =============
#!/bin/sh
echo cc -g `perl -MExtUtils::Embed -e ccopts` -c -o test-embed-perl.o
test-embed-perl.c
cc -g `perl -MExtUtils::Embed -e ccopts` -c -o test-embed-perl.o test-embed-perl.c
echo cc -g `perl -MExtUtils::Embed -e ldopts` -o te test-embed-perl.o -lperl
cc -g `perl -MExtUtils::Embed -e ldopts` -o te test-embed-perl.o -lperl

======== test-embed-perl.c =========
#include <EXTERN.h>
#include <perl.h>

static PerlInterpreter *my_perl = NULL;

int
make_embedded_interpreter()
{
char *argv[5];
int argc;

if (my_perl != NULL) {
perl_destruct(my_perl);
perl_free(my_perl);
my_perl = NULL;
}
my_perl = perl_alloc();
if (!my_perl) {
return -1;
}
PERL_SET_CONTEXT(my_perl);
/* If you leave out the PL_perl_destruct_level lines, it "works",
but leaks memory like crazy! */
PL_perl_destruct_level = 1;
perl_construct(my_perl);
PL_perl_destruct_level = 1;
argv[0] = "";
argv[1] = "-e";
argv[2] = "print(\"foo\\n\");";
argv[3] = NULL;
argc = 3;
perl_parse(my_perl, NULL, argc, argv, NULL);
perl_run(my_perl);
return 0;
}

int
main()
{
while(1) {
make_embedded_interpreter();
}
}

============= Output of perl -V on system where it fails ============
Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
Platform:
osname=linux, osvers=2.4.17-0.13smp, archname=i386-linux
uname='linux daffy.perf.redhat.com 2.4.17-0.13smp #1 smp fri feb 1 10:30:48 est 2002 i686 unknown '
config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Uusethreads -Uuseithreads -Uuselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
Compiler:
cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include',
optimize='-O2 -march=i386 -mcpu=i686',
cppflags='-fno-strict-aliasing -I/usr/local/include'
ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.2 2.96-109)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
alignbytes=4, usemymalloc=n, prototype=define
Linker and Libraries:
ld='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lc -lcrypt -lutil
perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'


Characteristics of this binary (from libperl):
Compile-time options:
Built under linux
Compiled at Apr 1 2002 12:23:22
@INC:
/usr/lib/perl5/5.6.1/i386-linux
/usr/lib/perl5/5.6.1
/usr/lib/perl5/site_perl/5.6.1/i386-linux
/usr/lib/perl5/site_perl/5.6.1
/usr/lib/perl5/site_perl/5.6.0
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.6.1/i386-linux
/usr/lib/perl5/vendor_perl/5.6.1
/usr/lib/perl5/vendor_perl
 
Reply With Quote
 
 
 
 
dinser
Guest
Posts: n/a
 
      11-14-2003
Am new to perl but know C alright. Perhaps you
need to allocate some memory to char *argv[]
before you put values into it? Good luck.

Ray


> ======== test-embed-perl.c =========
> #include <EXTERN.h>
> #include <perl.h>
>
> static PerlInterpreter *my_perl = NULL;
>
> int
> make_embedded_interpreter()
> {
> char *argv[5];
> int argc;
>
> if (my_perl != NULL) {
> perl_destruct(my_perl);
> perl_free(my_perl);
> my_perl = NULL;
> }
> my_perl = perl_alloc();
> if (!my_perl) {
> return -1;
> }
> PERL_SET_CONTEXT(my_perl);
> /* If you leave out the PL_perl_destruct_level lines, it "works",
> but leaks memory like crazy! */
> PL_perl_destruct_level = 1;
> perl_construct(my_perl);
> PL_perl_destruct_level = 1;
> argv[0] = "";
> argv[1] = "-e";
> argv[2] = "print(\"foo\\n\");";
> argv[3] = NULL;
> argc = 3;
> perl_parse(my_perl, NULL, argc, argv, NULL);
> perl_run(my_perl);
> return 0;

 
Reply With Quote
 
 
 
 
David F. Skoll
Guest
Posts: n/a
 
      11-14-2003
dinser wrote:

> Am new to perl but know C alright. Perhaps you
> need to allocate some memory to char *argv[]
> before you put values into it? Good luck.


Nope; that's not it. I guess you're not as familiar with C as you think.

Anyone else?

Regards,

David.

>> ======== test-embed-perl.c =========
>> #include <EXTERN.h>
>> #include <perl.h>
>>
>> static PerlInterpreter *my_perl = NULL;
>>
>> int
>> make_embedded_interpreter()
>> {
>> char *argv[5];
>> int argc;
>>
>> if (my_perl != NULL) {
>> perl_destruct(my_perl);
>> perl_free(my_perl);
>> my_perl = NULL;
>> }
>> my_perl = perl_alloc();
>> if (!my_perl) {
>> return -1;
>> }
>> PERL_SET_CONTEXT(my_perl);
>> /* If you leave out the PL_perl_destruct_level lines, it "works",
>> but leaks memory like crazy! */
>> PL_perl_destruct_level = 1;
>> perl_construct(my_perl);
>> PL_perl_destruct_level = 1;
>> argv[0] = "";
>> argv[1] = "-e";
>> argv[2] = "print(\"foo\\n\");";
>> argv[3] = NULL;
>> argc = 3;
>> perl_parse(my_perl, NULL, argc, argv, NULL);
>> perl_run(my_perl);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
weird embedding problem DavidM Python 4 12-08-2007 10:57 AM
newbie with a weird technical problem (@ least I think it's weird) will Ruby 6 12-27-2006 04:46 PM
Perl experts wanted Wim Hoogenraad Perl Misc 1 07-29-2005 06:58 PM
embedding perl within perl.... suraj Perl Misc 4 11-07-2003 06:02 PM
Taint - having some real trouble here, taint/perl experts, please help Ben Perl Misc 17 10-24-2003 12:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57