3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'Very useful, no doubt, that was to Saruman; yet it seems that he was
13 * not content.' --Gandalf to Pippin
15 * [p.598 of _The Lord of the Rings_, III/xi: "The PalantÃr"]
18 /* This file contains assorted utility routines.
19 * Which is a polite way of saying any stuff that people couldn't think of
20 * a better place for. Amongst other things, it includes the warning and
21 * dieing stuff, plus wrappers for malloc code.
25 #define PERL_IN_UTIL_C
29 #if defined(USE_PERLIO)
30 #include "perliol.h" /* For PerlIOUnix_refcnt */
36 # define SIG_ERR ((Sighandler_t) -1)
44 /* Missing protos on LynxOS */
49 # include "amigaos4/amigaio.h"
54 # include <sys/select.h>
58 #ifdef USE_C_BACKTRACE
62 # undef USE_BFD /* BFD is useless in OS X. */
72 # include <execinfo.h>
76 #ifdef PERL_DEBUG_READONLY_COW
77 # include <sys/mman.h>
82 /* NOTE: Do not call the next three routines directly. Use the macros
83 * in handy.h, so that we can easily redefine everything to do tracking of
84 * allocated hunks back to the original New to track down any memory leaks.
85 * XXX This advice seems to be widely ignored :-( --AD August 1996.
88 #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL)
89 # define ALWAYS_NEED_THX
92 #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW)
94 S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header)
97 && mprotect(header, header->size, PROT_READ|PROT_WRITE))
98 Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
99 header, header->size, errno);
103 S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header)
106 && mprotect(header, header->size, PROT_READ))
107 Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
108 header, header->size, errno);
110 # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo)
111 # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo)
113 # define maybe_protect_rw(foo) NOOP
114 # define maybe_protect_ro(foo) NOOP
117 #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW)
118 /* Use memory_debug_header */
120 # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \
121 || defined(PERL_DEBUG_READONLY_COW)
122 # define MDH_HAS_SIZE
126 /* paranoid version of system's malloc() */
129 Perl_safesysmalloc(MEM_SIZE size)
131 #ifdef ALWAYS_NEED_THX
138 if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
140 size += PERL_MEMORY_DEBUG_HEADER_SIZE;
143 if ((SSize_t)size < 0)
144 Perl_croak_nocontext("panic: malloc, size=%" UVuf, (UV) size);
146 if (!size) size = 1; /* malloc(0) is NASTY on our system */
148 #ifdef PERL_DEBUG_READONLY_COW
149 if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
150 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
151 perror("mmap failed");
155 ptr = (Malloc_t)PerlMem_malloc(size);
157 PERL_ALLOC_CHECK(ptr);
160 struct perl_memory_debug_header *const header
161 = (struct perl_memory_debug_header *)ptr;
165 PoisonNew(((char *)ptr), size, char);
168 #ifdef PERL_TRACK_MEMPOOL
169 header->interpreter = aTHX;
170 /* Link us into the list. */
171 header->prev = &PL_memory_debug_header;
172 header->next = PL_memory_debug_header.next;
173 PL_memory_debug_header.next = header;
174 maybe_protect_rw(header->next);
175 header->next->prev = header;
176 maybe_protect_ro(header->next);
177 # ifdef PERL_DEBUG_READONLY_COW
178 header->readonly = 0;
184 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
185 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
187 /* malloc() can modify errno() even on success, but since someone
188 writing perl code doesn't have any control over when perl calls
189 malloc() we need to hide that.
198 #ifndef ALWAYS_NEED_THX
210 /* paranoid version of system's realloc() */
213 Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
215 #ifdef ALWAYS_NEED_THX
219 #ifdef PERL_DEBUG_READONLY_COW
220 const MEM_SIZE oldsize = where
221 ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size
230 ptr = safesysmalloc(size);
235 where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
236 if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
238 size += PERL_MEMORY_DEBUG_HEADER_SIZE;
240 struct perl_memory_debug_header *const header
241 = (struct perl_memory_debug_header *)where;
243 # ifdef PERL_TRACK_MEMPOOL
244 if (header->interpreter != aTHX) {
245 Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p",
246 header->interpreter, aTHX);
248 assert(header->next->prev == header);
249 assert(header->prev->next == header);
251 if (header->size > size) {
252 const MEM_SIZE freed_up = header->size - size;
253 char *start_of_freed = ((char *)where) + size;
254 PoisonFree(start_of_freed, freed_up, char);
264 if ((SSize_t)size < 0)
265 Perl_croak_nocontext("panic: realloc, size=%" UVuf, (UV)size);
267 #ifdef PERL_DEBUG_READONLY_COW
268 if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
269 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
270 perror("mmap failed");
273 Copy(where,ptr,oldsize < size ? oldsize : size,char);
274 if (munmap(where, oldsize)) {
275 perror("munmap failed");
279 ptr = (Malloc_t)PerlMem_realloc(where,size);
281 PERL_ALLOC_CHECK(ptr);
283 /* MUST do this fixup first, before doing ANYTHING else, as anything else
284 might allocate memory/free/move memory, and until we do the fixup, it
285 may well be chasing (and writing to) free memory. */
287 #ifdef PERL_TRACK_MEMPOOL
288 struct perl_memory_debug_header *const header
289 = (struct perl_memory_debug_header *)ptr;
292 if (header->size < size) {
293 const MEM_SIZE fresh = size - header->size;
294 char *start_of_fresh = ((char *)ptr) + size;
295 PoisonNew(start_of_fresh, fresh, char);
299 maybe_protect_rw(header->next);
300 header->next->prev = header;
301 maybe_protect_ro(header->next);
302 maybe_protect_rw(header->prev);
303 header->prev->next = header;
304 maybe_protect_ro(header->prev);
306 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
308 /* realloc() can modify errno() even on success, but since someone
309 writing perl code doesn't have any control over when perl calls
310 realloc() we need to hide that.
315 /* In particular, must do that fixup above before logging anything via
316 *printf(), as it can reallocate memory, which can cause SEGVs. */
318 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
319 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
326 #ifndef ALWAYS_NEED_THX
339 /* safe version of system's free() */
342 Perl_safesysfree(Malloc_t where)
344 #ifdef ALWAYS_NEED_THX
347 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
350 Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
352 struct perl_memory_debug_header *const header
353 = (struct perl_memory_debug_header *)where_intrn;
356 const MEM_SIZE size = header->size;
358 # ifdef PERL_TRACK_MEMPOOL
359 if (header->interpreter != aTHX) {
360 Perl_croak_nocontext("panic: free from wrong pool, %p!=%p",
361 header->interpreter, aTHX);
364 Perl_croak_nocontext("panic: duplicate free");
367 Perl_croak_nocontext("panic: bad free, header->next==NULL");
368 if (header->next->prev != header || header->prev->next != header) {
369 Perl_croak_nocontext("panic: bad free, ->next->prev=%p, "
370 "header=%p, ->prev->next=%p",
371 header->next->prev, header,
374 /* Unlink us from the chain. */
375 maybe_protect_rw(header->next);
376 header->next->prev = header->prev;
377 maybe_protect_ro(header->next);
378 maybe_protect_rw(header->prev);
379 header->prev->next = header->next;
380 maybe_protect_ro(header->prev);
381 maybe_protect_rw(header);
383 PoisonNew(where_intrn, size, char);
385 /* Trigger the duplicate free warning. */
388 # ifdef PERL_DEBUG_READONLY_COW
389 if (munmap(where_intrn, size)) {
390 perror("munmap failed");
396 Malloc_t where_intrn = where;
398 #ifndef PERL_DEBUG_READONLY_COW
399 PerlMem_free(where_intrn);
404 /* safe version of system's calloc() */
407 Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
409 #ifdef ALWAYS_NEED_THX
413 #if defined(USE_MDH) || defined(DEBUGGING)
414 MEM_SIZE total_size = 0;
417 /* Even though calloc() for zero bytes is strange, be robust. */
418 if (size && (count <= MEM_SIZE_MAX / size)) {
419 #if defined(USE_MDH) || defined(DEBUGGING)
420 total_size = size * count;
426 if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size)
427 total_size += PERL_MEMORY_DEBUG_HEADER_SIZE;
432 if ((SSize_t)size < 0 || (SSize_t)count < 0)
433 Perl_croak_nocontext("panic: calloc, size=%" UVuf ", count=%" UVuf,
434 (UV)size, (UV)count);
436 #ifdef PERL_DEBUG_READONLY_COW
437 if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE,
438 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
439 perror("mmap failed");
442 #elif defined(PERL_TRACK_MEMPOOL)
443 /* Have to use malloc() because we've added some space for our tracking
445 /* malloc(0) is non-portable. */
446 ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1);
448 /* Use calloc() because it might save a memset() if the memory is fresh
449 and clean from the OS. */
451 ptr = (Malloc_t)PerlMem_calloc(count, size);
452 else /* calloc(0) is non-portable. */
453 ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1);
455 PERL_ALLOC_CHECK(ptr);
456 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) calloc %zu x %zu = %zu bytes\n",PTR2UV(ptr),(long)PL_an++, count, size, total_size));
460 struct perl_memory_debug_header *const header
461 = (struct perl_memory_debug_header *)ptr;
463 # ifndef PERL_DEBUG_READONLY_COW
464 memset((void*)ptr, 0, total_size);
466 # ifdef PERL_TRACK_MEMPOOL
467 header->interpreter = aTHX;
468 /* Link us into the list. */
469 header->prev = &PL_memory_debug_header;
470 header->next = PL_memory_debug_header.next;
471 PL_memory_debug_header.next = header;
472 maybe_protect_rw(header->next);
473 header->next->prev = header;
474 maybe_protect_ro(header->next);
475 # ifdef PERL_DEBUG_READONLY_COW
476 header->readonly = 0;
480 header->size = total_size;
482 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
488 #ifndef ALWAYS_NEED_THX
497 /* These must be defined when not using Perl's malloc for binary
502 Malloc_t Perl_malloc (MEM_SIZE nbytes)
504 #ifdef PERL_IMPLICIT_SYS
507 return (Malloc_t)PerlMem_malloc(nbytes);
510 Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
512 #ifdef PERL_IMPLICIT_SYS
515 return (Malloc_t)PerlMem_calloc(elements, size);
518 Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
520 #ifdef PERL_IMPLICIT_SYS
523 return (Malloc_t)PerlMem_realloc(where, nbytes);
526 Free_t Perl_mfree (Malloc_t where)
528 #ifdef PERL_IMPLICIT_SYS
536 /* This is the value stored in *retlen in the two delimcpy routines below when
537 * there wasn't enough room in the destination to store everything it was asked
538 * to. The value is deliberately very large so that hopefully if code uses it
539 * unquestioninly to access memory, it will likely segfault. And it is small
540 * enough that if the caller does some arithmetic on it before accessing, it
541 * won't overflow into a small legal number. */
542 #define DELIMCPY_OUT_OF_BOUNDS_RET I32_MAX
545 =for apidoc_section String Handling
546 =for apidoc delimcpy_no_escape
548 Copy a source buffer to a destination buffer, stopping at (but not including)
549 the first occurrence of the delimiter byte C<delim>, in the source. The source
550 is the bytes between C<from> and C<fromend> inclusive. The dest is C<to>
553 Nothing is copied beyond what fits between C<to> through C<toend>. If C<delim>
554 doesn't occur in the source buffer, as much of the source as will fit is copied
557 The actual number of bytes copied is written to C<*retlen>.
559 If there is room in the destination available after the copy, an extra
560 terminating safety NUL byte is written (not included in the returned length).
565 Perl_delimcpy_no_escape(char *to, const char *toend, const char *from,
566 const char *fromend, int delim, I32 *retlen)
568 const char * delim_pos;
569 Ptrdiff_t to_len = toend - to;
571 /* Only use the minimum of the available source/dest */
572 Ptrdiff_t copy_len = MIN(fromend - from, to_len);
574 PERL_ARGS_ASSERT_DELIMCPY_NO_ESCAPE;
576 assert(copy_len >= 0);
578 /* Look for the first delimiter in the portion of the source we are allowed
579 * to look at (determined by the input bounds). */
580 delim_pos = (const char *) memchr(from, delim, copy_len);
582 copy_len = delim_pos - from;
583 } /* else didn't find it: copy all of the source permitted */
585 Copy(from, to, copy_len, char);
591 /* If there is extra space available, add a trailing NUL */
592 if (copy_len < to_len) {
596 return (char *) from + copy_len;
602 Copy a source buffer to a destination buffer, stopping at (but not including)
603 the first occurrence in the source of an unescaped (defined below) delimiter
604 byte, C<delim>. The source is the bytes between S<C<from> and C<from_end> -
605 1>. Similarly, the dest is C<to> up to C<to_end>.
607 The number of bytes copied is written to C<*retlen>.
609 Returns the position of the first uncopied C<delim> in the C<from> buffer, but
610 if there is no such occurrence before C<from_end>, then C<from_end> is returned,
611 and the entire buffer S<C<from> .. C<from_end> - 1> is copied.
613 If there is room in the destination available after the copy, an extra
614 terminating safety C<NUL> byte is appended (not included in the returned
617 The error case is if the destination buffer is not large enough to accommodate
618 everything that should be copied. In this situation, a value larger than
619 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
620 fits will be written to the destination. Not having room for the safety C<NUL>
621 is not considered an error.
623 In the following examples, let C<x> be the delimiter, and C<0> represent a C<NUL>
624 byte (B<NOT> the digit C<0>). Then we would have
629 provided the destination buffer is at least 4 bytes long.
631 An escaped delimiter is one which is immediately preceded by a single
632 backslash. Escaped delimiters are copied, and the copy continues past the
633 delimiter; the backslash is not copied:
638 (provided the destination buffer is at least 8 bytes long).
640 It's actually somewhat more complicated than that. A sequence of any odd number
641 of backslashes escapes the following delimiter, and the copy continues with
642 exactly one of the backslashes stripped.
646 abc\\\xdef abc\\xdef0
647 abc\\\\\xdef abc\\\\xdef0
649 (as always, if the destination is large enough)
651 An even number of preceding backslashes does not escape the delimiter, so that
652 the copy stops just before it, and includes all the backslashes (no stripping;
653 zero is considered even):
664 Perl_delimcpy(char *to, const char *to_end,
665 const char *from, const char *from_end,
666 const int delim, I32 *retlen)
668 const char * const orig_to = to;
669 Ptrdiff_t copy_len = 0;
670 bool stopped_early = FALSE; /* Ran out of room to copy to */
672 PERL_ARGS_ASSERT_DELIMCPY;
673 assert(from_end >= from);
674 assert(to_end >= to);
676 /* Don't use the loop for the trivial case of the first character being the
677 * delimiter; otherwise would have to worry inside the loop about backing
678 * up before the start of 'from' */
679 if (LIKELY(from_end > from && *from != delim)) {
680 while ((copy_len = from_end - from) > 0) {
681 const char * backslash_pos;
682 const char * delim_pos;
684 /* Look for the next delimiter in the remaining portion of the
685 * source. A loop invariant is that we already know that the copy
686 * should include *from; this comes from the conditional before the
687 * loop, and how we set things up at the end of each iteration */
688 delim_pos = (const char *) memchr(from + 1, delim, copy_len - 1);
690 /* If didn't find it, done looking; set up so copies all of the
693 copy_len = from_end - from;
697 /* Look for a backslash immediately before the delimiter */
698 backslash_pos = delim_pos - 1;
700 /* If the delimiter is not escaped, this ends the copy */
701 if (*backslash_pos != '\\') {
702 copy_len = delim_pos - from;
706 /* Here there is a backslash just before the delimiter, but it
707 * could be the final backslash in a sequence of them. Backup to
708 * find the first one in it. */
712 while (backslash_pos >= from && *backslash_pos == '\\');
714 /* If the number of backslashes is even, they just escape one
715 * another, leaving the delimiter unescaped, and stopping the copy.
717 if (! ((delim_pos - (backslash_pos + 1)) & 1)) {
718 copy_len = delim_pos - from; /* even, copy up to delimiter */
722 /* Here is odd, so the delimiter is escaped. We will try to copy
723 * all but the final backslash in the sequence */
724 copy_len = delim_pos - 1 - from;
726 /* Do the copy, but not beyond the end of the destination */
727 if (copy_len >= to_end - to) {
728 Copy(from, to, to_end - to, char);
729 stopped_early = TRUE;
730 to = (char *) to_end;
733 Copy(from, to, copy_len, char);
737 /* Set up so next iteration will include the delimiter */
742 /* Here, have found the final segment to copy. Copy that, but not beyond
743 * the size of the destination. If not enough room, copy as much as can
744 * fit, and set error return */
745 if (stopped_early || copy_len > to_end - to) {
746 Copy(from, to, to_end - to, char);
747 *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
750 Copy(from, to, copy_len, char);
754 /* If there is extra space available, add a trailing NUL */
759 *retlen = to - orig_to;
762 return (char *) from + copy_len;
768 Find the first (leftmost) occurrence of a sequence of bytes within another
769 sequence. This is the Perl version of C<strstr()>, extended to handle
770 arbitrary sequences, potentially containing embedded C<NUL> characters (C<NUL>
771 is what the initial C<n> in the function name stands for; some systems have an
772 equivalent, C<memmem()>, but with a somewhat different API).
774 Another way of thinking about this function is finding a needle in a haystack.
775 C<big> points to the first byte in the haystack. C<big_end> points to one byte
776 beyond the final byte in the haystack. C<little> points to the first byte in
777 the needle. C<little_end> points to one byte beyond the final byte in the
778 needle. All the parameters must be non-C<NULL>.
780 The function returns C<NULL> if there is no occurrence of C<little> within
781 C<big>. If C<little> is the empty string, C<big> is returned.
783 Because this function operates at the byte level, and because of the inherent
784 characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the
785 needle and the haystack are strings with the same UTF-8ness, but not if the
793 Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend)
795 PERL_ARGS_ASSERT_NINSTR;
798 return ninstr(big, bigend, little, lend);
801 if (little >= lend) {
805 const U8 first = *little;
808 /* No match can start closer to the end of the haystack than the length
810 bigend -= lend - little;
811 little++; /* Look for 'first', then the remainder is in here */
812 lsize = lend - little;
814 while (big <= bigend) {
815 big = (char *) memchr((U8 *) big, first, bigend - big + 1);
816 if (big == NULL || big > bigend) {
820 if (memEQ(big + 1, little, lsize)) {
836 Like C<L</ninstr>>, but instead finds the final (rightmost) occurrence of a
837 sequence of bytes within another sequence, returning C<NULL> if there is no
845 Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend)
847 const Ptrdiff_t little_len = lend - little;
848 const Ptrdiff_t big_len = bigend - big;
850 PERL_ARGS_ASSERT_RNINSTR;
852 /* A non-existent needle trivially matches the rightmost possible position
854 if (UNLIKELY(little_len <= 0)) {
855 return (char*)bigend;
858 /* If the needle is larger than the haystack, the needle can't possibly fit
859 * inside the haystack. */
860 if (UNLIKELY(little_len > big_len)) {
864 /* Special case length 1 needles. It's trivial if we have memrchr();
865 * and otherwise we just do a per-byte search backwards.
867 * XXX When we don't have memrchr, we could use something like
868 * S_find_next_masked( or S_find_span_end() to do per-word searches */
869 if (little_len == 1) {
870 const char final = *little;
874 return (char *) memrchr(big, final, big_len);
876 const char * cur = bigend - 1;
882 } while (--cur >= big);
888 else { /* Below, the needle is longer than a single byte */
890 /* We search backwards in the haystack for the final character of the
891 * needle. Each time one is found, we see if the characters just
892 * before it in the haystack match the rest of the needle. */
893 const char final = *(lend - 1);
895 /* What matches consists of 'little_len'-1 characters, then the final
897 const Size_t prefix_len = little_len - 1;
899 /* If the final character in the needle is any closer than this to the
900 * left edge, there wouldn't be enough room for all of it to fit in the
902 const char * const left_fence = big + prefix_len;
904 /* Start at the right edge */
905 char * cur = (char *) bigend;
907 /* memrchr() makes the search easy (and fast); otherwise, look
908 * backwards byte-by-byte. */
913 cur = (char *) memrchr(left_fence, final, cur - left_fence);
920 if (cur < left_fence) {
924 while (*cur != final);
927 /* Here, we know that *cur is 'final'; see if the preceding bytes
928 * of the needle also match the corresponding haystack bytes */
929 if memEQ(cur - prefix_len, little, prefix_len) {
930 return cur - prefix_len;
932 } while (cur > left_fence);
938 /* As a space optimization, we do not compile tables for strings of length
939 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
940 special-cased in fbm_instr().
942 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
946 =for apidoc fbm_compile
948 Analyzes the string in order to make fast searches on it using C<fbm_instr()>
949 -- the Boyer-Moore algorithm.
955 Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
962 PERL_ARGS_ASSERT_FBM_COMPILE;
964 if (isGV_with_GP(sv) || SvROK(sv))
970 if (flags & FBMcf_TAIL) {
971 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
972 sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */
973 if (mg && mg->mg_len >= 0)
976 if (!SvPOK(sv) || SvNIOKp(sv))
977 s = (U8*)SvPV_force_mutable(sv, len);
978 else s = (U8 *)SvPV_mutable(sv, len);
979 if (len == 0) /* TAIL might be on a zero-length string. */
981 SvUPGRADE(sv, SVt_PVMG);
985 /* add PERL_MAGIC_bm magic holding the FBM lookup table */
987 assert(!mg_find(sv, PERL_MAGIC_bm));
988 mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0);
992 /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use
994 const U8 mlen = (len>255) ? 255 : (U8)len;
995 const unsigned char *const sb = s + len - mlen; /* first char (maybe) */
998 Newx(table, 256, U8);
999 memset((void*)table, mlen, 256);
1000 mg->mg_ptr = (char *)table;
1003 s += len - 1; /* last char */
1006 if (table[*s] == mlen)
1012 BmUSEFUL(sv) = 100; /* Initial value */
1013 ((XPVNV*)SvANY(sv))->xnv_u.xnv_bm_tail = cBOOL(flags & FBMcf_TAIL);
1018 =for apidoc fbm_instr
1020 Returns the location of the SV in the string delimited by C<big> and
1021 C<bigend> (C<bigend>) is the char following the last char).
1022 It returns C<NULL> if the string can't be found. The C<sv>
1023 does not have to be C<fbm_compiled>, but the search will not be as fast
1028 If SvTAIL(littlestr) is true, a fake "\n" was appended to the string
1029 during FBM compilation due to FBMcf_TAIL in flags. It indicates that
1030 the littlestr must be anchored to the end of bigstr (or to any \n if
1033 E.g. The regex compiler would compile /abc/ to a littlestr of "abc",
1034 while /abc$/ compiles to "abc\n" with SvTAIL() true.
1036 A littlestr of "abc", !SvTAIL matches as /abc/;
1037 a littlestr of "ab\n", SvTAIL matches as:
1038 without FBMrf_MULTILINE: /ab\n?\z/
1039 with FBMrf_MULTILINE: /ab\n/ || /ab\z/;
1041 (According to Ilya from 1999; I don't know if this is still true, DAPM 2015):
1042 "If SvTAIL is actually due to \Z or \z, this gives false positives
1048 Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags)
1052 const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l);
1053 STRLEN littlelen = l;
1054 const I32 multiline = flags & FBMrf_MULTILINE;
1055 bool valid = SvVALID(littlestr);
1056 bool tail = valid ? cBOOL(SvTAIL(littlestr)) : FALSE;
1058 PERL_ARGS_ASSERT_FBM_INSTR;
1060 assert(bigend >= big);
1062 if ((STRLEN)(bigend - big) < littlelen) {
1064 && ((STRLEN)(bigend - big) == littlelen - 1)
1066 || (*big == *little &&
1067 memEQ((char *)big, (char *)little, littlelen - 1))))
1072 switch (littlelen) { /* Special cases for 0, 1 and 2 */
1074 return (char*)big; /* Cannot be SvTAIL! */
1077 if (tail && !multiline) /* Anchor only! */
1078 /* [-1] is safe because we know that bigend != big. */
1079 return (char *) (bigend - (bigend[-1] == '\n'));
1081 s = (unsigned char *)memchr((void*)big, *little, bigend-big);
1085 return (char *) bigend;
1089 if (tail && !multiline) {
1090 /* a littlestr with SvTAIL must be of the form "X\n" (where X
1091 * is a single char). It is anchored, and can only match
1092 * "....X\n" or "....X" */
1093 if (bigend[-2] == *little && bigend[-1] == '\n')
1094 return (char*)bigend - 2;
1095 if (bigend[-1] == *little)
1096 return (char*)bigend - 1;
1101 /* memchr() is likely to be very fast, possibly using whatever
1102 * hardware support is available, such as checking a whole
1103 * cache line in one instruction.
1104 * So for a 2 char pattern, calling memchr() is likely to be
1105 * faster than running FBM, or rolling our own. The previous
1106 * version of this code was roll-your-own which typically
1107 * only needed to read every 2nd char, which was good back in
1108 * the day, but no longer.
1110 unsigned char c1 = little[0];
1111 unsigned char c2 = little[1];
1113 /* *** for all this case, bigend points to the last char,
1114 * not the trailing \0: this makes the conditions slightly
1119 while (s < bigend) {
1120 /* do a quick test for c1 before calling memchr();
1121 * this avoids the expensive fn call overhead when
1122 * there are lots of c1's */
1123 if (LIKELY(*s != c1)) {
1125 s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1132 /* failed; try searching for c2 this time; that way
1133 * we don't go pathologically slow when the string
1134 * consists mostly of c1's or vice versa.
1139 s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1);
1143 return (char*)s - 1;
1147 /* c1, c2 the same */
1148 while (s < bigend) {
1157 s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1158 if (!s || s >= bigend)
1165 /* failed to find 2 chars; try anchored match at end without
1167 if (tail && bigend[0] == little[0])
1168 return (char *)bigend;
1173 break; /* Only lengths 0 1 and 2 have special-case code. */
1176 if (tail && !multiline) { /* tail anchored? */
1177 s = bigend - littlelen;
1178 if (s >= big && bigend[-1] == '\n' && *s == *little
1179 /* Automatically of length > 2 */
1180 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
1182 return (char*)s; /* how sweet it is */
1185 && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
1187 return (char*)s + 1; /* how sweet it is */
1193 /* not compiled; use Perl_ninstr() instead */
1194 char * const b = ninstr((char*)big,(char*)bigend,
1195 (char*)little, (char*)little + littlelen);
1197 assert(!tail); /* valid => FBM; tail only set on SvVALID SVs */
1201 /* Do actual FBM. */
1202 if (littlelen > (STRLEN)(bigend - big))
1206 const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm);
1207 const unsigned char *oldlittle;
1211 --littlelen; /* Last char found by table lookup */
1213 s = big + littlelen;
1214 little += littlelen; /* last char */
1217 const unsigned char * const table = (const unsigned char *) mg->mg_ptr;
1218 const unsigned char lastc = *little;
1222 if ((tmp = table[*s])) {
1223 /* *s != lastc; earliest position it could match now is
1224 * tmp slots further on */
1225 if ((s += tmp) >= bigend)
1227 if (LIKELY(*s != lastc)) {
1229 s = (unsigned char *)memchr((void*)s, lastc, bigend - s);
1239 /* hand-rolled strncmp(): less expensive than calling the
1240 * real function (maybe???) */
1242 unsigned char * const olds = s;
1247 if (*--s == *--little)
1249 s = olds + 1; /* here we pay the price for failure */
1251 if (s < bigend) /* fake up continue to outer loop */
1261 && memEQ((char *)(bigend - littlelen),
1262 (char *)(oldlittle - littlelen), littlelen) )
1263 return (char*)bigend - littlelen;
1269 Perl_cntrl_to_mnemonic(const U8 c)
1271 /* Returns the mnemonic string that represents character 'c', if one
1272 * exists; NULL otherwise. The only ones that exist for the purposes of
1273 * this routine are a few control characters */
1276 case '\a': return "\\a";
1277 case '\b': return "\\b";
1278 case ESC_NATIVE: return "\\e";
1279 case '\f': return "\\f";
1280 case '\n': return "\\n";
1281 case '\r': return "\\r";
1282 case '\t': return "\\t";
1288 /* copy a string to a safe spot */
1291 =for apidoc_section String Handling
1294 Perl's version of C<strdup()>. Returns a pointer to a newly allocated
1295 string which is a duplicate of C<pv>. The size of the string is
1296 determined by C<strlen()>, which means it may not contain embedded C<NUL>
1297 characters and must have a trailing C<NUL>. To prevent memory leaks, the
1298 memory allocated for the new string needs to be freed when no longer needed.
1299 This can be done with the C<L</Safefree>> function, or
1300 L<C<SAVEFREEPV>|perlguts/SAVEFREEPV(p)>.
1302 On some platforms, Windows for example, all allocated memory owned by a thread
1303 is deallocated when that thread ends. So if you need that not to happen, you
1304 need to use the shared memory functions, such as C<L</savesharedpv>>.
1310 Perl_savepv(pTHX_ const char *pv)
1312 PERL_UNUSED_CONTEXT;
1317 const STRLEN pvlen = strlen(pv)+1;
1318 Newx(newaddr, pvlen, char);
1319 return (char*)memcpy(newaddr, pv, pvlen);
1323 /* same thing but with a known length */
1328 Perl's version of what C<strndup()> would be if it existed. Returns a
1329 pointer to a newly allocated string which is a duplicate of the first
1330 C<len> bytes from C<pv>, plus a trailing
1331 C<NUL> byte. The memory allocated for
1332 the new string can be freed with the C<Safefree()> function.
1334 On some platforms, Windows for example, all allocated memory owned by a thread
1335 is deallocated when that thread ends. So if you need that not to happen, you
1336 need to use the shared memory functions, such as C<L</savesharedpvn>>.
1342 Perl_savepvn(pTHX_ const char *pv, Size_t len)
1345 PERL_UNUSED_CONTEXT;
1347 Newx(newaddr,len+1,char);
1348 /* Give a meaning to NULL pointer mainly for the use in sv_magic() */
1350 /* might not be null terminated */
1351 newaddr[len] = '\0';
1352 return (char *) CopyD(pv,newaddr,len,char);
1355 return (char *) ZeroD(newaddr,len+1,char);
1360 =for apidoc savesharedpv
1362 A version of C<savepv()> which allocates the duplicate string in memory
1363 which is shared between threads.
1368 Perl_savesharedpv(pTHX_ const char *pv)
1373 PERL_UNUSED_CONTEXT;
1378 pvlen = strlen(pv)+1;
1379 newaddr = (char*)PerlMemShared_malloc(pvlen);
1383 return (char*)memcpy(newaddr, pv, pvlen);
1387 =for apidoc savesharedpvn
1389 A version of C<savepvn()> which allocates the duplicate string in memory
1390 which is shared between threads. (With the specific difference that a C<NULL>
1391 pointer is not acceptable)
1396 Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len)
1398 char *const newaddr = (char*)PerlMemShared_malloc(len + 1);
1400 PERL_UNUSED_CONTEXT;
1401 /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */
1406 newaddr[len] = '\0';
1407 return (char*)memcpy(newaddr, pv, len);
1411 =for apidoc savesvpv
1413 A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from
1414 the passed in SV using C<SvPV()>
1416 On some platforms, Windows for example, all allocated memory owned by a thread
1417 is deallocated when that thread ends. So if you need that not to happen, you
1418 need to use the shared memory functions, such as C<L</savesharedsvpv>>.
1424 Perl_savesvpv(pTHX_ SV *sv)
1427 const char * const pv = SvPV_const(sv, len);
1430 PERL_ARGS_ASSERT_SAVESVPV;
1433 Newx(newaddr,len,char);
1434 return (char *) CopyD(pv,newaddr,len,char);
1438 =for apidoc savesharedsvpv
1440 A version of C<savesharedpv()> which allocates the duplicate string in
1441 memory which is shared between threads.
1447 Perl_savesharedsvpv(pTHX_ SV *sv)
1450 const char * const pv = SvPV_const(sv, len);
1452 PERL_ARGS_ASSERT_SAVESHAREDSVPV;
1454 return savesharedpvn(pv, len);
1457 /* the SV for Perl_form() and mess() is not kept in an arena */
1465 if (PL_phase != PERL_PHASE_DESTRUCT)
1466 return newSVpvs_flags("", SVs_TEMP);
1471 /* Create as PVMG now, to avoid any upgrading later */
1473 Newxz(any, 1, XPVMG);
1474 SvFLAGS(sv) = SVt_PVMG;
1475 SvANY(sv) = (void*)any;
1477 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1482 #if defined(PERL_IMPLICIT_CONTEXT)
1484 Perl_form_nocontext(const char* pat, ...)
1489 PERL_ARGS_ASSERT_FORM_NOCONTEXT;
1490 va_start(args, pat);
1491 retval = vform(pat, &args);
1495 #endif /* PERL_IMPLICIT_CONTEXT */
1498 =for apidoc_section Display and Dump functions
1500 =for apidoc_item form_nocontext
1502 These take a sprintf-style format pattern and conventional
1503 (non-SV) arguments and return the formatted string.
1505 (char *) Perl_form(pTHX_ const char* pat, ...)
1507 can be used any place a string (char *) is required:
1509 char * s = Perl_form("%d.%d",major,minor);
1511 They use a single private buffer so if you want to format several strings you
1512 must explicitly copy the earlier strings away (and free the copies when you
1515 The two forms differ only in that C<form_nocontext> does not take a thread
1516 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1517 already have the thread context.
1520 Like C<L</form>> but but the arguments are an encapsulated argument list.
1526 Perl_form(pTHX_ const char* pat, ...)
1530 PERL_ARGS_ASSERT_FORM;
1531 va_start(args, pat);
1532 retval = vform(pat, &args);
1538 Perl_vform(pTHX_ const char *pat, va_list *args)
1540 SV * const sv = mess_alloc();
1541 PERL_ARGS_ASSERT_VFORM;
1542 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1548 =for apidoc_item mess_nocontext
1550 These take a sprintf-style format pattern and argument list, which are used to
1551 generate a string message. If the message does not end with a newline, then it
1552 will be extended with some indication of the current location in the code, as
1553 described for C<L</mess_sv>>.
1555 Normally, the resulting message is returned in a new mortal SV.
1556 But during global destruction a single SV may be shared between uses of
1559 The two forms differ only in that C<mess_nocontext> does not take a thread
1560 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1561 already have the thread context.
1566 #if defined(PERL_IMPLICIT_CONTEXT)
1568 Perl_mess_nocontext(const char *pat, ...)
1573 PERL_ARGS_ASSERT_MESS_NOCONTEXT;
1574 va_start(args, pat);
1575 retval = vmess(pat, &args);
1579 #endif /* PERL_IMPLICIT_CONTEXT */
1582 Perl_mess(pTHX_ const char *pat, ...)
1586 PERL_ARGS_ASSERT_MESS;
1587 va_start(args, pat);
1588 retval = vmess(pat, &args);
1594 Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop,
1597 /* Look for curop starting from o. cop is the last COP we've seen. */
1598 /* opnext means that curop is actually the ->op_next of the op we are
1601 PERL_ARGS_ASSERT_CLOSEST_COP;
1603 if (!o || !curop || (
1604 opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop
1608 if (o->op_flags & OPf_KIDS) {
1610 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1613 /* If the OP_NEXTSTATE has been optimised away we can still use it
1614 * the get the file and line number. */
1616 if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
1617 cop = (const COP *)kid;
1619 /* Keep searching, and return when we've found something. */
1621 new_cop = closest_cop(cop, kid, curop, opnext);
1627 /* Nothing found. */
1635 Expands a message, intended for the user, to include an indication of
1636 the current location in the code, if the message does not already appear
1639 C<basemsg> is the initial message or object. If it is a reference, it
1640 will be used as-is and will be the result of this function. Otherwise it
1641 is used as a string, and if it already ends with a newline, it is taken
1642 to be complete, and the result of this function will be the same string.
1643 If the message does not end with a newline, then a segment such as C<at
1644 foo.pl line 37> will be appended, and possibly other clauses indicating
1645 the current state of execution. The resulting message will end with a
1648 Normally, the resulting message is returned in a new mortal SV.
1649 During global destruction a single SV may be shared between uses of this
1650 function. If C<consume> is true, then the function is permitted (but not
1651 required) to modify and return C<basemsg> instead of allocating a new SV.
1657 Perl_mess_sv(pTHX_ SV *basemsg, bool consume)
1661 #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR)
1665 /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */
1666 if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR"))
1667 && grok_atoUV(ws, &wi, NULL)
1668 && wi <= PERL_INT_MAX
1670 Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1);
1675 PERL_ARGS_ASSERT_MESS_SV;
1677 if (SvROK(basemsg)) {
1683 sv_setsv(sv, basemsg);
1688 if (SvPOK(basemsg) && consume) {
1693 sv_copypv(sv, basemsg);
1696 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1698 * Try and find the file and line for PL_op. This will usually be
1699 * PL_curcop, but it might be a cop that has been optimised away. We
1700 * can try to find such a cop by searching through the optree starting
1701 * from the sibling of PL_curcop.
1706 closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE);
1711 Perl_sv_catpvf(aTHX_ sv, " at %s line %" IVdf,
1712 OutCopFILE(cop), (IV)CopLINE(cop));
1715 /* Seems that GvIO() can be untrustworthy during global destruction. */
1716 if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO)
1717 && IoLINES(GvIOp(PL_last_in_gv)))
1720 const bool line_mode = (RsSIMPLE(PL_rs) &&
1721 *SvPV_const(PL_rs,l) == '\n' && l == 1);
1722 Perl_sv_catpvf(aTHX_ sv, ", <%" SVf "> %s %" IVdf,
1723 SVfARG(PL_last_in_gv == PL_argvgv
1725 : sv_2mortal(newSVhek(GvNAME_HEK(PL_last_in_gv)))),
1726 line_mode ? "line" : "chunk",
1727 (IV)IoLINES(GvIOp(PL_last_in_gv)));
1729 if (PL_phase == PERL_PHASE_DESTRUCT)
1730 sv_catpvs(sv, " during global destruction");
1731 sv_catpvs(sv, ".\n");
1739 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1740 argument list, respectively. These are used to generate a string message. If
1742 message does not end with a newline, then it will be extended with
1743 some indication of the current location in the code, as described for
1746 Normally, the resulting message is returned in a new mortal SV.
1747 During global destruction a single SV may be shared between uses of
1754 Perl_vmess(pTHX_ const char *pat, va_list *args)
1756 SV * const sv = mess_alloc();
1758 PERL_ARGS_ASSERT_VMESS;
1760 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1761 return mess_sv(sv, 1);
1765 Perl_write_to_stderr(pTHX_ SV* msv)
1770 PERL_ARGS_ASSERT_WRITE_TO_STDERR;
1772 if (PL_stderrgv && SvREFCNT(PL_stderrgv)
1773 && (io = GvIO(PL_stderrgv))
1774 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1775 Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT),
1776 G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv);
1778 PerlIO * const serr = Perl_error_log;
1780 do_print(msv, serr);
1781 (void)PerlIO_flush(serr);
1786 =for apidoc_section Warning and Dieing
1789 /* Common code used in dieing and warning */
1792 S_with_queued_errors(pTHX_ SV *ex)
1794 PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS;
1795 if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) {
1796 sv_catsv(PL_errors, ex);
1797 ex = sv_mortalcopy(PL_errors);
1798 SvCUR_set(PL_errors, 0);
1804 S_invoke_exception_hook(pTHX_ SV *ex, bool warn)
1809 SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1810 /* sv_2cv might call Perl_croak() or Perl_warner() */
1811 SV * const oldhook = *hook;
1813 if (!oldhook || oldhook == PERL_WARNHOOK_FATAL)
1819 cv = sv_2cv(oldhook, &stash, &gv, 0);
1821 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1831 exarg = newSVsv(ex);
1832 SvREADONLY_on(exarg);
1835 PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
1839 call_sv(MUTABLE_SV(cv), G_DISCARD);
1849 =for apidoc_item die_nocontext
1851 These ehave the same as L</croak_sv>, except for the return type.
1852 It should be used only where the C<OP *> return type is required.
1853 The functions never actually return.
1855 The two forms differ only in that C<die_nocontext> does not take a thread
1856 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1857 already have the thread context.
1862 /* silence __declspec(noreturn) warnings */
1863 MSVC_DIAG_IGNORE(4646 4645)
1865 Perl_die_sv(pTHX_ SV *baseex)
1867 PERL_ARGS_ASSERT_DIE_SV;
1870 NORETURN_FUNCTION_END;
1877 Behaves the same as L</croak>, except for the return type.
1878 It should be used only where the C<OP *> return type is required.
1879 The function never actually returns.
1884 #if defined(PERL_IMPLICIT_CONTEXT)
1886 /* silence __declspec(noreturn) warnings */
1887 MSVC_DIAG_IGNORE(4646 4645)
1889 Perl_die_nocontext(const char* pat, ...)
1893 va_start(args, pat);
1895 NOT_REACHED; /* NOTREACHED */
1897 NORETURN_FUNCTION_END;
1901 #endif /* PERL_IMPLICIT_CONTEXT */
1903 /* silence __declspec(noreturn) warnings */
1904 MSVC_DIAG_IGNORE(4646 4645)
1906 Perl_die(pTHX_ const char* pat, ...)
1909 va_start(args, pat);
1911 NOT_REACHED; /* NOTREACHED */
1913 NORETURN_FUNCTION_END;
1918 =for apidoc croak_sv
1920 This is an XS interface to Perl's C<die> function.
1922 C<baseex> is the error message or object. If it is a reference, it
1923 will be used as-is. Otherwise it is used as a string, and if it does
1924 not end with a newline then it will be extended with some indication of
1925 the current location in the code, as described for L</mess_sv>.
1927 The error message or object will be used as an exception, by default
1928 returning control to the nearest enclosing C<eval>, but subject to
1929 modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv>
1930 function never returns normally.
1932 To die with a simple string message, the L</croak> function may be
1939 Perl_croak_sv(pTHX_ SV *baseex)
1941 SV *ex = with_queued_errors(mess_sv(baseex, 0));
1942 PERL_ARGS_ASSERT_CROAK_SV;
1943 invoke_exception_hook(ex, FALSE);
1950 This is an XS interface to Perl's C<die> function.
1952 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1953 argument list. These are used to generate a string message. If the
1954 message does not end with a newline, then it will be extended with
1955 some indication of the current location in the code, as described for
1958 The error message will be used as an exception, by default
1959 returning control to the nearest enclosing C<eval>, but subject to
1960 modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak>
1961 function never returns normally.
1963 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1964 (C<$@>) will be used as an error message or object instead of building an
1965 error message from arguments. If you want to throw a non-string object,
1966 or build an error message in an SV yourself, it is preferable to use
1967 the L</croak_sv> function, which does not involve clobbering C<ERRSV>.
1973 Perl_vcroak(pTHX_ const char* pat, va_list *args)
1975 SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0));
1976 invoke_exception_hook(ex, FALSE);
1982 =for apidoc_item croak_nocontext
1984 These are XS interfaces to Perl's C<die> function.
1986 They take a sprintf-style format pattern and argument list, which are used to
1987 generate a string message. If the message does not end with a newline, then it
1988 will be extended with some indication of the current location in the code, as
1989 described for C<L</mess_sv>>.
1991 The error message will be used as an exception, by default
1992 returning control to the nearest enclosing C<eval>, but subject to
1993 modification by a C<$SIG{__DIE__}> handler. In any case, these croak
1994 functions never return normally.
1996 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1997 (C<$@>) will be used as an error message or object instead of building an
1998 error message from arguments. If you want to throw a non-string object,
1999 or build an error message in an SV yourself, it is preferable to use
2000 the C<L</croak_sv>> function, which does not involve clobbering C<ERRSV>.
2002 The two forms differ only in that C<croak_nocontext> does not take a thread
2003 context (C<aTHX>) parameter. It is usually preferred as it takes up fewer
2004 bytes of code than plain C<Perl_croak>, and time is rarely a critical resource
2005 when you are about to throw an exception.
2010 #if defined(PERL_IMPLICIT_CONTEXT)
2012 Perl_croak_nocontext(const char *pat, ...)
2016 va_start(args, pat);
2018 NOT_REACHED; /* NOTREACHED */
2021 #endif /* PERL_IMPLICIT_CONTEXT */
2023 /* saves machine code for a common noreturn idiom typically used in Newx*() */
2024 GCC_DIAG_IGNORE_DECL(-Wunused-function);
2026 Perl_croak_memory_wrap(void)
2028 Perl_croak_nocontext("%s",PL_memory_wrap);
2030 GCC_DIAG_RESTORE_DECL;
2033 Perl_croak(pTHX_ const char *pat, ...)
2036 va_start(args, pat);
2038 NOT_REACHED; /* NOTREACHED */
2043 =for apidoc croak_no_modify
2045 This encapsulates a common reason for dying, generating terser object code than
2046 using the generic C<Perl_croak>. It is exactly equivalent to
2047 C<Perl_croak(aTHX_ "%s", PL_no_modify)> (which expands to something like
2048 "Modification of a read-only value attempted").
2050 Less code used on exception code paths reduces CPU cache pressure.
2056 Perl_croak_no_modify(void)
2058 Perl_croak_nocontext( "%s", PL_no_modify);
2061 /* does not return, used in util.c perlio.c and win32.c
2062 This is typically called when malloc returns NULL.
2065 Perl_croak_no_mem(void)
2069 int fd = PerlIO_fileno(Perl_error_log);
2071 SETERRNO(EBADF,RMS_IFI);
2073 /* Can't use PerlIO to write as it allocates memory */
2074 PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1));
2079 /* does not return, used only in POPSTACK */
2081 Perl_croak_popstack(void)
2084 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");
2091 This is an XS interface to Perl's C<warn> function.
2093 C<baseex> is the error message or object. If it is a reference, it
2094 will be used as-is. Otherwise it is used as a string, and if it does
2095 not end with a newline then it will be extended with some indication of
2096 the current location in the code, as described for L</mess_sv>.
2098 The error message or object will by default be written to standard error,
2099 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2101 To warn with a simple string message, the L</warn> function may be
2108 Perl_warn_sv(pTHX_ SV *baseex)
2110 SV *ex = mess_sv(baseex, 0);
2111 PERL_ARGS_ASSERT_WARN_SV;
2112 if (!invoke_exception_hook(ex, TRUE))
2113 write_to_stderr(ex);
2119 This is an XS interface to Perl's C<warn> function.
2121 This is like C<L</warn>>, but C<args> are an encapsulated
2124 Unlike with L</vcroak>, C<pat> is not permitted to be null.
2130 Perl_vwarn(pTHX_ const char* pat, va_list *args)
2132 SV *ex = vmess(pat, args);
2133 PERL_ARGS_ASSERT_VWARN;
2134 if (!invoke_exception_hook(ex, TRUE))
2135 write_to_stderr(ex);
2140 =for apidoc_item warn_nocontext
2142 These are XS interfaces to Perl's C<warn> function.
2144 They take a sprintf-style format pattern and argument list, which are used to
2145 generate a string message. If the message does not end with a newline, then it
2146 will be extended with some indication of the current location in the code, as
2147 described for C<L</mess_sv>>.
2149 The error message or object will by default be written to standard error,
2150 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2152 Unlike with C<L</croak>>, C<pat> is not permitted to be null.
2154 The two forms differ only in that C<warn_nocontext> does not take a thread
2155 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2156 already have the thread context.
2161 #if defined(PERL_IMPLICIT_CONTEXT)
2163 Perl_warn_nocontext(const char *pat, ...)
2167 PERL_ARGS_ASSERT_WARN_NOCONTEXT;
2168 va_start(args, pat);
2172 #endif /* PERL_IMPLICIT_CONTEXT */
2175 Perl_warn(pTHX_ const char *pat, ...)
2178 PERL_ARGS_ASSERT_WARN;
2179 va_start(args, pat);
2186 =for apidoc_item warner_nocontext
2188 These output a warning of the specified category (or categories) given by
2189 C<err>, using the sprintf-style format pattern C<pat>, and argument list.
2191 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2192 C<packWARN4> macros populated with the appropriate number of warning
2193 categories. If any of the warning categories they specify is fatal, a fatal
2194 exception is thrown.
2196 In any event a message is generated by the pattern and arguments. If the
2197 message does not end with a newline, then it will be extended with some
2198 indication of the current location in the code, as described for L</mess_sv>.
2200 The error message or object will by default be written to standard error,
2201 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2203 C<pat> is not permitted to be null.
2205 The two forms differ only in that C<warner_nocontext> does not take a thread
2206 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2207 already have the thread context.
2209 These functions differ from the similarly named C<L</warn>> functions, in that
2210 the latter are for XS code to unconditionally display a warning, whereas these
2211 are for code that may be compiling a perl program, and does extra checking to
2212 see if the warning should be fatal.
2214 =for apidoc ck_warner
2215 =for apidoc_item ck_warner_d
2216 If none of the warning categories given by C<err> are enabled, do nothing;
2217 otherwise call C<L</warner>> or C<L</warner_nocontext>> with the passed-in
2220 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2221 C<packWARN4> macros populated with the appropriate number of warning
2224 The two forms differ only in that C<ck_warner_d> should be used if warnings for
2225 any of the categories are by default enabled.
2228 This is like C<L</warner>>, but C<args> are an encapsulated argument list.
2233 #if defined(PERL_IMPLICIT_CONTEXT)
2235 Perl_warner_nocontext(U32 err, const char *pat, ...)
2239 PERL_ARGS_ASSERT_WARNER_NOCONTEXT;
2240 va_start(args, pat);
2241 vwarner(err, pat, &args);
2244 #endif /* PERL_IMPLICIT_CONTEXT */
2247 Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...)
2249 PERL_ARGS_ASSERT_CK_WARNER_D;
2251 if (Perl_ckwarn_d(aTHX_ err)) {
2253 va_start(args, pat);
2254 vwarner(err, pat, &args);
2260 Perl_ck_warner(pTHX_ U32 err, const char* pat, ...)
2262 PERL_ARGS_ASSERT_CK_WARNER;
2264 if (Perl_ckwarn(aTHX_ err)) {
2266 va_start(args, pat);
2267 vwarner(err, pat, &args);
2273 Perl_warner(pTHX_ U32 err, const char* pat,...)
2276 PERL_ARGS_ASSERT_WARNER;
2277 va_start(args, pat);
2278 vwarner(err, pat, &args);
2283 Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args)
2285 PERL_ARGS_ASSERT_VWARNER;
2287 (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) &&
2288 !(PL_in_eval & EVAL_KEEPERR)
2290 SV * const msv = vmess(pat, args);
2292 if (PL_parser && PL_parser->error_count) {
2296 invoke_exception_hook(msv, FALSE);
2301 Perl_vwarn(aTHX_ pat, args);
2305 /* implements the ckWARN? macros */
2308 Perl_ckwarn(pTHX_ U32 w)
2310 /* If lexical warnings have not been set, use $^W. */
2312 return PL_dowarn & G_WARN_ON;
2314 return ckwarn_common(w);
2317 /* implements the ckWARN?_d macro */
2320 Perl_ckwarn_d(pTHX_ U32 w)
2322 /* If lexical warnings have not been set then default classes warn. */
2326 return ckwarn_common(w);
2330 S_ckwarn_common(pTHX_ U32 w)
2332 if (PL_curcop->cop_warnings == pWARN_ALL)
2335 if (PL_curcop->cop_warnings == pWARN_NONE)
2338 /* Check the assumption that at least the first slot is non-zero. */
2339 assert(unpackWARN1(w));
2341 /* Check the assumption that it is valid to stop as soon as a zero slot is
2343 if (!unpackWARN2(w)) {
2344 assert(!unpackWARN3(w));
2345 assert(!unpackWARN4(w));
2346 } else if (!unpackWARN3(w)) {
2347 assert(!unpackWARN4(w));
2350 /* Right, dealt with all the special cases, which are implemented as non-
2351 pointers, so there is a pointer to a real warnings mask. */
2353 if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w)))
2355 } while (w >>= WARNshift);
2360 /* Set buffer=NULL to get a new one. */
2362 Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits,
2364 const MEM_SIZE len_wanted =
2365 sizeof(STRLEN) + (size > WARNsize ? size : WARNsize);
2366 PERL_UNUSED_CONTEXT;
2367 PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD;
2370 (specialWARN(buffer) ?
2371 PerlMemShared_malloc(len_wanted) :
2372 PerlMemShared_realloc(buffer, len_wanted));
2374 Copy(bits, (buffer + 1), size, char);
2375 if (size < WARNsize)
2376 Zero((char *)(buffer + 1) + size, WARNsize - size, char);
2380 /* since we've already done strlen() for both nam and val
2381 * we can use that info to make things faster than
2382 * sprintf(s, "%s=%s", nam, val)
2384 #define my_setenv_format(s, nam, nlen, val, vlen) \
2385 Copy(nam, s, nlen, char); \
2387 Copy(val, s+(nlen+1), vlen, char); \
2388 *(s+(nlen+1+vlen)) = '\0'
2392 #ifdef USE_ENVIRON_ARRAY
2393 /* NB: VMS' my_setenv() is in vms.c */
2395 /* Configure doesn't test for HAS_SETENV yet, so decide based on platform.
2396 * For Solaris, setenv() and unsetenv() were introduced in Solaris 9, so
2397 * testing for HAS UNSETENV is sufficient.
2399 # if defined(__CYGWIN__)|| defined(__riscos__) || (defined(__sun) && defined(HAS_UNSETENV)) || defined(PERL_DARWIN)
2400 # define MY_HAS_SETENV
2403 /* small wrapper for use by Perl_my_setenv that mallocs, or reallocs if
2404 * 'current' is non-null, with up to three sizes that are added together.
2405 * It handles integer overflow.
2407 # ifndef MY_HAS_SETENV
2409 S_env_alloc(void *current, Size_t l1, Size_t l2, Size_t l3, Size_t size)
2412 Size_t sl, l = l1 + l2;
2424 ? safesysrealloc(current, sl)
2425 : safesysmalloc(sl);
2430 croak_memory_wrap();
2435 # if !defined(WIN32) && !defined(NETWARE)
2438 =for apidoc_section Utility Functions
2439 =for apidoc my_setenv
2441 A wrapper for the C library L<setenv(3)>. Don't use the latter, as the perl
2442 version has desirable safeguards
2448 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2450 # ifdef __amigaos4__
2451 amigaos4_obtain_environ(__FUNCTION__);
2454 # ifdef USE_ITHREADS
2455 /* only parent thread can modify process environment, so no need to use a
2457 if (PL_curinterp == aTHX)
2461 # ifndef PERL_USE_SAFE_PUTENV
2462 if (!PL_use_safe_putenv) {
2463 /* most putenv()s leak, so we manipulate environ directly */
2465 Size_t vlen, nlen = strlen(nam);
2467 /* where does it go? */
2468 for (i = 0; environ[i]; i++) {
2469 if (strnEQ(environ[i], nam, nlen) && environ[i][nlen] == '=')
2473 if (environ == PL_origenviron) { /* need we copy environment? */
2478 while (environ[max])
2481 /* XXX shouldn't that be max+1 rather than max+2 ??? - DAPM */
2482 tmpenv = (char**)S_env_alloc(NULL, max, 2, 0, sizeof(char*));
2484 for (j=0; j<max; j++) { /* copy environment */
2485 const Size_t len = strlen(environ[j]);
2486 tmpenv[j] = S_env_alloc(NULL, len, 1, 0, 1);
2487 Copy(environ[j], tmpenv[j], len+1, char);
2491 environ = tmpenv; /* tell exec where it is now */
2495 safesysfree(environ[i]);
2496 while (environ[i]) {
2497 environ[i] = environ[i+1];
2500 # ifdef __amigaos4__
2507 if (!environ[i]) { /* does not exist yet */
2508 environ = (char**)S_env_alloc(environ, i, 2, 0, sizeof(char*));
2509 environ[i+1] = NULL; /* make sure it's null terminated */
2512 safesysfree(environ[i]);
2516 environ[i] = S_env_alloc(NULL, nlen, vlen, 2, 1);
2517 /* all that work just for this */
2518 my_setenv_format(environ[i], nam, nlen, val, vlen);
2522 # endif /* !PERL_USE_SAFE_PUTENV */
2524 # ifdef MY_HAS_SETENV
2525 # if defined(HAS_UNSETENV)
2527 (void)unsetenv(nam);
2529 (void)setenv(nam, val, 1);
2531 # else /* ! HAS_UNSETENV */
2532 (void)setenv(nam, val, 1);
2533 # endif /* HAS_UNSETENV */
2535 # elif defined(HAS_UNSETENV)
2538 if (environ) /* old glibc can crash with null environ */
2539 (void)unsetenv(nam);
2541 const Size_t nlen = strlen(nam);
2542 const Size_t vlen = strlen(val);
2543 char * const new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2544 my_setenv_format(new_env, nam, nlen, val, vlen);
2545 (void)putenv(new_env);
2548 # else /* ! HAS_UNSETENV */
2551 const Size_t nlen = strlen(nam);
2557 new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2558 /* all that work just for this */
2559 my_setenv_format(new_env, nam, nlen, val, vlen);
2560 (void)putenv(new_env);
2562 # endif /* MY_HAS_SETENV */
2564 # ifndef PERL_USE_SAFE_PUTENV
2569 # ifdef __amigaos4__
2571 amigaos4_release_environ(__FUNCTION__);
2575 # else /* WIN32 || NETWARE */
2578 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2581 const Size_t nlen = strlen(nam);
2588 envstr = S_env_alloc(NULL, nlen, vlen, 2, 1);
2589 my_setenv_format(envstr, nam, nlen, val, vlen);
2590 (void)PerlEnv_putenv(envstr);
2591 safesysfree(envstr);
2594 # endif /* WIN32 || NETWARE */
2596 #endif /* USE_ENVIRON_ARRAY */
2601 #ifdef UNLINK_ALL_VERSIONS
2603 Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */
2607 PERL_ARGS_ASSERT_UNLNK;
2609 while (PerlLIO_unlink(f) >= 0)
2611 return retries ? 0 : -1;
2616 Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
2618 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(NETWARE) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2626 PERL_ARGS_ASSERT_MY_POPEN_LIST;
2628 PERL_FLUSHALL_FOR_CHILD;
2629 This = (*mode == 'w');
2633 taint_proper("Insecure %s%s", "EXEC");
2635 if (PerlProc_pipe_cloexec(p) < 0)
2637 /* Try for another pipe pair for error return */
2638 if (PerlProc_pipe_cloexec(pp) >= 0)
2640 while ((pid = PerlProc_fork()) < 0) {
2641 if (errno != EAGAIN) {
2642 PerlLIO_close(p[This]);
2643 PerlLIO_close(p[that]);
2645 PerlLIO_close(pp[0]);
2646 PerlLIO_close(pp[1]);
2650 Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2659 /* Close parent's end of error status pipe (if any) */
2661 PerlLIO_close(pp[0]);
2662 /* Now dup our end of _the_ pipe to right position */
2663 if (p[THIS] != (*mode == 'r')) {
2664 PerlLIO_dup2(p[THIS], *mode == 'r');
2665 PerlLIO_close(p[THIS]);
2666 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2667 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
2670 setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2671 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
2673 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2674 /* No automatic close - do it by hand */
2681 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
2687 do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
2694 PerlLIO_close(pp[1]);
2695 /* Keep the lower of the two fd numbers */
2696 if (p[that] < p[This]) {
2697 PerlLIO_dup2_cloexec(p[This], p[that]);
2698 PerlLIO_close(p[This]);
2702 PerlLIO_close(p[that]); /* close child's end of pipe */
2704 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2705 SvUPGRADE(sv,SVt_IV);
2707 PL_forkprocess = pid;
2708 /* If we managed to get status pipe check for exec fail */
2709 if (did_pipes && pid > 0) {
2711 unsigned read_total = 0;
2713 while (read_total < sizeof(int)) {
2714 const SSize_t n1 = PerlLIO_read(pp[0],
2715 (void*)(((char*)&errkid)+read_total),
2716 (sizeof(int)) - read_total);
2721 PerlLIO_close(pp[0]);
2723 if (read_total) { /* Error */
2725 PerlLIO_close(p[This]);
2726 if (read_total != sizeof(int))
2727 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", read_total);
2729 pid2 = wait4pid(pid, &status, 0);
2730 } while (pid2 == -1 && errno == EINTR);
2731 errno = errkid; /* Propagate errno from kid */
2736 PerlLIO_close(pp[0]);
2737 return PerlIO_fdopen(p[This], mode);
2739 # if defined(OS2) /* Same, without fork()ing and all extra overhead... */
2740 return my_syspopen4(aTHX_ NULL, mode, n, args);
2741 # elif defined(WIN32)
2742 return win32_popenlist(mode, n, args);
2744 Perl_croak(aTHX_ "List form of piped open not implemented");
2745 return (PerlIO *) NULL;
2750 /* VMS' my_popen() is in VMS.c, same with OS/2 and AmigaOS 4. */
2751 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2753 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2759 const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
2763 PERL_ARGS_ASSERT_MY_POPEN;
2765 PERL_FLUSHALL_FOR_CHILD;
2768 return my_syspopen(aTHX_ cmd,mode);
2771 This = (*mode == 'w');
2773 if (doexec && TAINTING_get) {
2775 taint_proper("Insecure %s%s", "EXEC");
2777 if (PerlProc_pipe_cloexec(p) < 0)
2779 if (doexec && PerlProc_pipe_cloexec(pp) >= 0)
2781 while ((pid = PerlProc_fork()) < 0) {
2782 if (errno != EAGAIN) {
2783 PerlLIO_close(p[This]);
2784 PerlLIO_close(p[that]);
2786 PerlLIO_close(pp[0]);
2787 PerlLIO_close(pp[1]);
2790 Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno));
2793 Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2803 PerlLIO_close(pp[0]);
2804 if (p[THIS] != (*mode == 'r')) {
2805 PerlLIO_dup2(p[THIS], *mode == 'r');
2806 PerlLIO_close(p[THIS]);
2807 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2808 PerlLIO_close(p[THAT]);
2811 setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2812 PerlLIO_close(p[THAT]);
2816 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2823 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2828 /* may or may not use the shell */
2829 do_exec3(cmd, pp[1], did_pipes);
2832 #endif /* defined OS2 */
2834 #ifdef PERLIO_USING_CRLF
2835 /* Since we circumvent IO layers when we manipulate low-level
2836 filedescriptors directly, need to manually switch to the
2837 default, binary, low-level mode; see PerlIOBuf_open(). */
2838 PerlLIO_setmode((*mode == 'r'), O_BINARY);
2841 #ifdef PERL_USES_PL_PIDSTATUS
2842 hv_clear(PL_pidstatus); /* we have no children */
2849 PerlLIO_close(pp[1]);
2850 if (p[that] < p[This]) {
2851 PerlLIO_dup2_cloexec(p[This], p[that]);
2852 PerlLIO_close(p[This]);
2856 PerlLIO_close(p[that]);
2858 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2859 SvUPGRADE(sv,SVt_IV);
2861 PL_forkprocess = pid;
2862 if (did_pipes && pid > 0) {
2866 while (n < sizeof(int)) {
2867 const SSize_t n1 = PerlLIO_read(pp[0],
2868 (void*)(((char*)&errkid)+n),
2874 PerlLIO_close(pp[0]);
2876 if (n) { /* Error */
2878 PerlLIO_close(p[This]);
2879 if (n != sizeof(int))
2880 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n);
2882 pid2 = wait4pid(pid, &status, 0);
2883 } while (pid2 == -1 && errno == EINTR);
2884 errno = errkid; /* Propagate errno from kid */
2889 PerlLIO_close(pp[0]);
2890 return PerlIO_fdopen(p[This], mode);
2892 #elif defined(DJGPP)
2893 FILE *djgpp_popen();
2895 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2897 PERL_FLUSHALL_FOR_CHILD;
2898 /* Call system's popen() to get a FILE *, then import it.
2899 used 0 for 2nd parameter to PerlIO_importFILE;
2902 return PerlIO_importFILE(djgpp_popen(cmd, mode), 0);
2904 #elif defined(__LIBCATAMOUNT__)
2906 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2911 #endif /* !DOSISH */
2913 /* this is called in parent before the fork() */
2915 Perl_atfork_lock(void)
2916 #if defined(USE_ITHREADS)
2918 PERL_TSA_ACQUIRE(PL_perlio_mutex)
2921 PERL_TSA_ACQUIRE(PL_malloc_mutex)
2923 PERL_TSA_ACQUIRE(PL_op_mutex)
2926 #if defined(USE_ITHREADS)
2927 /* locks must be held in locking order (if any) */
2929 MUTEX_LOCK(&PL_perlio_mutex);
2932 MUTEX_LOCK(&PL_malloc_mutex);
2938 /* this is called in both parent and child after the fork() */
2940 Perl_atfork_unlock(void)
2941 #if defined(USE_ITHREADS)
2943 PERL_TSA_RELEASE(PL_perlio_mutex)
2946 PERL_TSA_RELEASE(PL_malloc_mutex)
2948 PERL_TSA_RELEASE(PL_op_mutex)
2951 #if defined(USE_ITHREADS)
2952 /* locks must be released in same order as in atfork_lock() */
2954 MUTEX_UNLOCK(&PL_perlio_mutex);
2957 MUTEX_UNLOCK(&PL_malloc_mutex);
2966 #if defined(HAS_FORK)
2968 #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
2973 /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2974 * handlers elsewhere in the code */
2978 #elif defined(__amigaos4__)
2979 return amigaos_fork();
2981 /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2982 Perl_croak_nocontext("fork() not available");
2984 #endif /* HAS_FORK */
2989 dup2(int oldfd, int newfd)
2991 #if defined(HAS_FCNTL) && defined(F_DUPFD)
2994 PerlLIO_close(newfd);
2995 return fcntl(oldfd, F_DUPFD, newfd);
2997 #define DUP2_MAX_FDS 256
2998 int fdtmp[DUP2_MAX_FDS];
3004 PerlLIO_close(newfd);
3005 /* good enough for low fd's... */
3006 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
3007 if (fdx >= DUP2_MAX_FDS) {
3015 PerlLIO_close(fdtmp[--fdx]);
3022 #ifdef HAS_SIGACTION
3025 =for apidoc_section Signals
3028 A wrapper for the C library L<signal(2)>. Don't use the latter, as the Perl
3029 version knows things that interact with the rest of the perl interpreter.
3035 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
3037 struct sigaction act, oact;
3040 /* only "parent" interpreter can diddle signals */
3041 if (PL_curinterp != aTHX)
3042 return (Sighandler_t) SIG_ERR;
3045 act.sa_handler = handler;
3046 sigemptyset(&act.sa_mask);
3049 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3050 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
3052 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
3053 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
3054 act.sa_flags |= SA_NOCLDWAIT;
3056 if (sigaction(signo, &act, &oact) == -1)
3057 return (Sighandler_t) SIG_ERR;
3059 return (Sighandler_t) oact.sa_handler;
3063 Perl_rsignal_state(pTHX_ int signo)
3065 struct sigaction oact;
3066 PERL_UNUSED_CONTEXT;
3068 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
3069 return (Sighandler_t) SIG_ERR;
3071 return (Sighandler_t) oact.sa_handler;
3075 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3079 struct sigaction act;
3081 PERL_ARGS_ASSERT_RSIGNAL_SAVE;
3084 /* only "parent" interpreter can diddle signals */
3085 if (PL_curinterp != aTHX)
3089 act.sa_handler = handler;
3090 sigemptyset(&act.sa_mask);
3093 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
3094 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
3096 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
3097 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
3098 act.sa_flags |= SA_NOCLDWAIT;
3100 return sigaction(signo, &act, save);
3104 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3108 PERL_UNUSED_CONTEXT;
3110 /* only "parent" interpreter can diddle signals */
3111 if (PL_curinterp != aTHX)
3115 return sigaction(signo, save, (struct sigaction *)NULL);
3118 #else /* !HAS_SIGACTION */
3121 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
3123 #if defined(USE_ITHREADS) && !defined(WIN32)
3124 /* only "parent" interpreter can diddle signals */
3125 if (PL_curinterp != aTHX)
3126 return (Sighandler_t) SIG_ERR;
3129 return PerlProc_signal(signo, handler);
3139 Perl_rsignal_state(pTHX_ int signo)
3141 Sighandler_t oldsig;
3143 #if defined(USE_ITHREADS) && !defined(WIN32)
3144 /* only "parent" interpreter can diddle signals */
3145 if (PL_curinterp != aTHX)
3146 return (Sighandler_t) SIG_ERR;
3150 oldsig = PerlProc_signal(signo, sig_trap);
3151 PerlProc_signal(signo, oldsig);
3153 PerlProc_kill(PerlProc_getpid(), signo);
3158 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3160 #if defined(USE_ITHREADS) && !defined(WIN32)
3161 /* only "parent" interpreter can diddle signals */
3162 if (PL_curinterp != aTHX)
3165 *save = PerlProc_signal(signo, handler);
3166 return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
3170 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3172 #if defined(USE_ITHREADS) && !defined(WIN32)
3173 /* only "parent" interpreter can diddle signals */
3174 if (PL_curinterp != aTHX)
3177 return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
3180 #endif /* !HAS_SIGACTION */
3181 #endif /* !PERL_MICRO */
3183 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
3184 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
3186 Perl_my_pclose(pTHX_ PerlIO *ptr)
3194 const int fd = PerlIO_fileno(ptr);
3197 svp = av_fetch(PL_fdpid,fd,TRUE);
3198 pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
3202 #if defined(USE_PERLIO)
3203 /* Find out whether the refcount is low enough for us to wait for the
3204 child proc without blocking. */
3205 should_wait = PerlIOUnix_refcnt(fd) == 1 && pid > 0;
3207 should_wait = pid > 0;
3211 if (pid == -1) { /* Opened by popen. */
3212 return my_syspclose(ptr);
3215 close_failed = (PerlIO_close(ptr) == EOF);
3217 if (should_wait) do {
3218 pid2 = wait4pid(pid, &status, 0);
3219 } while (pid2 == -1 && errno == EINTR);
3226 ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)
3230 #elif defined(__LIBCATAMOUNT__)
3232 Perl_my_pclose(pTHX_ PerlIO *ptr)
3236 #endif /* !DOSISH */
3238 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(__LIBCATAMOUNT__)
3240 Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
3243 PERL_ARGS_ASSERT_WAIT4PID;
3244 #ifdef PERL_USES_PL_PIDSTATUS
3246 /* PERL_USES_PL_PIDSTATUS is only defined when neither
3247 waitpid() nor wait4() is available, or on OS/2, which
3248 doesn't appear to support waiting for a progress group
3249 member, so we can only treat a 0 pid as an unknown child.
3256 /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the
3257 pid, rather than a string form. */
3258 SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE);
3259 if (svp && *svp != &PL_sv_undef) {
3260 *statusp = SvIVX(*svp);
3261 (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),
3269 hv_iterinit(PL_pidstatus);
3270 if ((entry = hv_iternext(PL_pidstatus))) {
3271 SV * const sv = hv_iterval(PL_pidstatus,entry);
3273 const char * const spid = hv_iterkey(entry,&len);
3275 assert (len == sizeof(Pid_t));
3276 memcpy((char *)&pid, spid, len);
3277 *statusp = SvIVX(sv);
3278 /* The hash iterator is currently on this entry, so simply
3279 calling hv_delete would trigger the lazy delete, which on
3280 aggregate does more work, because next call to hv_iterinit()
3281 would spot the flag, and have to call the delete routine,
3282 while in the meantime any new entries can't re-use that
3284 hv_iterinit(PL_pidstatus);
3285 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
3292 # ifdef HAS_WAITPID_RUNTIME
3293 if (!HAS_WAITPID_RUNTIME)
3296 result = PerlProc_waitpid(pid,statusp,flags);
3299 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
3300 result = wait4(pid,statusp,flags,NULL);
3303 #ifdef PERL_USES_PL_PIDSTATUS
3304 #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
3309 Perl_croak(aTHX_ "Can't do waitpid with flags");
3311 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
3312 pidgone(result,*statusp);
3318 #if defined(HAS_WAITPID) || defined(HAS_WAIT4)
3321 if (result < 0 && errno == EINTR) {
3323 errno = EINTR; /* reset in case a signal handler changed $! */
3327 #endif /* !DOSISH || OS2 || WIN32 || NETWARE */
3329 #ifdef PERL_USES_PL_PIDSTATUS
3331 S_pidgone(pTHX_ Pid_t pid, int status)
3335 sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE);
3336 SvUPGRADE(sv,SVt_IV);
3337 SvIV_set(sv, status);
3345 int /* Cannot prototype with I32
3347 my_syspclose(PerlIO *ptr)
3350 Perl_my_pclose(pTHX_ PerlIO *ptr)
3353 /* Needs work for PerlIO ! */
3354 FILE * const f = PerlIO_findFILE(ptr);
3355 const I32 result = pclose(f);
3356 PerlIO_releaseFILE(ptr,f);
3364 Perl_my_pclose(pTHX_ PerlIO *ptr)
3366 /* Needs work for PerlIO ! */
3367 FILE * const f = PerlIO_findFILE(ptr);
3368 I32 result = djgpp_pclose(f);
3369 result = (result << 8) & 0xff00;
3370 PerlIO_releaseFILE(ptr,f);
3375 #define PERL_REPEATCPY_LINEAR 4
3377 Perl_repeatcpy(char *to, const char *from, I32 len, IV count)
3379 PERL_ARGS_ASSERT_REPEATCPY;
3384 croak_memory_wrap();
3387 memset(to, *from, count);
3390 IV items, linear, half;
3392 linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR;
3393 for (items = 0; items < linear; ++items) {
3394 const char *q = from;
3396 for (todo = len; todo > 0; todo--)
3401 while (items <= half) {
3402 IV size = items * len;
3403 memcpy(p, to, size);
3409 memcpy(p, to, (count - items) * len);
3415 Perl_same_dirent(pTHX_ const char *a, const char *b)
3417 char *fa = strrchr(a,'/');
3418 char *fb = strrchr(b,'/');
3421 SV * const tmpsv = sv_newmortal();
3423 PERL_ARGS_ASSERT_SAME_DIRENT;
3436 sv_setpvs(tmpsv, ".");
3438 sv_setpvn(tmpsv, a, fa - a);
3439 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
3442 sv_setpvs(tmpsv, ".");
3444 sv_setpvn(tmpsv, b, fb - b);
3445 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
3447 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
3448 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
3450 #endif /* !HAS_RENAME */
3453 Perl_find_script(pTHX_ const char *scriptname, bool dosearch,
3454 const char *const *const search_ext, I32 flags)
3456 const char *xfound = NULL;
3457 char *xfailed = NULL;
3458 char tmpbuf[MAXPATHLEN];
3463 #if defined(DOSISH) && !defined(OS2)
3464 # define SEARCH_EXTS ".bat", ".cmd", NULL
3465 # define MAX_EXT_LEN 4
3468 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
3469 # define MAX_EXT_LEN 4
3472 # define SEARCH_EXTS ".pl", ".com", NULL
3473 # define MAX_EXT_LEN 4
3475 /* additional extensions to try in each dir if scriptname not found */
3477 static const char *const exts[] = { SEARCH_EXTS };
3478 const char *const *const ext = search_ext ? search_ext : exts;
3479 int extidx = 0, i = 0;
3480 const char *curext = NULL;
3482 PERL_UNUSED_ARG(search_ext);
3483 # define MAX_EXT_LEN 0
3486 PERL_ARGS_ASSERT_FIND_SCRIPT;
3489 * If dosearch is true and if scriptname does not contain path
3490 * delimiters, search the PATH for scriptname.
3492 * If SEARCH_EXTS is also defined, will look for each
3493 * scriptname{SEARCH_EXTS} whenever scriptname is not found
3494 * while searching the PATH.
3496 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
3497 * proceeds as follows:
3498 * If DOSISH or VMSISH:
3499 * + look for ./scriptname{,.foo,.bar}
3500 * + search the PATH for scriptname{,.foo,.bar}
3503 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
3504 * this will not look in '.' if it's not in the PATH)
3509 # ifdef ALWAYS_DEFTYPES
3510 len = strlen(scriptname);
3511 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
3512 int idx = 0, deftypes = 1;
3515 const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
3518 int idx = 0, deftypes = 1;
3521 const int hasdir = (strpbrk(scriptname,":[</") != NULL);
3523 /* The first time through, just add SEARCH_EXTS to whatever we
3524 * already have, so we can check for default file types. */
3526 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
3533 if ((strlen(tmpbuf) + strlen(scriptname)
3534 + MAX_EXT_LEN) >= sizeof tmpbuf)
3535 continue; /* don't search dir with too-long name */
3536 my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
3540 if (strEQ(scriptname, "-"))
3542 if (dosearch) { /* Look in '.' first. */
3543 const char *cur = scriptname;
3545 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
3547 if (strEQ(ext[i++],curext)) {
3548 extidx = -1; /* already has an ext */
3553 DEBUG_p(PerlIO_printf(Perl_debug_log,
3554 "Looking for %s\n",cur));
3557 if (PerlLIO_stat(cur,&statbuf) >= 0
3558 && !S_ISDIR(statbuf.st_mode)) {
3567 if (cur == scriptname) {
3568 len = strlen(scriptname);
3569 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
3571 my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
3574 } while (extidx >= 0 && ext[extidx] /* try an extension? */
3575 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len));
3580 if (dosearch && !strchr(scriptname, '/')
3582 && !strchr(scriptname, '\\')
3584 && (s = PerlEnv_getenv("PATH")))
3588 bufend = s + strlen(s);
3589 while (s < bufend) {
3593 && *s != ';'; len++, s++) {
3594 if (len < sizeof tmpbuf)
3597 if (len < sizeof tmpbuf)
3600 s = delimcpy_no_escape(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend,
3605 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
3606 continue; /* don't search dir with too-long name */
3609 && tmpbuf[len - 1] != '/'
3610 && tmpbuf[len - 1] != '\\'
3613 tmpbuf[len++] = '/';
3614 if (len == 2 && tmpbuf[0] == '.')
3616 (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
3620 len = strlen(tmpbuf);
3621 if (extidx > 0) /* reset after previous loop */
3625 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3626 retval = PerlLIO_stat(tmpbuf,&statbuf);
3627 if (S_ISDIR(statbuf.st_mode)) {
3631 } while ( retval < 0 /* not there */
3632 && extidx>=0 && ext[extidx] /* try an extension? */
3633 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)
3638 if (S_ISREG(statbuf.st_mode)
3639 && cando(S_IRUSR,TRUE,&statbuf)
3640 #if !defined(DOSISH)
3641 && cando(S_IXUSR,TRUE,&statbuf)
3645 xfound = tmpbuf; /* bingo! */
3649 xfailed = savepv(tmpbuf);
3654 if (!xfound && !seen_dot && !xfailed &&
3655 (PerlLIO_stat(scriptname,&statbuf) < 0
3656 || S_ISDIR(statbuf.st_mode)))
3658 seen_dot = 1; /* Disable message. */
3663 if (flags & 1) { /* do or die? */
3664 /* diag_listed_as: Can't execute %s */
3665 Perl_croak(aTHX_ "Can't %s %s%s%s",
3666 (xfailed ? "execute" : "find"),
3667 (xfailed ? xfailed : scriptname),
3668 (xfailed ? "" : " on PATH"),
3669 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3674 scriptname = xfound;
3676 return (scriptname ? savepv(scriptname) : NULL);
3679 #ifndef PERL_GET_CONTEXT_DEFINED
3682 Perl_get_context(void)
3684 #if defined(USE_ITHREADS)
3685 # ifdef OLD_PTHREADS_API
3687 int error = pthread_getspecific(PL_thr_key, &t);
3689 Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error);
3691 # elif defined(I_MACH_CTHREADS)
3692 return (void*)cthread_data(cthread_self());
3694 return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
3702 Perl_set_context(void *t)
3704 #if defined(USE_ITHREADS)
3706 PERL_ARGS_ASSERT_SET_CONTEXT;
3707 #if defined(USE_ITHREADS)
3708 # ifdef I_MACH_CTHREADS
3709 cthread_set_data(cthread_self(), t);
3712 const int error = pthread_setspecific(PL_thr_key, t);
3714 Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error);
3722 #endif /* !PERL_GET_CONTEXT_DEFINED */
3725 Perl_get_op_names(pTHX)
3727 PERL_UNUSED_CONTEXT;
3728 return (char **)PL_op_name;
3732 Perl_get_op_descs(pTHX)
3734 PERL_UNUSED_CONTEXT;
3735 return (char **)PL_op_desc;
3739 Perl_get_no_modify(pTHX)
3741 PERL_UNUSED_CONTEXT;
3742 return PL_no_modify;
3746 Perl_get_opargs(pTHX)
3748 PERL_UNUSED_CONTEXT;
3749 return (U32 *)PL_opargs;
3753 Perl_get_ppaddr(pTHX)
3755 PERL_UNUSED_CONTEXT;
3756 return (PPADDR_t*)PL_ppaddr;
3759 #ifndef HAS_GETENV_LEN
3761 Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
3763 char * const env_trans = PerlEnv_getenv(env_elem);
3764 PERL_UNUSED_CONTEXT;
3765 PERL_ARGS_ASSERT_GETENV_LEN;
3767 *len = strlen(env_trans);
3774 Perl_get_vtbl(pTHX_ int vtbl_id)
3776 PERL_UNUSED_CONTEXT;
3778 return (vtbl_id < 0 || vtbl_id >= magic_vtable_max)
3779 ? NULL : (MGVTBL*)PL_magic_vtables + vtbl_id;
3783 Perl_my_fflush_all(pTHX)
3785 #if defined(USE_PERLIO) || defined(FFLUSH_NULL)
3786 return PerlIO_flush(NULL);
3788 # if defined(HAS__FWALK)
3789 extern int fflush(FILE *);
3790 /* undocumented, unprototyped, but very useful BSDism */
3791 extern void _fwalk(int (*)(FILE *));
3795 # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3797 # ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3798 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3799 # elif defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3800 open_max = sysconf(_SC_OPEN_MAX);
3801 # elif defined(FOPEN_MAX)
3802 open_max = FOPEN_MAX;
3803 # elif defined(OPEN_MAX)
3804 open_max = OPEN_MAX;
3805 # elif defined(_NFILE)
3810 for (i = 0; i < open_max; i++)
3811 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3812 STDIO_STREAM_ARRAY[i]._file < open_max &&
3813 STDIO_STREAM_ARRAY[i]._flag)
3814 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3818 SETERRNO(EBADF,RMS_IFI);
3825 Perl_report_wrongway_fh(pTHX_ const GV *gv, const char have)
3827 if (ckWARN(WARN_IO)) {
3829 = gv && (isGV_with_GP(gv))
3832 const char * const direction = have == '>' ? "out" : "in";
3834 if (name && HEK_LEN(name))
3835 Perl_warner(aTHX_ packWARN(WARN_IO),
3836 "Filehandle %" HEKf " opened only for %sput",
3837 HEKfARG(name), direction);
3839 Perl_warner(aTHX_ packWARN(WARN_IO),
3840 "Filehandle opened only for %sput", direction);
3845 Perl_report_evil_fh(pTHX_ const GV *gv)
3847 const IO *io = gv ? GvIO(gv) : NULL;
3848 const PERL_BITFIELD16 op = PL_op->op_type;
3852 if (io && IoTYPE(io) == IoTYPE_CLOSED) {
3854 warn_type = WARN_CLOSED;
3858 warn_type = WARN_UNOPENED;
3861 if (ckWARN(warn_type)) {
3863 = gv && isGV_with_GP(gv) && GvENAMELEN(gv) ?
3864 sv_2mortal(newSVhek(GvENAME_HEK(gv))) : NULL;
3865 const char * const pars =
3866 (const char *)(OP_IS_FILETEST(op) ? "" : "()");
3867 const char * const func =
3869 (op == OP_READLINE || op == OP_RCATLINE
3870 ? "readline" : /* "<HANDLE>" not nice */
3871 op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */
3873 const char * const type =
3875 (OP_IS_SOCKET(op) || (io && IoTYPE(io) == IoTYPE_SOCKET)
3876 ? "socket" : "filehandle");
3877 const bool have_name = name && SvCUR(name);
3878 Perl_warner(aTHX_ packWARN(warn_type),
3879 "%s%s on %s %s%s%" SVf, func, pars, vile, type,
3880 have_name ? " " : "",
3881 SVfARG(have_name ? name : &PL_sv_no));
3882 if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3884 aTHX_ packWARN(warn_type),
3885 "\t(Are you trying to call %s%s on dirhandle%s%" SVf "?)\n",
3886 func, pars, have_name ? " " : "",
3887 SVfARG(have_name ? name : &PL_sv_no)
3892 /* To workaround core dumps from the uninitialised tm_zone we get the
3893 * system to give us a reasonable struct to copy. This fix means that
3894 * strftime uses the tm_zone and tm_gmtoff values returned by
3895 * localtime(time()). That should give the desired result most of the
3896 * time. But probably not always!
3898 * This does not address tzname aspects of NETaa14816.
3903 # ifndef STRUCT_TM_HASZONE
3904 # define STRUCT_TM_HASZONE
3908 #ifdef STRUCT_TM_HASZONE /* Backward compat */
3909 # ifndef HAS_TM_TM_ZONE
3910 # define HAS_TM_TM_ZONE
3915 Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */
3917 #ifdef HAS_TM_TM_ZONE
3919 const struct tm* my_tm;
3920 PERL_UNUSED_CONTEXT;
3921 PERL_ARGS_ASSERT_INIT_TM;
3923 ENV_LOCALE_READ_LOCK;
3924 my_tm = localtime(&now);
3926 Copy(my_tm, ptm, 1, struct tm);
3927 ENV_LOCALE_READ_UNLOCK;
3929 PERL_UNUSED_CONTEXT;
3930 PERL_ARGS_ASSERT_INIT_TM;
3931 PERL_UNUSED_ARG(ptm);
3936 =for apidoc_section Time
3937 =for apidoc mini_mktime
3938 normalise S<C<struct tm>> values without the localtime() semantics (and
3939 overhead) of mktime().
3944 Perl_mini_mktime(struct tm *ptm)
3948 int month, mday, year, jday;
3949 int odd_cent, odd_year;
3951 PERL_ARGS_ASSERT_MINI_MKTIME;
3953 #define DAYS_PER_YEAR 365
3954 #define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1)
3955 #define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1)
3956 #define DAYS_PER_QCENT (4*DAYS_PER_CENT+1)
3957 #define SECS_PER_HOUR (60*60)
3958 #define SECS_PER_DAY (24*SECS_PER_HOUR)
3959 /* parentheses deliberately absent on these two, otherwise they don't work */
3960 #define MONTH_TO_DAYS 153/5
3961 #define DAYS_TO_MONTH 5/153
3962 /* offset to bias by March (month 4) 1st between month/mday & year finding */
3963 #define YEAR_ADJUST (4*MONTH_TO_DAYS+1)
3964 /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */
3965 #define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */
3968 * Year/day algorithm notes:
3970 * With a suitable offset for numeric value of the month, one can find
3971 * an offset into the year by considering months to have 30.6 (153/5) days,
3972 * using integer arithmetic (i.e., with truncation). To avoid too much
3973 * messing about with leap days, we consider January and February to be
3974 * the 13th and 14th month of the previous year. After that transformation,
3975 * we need the month index we use to be high by 1 from 'normal human' usage,
3976 * so the month index values we use run from 4 through 15.
3978 * Given that, and the rules for the Gregorian calendar (leap years are those
3979 * divisible by 4 unless also divisible by 100, when they must be divisible
3980 * by 400 instead), we can simply calculate the number of days since some
3981 * arbitrary 'beginning of time' by futzing with the (adjusted) year number,
3982 * the days we derive from our month index, and adding in the day of the
3983 * month. The value used here is not adjusted for the actual origin which
3984 * it normally would use (1 January A.D. 1), since we're not exposing it.
3985 * We're only building the value so we can turn around and get the
3986 * normalised values for the year, month, day-of-month, and day-of-year.
3988 * For going backward, we need to bias the value we're using so that we find
3989 * the right year value. (Basically, we don't want the contribution of
3990 * March 1st to the number to apply while deriving the year). Having done
3991 * that, we 'count up' the contribution to the year number by accounting for
3992 * full quadracenturies (400-year periods) with their extra leap days, plus
3993 * the contribution from full centuries (to avoid counting in the lost leap
3994 * days), plus the contribution from full quad-years (to count in the normal
3995 * leap days), plus the leftover contribution from any non-leap years.
3996 * At this point, if we were working with an actual leap day, we'll have 0
3997 * days left over. This is also true for March 1st, however. So, we have
3998 * to special-case that result, and (earlier) keep track of the 'odd'
3999 * century and year contributions. If we got 4 extra centuries in a qcent,
4000 * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb.
4001 * Otherwise, we add back in the earlier bias we removed (the 123 from
4002 * figuring in March 1st), find the month index (integer division by 30.6),
4003 * and the remainder is the day-of-month. We then have to convert back to
4004 * 'real' months (including fixing January and February from being 14/15 in
4005 * the previous year to being in the proper year). After that, to get
4006 * tm_yday, we work with the normalised year and get a new yearday value for
4007 * January 1st, which we subtract from the yearday value we had earlier,
4008 * representing the date we've re-built. This is done from January 1
4009 * because tm_yday is 0-origin.
4011 * Since POSIX time routines are only guaranteed to work for times since the
4012 * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm
4013 * applies Gregorian calendar rules even to dates before the 16th century
4014 * doesn't bother me. Besides, you'd need cultural context for a given
4015 * date to know whether it was Julian or Gregorian calendar, and that's
4016 * outside the scope for this routine. Since we convert back based on the
4017 * same rules we used to build the yearday, you'll only get strange results
4018 * for input which needed normalising, or for the 'odd' century years which
4019 * were leap years in the Julian calendar but not in the Gregorian one.
4020 * I can live with that.
4022 * This algorithm also fails to handle years before A.D. 1 gracefully, but
4023 * that's still outside the scope for POSIX time manipulation, so I don't
4029 year = 1900 + ptm->tm_year;
4030 month = ptm->tm_mon;
4031 mday = ptm->tm_mday;
4037 yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400;
4038 yearday += month*MONTH_TO_DAYS + mday + jday;
4040 * Note that we don't know when leap-seconds were or will be,
4041 * so we have to trust the user if we get something which looks
4042 * like a sensible leap-second. Wild values for seconds will
4043 * be rationalised, however.
4045 if ((unsigned) ptm->tm_sec <= 60) {
4052 secs += 60 * ptm->tm_min;
4053 secs += SECS_PER_HOUR * ptm->tm_hour;
4055 if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) {
4056 /* got negative remainder, but need positive time */
4057 /* back off an extra day to compensate */
4058 yearday += (secs/SECS_PER_DAY)-1;
4059 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1);
4062 yearday += (secs/SECS_PER_DAY);
4063 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY);
4066 else if (secs >= SECS_PER_DAY) {
4067 yearday += (secs/SECS_PER_DAY);
4068 secs %= SECS_PER_DAY;
4070 ptm->tm_hour = secs/SECS_PER_HOUR;
4071 secs %= SECS_PER_HOUR;
4072 ptm->tm_min = secs/60;
4074 ptm->tm_sec += secs;
4075 /* done with time of day effects */
4077 * The algorithm for yearday has (so far) left it high by 428.
4078 * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to
4079 * bias it by 123 while trying to figure out what year it
4080 * really represents. Even with this tweak, the reverse
4081 * translation fails for years before A.D. 0001.
4082 * It would still fail for Feb 29, but we catch that one below.
4084 jday = yearday; /* save for later fixup vis-a-vis Jan 1 */
4085 yearday -= YEAR_ADJUST;
4086 year = (yearday / DAYS_PER_QCENT) * 400;
4087 yearday %= DAYS_PER_QCENT;
4088 odd_cent = yearday / DAYS_PER_CENT;
4089 year += odd_cent * 100;
4090 yearday %= DAYS_PER_CENT;
4091 year += (yearday / DAYS_PER_QYEAR) * 4;
4092 yearday %= DAYS_PER_QYEAR;
4093 odd_year = yearday / DAYS_PER_YEAR;
4095 yearday %= DAYS_PER_YEAR;
4096 if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */
4101 yearday += YEAR_ADJUST; /* recover March 1st crock */
4102 month = yearday*DAYS_TO_MONTH;
4103 yearday -= month*MONTH_TO_DAYS;
4104 /* recover other leap-year adjustment */
4113 ptm->tm_year = year - 1900;
4115 ptm->tm_mday = yearday;
4116 ptm->tm_mon = month;
4120 ptm->tm_mon = month - 1;
4122 /* re-build yearday based on Jan 1 to get tm_yday */
4124 yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400;
4125 yearday += 14*MONTH_TO_DAYS + 1;
4126 ptm->tm_yday = jday - yearday;
4127 ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
4131 Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst)
4136 =for apidoc_section Time
4137 =for apidoc my_strftime
4138 strftime(), but with a different API so that the return value is a pointer
4139 to the formatted result (which MUST be arranged to be FREED BY THE
4140 CALLER). This allows this function to increase the buffer size as needed,
4141 so that the caller doesn't have to worry about that.
4143 Note that yday and wday effectively are ignored by this function, as
4144 mini_mktime() overwrites them
4154 PERL_ARGS_ASSERT_MY_STRFTIME;
4156 init_tm(&mytm); /* XXX workaround - see init_tm() above */
4159 mytm.tm_hour = hour;
4160 mytm.tm_mday = mday;
4162 mytm.tm_year = year;
4163 mytm.tm_wday = wday;
4164 mytm.tm_yday = yday;
4165 mytm.tm_isdst = isdst;
4167 /* use libc to get the values for tm_gmtoff and tm_zone [perl #18238] */
4168 #if defined(HAS_MKTIME) && (defined(HAS_TM_TM_GMTOFF) || defined(HAS_TM_TM_ZONE))
4173 #ifdef HAS_TM_TM_GMTOFF
4174 mytm.tm_gmtoff = mytm2.tm_gmtoff;
4176 #ifdef HAS_TM_TM_ZONE
4177 mytm.tm_zone = mytm2.tm_zone;
4182 Newx(buf, buflen, char);
4184 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
4185 len = strftime(buf, buflen, fmt, &mytm);
4186 GCC_DIAG_RESTORE_STMT;
4189 ** The following is needed to handle to the situation where
4190 ** tmpbuf overflows. Basically we want to allocate a buffer
4191 ** and try repeatedly. The reason why it is so complicated
4192 ** is that getting a return value of 0 from strftime can indicate
4193 ** one of the following:
4194 ** 1. buffer overflowed,
4195 ** 2. illegal conversion specifier, or
4196 ** 3. the format string specifies nothing to be returned(not
4197 ** an error). This could be because format is an empty string
4198 ** or it specifies %p that yields an empty string in some locale.
4199 ** If there is a better way to make it portable, go ahead by
4202 if (inRANGE(len, 1, buflen - 1) || (len == 0 && *fmt == '\0'))
4205 /* Possibly buf overflowed - try again with a bigger buf */
4206 const int fmtlen = strlen(fmt);
4207 int bufsize = fmtlen + buflen;
4209 Renew(buf, bufsize, char);
4212 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
4213 buflen = strftime(buf, bufsize, fmt, &mytm);
4214 GCC_DIAG_RESTORE_STMT;
4216 if (inRANGE(buflen, 1, bufsize - 1))
4218 /* heuristic to prevent out-of-memory errors */
4219 if (bufsize > 100*fmtlen) {
4225 Renew(buf, bufsize, char);
4230 Perl_croak(aTHX_ "panic: no strftime");
4236 #define SV_CWD_RETURN_UNDEF \
4240 #define SV_CWD_ISDOT(dp) \
4241 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
4242 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
4245 =for apidoc_section Utility Functions
4247 =for apidoc getcwd_sv
4249 Fill C<sv> with current working directory
4254 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
4255 * rewritten again by dougm, optimized for use with xs TARG, and to prefer
4256 * getcwd(3) if available
4257 * Comments from the original:
4258 * This is a faster version of getcwd. It's also more dangerous
4259 * because you might chdir out of a directory that you can't chdir
4263 Perl_getcwd_sv(pTHX_ SV *sv)
4268 PERL_ARGS_ASSERT_GETCWD_SV;
4272 char buf[MAXPATHLEN];
4274 /* Some getcwd()s automatically allocate a buffer of the given
4275 * size from the heap if they are given a NULL buffer pointer.
4276 * The problem is that this behaviour is not portable. */
4277 if (getcwd(buf, sizeof(buf) - 1)) {
4282 SV_CWD_RETURN_UNDEF;
4289 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
4293 SvUPGRADE(sv, SVt_PV);
4295 if (PerlLIO_lstat(".", &statbuf) < 0) {
4296 SV_CWD_RETURN_UNDEF;
4299 orig_cdev = statbuf.st_dev;
4300 orig_cino = statbuf.st_ino;
4310 if (PerlDir_chdir("..") < 0) {
4311 SV_CWD_RETURN_UNDEF;
4313 if (PerlLIO_stat(".", &statbuf) < 0) {
4314 SV_CWD_RETURN_UNDEF;
4317 cdev = statbuf.st_dev;
4318 cino = statbuf.st_ino;
4320 if (odev == cdev && oino == cino) {
4323 if (!(dir = PerlDir_open("."))) {
4324 SV_CWD_RETURN_UNDEF;
4327 while ((dp = PerlDir_read(dir)) != NULL) {
4329 namelen = dp->d_namlen;
4331 namelen = strlen(dp->d_name);
4334 if (SV_CWD_ISDOT(dp)) {
4338 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
4339 SV_CWD_RETURN_UNDEF;
4342 tdev = statbuf.st_dev;
4343 tino = statbuf.st_ino;
4344 if (tino == oino && tdev == odev) {
4350 SV_CWD_RETURN_UNDEF;
4353 if (pathlen + namelen + 1 >= MAXPATHLEN) {
4354 SV_CWD_RETURN_UNDEF;
4357 SvGROW(sv, pathlen + namelen + 1);
4361 Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char);
4364 /* prepend current directory to the front */
4366 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
4367 pathlen += (namelen + 1);
4369 #ifdef VOID_CLOSEDIR
4372 if (PerlDir_close(dir) < 0) {
4373 SV_CWD_RETURN_UNDEF;
4379 SvCUR_set(sv, pathlen);
4383 if (PerlDir_chdir(SvPVX_const(sv)) < 0) {
4384 SV_CWD_RETURN_UNDEF;
4387 if (PerlLIO_stat(".", &statbuf) < 0) {
4388 SV_CWD_RETURN_UNDEF;
4391 cdev = statbuf.st_dev;
4392 cino = statbuf.st_ino;
4394 if (cdev != orig_cdev || cino != orig_cino) {
4395 Perl_croak(aTHX_ "Unstable directory path, "
4396 "current directory changed unexpectedly");
4409 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT)
4410 # define EMULATE_SOCKETPAIR_UDP
4413 #ifdef EMULATE_SOCKETPAIR_UDP
4415 S_socketpair_udp (int fd[2]) {
4417 /* Fake a datagram socketpair using UDP to localhost. */
4418 int sockets[2] = {-1, -1};
4419 struct sockaddr_in addresses[2];
4421 Sock_size_t size = sizeof(struct sockaddr_in);
4422 unsigned short port;
4425 memset(&addresses, 0, sizeof(addresses));
4428 sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET);
4429 if (sockets[i] == -1)
4430 goto tidy_up_and_fail;
4432 addresses[i].sin_family = AF_INET;
4433 addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4434 addresses[i].sin_port = 0; /* kernel choses port. */
4435 if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i],
4436 sizeof(struct sockaddr_in)) == -1)
4437 goto tidy_up_and_fail;
4440 /* Now have 2 UDP sockets. Find out which port each is connected to, and
4441 for each connect the other socket to it. */
4444 if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i],
4446 goto tidy_up_and_fail;
4447 if (size != sizeof(struct sockaddr_in))
4448 goto abort_tidy_up_and_fail;
4449 /* !1 is 0, !0 is 1 */
4450 if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i],
4451 sizeof(struct sockaddr_in)) == -1)
4452 goto tidy_up_and_fail;
4455 /* Now we have 2 sockets connected to each other. I don't trust some other
4456 process not to have already sent a packet to us (by random) so send
4457 a packet from each to the other. */
4460 /* I'm going to send my own port number. As a short.
4461 (Who knows if someone somewhere has sin_port as a bitfield and needs
4462 this routine. (I'm assuming crays have socketpair)) */
4463 port = addresses[i].sin_port;
4464 got = PerlLIO_write(sockets[i], &port, sizeof(port));
4465 if (got != sizeof(port)) {
4467 goto tidy_up_and_fail;
4468 goto abort_tidy_up_and_fail;
4472 /* Packets sent. I don't trust them to have arrived though.
4473 (As I understand it Solaris TCP stack is multithreaded. Non-blocking
4474 connect to localhost will use a second kernel thread. In 2.6 the
4475 first thread running the connect() returns before the second completes,
4476 so EINPROGRESS> In 2.7 the improved stack is faster and connect()
4477 returns 0. Poor programs have tripped up. One poor program's authors'
4478 had a 50-1 reverse stock split. Not sure how connected these were.)
4479 So I don't trust someone not to have an unpredictable UDP stack.
4483 struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */
4484 int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0];
4488 FD_SET((unsigned int)sockets[0], &rset);
4489 FD_SET((unsigned int)sockets[1], &rset);
4491 got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor);
4492 if (got != 2 || !FD_ISSET(sockets[0], &rset)
4493 || !FD_ISSET(sockets[1], &rset)) {
4494 /* I hope this is portable and appropriate. */
4496 goto tidy_up_and_fail;
4497 goto abort_tidy_up_and_fail;
4501 /* And the paranoia department even now doesn't trust it to have arrive
4502 (hence MSG_DONTWAIT). Or that what arrives was sent by us. */
4504 struct sockaddr_in readfrom;
4505 unsigned short buffer[2];
4510 got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4511 sizeof(buffer), MSG_DONTWAIT,
4512 (struct sockaddr *) &readfrom, &size);
4514 got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4516 (struct sockaddr *) &readfrom, &size);
4520 goto tidy_up_and_fail;
4521 if (got != sizeof(port)
4522 || size != sizeof(struct sockaddr_in)
4523 /* Check other socket sent us its port. */
4524 || buffer[0] != (unsigned short) addresses[!i].sin_port
4525 /* Check kernel says we got the datagram from that socket */
4526 || readfrom.sin_family != addresses[!i].sin_family
4527 || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr
4528 || readfrom.sin_port != addresses[!i].sin_port)
4529 goto abort_tidy_up_and_fail;
4532 /* My caller (my_socketpair) has validated that this is non-NULL */
4535 /* I hereby declare this connection open. May God bless all who cross
4539 abort_tidy_up_and_fail:
4540 errno = ECONNABORTED;
4544 if (sockets[0] != -1)
4545 PerlLIO_close(sockets[0]);
4546 if (sockets[1] != -1)
4547 PerlLIO_close(sockets[1]);
4552 #endif /* EMULATE_SOCKETPAIR_UDP */
4554 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET)
4556 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4557 /* Stevens says that family must be AF_LOCAL, protocol 0.
4558 I'm going to enforce that, then ignore it, and use TCP (or UDP). */
4563 struct sockaddr_in listen_addr;
4564 struct sockaddr_in connect_addr;
4569 || family != AF_UNIX
4572 errno = EAFNOSUPPORT;
4581 type &= ~SOCK_CLOEXEC;
4584 #ifdef EMULATE_SOCKETPAIR_UDP
4585 if (type == SOCK_DGRAM)
4586 return S_socketpair_udp(fd);
4589 aTHXa(PERL_GET_THX);
4590 listener = PerlSock_socket(AF_INET, type, 0);
4593 memset(&listen_addr, 0, sizeof(listen_addr));
4594 listen_addr.sin_family = AF_INET;
4595 listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4596 listen_addr.sin_port = 0; /* kernel choses port. */
4597 if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr,
4598 sizeof(listen_addr)) == -1)
4599 goto tidy_up_and_fail;
4600 if (PerlSock_listen(listener, 1) == -1)
4601 goto tidy_up_and_fail;
4603 connector = PerlSock_socket(AF_INET, type, 0);
4604 if (connector == -1)
4605 goto tidy_up_and_fail;
4606 /* We want to find out the port number to connect to. */
4607 size = sizeof(connect_addr);
4608 if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr,
4610 goto tidy_up_and_fail;
4611 if (size != sizeof(connect_addr))
4612 goto abort_tidy_up_and_fail;
4613 if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr,
4614 sizeof(connect_addr)) == -1)
4615 goto tidy_up_and_fail;
4617 size = sizeof(listen_addr);
4618 acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr,
4621 goto tidy_up_and_fail;
4622 if (size != sizeof(listen_addr))
4623 goto abort_tidy_up_and_fail;
4624 PerlLIO_close(listener);
4625 /* Now check we are talking to ourself by matching port and host on the
4627 if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr,
4629 goto tidy_up_and_fail;
4630 if (size != sizeof(connect_addr)
4631 || listen_addr.sin_family != connect_addr.sin_family
4632 || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
4633 || listen_addr.sin_port != connect_addr.sin_port) {
4634 goto abort_tidy_up_and_fail;
4640 abort_tidy_up_and_fail:
4642 errno = ECONNABORTED; /* This would be the standard thing to do. */
4643 #elif defined(ECONNREFUSED)
4644 errno = ECONNREFUSED; /* some OSes might not have ECONNABORTED. */
4646 errno = ETIMEDOUT; /* Desperation time. */
4652 PerlLIO_close(listener);
4653 if (connector != -1)
4654 PerlLIO_close(connector);
4656 PerlLIO_close(acceptor);
4662 /* In any case have a stub so that there's code corresponding
4663 * to the my_socketpair in embed.fnc. */
4665 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4666 #ifdef HAS_SOCKETPAIR
4667 return socketpair(family, type, protocol, fd);
4676 =for apidoc sv_nosharing
4678 Dummy routine which "shares" an SV when there is no sharing module present.
4679 Or "locks" it. Or "unlocks" it. In other
4680 words, ignores its single SV argument.
4681 Exists to avoid test for a C<NULL> function pointer and because it could
4682 potentially warn under some level of strict-ness.
4688 Perl_sv_nosharing(pTHX_ SV *sv)
4690 PERL_UNUSED_CONTEXT;
4691 PERL_UNUSED_ARG(sv);
4696 =for apidoc sv_destroyable
4698 Dummy routine which reports that object can be destroyed when there is no
4699 sharing module present. It ignores its single SV argument, and returns
4700 'true'. Exists to avoid test for a C<NULL> function pointer and because it
4701 could potentially warn under some level of strict-ness.
4707 Perl_sv_destroyable(pTHX_ SV *sv)
4709 PERL_UNUSED_CONTEXT;
4710 PERL_UNUSED_ARG(sv);
4715 Perl_parse_unicode_opts(pTHX_ const char **popt)
4717 const char *p = *popt;
4720 PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS;
4724 const char* endptr = p + strlen(p);
4726 if (grok_atoUV(p, &uv, &endptr) && uv <= U32_MAX) {
4729 if (p && *p && *p != '\n' && *p != '\r') {
4731 goto the_end_of_the_opts_parser;
4733 Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p);
4737 Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p);
4743 case PERL_UNICODE_STDIN:
4744 opt |= PERL_UNICODE_STDIN_FLAG; break;
4745 case PERL_UNICODE_STDOUT:
4746 opt |= PERL_UNICODE_STDOUT_FLAG; break;
4747 case PERL_UNICODE_STDERR:
4748 opt |= PERL_UNICODE_STDERR_FLAG; break;
4749 case PERL_UNICODE_STD:
4750 opt |= PERL_UNICODE_STD_FLAG; break;
4751 case PERL_UNICODE_IN:
4752 opt |= PERL_UNICODE_IN_FLAG; break;
4753 case PERL_UNICODE_OUT:
4754 opt |= PERL_UNICODE_OUT_FLAG; break;
4755 case PERL_UNICODE_INOUT:
4756 opt |= PERL_UNICODE_INOUT_FLAG; break;
4757 case PERL_UNICODE_LOCALE:
4758 opt |= PERL_UNICODE_LOCALE_FLAG; break;
4759 case PERL_UNICODE_ARGV:
4760 opt |= PERL_UNICODE_ARGV_FLAG; break;
4761 case PERL_UNICODE_UTF8CACHEASSERT:
4762 opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break;
4764 if (*p != '\n' && *p != '\r') {
4765 if(isSPACE(*p)) goto the_end_of_the_opts_parser;
4768 "Unknown Unicode option letter '%c'", *p);
4775 opt = PERL_UNICODE_DEFAULT_FLAGS;
4777 the_end_of_the_opts_parser:
4779 if (opt & ~PERL_UNICODE_ALL_FLAGS)
4780 Perl_croak(aTHX_ "Unknown Unicode option value %" UVuf,
4781 (UV) (opt & ~PERL_UNICODE_ALL_FLAGS));
4789 # include <starlet.h>
4796 * This is really just a quick hack which grabs various garbage
4797 * values. It really should be a real hash algorithm which
4798 * spreads the effect of every input bit onto every output bit,
4799 * if someone who knows about such things would bother to write it.
4800 * Might be a good idea to add that function to CORE as well.
4801 * No numbers below come from careful analysis or anything here,
4802 * except they are primes and SEED_C1 > 1E6 to get a full-width
4803 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
4804 * probably be bigger too.
4807 # define SEED_C1 1000003
4808 #define SEED_C4 73819
4810 # define SEED_C1 25747
4811 #define SEED_C4 20639
4815 #define SEED_C5 26107
4817 #ifndef PERL_NO_DEV_RANDOM
4821 #ifdef HAS_GETTIMEOFDAY
4822 struct timeval when;
4827 /* This test is an escape hatch, this symbol isn't set by Configure. */
4828 #ifndef PERL_NO_DEV_RANDOM
4829 #ifndef PERL_RANDOM_DEVICE
4830 /* /dev/random isn't used by default because reads from it will block
4831 * if there isn't enough entropy available. You can compile with
4832 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
4833 * is enough real entropy to fill the seed. */
4834 # ifdef __amigaos4__
4835 # define PERL_RANDOM_DEVICE "RANDOM:SIZE=4"
4837 # define PERL_RANDOM_DEVICE "/dev/urandom"
4840 fd = PerlLIO_open_cloexec(PERL_RANDOM_DEVICE, 0);
4842 if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u)
4850 #ifdef HAS_GETTIMEOFDAY
4851 PerlProc_gettimeofday(&when,NULL);
4852 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
4855 u = (U32)SEED_C1 * when;
4857 u += SEED_C3 * (U32)PerlProc_getpid();
4858 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
4859 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
4860 u += SEED_C5 * (U32)PTR2UV(&when);
4866 Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
4868 #ifndef NO_PERL_HASH_ENV
4873 PERL_ARGS_ASSERT_GET_HASH_SEED;
4875 #ifndef NO_PERL_HASH_ENV
4876 env_pv= PerlEnv_getenv("PERL_HASH_SEED");
4880 /* ignore leading spaces */
4881 while (isSPACE(*env_pv))
4883 # ifdef USE_PERL_PERTURB_KEYS
4884 /* if they set it to "0" we disable key traversal randomization completely */
4885 if (strEQ(env_pv,"0")) {
4886 PL_hash_rand_bits_enabled= 0;
4888 /* otherwise switch to deterministic mode */
4889 PL_hash_rand_bits_enabled= 2;
4892 /* ignore a leading 0x... if it is there */
4893 if (env_pv[0] == '0' && env_pv[1] == 'x')
4896 for( i = 0; isXDIGIT(*env_pv) && i < PERL_HASH_SEED_BYTES; i++ ) {
4897 seed_buffer[i] = READ_XDIGIT(env_pv) << 4;
4898 if ( isXDIGIT(*env_pv)) {
4899 seed_buffer[i] |= READ_XDIGIT(env_pv);
4902 while (isSPACE(*env_pv))
4905 if (*env_pv && !isXDIGIT(*env_pv)) {
4906 Perl_warn(aTHX_ "perl: warning: Non hex character in '$ENV{PERL_HASH_SEED}', seed only partially set\n");
4908 /* should we check for unparsed crap? */
4909 /* should we warn about unused hex? */
4910 /* should we warn about insufficient hex? */
4913 #endif /* NO_PERL_HASH_ENV */
4915 for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
4916 seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
4919 #ifdef USE_PERL_PERTURB_KEYS
4920 { /* initialize PL_hash_rand_bits from the hash seed.
4921 * This value is highly volatile, it is updated every
4922 * hash insert, and is used as part of hash bucket chain
4923 * randomization and hash iterator randomization. */
4924 PL_hash_rand_bits= 0xbe49d17f; /* I just picked a number */
4925 for( i = 0; i < sizeof(UV) ; i++ ) {
4926 PL_hash_rand_bits += seed_buffer[i % PERL_HASH_SEED_BYTES];
4927 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,8);
4930 # ifndef NO_PERL_HASH_ENV
4931 env_pv= PerlEnv_getenv("PERL_PERTURB_KEYS");
4933 if (strEQ(env_pv,"0") || strEQ(env_pv,"NO")) {
4934 PL_hash_rand_bits_enabled= 0;
4935 } else if (strEQ(env_pv,"1") || strEQ(env_pv,"RANDOM")) {
4936 PL_hash_rand_bits_enabled= 1;
4937 } else if (strEQ(env_pv,"2") || strEQ(env_pv,"DETERMINISTIC")) {
4938 PL_hash_rand_bits_enabled= 2;
4940 Perl_warn(aTHX_ "perl: warning: strange setting in '$ENV{PERL_PERTURB_KEYS}': '%s'\n", env_pv);
4949 /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including
4950 * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also
4951 * given, and you supply your own implementation.
4953 * The default implementation reads a single env var, PERL_MEM_LOG,
4954 * expecting one or more of the following:
4956 * \d+ - fd fd to write to : must be 1st (grok_atoUV)
4957 * 'm' - memlog was PERL_MEM_LOG=1
4958 * 's' - svlog was PERL_SV_LOG=1
4959 * 't' - timestamp was PERL_MEM_LOG_TIMESTAMP=1
4961 * This makes the logger controllable enough that it can reasonably be
4962 * added to the system perl.
4965 /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer
4966 * the Perl_mem_log_...() will use (either via sprintf or snprintf).
4968 #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 128
4970 /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...()
4971 * writes to. In the default logger, this is settable at runtime.
4973 #ifndef PERL_MEM_LOG_FD
4974 # define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */
4977 #ifndef PERL_MEM_LOG_NOIMPL
4979 # ifdef DEBUG_LEAKING_SCALARS
4980 # define SV_LOG_SERIAL_FMT " [%lu]"
4981 # define _SV_LOG_SERIAL_ARG(sv) , (unsigned long) (sv)->sv_debug_serial
4983 # define SV_LOG_SERIAL_FMT
4984 # define _SV_LOG_SERIAL_ARG(sv)
4988 S_mem_log_common(enum mem_log_type mlt, const UV n,
4989 const UV typesize, const char *type_name, const SV *sv,
4990 Malloc_t oldalloc, Malloc_t newalloc,
4991 const char *filename, const int linenumber,
4992 const char *funcname)
4996 PERL_ARGS_ASSERT_MEM_LOG_COMMON;
4998 pmlenv = PerlEnv_getenv("PERL_MEM_LOG");
5001 if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s'))
5003 /* We can't use SVs or PerlIO for obvious reasons,
5004 * so we'll use stdio and low-level IO instead. */
5005 char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE];
5007 # ifdef HAS_GETTIMEOFDAY
5008 # define MEM_LOG_TIME_FMT "%10d.%06d: "
5009 # define MEM_LOG_TIME_ARG (int)tv.tv_sec, (int)tv.tv_usec
5011 gettimeofday(&tv, 0);
5013 # define MEM_LOG_TIME_FMT "%10d: "
5014 # define MEM_LOG_TIME_ARG (int)when
5018 /* If there are other OS specific ways of hires time than
5019 * gettimeofday() (see dist/Time-HiRes), the easiest way is
5020 * probably that they would be used to fill in the struct
5024 const char* endptr = pmlenv + strlen(pmlenv);
5027 if (grok_atoUV(pmlenv, &uv, &endptr) /* Ignore endptr. */
5028 && uv && uv <= PERL_INT_MAX
5032 fd = PERL_MEM_LOG_FD;
5035 if (strchr(pmlenv, 't')) {
5036 len = my_snprintf(buf, sizeof(buf),
5037 MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG);
5038 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
5042 len = my_snprintf(buf, sizeof(buf),
5043 "alloc: %s:%d:%s: %" IVdf " %" UVuf
5044 " %s = %" IVdf ": %" UVxf "\n",
5045 filename, linenumber, funcname, n, typesize,
5046 type_name, n * typesize, PTR2UV(newalloc));
5049 len = my_snprintf(buf, sizeof(buf),
5050 "realloc: %s:%d:%s: %" IVdf " %" UVuf
5051 " %s = %" IVdf ": %" UVxf " -> %" UVxf "\n",
5052 filename, linenumber, funcname, n, typesize,
5053 type_name, n * typesize, PTR2UV(oldalloc),
5057 len = my_snprintf(buf, sizeof(buf),
5058 "free: %s:%d:%s: %" UVxf "\n",
5059 filename, linenumber, funcname,
5064 len = my_snprintf(buf, sizeof(buf),
5065 "%s_SV: %s:%d:%s: %" UVxf SV_LOG_SERIAL_FMT "\n",
5066 mlt == MLT_NEW_SV ? "new" : "del",
5067 filename, linenumber, funcname,
5068 PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv));
5073 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
5077 #endif /* !PERL_MEM_LOG_NOIMPL */
5079 #ifndef PERL_MEM_LOG_NOIMPL
5081 mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \
5082 mem_log_common (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm)
5084 /* this is suboptimal, but bug compatible. User is providing their
5085 own implementation, but is getting these functions anyway, and they
5086 do nothing. But _NOIMPL users should be able to cope or fix */
5088 mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \
5089 /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */
5093 Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name,
5095 const char *filename, const int linenumber,
5096 const char *funcname)
5098 PERL_ARGS_ASSERT_MEM_LOG_ALLOC;
5100 mem_log_common_if(MLT_ALLOC, n, typesize, type_name,
5101 NULL, NULL, newalloc,
5102 filename, linenumber, funcname);
5107 Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name,
5108 Malloc_t oldalloc, Malloc_t newalloc,
5109 const char *filename, const int linenumber,
5110 const char *funcname)
5112 PERL_ARGS_ASSERT_MEM_LOG_REALLOC;
5114 mem_log_common_if(MLT_REALLOC, n, typesize, type_name,
5115 NULL, oldalloc, newalloc,
5116 filename, linenumber, funcname);
5121 Perl_mem_log_free(Malloc_t oldalloc,
5122 const char *filename, const int linenumber,
5123 const char *funcname)
5125 PERL_ARGS_ASSERT_MEM_LOG_FREE;
5127 mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL,
5128 filename, linenumber, funcname);
5133 Perl_mem_log_new_sv(const SV *sv,
5134 const char *filename, const int linenumber,
5135 const char *funcname)
5137 mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL,
5138 filename, linenumber, funcname);
5142 Perl_mem_log_del_sv(const SV *sv,
5143 const char *filename, const int linenumber,
5144 const char *funcname)
5146 mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL,
5147 filename, linenumber, funcname);
5150 #endif /* PERL_MEM_LOG */
5153 =for apidoc_section String Handling
5154 =for apidoc quadmath_format_valid
5156 C<quadmath_snprintf()> is very strict about its C<format> string and will
5157 fail, returning -1, if the format is invalid. It accepts exactly
5160 C<quadmath_format_valid()> checks that the intended single spec looks
5161 sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>,
5162 and has C<Q> before it. This is not a full "printf syntax check",
5165 Returns true if it is valid, false if not.
5167 See also L</quadmath_format_needed>.
5173 Perl_quadmath_format_valid(const char* format)
5177 PERL_ARGS_ASSERT_QUADMATH_FORMAT_VALID;
5179 if (format[0] != '%' || strchr(format + 1, '%'))
5181 len = strlen(format);
5182 /* minimum length three: %Qg */
5183 if (len < 3 || memCHRs("efgaEFGA", format[len - 1]) == NULL)
5185 if (format[len - 2] != 'Q')
5192 =for apidoc quadmath_format_needed
5194 C<quadmath_format_needed()> returns true if the C<format> string seems to
5195 contain at least one non-Q-prefixed C<%[efgaEFGA]> format specifier,
5196 or returns false otherwise.
5198 The format specifier detection is not complete printf-syntax detection,
5199 but it should catch most common cases.
5201 If true is returned, those arguments B<should> in theory be processed
5202 with C<quadmath_snprintf()>, but in case there is more than one such
5203 format specifier (see L</quadmath_format_valid>), and if there is
5204 anything else beyond that one (even just a single byte), they
5205 B<cannot> be processed because C<quadmath_snprintf()> is very strict,
5206 accepting only one format spec, and nothing else.
5207 In this case, the code should probably fail.
5213 Perl_quadmath_format_needed(const char* format)
5215 const char *p = format;
5218 PERL_ARGS_ASSERT_QUADMATH_FORMAT_NEEDED;
5220 while ((q = strchr(p, '%'))) {
5222 if (*q == '+') /* plus */
5224 if (*q == '#') /* alt */
5226 if (*q == '*') /* width */
5230 while (isDIGIT(*q)) q++;
5233 if (*q == '.' && (q[1] == '*' || isDIGIT(q[1]))) { /* prec */
5238 while (isDIGIT(*q)) q++;
5240 if (memCHRs("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
5249 =for apidoc my_snprintf
5251 The C library C<snprintf> functionality, if available and
5252 standards-compliant (uses C<vsnprintf>, actually). However, if the
5253 C<vsnprintf> is not available, will unfortunately use the unsafe
5254 C<vsprintf> which can overrun the buffer (there is an overrun check,
5255 but that may be too late). Consider using C<sv_vcatpvf> instead, or
5256 getting C<vsnprintf>.
5261 Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...)
5265 PERL_ARGS_ASSERT_MY_SNPRINTF;
5266 #ifndef HAS_VSNPRINTF
5267 PERL_UNUSED_VAR(len);
5269 va_start(ap, format);
5272 bool quadmath_valid = FALSE;
5273 if (quadmath_format_valid(format)) {
5274 /* If the format looked promising, use it as quadmath. */
5275 retval = quadmath_snprintf(buffer, len, format, va_arg(ap, NV));
5277 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5279 quadmath_valid = TRUE;
5281 /* quadmath_format_single() will return false for example for
5282 * "foo = %g", or simply "%g". We could handle the %g by
5283 * using quadmath for the NV args. More complex cases of
5284 * course exist: "foo = %g, bar = %g", or "foo=%Qg" (otherwise
5285 * quadmath-valid but has stuff in front).
5287 * Handling the "Q-less" cases right would require walking
5288 * through the va_list and rewriting the format, calling
5289 * quadmath for the NVs, building a new va_list, and then
5290 * letting vsnprintf/vsprintf to take care of the other
5291 * arguments. This may be doable.
5293 * We do not attempt that now. But for paranoia, we here try
5294 * to detect some common (but not all) cases where the
5295 * "Q-less" %[efgaEFGA] formats are present, and die if
5296 * detected. This doesn't fix the problem, but it stops the
5297 * vsnprintf/vsprintf pulling doubles off the va_list when
5298 * __float128 NVs should be pulled off instead.
5300 * If quadmath_format_needed() returns false, we are reasonably
5301 * certain that we can call vnsprintf() or vsprintf() safely. */
5302 if (!quadmath_valid && quadmath_format_needed(format))
5303 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5308 #ifdef HAS_VSNPRINTF
5309 retval = vsnprintf(buffer, len, format, ap);
5311 retval = vsprintf(buffer, format, ap);
5314 /* vsprintf() shows failure with < 0 */
5316 #ifdef HAS_VSNPRINTF
5317 /* vsnprintf() shows failure with >= len */
5319 (len > 0 && (Size_t)retval >= len)
5322 Perl_croak_nocontext("panic: my_snprintf buffer overflow");
5327 =for apidoc my_vsnprintf
5329 The C library C<vsnprintf> if available and standards-compliant.
5330 However, if the C<vsnprintf> is not available, will unfortunately
5331 use the unsafe C<vsprintf> which can overrun the buffer (there is an
5332 overrun check, but that may be too late). Consider using
5333 C<sv_vcatpvf> instead, or getting C<vsnprintf>.
5338 Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap)
5341 PERL_UNUSED_ARG(buffer);
5342 PERL_UNUSED_ARG(len);
5343 PERL_UNUSED_ARG(format);
5344 /* the cast is to avoid gcc -Wsizeof-array-argument complaining */
5345 PERL_UNUSED_ARG((void*)ap);
5346 Perl_croak_nocontext("panic: my_vsnprintf not available with quadmath");
5353 PERL_ARGS_ASSERT_MY_VSNPRINTF;
5354 Perl_va_copy(ap, apc);
5355 # ifdef HAS_VSNPRINTF
5356 retval = vsnprintf(buffer, len, format, apc);
5358 PERL_UNUSED_ARG(len);
5359 retval = vsprintf(buffer, format, apc);
5363 # ifdef HAS_VSNPRINTF
5364 retval = vsnprintf(buffer, len, format, ap);
5366 PERL_UNUSED_ARG(len);
5367 retval = vsprintf(buffer, format, ap);
5369 #endif /* #ifdef NEED_VA_COPY */
5370 /* vsprintf() shows failure with < 0 */
5372 #ifdef HAS_VSNPRINTF
5373 /* vsnprintf() shows failure with >= len */
5375 (len > 0 && (Size_t)retval >= len)
5378 Perl_croak_nocontext("panic: my_vsnprintf buffer overflow");
5384 Perl_my_clearenv(pTHX)
5386 #if ! defined(PERL_MICRO)
5387 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
5389 # else /* ! (PERL_IMPLICIT_SYS || WIN32) */
5390 # if defined(USE_ENVIRON_ARRAY)
5391 # if defined(USE_ITHREADS)
5392 /* only the parent thread can clobber the process environment, so no need
5394 if (PL_curinterp == aTHX)
5395 # endif /* USE_ITHREADS */
5397 # if ! defined(PERL_USE_SAFE_PUTENV)
5398 if ( !PL_use_safe_putenv) {
5400 if (environ == PL_origenviron)
5401 environ = (char**)safesysmalloc(sizeof(char*));
5403 for (i = 0; environ[i]; i++)
5404 (void)safesysfree(environ[i]);
5407 # else /* PERL_USE_SAFE_PUTENV */
5408 # if defined(HAS_CLEARENV)
5410 # elif defined(HAS_UNSETENV)
5411 int bsiz = 80; /* Most envvar names will be shorter than this. */
5412 char *buf = (char*)safesysmalloc(bsiz);
5413 while (*environ != NULL) {
5414 char *e = strchr(*environ, '=');
5415 int l = e ? e - *environ : (int)strlen(*environ);
5417 (void)safesysfree(buf);
5418 bsiz = l + 1; /* + 1 for the \0. */
5419 buf = (char*)safesysmalloc(bsiz);
5421 memcpy(buf, *environ, l);
5423 (void)unsetenv(buf);
5425 (void)safesysfree(buf);
5426 # else /* ! HAS_CLEARENV && ! HAS_UNSETENV */
5427 /* Just null environ and accept the leakage. */
5429 # endif /* HAS_CLEARENV || HAS_UNSETENV */
5430 # endif /* ! PERL_USE_SAFE_PUTENV */
5432 # endif /* USE_ENVIRON_ARRAY */
5433 # endif /* PERL_IMPLICIT_SYS || WIN32 */
5434 #endif /* PERL_MICRO */
5437 #ifdef PERL_IMPLICIT_CONTEXT
5440 /* Implements the MY_CXT_INIT macro. The first time a module is loaded,
5441 the global PL_my_cxt_index is incremented, and that value is assigned to
5442 that module's static my_cxt_index (who's address is passed as an arg).
5443 Then, for each interpreter this function is called for, it makes sure a
5444 void* slot is available to hang the static data off, by allocating or
5445 extending the interpreter's PL_my_cxt_list array */
5448 Perl_my_cxt_init(pTHX_ int *indexp, size_t size)
5453 PERL_ARGS_ASSERT_MY_CXT_INIT;
5456 /* do initial check without locking.
5457 * -1: not allocated or another thread currently allocating
5458 * other: already allocated by another thread
5461 MUTEX_LOCK(&PL_my_ctx_mutex);
5462 /*now a stricter check with locking */
5465 /* this module hasn't been allocated an index yet */
5466 *indexp = PL_my_cxt_index++;
5468 MUTEX_UNLOCK(&PL_my_ctx_mutex);
5471 /* make sure the array is big enough */
5472 if (PL_my_cxt_size <= index) {
5473 if (PL_my_cxt_size) {
5474 IV new_size = PL_my_cxt_size;
5475 while (new_size <= index)
5477 Renew(PL_my_cxt_list, new_size, void *);
5478 PL_my_cxt_size = new_size;
5481 PL_my_cxt_size = 16;
5482 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
5485 /* newSV() allocates one more than needed */
5486 p = (void*)SvPVX(newSV(size-1));
5487 PL_my_cxt_list[index] = p;
5488 Zero(p, size, char);
5492 #endif /* PERL_IMPLICIT_CONTEXT */
5495 /* Perl_xs_handshake():
5496 implement the various XS_*_BOOTCHECK macros, which are added to .c
5497 files by ExtUtils::ParseXS, to check that the perl the module was built
5498 with is binary compatible with the running perl.
5501 Perl_xs_handshake(U32 key, void * v_my_perl, const char * file,
5502 [U32 items, U32 ax], [char * api_version], [char * xs_version])
5504 The meaning of the varargs is determined the U32 key arg (which is not
5505 a format string). The fields of key are assembled by using HS_KEY().
5507 Under PERL_IMPLICIT_CONTEX, the v_my_perl arg is of type
5508 "PerlInterpreter *" and represents the callers context; otherwise it is
5509 of type "CV *", and is the boot xsub's CV.
5511 v_my_perl will catch where a threaded future perl526.dll calling IO.dll
5512 for example, and IO.dll was linked with threaded perl524.dll, and both
5513 perl526.dll and perl524.dll are in %PATH and the Win32 DLL loader
5514 successfully can load IO.dll into the process but simultaneously it
5515 loaded an interpreter of a different version into the process, and XS
5516 code will naturally pass SV*s created by perl524.dll for perl526.dll to
5517 use through perl526.dll's my_perl->Istack_base.
5519 v_my_perl cannot be the first arg, since then 'key' will be out of
5520 place in a threaded vs non-threaded mixup; and analyzing the key
5521 number's bitfields won't reveal the problem, since it will be a valid
5522 key (unthreaded perl) on interp side, but croak will report the XS mod's
5523 key as gibberish (it is really a my_perl ptr) (threaded XS mod); or if
5524 it's a threaded perl and an unthreaded XS module, threaded perl will
5525 look at an uninit C stack or an uninit register to get 'key'
5526 (remember that it assumes that the 1st arg is the interp cxt).
5528 'file' is the source filename of the caller.
5532 Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
5538 #ifdef PERL_IMPLICIT_CONTEXT
5545 PERL_ARGS_ASSERT_XS_HANDSHAKE;
5546 va_start(args, file);
5548 got = INT2PTR(void*, (UV)(key & HSm_KEY_MATCH));
5549 need = (void *)(HS_KEY(FALSE, FALSE, "", "") & HSm_KEY_MATCH);
5550 if (UNLIKELY(got != need))
5552 /* try to catch where a 2nd threaded perl interp DLL is loaded into a process
5553 by a XS DLL compiled against the wrong interl DLL b/c of bad @INC, and the
5554 2nd threaded perl interp DLL never initialized its TLS/PERL_SYS_INIT3 so
5555 dTHX call from 2nd interp DLL can't return the my_perl that pp_entersub
5556 passed to the XS DLL */
5557 #ifdef PERL_IMPLICIT_CONTEXT
5558 xs_interp = (tTHX)v_my_perl;
5562 /* try to catch where an unthreaded perl interp DLL (for ex. perl522.dll) is
5563 loaded into a process by a XS DLL built by an unthreaded perl522.dll perl,
5564 but the DynaLoder/Perl that started the process and loaded the XS DLL is
5565 unthreaded perl524.dll, since unthreadeds don't pass my_perl (a unique *)
5566 through pp_entersub, use a unique value (which is a pointer to PL_stack_sp's
5567 location in the unthreaded perl binary) stored in CV * to figure out if this
5568 Perl_xs_handshake was called by the same pp_entersub */
5569 cv = (CV*)v_my_perl;
5570 xs_spp = (SV***)CvHSCXT(cv);
5572 need = &PL_stack_sp;
5574 if(UNLIKELY(got != need)) {
5575 bad_handshake:/* recycle branch and string from above */
5576 if(got != (void *)HSf_NOCHK)
5577 noperl_die("%s: loadable library and perl binaries are mismatched"
5578 " (got handshake key %p, needed %p)\n",
5582 if(key & HSf_SETXSUBFN) { /* this might be called from a module bootstrap */
5583 SAVEPPTR(PL_xsubfilename);/* which was require'd from a XSUB BEGIN */
5584 PL_xsubfilename = file; /* so the old name must be restored for
5585 additional XSUBs to register themselves */
5586 /* XSUBs can't be perl lang/perl5db.pl debugged
5587 if (PERLDB_LINE_OR_SAVESRC)
5588 (void)gv_fetchfile(file); */
5591 if(key & HSf_POPMARK) {
5593 { SV **mark = PL_stack_base + ax++;
5595 items = (I32)(SP - MARK);
5599 items = va_arg(args, U32);
5600 ax = va_arg(args, U32);
5604 assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX);
5605 if((apiverlen = HS_GETAPIVERLEN(key))) {
5606 char * api_p = va_arg(args, char*);
5607 if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1
5608 || memNE(api_p, "v" PERL_API_VERSION_STRING,
5609 sizeof("v" PERL_API_VERSION_STRING)-1))
5610 Perl_croak_nocontext("Perl API version %s of %" SVf " does not match %s",
5611 api_p, SVfARG(PL_stack_base[ax + 0]),
5612 "v" PERL_API_VERSION_STRING);
5617 assert(HS_GETXSVERLEN(key) <= UCHAR_MAX && HS_GETXSVERLEN(key) <= HS_APIVERLEN_MAX);
5618 if((xsverlen = HS_GETXSVERLEN(key)))
5619 S_xs_version_bootcheck(aTHX_
5620 items, ax, va_arg(args, char*), xsverlen);
5628 S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p,
5632 const char *vn = NULL;
5633 SV *const module = PL_stack_base[ax];
5635 PERL_ARGS_ASSERT_XS_VERSION_BOOTCHECK;
5637 if (items >= 2) /* version supplied as bootstrap arg */
5638 sv = PL_stack_base[ax + 1];
5640 /* XXX GV_ADDWARN */
5642 sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5643 if (!sv || !SvOK(sv)) {
5645 sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5649 SV *xssv = Perl_newSVpvn_flags(aTHX_ xs_p, xs_len, SVs_TEMP);
5650 SV *pmsv = sv_isobject(sv) && sv_derived_from(sv, "version")
5651 ? sv : sv_2mortal(new_version(sv));
5652 xssv = upg_version(xssv, 0);
5653 if ( vcmp(pmsv,xssv) ) {
5654 SV *string = vstringify(xssv);
5655 SV *xpt = Perl_newSVpvf(aTHX_ "%" SVf " object version %" SVf
5656 " does not match ", SVfARG(module), SVfARG(string));
5658 SvREFCNT_dec(string);
5659 string = vstringify(pmsv);
5662 Perl_sv_catpvf(aTHX_ xpt, "$%" SVf "::%s %" SVf, SVfARG(module), vn,
5665 Perl_sv_catpvf(aTHX_ xpt, "bootstrap parameter %" SVf, SVfARG(string));
5667 SvREFCNT_dec(string);
5669 Perl_sv_2mortal(aTHX_ xpt);
5670 Perl_croak_sv(aTHX_ xpt);
5676 =for apidoc my_strlcat
5678 The C library C<strlcat> if available, or a Perl implementation of it.
5679 This operates on C C<NUL>-terminated strings.
5681 C<my_strlcat()> appends string C<src> to the end of C<dst>. It will append at
5682 most S<C<size - strlen(dst) - 1>> characters. It will then C<NUL>-terminate,
5683 unless C<size> is 0 or the original C<dst> string was longer than C<size> (in
5684 practice this should not happen as it means that either C<size> is incorrect or
5685 that C<dst> is not a proper C<NUL>-terminated string).
5687 Note that C<size> is the full size of the destination buffer and
5688 the result is guaranteed to be C<NUL>-terminated if there is room. Note that
5689 room for the C<NUL> should be included in C<size>.
5691 The return value is the total length that C<dst> would have if C<size> is
5692 sufficiently large. Thus it is the initial length of C<dst> plus the length of
5693 C<src>. If C<size> is smaller than the return, the excess was not appended.
5697 Description stolen from http://man.openbsd.org/strlcat.3
5701 Perl_my_strlcat(char *dst, const char *src, Size_t size)
5703 Size_t used, length, copy;
5706 length = strlen(src);
5707 if (size > 0 && used < size - 1) {
5708 copy = (length >= size - used) ? size - used - 1 : length;
5709 memcpy(dst + used, src, copy);
5710 dst[used + copy] = '\0';
5712 return used + length;
5718 =for apidoc my_strlcpy
5720 The C library C<strlcpy> if available, or a Perl implementation of it.
5721 This operates on C C<NUL>-terminated strings.
5723 C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src>
5724 to C<dst>, C<NUL>-terminating the result if C<size> is not 0.
5726 The return value is the total length C<src> would be if the copy completely
5727 succeeded. If it is larger than C<size>, the excess was not copied.
5731 Description stolen from http://man.openbsd.org/strlcpy.3
5735 Perl_my_strlcpy(char *dst, const char *src, Size_t size)
5737 Size_t length, copy;
5739 length = strlen(src);
5741 copy = (length >= size) ? size - 1 : length;
5742 memcpy(dst, src, copy);
5749 #if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_MSC_VER < 1400) && (WINVER < 0x0500)
5750 /* VC7 or 7.1, building with pre-VC7 runtime libraries. */
5751 long _ftol( double ); /* Defined by VC6 C libs. */
5752 long _ftol2( double dblSource ) { return _ftol( dblSource ); }
5755 PERL_STATIC_INLINE bool
5756 S_gv_has_usable_name(pTHX_ GV *gv)
5760 && HvENAME(GvSTASH(gv))
5761 && (gvp = (GV **)hv_fetchhek(
5762 GvSTASH(gv), GvNAME_HEK(gv), 0
5768 Perl_get_db_sub(pTHX_ SV **svp, CV *cv)
5770 SV * const dbsv = GvSVn(PL_DBsub);
5771 const bool save_taint = TAINT_get;
5773 /* When we are called from pp_goto (svp is null),
5774 * we do not care about using dbsv to call CV;
5775 * it's for informational purposes only.
5778 PERL_ARGS_ASSERT_GET_DB_SUB;
5782 if (!PERLDB_SUB_NN) {
5785 if (!svp && !CvLEXICAL(cv)) {
5786 gv_efullname3(dbsv, gv, NULL);
5788 else if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) || CvLEXICAL(cv)
5789 || strEQ(GvNAME(gv), "END")
5790 || ( /* Could be imported, and old sub redefined. */
5791 (GvCV(gv) != cv || !S_gv_has_usable_name(aTHX_ gv))
5793 !( (SvTYPE(*svp) == SVt_PVGV)
5794 && (GvCV((const GV *)*svp) == cv)
5795 /* Use GV from the stack as a fallback. */
5796 && S_gv_has_usable_name(aTHX_ gv = (GV *)*svp)
5800 /* GV is potentially non-unique, or contain different CV. */
5801 SV * const tmp = newRV(MUTABLE_SV(cv));
5802 sv_setsv(dbsv, tmp);
5806 sv_sethek(dbsv, HvENAME_HEK(GvSTASH(gv)));
5807 sv_catpvs(dbsv, "::");
5808 sv_cathek(dbsv, GvNAME_HEK(gv));
5812 const int type = SvTYPE(dbsv);
5813 if (type < SVt_PVIV && type != SVt_IV)
5814 sv_upgrade(dbsv, SVt_PVIV);
5815 (void)SvIOK_on(dbsv);
5816 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
5819 TAINT_IF(save_taint);
5820 #ifdef NO_TAINT_SUPPORT
5821 PERL_UNUSED_VAR(save_taint);
5826 Perl_my_dirfd(DIR * dir) {
5828 /* Most dirfd implementations have problems when passed NULL. */
5833 #elif defined(HAS_DIR_DD_FD)
5836 Perl_croak_nocontext(PL_no_func, "dirfd");
5837 NOT_REACHED; /* NOTREACHED */
5842 #if !defined(HAS_MKOSTEMP) || !defined(HAS_MKSTEMP)
5844 #define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
5845 #define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
5848 S_my_mkostemp(char *templte, int flags) {
5850 STRLEN len = strlen(templte);
5854 int delete_on_close = flags & O_VMS_DELETEONCLOSE;
5856 flags &= ~O_VMS_DELETEONCLOSE;
5860 templte[len-1] != 'X' || templte[len-2] != 'X' || templte[len-3] != 'X' ||
5861 templte[len-4] != 'X' || templte[len-5] != 'X' || templte[len-6] != 'X') {
5862 SETERRNO(EINVAL, LIB_INVARG);
5868 for (i = 1; i <= 6; ++i) {
5869 templte[len-i] = TEMP_FILE_CH[(int)(Perl_internal_drand48() * TEMP_FILE_CH_COUNT)];
5872 if (delete_on_close) {
5873 fd = open(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600, "fop=dlt");
5878 fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
5880 } while (fd == -1 && errno == EEXIST && ++attempts <= 100);
5887 #ifndef HAS_MKOSTEMP
5889 Perl_my_mkostemp(char *templte, int flags)
5891 PERL_ARGS_ASSERT_MY_MKOSTEMP;
5892 return S_my_mkostemp(templte, flags);
5898 Perl_my_mkstemp(char *templte)
5900 PERL_ARGS_ASSERT_MY_MKSTEMP;
5901 return S_my_mkostemp(templte, 0);
5906 Perl_get_re_arg(pTHX_ SV *sv) {
5912 sv = MUTABLE_SV(SvRV(sv));
5913 if (SvTYPE(sv) == SVt_REGEXP)
5914 return (REGEXP*) sv;
5921 * This code is derived from drand48() implementation from FreeBSD,
5922 * found in lib/libc/gen/_rand48.c.
5924 * The U64 implementation is original, based on the POSIX
5925 * specification for drand48().
5929 * Copyright (c) 1993 Martin Birgmeier
5930 * All rights reserved.
5932 * You may redistribute unmodified or modified versions of this source
5933 * code provided that the above copyright notice and this and the
5934 * following conditions are retained.
5936 * This software is provided ``as is'', and comes with no warranties
5937 * of any kind. I shall in no event be liable for anything that happens
5938 * to anyone/anything when using this software.
5941 #define FREEBSD_DRAND48_SEED_0 (0x330e)
5943 #ifdef PERL_DRAND48_QUAD
5945 #define DRAND48_MULT UINT64_C(0x5deece66d)
5946 #define DRAND48_ADD 0xb
5947 #define DRAND48_MASK UINT64_C(0xffffffffffff)
5951 #define FREEBSD_DRAND48_SEED_1 (0xabcd)
5952 #define FREEBSD_DRAND48_SEED_2 (0x1234)
5953 #define FREEBSD_DRAND48_MULT_0 (0xe66d)
5954 #define FREEBSD_DRAND48_MULT_1 (0xdeec)
5955 #define FREEBSD_DRAND48_MULT_2 (0x0005)
5956 #define FREEBSD_DRAND48_ADD (0x000b)
5958 const unsigned short _rand48_mult[3] = {
5959 FREEBSD_DRAND48_MULT_0,
5960 FREEBSD_DRAND48_MULT_1,
5961 FREEBSD_DRAND48_MULT_2
5963 const unsigned short _rand48_add = FREEBSD_DRAND48_ADD;
5968 Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed)
5970 PERL_ARGS_ASSERT_DRAND48_INIT_R;
5972 #ifdef PERL_DRAND48_QUAD
5973 *random_state = FREEBSD_DRAND48_SEED_0 + ((U64)seed << 16);
5975 random_state->seed[0] = FREEBSD_DRAND48_SEED_0;
5976 random_state->seed[1] = (U16) seed;
5977 random_state->seed[2] = (U16) (seed >> 16);
5982 Perl_drand48_r(perl_drand48_t *random_state)
5984 PERL_ARGS_ASSERT_DRAND48_R;
5986 #ifdef PERL_DRAND48_QUAD
5987 *random_state = (*random_state * DRAND48_MULT + DRAND48_ADD)
5990 return ldexp((double)*random_state, -48);
5996 accu = (U32) _rand48_mult[0] * (U32) random_state->seed[0]
5997 + (U32) _rand48_add;
5998 temp[0] = (U16) accu; /* lower 16 bits */
5999 accu >>= sizeof(U16) * 8;
6000 accu += (U32) _rand48_mult[0] * (U32) random_state->seed[1]
6001 + (U32) _rand48_mult[1] * (U32) random_state->seed[0];
6002 temp[1] = (U16) accu; /* middle 16 bits */
6003 accu >>= sizeof(U16) * 8;
6004 accu += _rand48_mult[0] * random_state->seed[2]
6005 + _rand48_mult[1] * random_state->seed[1]
6006 + _rand48_mult[2] * random_state->seed[0];
6007 random_state->seed[0] = temp[0];
6008 random_state->seed[1] = temp[1];
6009 random_state->seed[2] = (U16) accu;
6011 return ldexp((double) random_state->seed[0], -48) +
6012 ldexp((double) random_state->seed[1], -32) +
6013 ldexp((double) random_state->seed[2], -16);
6018 #ifdef USE_C_BACKTRACE
6020 /* Possibly move all this USE_C_BACKTRACE code into a new file. */
6025 /* abfd is the BFD handle. */
6027 /* bfd_syms is the BFD symbol table. */
6029 /* bfd_text is handle to the the ".text" section of the object file. */
6031 /* Since opening the executable and scanning its symbols is quite
6032 * heavy operation, we remember the filename we used the last time,
6033 * and do the opening and scanning only if the filename changes.
6034 * This removes most (but not all) open+scan cycles. */
6035 const char* fname_prev;
6038 /* Given a dl_info, update the BFD context if necessary. */
6039 static void bfd_update(bfd_context* ctx, Dl_info* dl_info)
6041 /* BFD open and scan only if the filename changed. */
6042 if (ctx->fname_prev == NULL ||
6043 strNE(dl_info->dli_fname, ctx->fname_prev)) {
6045 bfd_close(ctx->abfd);
6047 ctx->abfd = bfd_openr(dl_info->dli_fname, 0);
6049 if (bfd_check_format(ctx->abfd, bfd_object)) {
6050 IV symbol_size = bfd_get_symtab_upper_bound(ctx->abfd);
6051 if (symbol_size > 0) {
6052 Safefree(ctx->bfd_syms);
6053 Newx(ctx->bfd_syms, symbol_size, asymbol*);
6055 bfd_get_section_by_name(ctx->abfd, ".text");
6063 ctx->fname_prev = dl_info->dli_fname;
6067 /* Given a raw frame, try to symbolize it and store
6068 * symbol information (source file, line number) away. */
6069 static void bfd_symbolize(bfd_context* ctx,
6072 STRLEN* symbol_name_size,
6074 STRLEN* source_name_size,
6075 STRLEN* source_line)
6077 *symbol_name = NULL;
6078 *symbol_name_size = 0;
6080 IV offset = PTR2IV(raw_frame) - PTR2IV(ctx->bfd_text->vma);
6082 bfd_canonicalize_symtab(ctx->abfd, ctx->bfd_syms) > 0) {
6085 unsigned int line = 0;
6086 if (bfd_find_nearest_line(ctx->abfd, ctx->bfd_text,
6087 ctx->bfd_syms, offset,
6088 &file, &func, &line) &&
6089 file && func && line > 0) {
6090 /* Size and copy the source file, use only
6091 * the basename of the source file.
6093 * NOTE: the basenames are fine for the
6094 * Perl source files, but may not always
6095 * be the best idea for XS files. */
6096 const char *p, *b = NULL;
6097 /* Look for the last slash. */
6098 for (p = file; *p; p++) {
6102 if (b == NULL || *b == 0) {
6105 *source_name_size = p - b + 1;
6106 Newx(*source_name, *source_name_size + 1, char);
6107 Copy(b, *source_name, *source_name_size + 1, char);
6109 *symbol_name_size = strlen(func);
6110 Newx(*symbol_name, *symbol_name_size + 1, char);
6111 Copy(func, *symbol_name, *symbol_name_size + 1, char);
6113 *source_line = line;
6119 #endif /* #ifdef USE_BFD */
6123 /* OS X has no public API for for 'symbolicating' (Apple official term)
6124 * stack addresses to {function_name, source_file, line_number}.
6125 * Good news: there is command line utility atos(1) which does that.
6126 * Bad news 1: it's a command line utility.
6127 * Bad news 2: one needs to have the Developer Tools installed.
6128 * Bad news 3: in newer releases it needs to be run as 'xcrun atos'.
6130 * To recap: we need to open a pipe for reading for a utility which
6131 * might not exist, or exists in different locations, and then parse
6132 * the output. And since this is all for a low-level API, we cannot
6133 * use high-level stuff. Thanks, Apple. */
6136 /* tool is set to the absolute pathname of the tool to use:
6139 /* format is set to a printf format string used for building
6140 * the external command to run. */
6142 /* unavail is set if e.g. xcrun cannot be found, or something
6143 * else happens that makes getting the backtrace dubious. Note,
6144 * however, that the context isn't persistent, the next call to
6145 * get_c_backtrace() will start from scratch. */
6147 /* fname is the current object file name. */
6149 /* object_base_addr is the base address of the shared object. */
6150 void* object_base_addr;
6153 /* Given |dl_info|, updates the context. If the context has been
6154 * marked unavailable, return immediately. If not but the tool has
6155 * not been set, set it to either "xcrun atos" or "atos" (also set the
6156 * format to use for creating commands for piping), or if neither is
6157 * unavailable (one needs the Developer Tools installed), mark the context
6158 * an unavailable. Finally, update the filename (object name),
6159 * and its base address. */
6161 static void atos_update(atos_context* ctx,
6166 if (ctx->tool == NULL) {
6167 const char* tools[] = {
6171 const char* formats[] = {
6172 "/usr/bin/xcrun atos -o '%s' -l %08x %08x 2>&1",
6173 "/usr/bin/atos -d -o '%s' -l %08x %08x 2>&1"
6177 for (i = 0; i < C_ARRAY_LENGTH(tools); i++) {
6178 if (stat(tools[i], &st) == 0 && S_ISREG(st.st_mode)) {
6179 ctx->tool = tools[i];
6180 ctx->format = formats[i];
6184 if (ctx->tool == NULL) {
6185 ctx->unavail = TRUE;
6189 if (ctx->fname == NULL ||
6190 strNE(dl_info->dli_fname, ctx->fname)) {
6191 ctx->fname = dl_info->dli_fname;
6192 ctx->object_base_addr = dl_info->dli_fbase;
6196 /* Given an output buffer end |p| and its |start|, matches
6197 * for the atos output, extracting the source code location
6198 * and returning non-NULL if possible, returning NULL otherwise. */
6199 static const char* atos_parse(const char* p,
6201 STRLEN* source_name_size,
6202 STRLEN* source_line) {
6203 /* atos() output is something like:
6204 * perl_parse (in miniperl) (perl.c:2314)\n\n".
6205 * We cannot use Perl regular expressions, because we need to
6206 * stay low-level. Therefore here we have a rolled-out version
6207 * of a state machine which matches _backwards_from_the_end_ and
6208 * if there's a success, returns the starts of the filename,
6209 * also setting the filename size and the source line number.
6210 * The matched regular expression is roughly "\(.*:\d+\)\s*$" */
6211 const char* source_number_start;
6212 const char* source_name_end;
6213 const char* source_line_end = start;
6214 const char* close_paren;
6217 /* Skip trailing whitespace. */
6218 while (p > start && isSPACE(*p)) p--;
6219 /* Now we should be at the close paren. */
6220 if (p == start || *p != ')')
6224 /* Now we should be in the line number. */
6225 if (p == start || !isDIGIT(*p))
6227 /* Skip over the digits. */
6228 while (p > start && isDIGIT(*p))
6230 /* Now we should be at the colon. */
6231 if (p == start || *p != ':')
6233 source_number_start = p + 1;
6234 source_name_end = p; /* Just beyond the end. */
6236 /* Look for the open paren. */
6237 while (p > start && *p != '(')
6242 *source_name_size = source_name_end - p;
6243 if (grok_atoUV(source_number_start, &uv, &source_line_end)
6244 && source_line_end == close_paren
6245 && uv <= PERL_INT_MAX
6247 *source_line = (STRLEN)uv;
6253 /* Given a raw frame, read a pipe from the symbolicator (that's the
6254 * technical term) atos, reads the result, and parses the source code
6255 * location. We must stay low-level, so we use snprintf(), pipe(),
6256 * and fread(), and then also parse the output ourselves. */
6257 static void atos_symbolize(atos_context* ctx,
6260 STRLEN* source_name_size,
6261 STRLEN* source_line)
6269 /* Simple security measure: if there's any funny business with
6270 * the object name (used as "-o '%s'" ), leave since at least
6271 * partially the user controls it. */
6272 for (p = ctx->fname; *p; p++) {
6273 if (*p == '\'' || isCNTRL(*p)) {
6274 ctx->unavail = TRUE;
6278 cnt = snprintf(cmd, sizeof(cmd), ctx->format,
6279 ctx->fname, ctx->object_base_addr, raw_frame);
6280 if (cnt < sizeof(cmd)) {
6281 /* Undo nostdio.h #defines that disable stdio.
6282 * This is somewhat naughty, but is used elsewhere
6283 * in the core, and affects only OS X. */
6288 FILE* fp = popen(cmd, "r");
6289 /* At the moment we open a new pipe for each stack frame.
6290 * This is naturally somewhat slow, but hopefully generating
6291 * stack traces is never going to in a performance critical path.
6293 * We could play tricks with atos by batching the stack
6294 * addresses to be resolved: atos can either take multiple
6295 * addresses from the command line, or read addresses from
6296 * a file (though the mess of creating temporary files would
6297 * probably negate much of any possible speedup).
6299 * Normally there are only two objects present in the backtrace:
6300 * perl itself, and the libdyld.dylib. (Note that the object
6301 * filenames contain the full pathname, so perl may not always
6302 * be in the same place.) Whenever the object in the
6303 * backtrace changes, the base address also changes.
6305 * The problem with batching the addresses, though, would be
6306 * matching the results with the addresses: the parsing of
6307 * the results is already painful enough with a single address. */
6310 UV cnt = fread(out, 1, sizeof(out), fp);
6311 if (cnt < sizeof(out)) {
6312 const char* p = atos_parse(out + cnt - 1, out,
6317 *source_name_size, char);
6318 Copy(p, *source_name,
6319 *source_name_size, char);
6327 #endif /* #ifdef PERL_DARWIN */
6330 =for apidoc_section Display and Dump functions
6331 =for apidoc get_c_backtrace
6333 Collects the backtrace (aka "stacktrace") into a single linear
6334 malloced buffer, which the caller B<must> C<Perl_free_c_backtrace()>.
6336 Scans the frames back by S<C<depth + skip>>, then drops the C<skip> innermost,
6337 returning at most C<depth> frames.
6343 Perl_get_c_backtrace(pTHX_ int depth, int skip)
6345 /* Note that here we must stay as low-level as possible: Newx(),
6346 * Copy(), Safefree(); since we may be called from anywhere,
6347 * so we should avoid higher level constructs like SVs or AVs.
6349 * Since we are using safesysmalloc() via Newx(), don't try
6350 * getting backtrace() there, unless you like deep recursion. */
6352 /* Currently only implemented with backtrace() and dladdr(),
6353 * for other platforms NULL is returned. */
6355 #if defined(HAS_BACKTRACE) && defined(HAS_DLADDR)
6356 /* backtrace() is available via <execinfo.h> in glibc and in most
6357 * modern BSDs; dladdr() is available via <dlfcn.h>. */
6359 /* We try fetching this many frames total, but then discard
6360 * the |skip| first ones. For the remaining ones we will try
6361 * retrieving more information with dladdr(). */
6362 int try_depth = skip + depth;
6364 /* The addresses (program counters) returned by backtrace(). */
6367 /* Retrieved with dladdr() from the addresses returned by backtrace(). */
6370 /* Sizes _including_ the terminating \0 of the object name
6371 * and symbol name strings. */
6372 STRLEN* object_name_sizes;
6373 STRLEN* symbol_name_sizes;
6376 /* The symbol names comes either from dli_sname,
6377 * or if using BFD, they can come from BFD. */
6378 char** symbol_names;
6381 /* The source code location information. Dug out with e.g. BFD. */
6382 char** source_names;
6383 STRLEN* source_name_sizes;
6384 STRLEN* source_lines;
6386 Perl_c_backtrace* bt = NULL; /* This is what will be returned. */
6387 int got_depth; /* How many frames were returned from backtrace(). */
6388 UV frame_count = 0; /* How many frames we return. */
6389 UV total_bytes = 0; /* The size of the whole returned backtrace. */
6392 bfd_context bfd_ctx;
6395 atos_context atos_ctx;
6398 /* Here are probably possibilities for optimizing. We could for
6399 * example have a struct that contains most of these and then
6400 * allocate |try_depth| of them, saving a bunch of malloc calls.
6401 * Note, however, that |frames| could not be part of that struct
6402 * because backtrace() will want an array of just them. Also be
6403 * careful about the name strings. */
6404 Newx(raw_frames, try_depth, void*);
6405 Newx(dl_infos, try_depth, Dl_info);
6406 Newx(object_name_sizes, try_depth, STRLEN);
6407 Newx(symbol_name_sizes, try_depth, STRLEN);
6408 Newx(source_names, try_depth, char*);
6409 Newx(source_name_sizes, try_depth, STRLEN);
6410 Newx(source_lines, try_depth, STRLEN);
6412 Newx(symbol_names, try_depth, char*);
6415 /* Get the raw frames. */
6416 got_depth = (int)backtrace(raw_frames, try_depth);
6418 /* We use dladdr() instead of backtrace_symbols() because we want
6419 * the full details instead of opaque strings. This is useful for
6420 * two reasons: () the details are needed for further symbolic
6421 * digging, for example in OS X (2) by having the details we fully
6422 * control the output, which in turn is useful when more platforms
6423 * are added: we can keep out output "portable". */
6425 /* We want a single linear allocation, which can then be freed
6426 * with a single swoop. We will do the usual trick of first
6427 * walking over the structure and seeing how much we need to
6428 * allocate, then allocating, and then walking over the structure
6429 * the second time and populating it. */
6431 /* First we must compute the total size of the buffer. */
6432 total_bytes = sizeof(Perl_c_backtrace_header);
6433 if (got_depth > skip) {
6436 bfd_init(); /* Is this safe to call multiple times? */
6437 Zero(&bfd_ctx, 1, bfd_context);
6440 Zero(&atos_ctx, 1, atos_context);
6442 for (i = skip; i < try_depth; i++) {
6443 Dl_info* dl_info = &dl_infos[i];
6445 object_name_sizes[i] = 0;
6446 source_names[i] = NULL;
6447 source_name_sizes[i] = 0;
6448 source_lines[i] = 0;
6450 /* Yes, zero from dladdr() is failure. */
6451 if (dladdr(raw_frames[i], dl_info)) {
6452 total_bytes += sizeof(Perl_c_backtrace_frame);
6454 object_name_sizes[i] =
6455 dl_info->dli_fname ? strlen(dl_info->dli_fname) : 0;
6456 symbol_name_sizes[i] =
6457 dl_info->dli_sname ? strlen(dl_info->dli_sname) : 0;
6459 bfd_update(&bfd_ctx, dl_info);
6460 bfd_symbolize(&bfd_ctx, raw_frames[i],
6462 &symbol_name_sizes[i],
6464 &source_name_sizes[i],
6468 atos_update(&atos_ctx, dl_info);
6469 atos_symbolize(&atos_ctx,
6472 &source_name_sizes[i],
6476 /* Plus ones for the terminating \0. */
6477 total_bytes += object_name_sizes[i] + 1;
6478 total_bytes += symbol_name_sizes[i] + 1;
6479 total_bytes += source_name_sizes[i] + 1;
6487 Safefree(bfd_ctx.bfd_syms);
6491 /* Now we can allocate and populate the result buffer. */
6492 Newxc(bt, total_bytes, char, Perl_c_backtrace);
6493 Zero(bt, total_bytes, char);
6494 bt->header.frame_count = frame_count;
6495 bt->header.total_bytes = total_bytes;
6496 if (frame_count > 0) {
6497 Perl_c_backtrace_frame* frame = bt->frame_info;
6498 char* name_base = (char *)(frame + frame_count);
6499 char* name_curr = name_base; /* Outputting the name strings here. */
6501 for (i = skip; i < skip + frame_count; i++) {
6502 Dl_info* dl_info = &dl_infos[i];
6504 frame->addr = raw_frames[i];
6505 frame->object_base_addr = dl_info->dli_fbase;
6506 frame->symbol_addr = dl_info->dli_saddr;
6508 /* Copies a string, including the \0, and advances the name_curr.
6509 * Also copies the start and the size to the frame. */
6510 #define PERL_C_BACKTRACE_STRCPY(frame, doffset, src, dsize, size) \
6512 Copy(src, name_curr, size, char); \
6513 frame->doffset = name_curr - (char*)bt; \
6514 frame->dsize = size; \
6515 name_curr += size; \
6518 PERL_C_BACKTRACE_STRCPY(frame, object_name_offset,
6520 object_name_size, object_name_sizes[i]);
6523 PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6525 symbol_name_size, symbol_name_sizes[i]);
6526 Safefree(symbol_names[i]);
6528 PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6530 symbol_name_size, symbol_name_sizes[i]);
6533 PERL_C_BACKTRACE_STRCPY(frame, source_name_offset,
6535 source_name_size, source_name_sizes[i]);
6536 Safefree(source_names[i]);
6538 #undef PERL_C_BACKTRACE_STRCPY
6540 frame->source_line_number = source_lines[i];
6544 assert(total_bytes ==
6545 (UV)(sizeof(Perl_c_backtrace_header) +
6546 frame_count * sizeof(Perl_c_backtrace_frame) +
6547 name_curr - name_base));
6550 Safefree(symbol_names);
6552 bfd_close(bfd_ctx.abfd);
6555 Safefree(source_lines);
6556 Safefree(source_name_sizes);
6557 Safefree(source_names);
6558 Safefree(symbol_name_sizes);
6559 Safefree(object_name_sizes);
6560 /* Assuming the strings returned by dladdr() are pointers
6561 * to read-only static memory (the object file), so that
6562 * they do not need freeing (and cannot be). */
6564 Safefree(raw_frames);
6567 PERL_UNUSED_ARG(depth);
6568 PERL_UNUSED_ARG(skip);
6574 =for apidoc free_c_backtrace
6576 Deallocates a backtrace received from get_c_backtrace.
6582 =for apidoc_section Display and Dump functions
6583 =for apidoc get_c_backtrace_dump
6585 Returns a SV containing a dump of C<depth> frames of the call stack, skipping
6586 the C<skip> innermost ones. C<depth> of 20 is usually enough.
6588 The appended output looks like:
6591 1 10e004812:0082 Perl_croak util.c:1716 /usr/bin/perl
6592 2 10df8d6d2:1d72 perl_parse perl.c:3975 /usr/bin/perl
6595 The fields are tab-separated. The first column is the depth (zero
6596 being the innermost non-skipped frame). In the hex:offset, the hex is
6597 where the program counter was in C<S_parse_body>, and the :offset (might
6598 be missing) tells how much inside the C<S_parse_body> the program counter was.
6600 The C<util.c:1716> is the source code file and line number.
6602 The F</usr/bin/perl> is obvious (hopefully).
6604 Unknowns are C<"-">. Unknowns can happen unfortunately quite easily:
6605 if the platform doesn't support retrieving the information;
6606 if the binary is missing the debug information;
6607 if the optimizer has transformed the code by for example inlining.
6613 Perl_get_c_backtrace_dump(pTHX_ int depth, int skip)
6615 Perl_c_backtrace* bt;
6617 bt = get_c_backtrace(depth, skip + 1 /* Hide ourselves. */);
6619 Perl_c_backtrace_frame* frame;
6620 SV* dsv = newSVpvs("");
6622 for (i = 0, frame = bt->frame_info;
6623 i < bt->header.frame_count; i++, frame++) {
6624 Perl_sv_catpvf(aTHX_ dsv, "%d", (int)i);
6625 Perl_sv_catpvf(aTHX_ dsv, "\t%p", frame->addr ? frame->addr : "-");
6626 /* Symbol (function) names might disappear without debug info.
6628 * The source code location might disappear in case of the
6629 * optimizer inlining or otherwise rearranging the code. */
6630 if (frame->symbol_addr) {
6631 Perl_sv_catpvf(aTHX_ dsv, ":%04x",
6633 ((char*)frame->addr - (char*)frame->symbol_addr));
6635 Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6636 frame->symbol_name_size &&
6637 frame->symbol_name_offset ?
6638 (char*)bt + frame->symbol_name_offset : "-");
6639 if (frame->source_name_size &&
6640 frame->source_name_offset &&
6641 frame->source_line_number) {
6642 Perl_sv_catpvf(aTHX_ dsv, "\t%s:%" UVuf,
6643 (char*)bt + frame->source_name_offset,
6644 (UV)frame->source_line_number);
6646 Perl_sv_catpvf(aTHX_ dsv, "\t-");
6648 Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6649 frame->object_name_size &&
6650 frame->object_name_offset ?
6651 (char*)bt + frame->object_name_offset : "-");
6652 /* The frame->object_base_addr is not output,
6653 * but it is used for symbolizing/symbolicating. */
6654 sv_catpvs(dsv, "\n");
6657 Perl_free_c_backtrace(bt);
6666 =for apidoc dump_c_backtrace
6668 Dumps the C backtrace to the given C<fp>.
6670 Returns true if a backtrace could be retrieved, false if not.
6676 Perl_dump_c_backtrace(pTHX_ PerlIO* fp, int depth, int skip)
6680 PERL_ARGS_ASSERT_DUMP_C_BACKTRACE;
6682 sv = Perl_get_c_backtrace_dump(aTHX_ depth, skip);
6685 PerlIO_printf(fp, "%s", SvPV_nolen(sv));
6691 #endif /* #ifdef USE_C_BACKTRACE */
6693 #if defined(USE_ITHREADS) && defined(I_PTHREAD)
6695 /* pthread_mutex_t and perl_mutex are typedef equivalent
6696 * so casting the pointers is fine. */
6698 int perl_tsa_mutex_lock(perl_mutex* mutex)
6700 return pthread_mutex_lock((pthread_mutex_t *) mutex);
6703 int perl_tsa_mutex_unlock(perl_mutex* mutex)
6705 return pthread_mutex_unlock((pthread_mutex_t *) mutex);
6708 int perl_tsa_mutex_destroy(perl_mutex* mutex)
6710 return pthread_mutex_destroy((pthread_mutex_t *) mutex);
6717 /* log a sub call or return */
6720 Perl_dtrace_probe_call(pTHX_ CV *cv, bool is_call)
6728 PERL_ARGS_ASSERT_DTRACE_PROBE_CALL;
6731 HEK *hek = CvNAME_HEK(cv);
6732 func = HEK_KEY(hek);
6738 start = (const COP *)CvSTART(cv);
6739 file = CopFILE(start);
6740 line = CopLINE(start);
6741 stash = CopSTASHPV(start);
6744 PERL_SUB_ENTRY(func, file, line, stash);
6747 PERL_SUB_RETURN(func, file, line, stash);
6752 /* log a require file loading/loaded */
6755 Perl_dtrace_probe_load(pTHX_ const char *name, bool is_loading)
6757 PERL_ARGS_ASSERT_DTRACE_PROBE_LOAD;
6760 PERL_LOADING_FILE(name);
6763 PERL_LOADED_FILE(name);
6768 /* log an op execution */
6771 Perl_dtrace_probe_op(pTHX_ const OP *op)
6773 PERL_ARGS_ASSERT_DTRACE_PROBE_OP;
6775 PERL_OP_ENTRY(OP_NAME(op));
6779 /* log a compile/run phase change */
6782 Perl_dtrace_probe_phase(pTHX_ enum perl_phase phase)
6784 const char *ph_old = PL_phase_names[PL_phase];
6785 const char *ph_new = PL_phase_names[phase];
6787 PERL_PHASE_CHANGE(ph_new, ph_old);
6793 * ex: set ts=8 sts=4 sw=4 et: