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 */
35 # define SIG_ERR ((Sighandler_t) -1)
42 /* Missing protos on LynxOS */
47 # include "amigaos4/amigaio.h"
52 # include <sys/select.h>
56 #ifdef USE_C_BACKTRACE
60 # undef USE_BFD /* BFD is useless in OS X. */
70 # include <execinfo.h>
74 #ifdef PERL_DEBUG_READONLY_COW
75 # include <sys/mman.h>
80 /* NOTE: Do not call the next three routines directly. Use the macros
81 * in handy.h, so that we can easily redefine everything to do tracking of
82 * allocated hunks back to the original New to track down any memory leaks.
83 * XXX This advice seems to be widely ignored :-( --AD August 1996.
86 #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL)
87 # define ALWAYS_NEED_THX
90 #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW)
92 S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header)
95 && mprotect(header, header->size, PROT_READ|PROT_WRITE))
96 Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
97 header, header->size, errno);
101 S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header)
104 && mprotect(header, header->size, PROT_READ))
105 Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
106 header, header->size, errno);
108 # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo)
109 # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo)
111 # define maybe_protect_rw(foo) NOOP
112 # define maybe_protect_ro(foo) NOOP
115 #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW)
116 /* Use memory_debug_header */
118 # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \
119 || defined(PERL_DEBUG_READONLY_COW)
120 # define MDH_HAS_SIZE
125 =for apidoc_section $memory
126 =for apidoc safesysmalloc
127 Paranoid version of system's malloc()
133 Perl_safesysmalloc(MEM_SIZE size)
135 #ifdef ALWAYS_NEED_THX
142 if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
144 size += PERL_MEMORY_DEBUG_HEADER_SIZE;
147 if ((SSize_t)size < 0)
148 Perl_croak_nocontext("panic: malloc, size=%" UVuf, (UV) size);
150 if (!size) size = 1; /* malloc(0) is NASTY on our system */
152 #ifdef PERL_DEBUG_READONLY_COW
153 if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
154 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
155 perror("mmap failed");
159 ptr = (Malloc_t)PerlMem_malloc(size);
161 PERL_ALLOC_CHECK(ptr);
164 struct perl_memory_debug_header *const header
165 = (struct perl_memory_debug_header *)ptr;
169 PoisonNew(((char *)ptr), size, char);
172 #ifdef PERL_TRACK_MEMPOOL
173 header->interpreter = aTHX;
174 /* Link us into the list. */
175 header->prev = &PL_memory_debug_header;
176 header->next = PL_memory_debug_header.next;
177 PL_memory_debug_header.next = header;
178 maybe_protect_rw(header->next);
179 header->next->prev = header;
180 maybe_protect_ro(header->next);
181 # ifdef PERL_DEBUG_READONLY_COW
182 header->readonly = 0;
188 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
189 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
191 /* malloc() can modify errno() even on success, but since someone
192 writing perl code doesn't have any control over when perl calls
193 malloc() we need to hide that.
202 #ifndef ALWAYS_NEED_THX
215 =for apidoc safesysrealloc
216 Paranoid version of system's realloc()
222 Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
224 #ifdef ALWAYS_NEED_THX
228 #ifdef PERL_DEBUG_READONLY_COW
229 const MEM_SIZE oldsize = where
230 ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size
239 ptr = safesysmalloc(size);
243 PERL_DEB(UV was_where = PTR2UV(where)); /* used in diags below */
245 where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
246 if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
248 size += PERL_MEMORY_DEBUG_HEADER_SIZE;
250 struct perl_memory_debug_header *const header
251 = (struct perl_memory_debug_header *)where;
253 # ifdef PERL_TRACK_MEMPOOL
254 if (header->interpreter != aTHX) {
255 Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p",
256 header->interpreter, aTHX);
258 assert(header->next->prev == header);
259 assert(header->prev->next == header);
261 if (header->size > size) {
262 const MEM_SIZE freed_up = header->size - size;
263 char *start_of_freed = ((char *)where) + size;
264 PoisonFree(start_of_freed, freed_up, char);
274 if ((SSize_t)size < 0)
275 Perl_croak_nocontext("panic: realloc, size=%" UVuf, (UV)size);
277 #ifdef PERL_DEBUG_READONLY_COW
278 if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
279 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
280 perror("mmap failed");
283 Copy(where,ptr,oldsize < size ? oldsize : size,char);
284 if (munmap(where, oldsize)) {
285 perror("munmap failed");
289 ptr = (Malloc_t)PerlMem_realloc(where,size);
291 PERL_ALLOC_CHECK(ptr);
293 /* MUST do this fixup first, before doing ANYTHING else, as anything else
294 might allocate memory/free/move memory, and until we do the fixup, it
295 may well be chasing (and writing to) free memory. */
297 #ifdef PERL_TRACK_MEMPOOL
298 struct perl_memory_debug_header *const header
299 = (struct perl_memory_debug_header *)ptr;
302 if (header->size < size) {
303 const MEM_SIZE fresh = size - header->size;
304 char *start_of_fresh = ((char *)ptr) + size;
305 PoisonNew(start_of_fresh, fresh, char);
309 maybe_protect_rw(header->next);
310 header->next->prev = header;
311 maybe_protect_ro(header->next);
312 maybe_protect_rw(header->prev);
313 header->prev->next = header;
314 maybe_protect_ro(header->prev);
316 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
318 /* realloc() can modify errno() even on success, but since someone
319 writing perl code doesn't have any control over when perl calls
320 realloc() we need to hide that.
325 /* In particular, must do that fixup above before logging anything via
326 *printf(), as it can reallocate memory, which can cause SEGVs. */
328 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) rfree\n",was_where,(long)PL_an++));
329 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
336 #ifndef ALWAYS_NEED_THX
350 =for apidoc safesysfree
351 Safe version of system's free()
357 Perl_safesysfree(Malloc_t where)
359 #ifdef ALWAYS_NEED_THX
362 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
365 Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
367 struct perl_memory_debug_header *const header
368 = (struct perl_memory_debug_header *)where_intrn;
371 const MEM_SIZE size = header->size;
373 # ifdef PERL_TRACK_MEMPOOL
374 if (header->interpreter != aTHX) {
375 Perl_croak_nocontext("panic: free from wrong pool, %p!=%p",
376 header->interpreter, aTHX);
379 Perl_croak_nocontext("panic: duplicate free");
382 Perl_croak_nocontext("panic: bad free, header->next==NULL");
383 if (header->next->prev != header || header->prev->next != header) {
384 Perl_croak_nocontext("panic: bad free, ->next->prev=%p, "
385 "header=%p, ->prev->next=%p",
386 header->next->prev, header,
389 /* Unlink us from the chain. */
390 maybe_protect_rw(header->next);
391 header->next->prev = header->prev;
392 maybe_protect_ro(header->next);
393 maybe_protect_rw(header->prev);
394 header->prev->next = header->next;
395 maybe_protect_ro(header->prev);
396 maybe_protect_rw(header);
398 PoisonNew(where_intrn, size, char);
400 /* Trigger the duplicate free warning. */
403 # ifdef PERL_DEBUG_READONLY_COW
404 if (munmap(where_intrn, size)) {
405 perror("munmap failed");
411 Malloc_t where_intrn = where;
413 #ifndef PERL_DEBUG_READONLY_COW
414 PerlMem_free(where_intrn);
420 =for apidoc safesyscalloc
421 Safe version of system's calloc()
427 Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
429 #ifdef ALWAYS_NEED_THX
433 #if defined(USE_MDH) || defined(DEBUGGING)
434 MEM_SIZE total_size = 0;
437 /* Even though calloc() for zero bytes is strange, be robust. */
438 if (size && (count <= MEM_SIZE_MAX / size)) {
439 #if defined(USE_MDH) || defined(DEBUGGING)
440 total_size = size * count;
446 if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size)
447 total_size += PERL_MEMORY_DEBUG_HEADER_SIZE;
452 if ((SSize_t)size < 0 || (SSize_t)count < 0)
453 Perl_croak_nocontext("panic: calloc, size=%" UVuf ", count=%" UVuf,
454 (UV)size, (UV)count);
456 #ifdef PERL_DEBUG_READONLY_COW
457 if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE,
458 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
459 perror("mmap failed");
462 #elif defined(PERL_TRACK_MEMPOOL)
463 /* Have to use malloc() because we've added some space for our tracking
465 /* malloc(0) is non-portable. */
466 ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1);
468 /* Use calloc() because it might save a memset() if the memory is fresh
469 and clean from the OS. */
471 ptr = (Malloc_t)PerlMem_calloc(count, size);
472 else /* calloc(0) is non-portable. */
473 ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1);
475 PERL_ALLOC_CHECK(ptr);
476 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));
480 struct perl_memory_debug_header *const header
481 = (struct perl_memory_debug_header *)ptr;
483 # ifndef PERL_DEBUG_READONLY_COW
484 memset((void*)ptr, 0, total_size);
486 # ifdef PERL_TRACK_MEMPOOL
487 header->interpreter = aTHX;
488 /* Link us into the list. */
489 header->prev = &PL_memory_debug_header;
490 header->next = PL_memory_debug_header.next;
491 PL_memory_debug_header.next = header;
492 maybe_protect_rw(header->next);
493 header->next->prev = header;
494 maybe_protect_ro(header->next);
495 # ifdef PERL_DEBUG_READONLY_COW
496 header->readonly = 0;
500 header->size = total_size;
502 ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
508 #ifndef ALWAYS_NEED_THX
517 /* These must be defined when not using Perl's malloc for binary
522 Malloc_t Perl_malloc (MEM_SIZE nbytes)
524 #ifdef PERL_IMPLICIT_SYS
527 return (Malloc_t)PerlMem_malloc(nbytes);
530 Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
532 #ifdef PERL_IMPLICIT_SYS
535 return (Malloc_t)PerlMem_calloc(elements, size);
538 Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
540 #ifdef PERL_IMPLICIT_SYS
543 return (Malloc_t)PerlMem_realloc(where, nbytes);
546 Free_t Perl_mfree (Malloc_t where)
548 #ifdef PERL_IMPLICIT_SYS
556 /* This is the value stored in *retlen in the two delimcpy routines below when
557 * there wasn't enough room in the destination to store everything it was asked
558 * to. The value is deliberately very large so that hopefully if code uses it
559 * unquestioningly to access memory, it will likely segfault. And it is small
560 * enough that if the caller does some arithmetic on it before accessing, it
561 * won't overflow into a small legal number. */
562 #define DELIMCPY_OUT_OF_BOUNDS_RET I32_MAX
565 =for apidoc_section $string
566 =for apidoc delimcpy_no_escape
568 Copy a source buffer to a destination buffer, stopping at (but not including)
569 the first occurrence in the source of the delimiter byte, C<delim>. The source
570 is the bytes between S<C<from> and C<from_end> - 1>. Similarly, the dest is
571 C<to> up to C<to_end>.
573 The number of bytes copied is written to C<*retlen>.
575 Returns the position of C<delim> in the C<from> buffer, but if there is no
576 such occurrence before C<from_end>, then C<from_end> is returned, and the entire
577 buffer S<C<from> .. C<from_end> - 1> is copied.
579 If there is room in the destination available after the copy, an extra
580 terminating safety C<NUL> byte is appended (not included in the returned
583 The error case is if the destination buffer is not large enough to accommodate
584 everything that should be copied. In this situation, a value larger than
585 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
586 fits will be written to the destination. Not having room for the safety C<NUL>
587 is not considered an error.
592 Perl_delimcpy_no_escape(char *to, const char *to_end,
593 const char *from, const char *from_end,
594 const int delim, I32 *retlen)
596 const char * delim_pos;
597 Ptrdiff_t from_len = from_end - from;
598 Ptrdiff_t to_len = to_end - to;
601 PERL_ARGS_ASSERT_DELIMCPY_NO_ESCAPE;
603 assert(from_len >= 0);
606 /* Look for the first delimiter in the source */
607 delim_pos = (const char *) memchr(from, delim, from_len);
609 /* Copy up to where the delimiter was found, or the entire buffer if not
611 copy_len = (delim_pos) ? delim_pos - from : from_len;
613 /* If not enough room, copy as much as can fit, and set error return */
614 if (copy_len > to_len) {
615 Copy(from, to, to_len, char);
616 *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
619 Copy(from, to, copy_len, char);
621 /* If there is extra space available, add a trailing NUL */
622 if (copy_len < to_len) {
629 return (char *) from + copy_len;
635 Copy a source buffer to a destination buffer, stopping at (but not including)
636 the first occurrence in the source of an unescaped (defined below) delimiter
637 byte, C<delim>. The source is the bytes between S<C<from> and C<from_end> -
638 1>. Similarly, the dest is C<to> up to C<to_end>.
640 The number of bytes copied is written to C<*retlen>.
642 Returns the position of the first uncopied C<delim> in the C<from> buffer, but
643 if there is no such occurrence before C<from_end>, then C<from_end> is returned,
644 and the entire buffer S<C<from> .. C<from_end> - 1> is copied.
646 If there is room in the destination available after the copy, an extra
647 terminating safety C<NUL> byte is appended (not included in the returned
650 The error case is if the destination buffer is not large enough to accommodate
651 everything that should be copied. In this situation, a value larger than
652 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
653 fits will be written to the destination. Not having room for the safety C<NUL>
654 is not considered an error.
656 In the following examples, let C<x> be the delimiter, and C<0> represent a C<NUL>
657 byte (B<NOT> the digit C<0>). Then we would have
662 provided the destination buffer is at least 4 bytes long.
664 An escaped delimiter is one which is immediately preceded by a single
665 backslash. Escaped delimiters are copied, and the copy continues past the
666 delimiter; the backslash is not copied:
671 (provided the destination buffer is at least 8 bytes long).
673 It's actually somewhat more complicated than that. A sequence of any odd number
674 of backslashes escapes the following delimiter, and the copy continues with
675 exactly one of the backslashes stripped.
679 abc\\\xdef abc\\xdef0
680 abc\\\\\xdef abc\\\\xdef0
682 (as always, if the destination is large enough)
684 An even number of preceding backslashes does not escape the delimiter, so that
685 the copy stops just before it, and includes all the backslashes (no stripping;
686 zero is considered even):
697 Perl_delimcpy(char *to, const char *to_end,
698 const char *from, const char *from_end,
699 const int delim, I32 *retlen)
701 const char * const orig_to = to;
702 Ptrdiff_t copy_len = 0;
703 bool stopped_early = FALSE; /* Ran out of room to copy to */
705 PERL_ARGS_ASSERT_DELIMCPY;
706 assert(from_end >= from);
707 assert(to_end >= to);
709 /* Don't use the loop for the trivial case of the first character being the
710 * delimiter; otherwise would have to worry inside the loop about backing
711 * up before the start of 'from' */
712 if (LIKELY(from_end > from && *from != delim)) {
713 while ((copy_len = from_end - from) > 0) {
714 const char * backslash_pos;
715 const char * delim_pos;
717 /* Look for the next delimiter in the remaining portion of the
718 * source. A loop invariant is that we already know that the copy
719 * should include *from; this comes from the conditional before the
720 * loop, and how we set things up at the end of each iteration */
721 delim_pos = (const char *) memchr(from + 1, delim, copy_len - 1);
723 /* If didn't find it, done looking; set up so copies all of the
726 copy_len = from_end - from;
730 /* Look for a backslash immediately before the delimiter */
731 backslash_pos = delim_pos - 1;
733 /* If the delimiter is not escaped, this ends the copy */
734 if (*backslash_pos != '\\') {
735 copy_len = delim_pos - from;
739 /* Here there is a backslash just before the delimiter, but it
740 * could be the final backslash in a sequence of them. Backup to
741 * find the first one in it. */
745 while (backslash_pos >= from && *backslash_pos == '\\');
747 /* If the number of backslashes is even, they just escape one
748 * another, leaving the delimiter unescaped, and stopping the copy.
750 if (! ((delim_pos - (backslash_pos + 1)) & 1)) {
751 copy_len = delim_pos - from; /* even, copy up to delimiter */
755 /* Here is odd, so the delimiter is escaped. We will try to copy
756 * all but the final backslash in the sequence */
757 copy_len = delim_pos - 1 - from;
759 /* Do the copy, but not beyond the end of the destination */
760 if (copy_len >= to_end - to) {
761 Copy(from, to, to_end - to, char);
762 stopped_early = TRUE;
763 to = (char *) to_end;
766 Copy(from, to, copy_len, char);
770 /* Set up so next iteration will include the delimiter */
775 /* Here, have found the final segment to copy. Copy that, but not beyond
776 * the size of the destination. If not enough room, copy as much as can
777 * fit, and set error return */
778 if (stopped_early || copy_len > to_end - to) {
779 Copy(from, to, to_end - to, char);
780 *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
783 Copy(from, to, copy_len, char);
787 /* If there is extra space available, add a trailing NUL */
792 *retlen = to - orig_to;
795 return (char *) from + copy_len;
801 Find the first (leftmost) occurrence of a sequence of bytes within another
802 sequence. This is the Perl version of C<strstr()>, extended to handle
803 arbitrary sequences, potentially containing embedded C<NUL> characters (C<NUL>
804 is what the initial C<n> in the function name stands for; some systems have an
805 equivalent, C<memmem()>, but with a somewhat different API).
807 Another way of thinking about this function is finding a needle in a haystack.
808 C<big> points to the first byte in the haystack. C<big_end> points to one byte
809 beyond the final byte in the haystack. C<little> points to the first byte in
810 the needle. C<little_end> points to one byte beyond the final byte in the
811 needle. All the parameters must be non-C<NULL>.
813 The function returns C<NULL> if there is no occurrence of C<little> within
814 C<big>. If C<little> is the empty string, C<big> is returned.
816 Because this function operates at the byte level, and because of the inherent
817 characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the
818 needle and the haystack are strings with the same UTF-8ness, but not if the
826 Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend)
828 PERL_ARGS_ASSERT_NINSTR;
831 return ninstr(big, bigend, little, lend);
834 if (little >= lend) {
838 const U8 first = *little;
841 /* No match can start closer to the end of the haystack than the length
843 bigend -= lend - little;
844 little++; /* Look for 'first', then the remainder is in here */
845 lsize = lend - little;
847 while (big <= bigend) {
848 big = (char *) memchr((U8 *) big, first, bigend - big + 1);
849 if (big == NULL || big > bigend) {
853 if (memEQ(big + 1, little, lsize)) {
869 Like C<L</ninstr>>, but instead finds the final (rightmost) occurrence of a
870 sequence of bytes within another sequence, returning C<NULL> if there is no
878 Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend)
880 const Ptrdiff_t little_len = lend - little;
881 const Ptrdiff_t big_len = bigend - big;
883 PERL_ARGS_ASSERT_RNINSTR;
885 /* A non-existent needle trivially matches the rightmost possible position
887 if (UNLIKELY(little_len <= 0)) {
888 return (char*)bigend;
891 /* If the needle is larger than the haystack, the needle can't possibly fit
892 * inside the haystack. */
893 if (UNLIKELY(little_len > big_len)) {
897 /* Special case length 1 needles. It's trivial if we have memrchr();
898 * and otherwise we just do a per-byte search backwards.
900 * XXX When we don't have memrchr, we could use something like
901 * S_find_next_masked( or S_find_span_end() to do per-word searches */
902 if (little_len == 1) {
903 const char final = *little;
907 return (char *) memrchr(big, final, big_len);
909 const char * cur = bigend - 1;
915 } while (--cur >= big);
921 else { /* Below, the needle is longer than a single byte */
923 /* We search backwards in the haystack for the final character of the
924 * needle. Each time one is found, we see if the characters just
925 * before it in the haystack match the rest of the needle. */
926 const char final = *(lend - 1);
928 /* What matches consists of 'little_len'-1 characters, then the final
930 const Size_t prefix_len = little_len - 1;
932 /* If the final character in the needle is any closer than this to the
933 * left edge, there wouldn't be enough room for all of it to fit in the
935 const char * const left_fence = big + prefix_len;
937 /* Start at the right edge */
938 char * cur = (char *) bigend;
940 /* memrchr() makes the search easy (and fast); otherwise, look
941 * backwards byte-by-byte. */
946 cur = (char *) memrchr(left_fence, final, cur - left_fence);
953 if (cur < left_fence) {
957 while (*cur != final);
960 /* Here, we know that *cur is 'final'; see if the preceding bytes
961 * of the needle also match the corresponding haystack bytes */
962 if memEQ(cur - prefix_len, little, prefix_len) {
963 return cur - prefix_len;
965 } while (cur > left_fence);
971 /* As a space optimization, we do not compile tables for strings of length
972 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
973 special-cased in fbm_instr().
975 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
979 =for apidoc fbm_compile
981 Analyzes the string in order to make fast searches on it using C<fbm_instr()>
982 -- the Boyer-Moore algorithm.
988 Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
995 PERL_ARGS_ASSERT_FBM_COMPILE;
997 if (isGV_with_GP(sv) || SvROK(sv))
1003 if (flags & FBMcf_TAIL) {
1004 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
1005 sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */
1006 if (mg && mg->mg_len >= 0)
1009 if (!SvPOK(sv) || SvNIOKp(sv))
1010 s = (U8*)SvPV_force_mutable(sv, len);
1011 else s = (U8 *)SvPV_mutable(sv, len);
1012 if (len == 0) /* TAIL might be on a zero-length string. */
1014 SvUPGRADE(sv, SVt_PVMG);
1018 /* add PERL_MAGIC_bm magic holding the FBM lookup table */
1020 assert(!mg_find(sv, PERL_MAGIC_bm));
1021 mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0);
1025 /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use
1027 const U8 mlen = (len>255) ? 255 : (U8)len;
1028 const unsigned char *const sb = s + len - mlen; /* first char (maybe) */
1031 Newx(table, 256, U8);
1032 memset((void*)table, mlen, 256);
1033 mg->mg_ptr = (char *)table;
1036 s += len - 1; /* last char */
1039 if (table[*s] == mlen)
1045 BmUSEFUL(sv) = 100; /* Initial value */
1046 ((XPVNV*)SvANY(sv))->xnv_u.xnv_bm_tail = cBOOL(flags & FBMcf_TAIL);
1051 =for apidoc fbm_instr
1053 Returns the location of the SV in the string delimited by C<big> and
1054 C<bigend> (C<bigend>) is the char following the last char).
1055 It returns C<NULL> if the string can't be found. The C<sv>
1056 does not have to be C<fbm_compiled>, but the search will not be as fast
1061 If SvTAIL(littlestr) is true, a fake "\n" was appended to the string
1062 during FBM compilation due to FBMcf_TAIL in flags. It indicates that
1063 the littlestr must be anchored to the end of bigstr (or to any \n if
1066 E.g. The regex compiler would compile /abc/ to a littlestr of "abc",
1067 while /abc$/ compiles to "abc\n" with SvTAIL() true.
1069 A littlestr of "abc", !SvTAIL matches as /abc/;
1070 a littlestr of "ab\n", SvTAIL matches as:
1071 without FBMrf_MULTILINE: /ab\n?\z/
1072 with FBMrf_MULTILINE: /ab\n/ || /ab\z/;
1074 (According to Ilya from 1999; I don't know if this is still true, DAPM 2015):
1075 "If SvTAIL is actually due to \Z or \z, this gives false positives
1081 Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags)
1085 const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l);
1086 STRLEN littlelen = l;
1087 const I32 multiline = flags & FBMrf_MULTILINE;
1088 bool valid = SvVALID(littlestr);
1089 bool tail = valid ? cBOOL(SvTAIL(littlestr)) : FALSE;
1091 PERL_ARGS_ASSERT_FBM_INSTR;
1093 assert(bigend >= big);
1095 if ((STRLEN)(bigend - big) < littlelen) {
1097 && ((STRLEN)(bigend - big) == littlelen - 1)
1099 || (*big == *little &&
1100 memEQ((char *)big, (char *)little, littlelen - 1))))
1105 switch (littlelen) { /* Special cases for 0, 1 and 2 */
1107 return (char*)big; /* Cannot be SvTAIL! */
1110 if (tail && !multiline) /* Anchor only! */
1111 /* [-1] is safe because we know that bigend != big. */
1112 return (char *) (bigend - (bigend[-1] == '\n'));
1114 s = (unsigned char *)memchr((void*)big, *little, bigend-big);
1118 return (char *) bigend;
1122 if (tail && !multiline) {
1123 /* a littlestr with SvTAIL must be of the form "X\n" (where X
1124 * is a single char). It is anchored, and can only match
1125 * "....X\n" or "....X" */
1126 if (bigend[-2] == *little && bigend[-1] == '\n')
1127 return (char*)bigend - 2;
1128 if (bigend[-1] == *little)
1129 return (char*)bigend - 1;
1134 /* memchr() is likely to be very fast, possibly using whatever
1135 * hardware support is available, such as checking a whole
1136 * cache line in one instruction.
1137 * So for a 2 char pattern, calling memchr() is likely to be
1138 * faster than running FBM, or rolling our own. The previous
1139 * version of this code was roll-your-own which typically
1140 * only needed to read every 2nd char, which was good back in
1141 * the day, but no longer.
1143 unsigned char c1 = little[0];
1144 unsigned char c2 = little[1];
1146 /* *** for all this case, bigend points to the last char,
1147 * not the trailing \0: this makes the conditions slightly
1152 while (s < bigend) {
1153 /* do a quick test for c1 before calling memchr();
1154 * this avoids the expensive fn call overhead when
1155 * there are lots of c1's */
1156 if (LIKELY(*s != c1)) {
1158 s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1165 /* failed; try searching for c2 this time; that way
1166 * we don't go pathologically slow when the string
1167 * consists mostly of c1's or vice versa.
1172 s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1);
1176 return (char*)s - 1;
1180 /* c1, c2 the same */
1181 while (s < bigend) {
1190 s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1191 if (!s || s >= bigend)
1198 /* failed to find 2 chars; try anchored match at end without
1200 if (tail && bigend[0] == little[0])
1201 return (char *)bigend;
1206 break; /* Only lengths 0 1 and 2 have special-case code. */
1209 if (tail && !multiline) { /* tail anchored? */
1210 s = bigend - littlelen;
1211 if (s >= big && bigend[-1] == '\n' && *s == *little
1212 /* Automatically of length > 2 */
1213 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
1215 return (char*)s; /* how sweet it is */
1218 && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
1220 return (char*)s + 1; /* how sweet it is */
1226 /* not compiled; use Perl_ninstr() instead */
1227 char * const b = ninstr((char*)big,(char*)bigend,
1228 (char*)little, (char*)little + littlelen);
1230 assert(!tail); /* valid => FBM; tail only set on SvVALID SVs */
1234 /* Do actual FBM. */
1235 if (littlelen > (STRLEN)(bigend - big))
1239 const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm);
1240 const unsigned char *oldlittle;
1244 --littlelen; /* Last char found by table lookup */
1246 s = big + littlelen;
1247 little += littlelen; /* last char */
1250 const unsigned char * const table = (const unsigned char *) mg->mg_ptr;
1251 const unsigned char lastc = *little;
1255 if ((tmp = table[*s])) {
1256 /* *s != lastc; earliest position it could match now is
1257 * tmp slots further on */
1258 if ((s += tmp) >= bigend)
1260 if (LIKELY(*s != lastc)) {
1262 s = (unsigned char *)memchr((void*)s, lastc, bigend - s);
1272 /* hand-rolled strncmp(): less expensive than calling the
1273 * real function (maybe???) */
1275 unsigned char * const olds = s;
1280 if (*--s == *--little)
1282 s = olds + 1; /* here we pay the price for failure */
1284 if (s < bigend) /* fake up continue to outer loop */
1294 && memEQ((char *)(bigend - littlelen),
1295 (char *)(oldlittle - littlelen), littlelen) )
1296 return (char*)bigend - littlelen;
1302 Perl_cntrl_to_mnemonic(const U8 c)
1304 /* Returns the mnemonic string that represents character 'c', if one
1305 * exists; NULL otherwise. The only ones that exist for the purposes of
1306 * this routine are a few control characters */
1309 case '\a': return "\\a";
1310 case '\b': return "\\b";
1311 case ESC_NATIVE: return "\\e";
1312 case '\f': return "\\f";
1313 case '\n': return "\\n";
1314 case '\r': return "\\r";
1315 case '\t': return "\\t";
1322 =for apidoc savesharedpv
1324 A version of C<savepv()> which allocates the duplicate string in memory
1325 which is shared between threads.
1330 Perl_savesharedpv(pTHX_ const char *pv)
1335 PERL_UNUSED_CONTEXT;
1340 pvlen = strlen(pv)+1;
1341 newaddr = (char*)PerlMemShared_malloc(pvlen);
1345 return (char*)memcpy(newaddr, pv, pvlen);
1349 =for apidoc savesharedpvn
1351 A version of C<savepvn()> which allocates the duplicate string in memory
1352 which is shared between threads. (With the specific difference that a C<NULL>
1353 pointer is not acceptable)
1358 Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len)
1360 char *const newaddr = (char*)PerlMemShared_malloc(len + 1);
1362 PERL_UNUSED_CONTEXT;
1363 /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */
1368 newaddr[len] = '\0';
1369 return (char*)memcpy(newaddr, pv, len);
1372 /* the SV for Perl_form() and mess() is not kept in an arena */
1380 if (PL_phase != PERL_PHASE_DESTRUCT)
1381 return newSVpvs_flags("", SVs_TEMP);
1386 /* Create as PVMG now, to avoid any upgrading later */
1388 Newxz(any, 1, XPVMG);
1389 SvFLAGS(sv) = SVt_PVMG;
1390 SvANY(sv) = (void*)any;
1392 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1397 #if defined(MULTIPLICITY)
1399 Perl_form_nocontext(const char* pat, ...)
1404 PERL_ARGS_ASSERT_FORM_NOCONTEXT;
1405 va_start(args, pat);
1406 retval = vform(pat, &args);
1410 #endif /* MULTIPLICITY */
1413 =for apidoc_section $display
1415 =for apidoc_item form_nocontext
1417 These take a sprintf-style format pattern and conventional
1418 (non-SV) arguments and return the formatted string.
1420 (char *) Perl_form(pTHX_ const char* pat, ...)
1422 can be used any place a string (char *) is required:
1424 char * s = Perl_form("%d.%d",major,minor);
1426 They use a single (per-thread) private buffer so if you want to format several
1427 strings you must explicitly copy the earlier strings away (and free the copies
1430 The two forms differ only in that C<form_nocontext> does not take a thread
1431 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1432 already have the thread context.
1435 Like C<L</form>> but but the arguments are an encapsulated argument list.
1441 Perl_form(pTHX_ const char* pat, ...)
1445 PERL_ARGS_ASSERT_FORM;
1446 va_start(args, pat);
1447 retval = vform(pat, &args);
1453 Perl_vform(pTHX_ const char *pat, va_list *args)
1455 SV * const sv = mess_alloc();
1456 PERL_ARGS_ASSERT_VFORM;
1457 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1463 =for apidoc_item mess_nocontext
1465 These take a sprintf-style format pattern and argument list, which are used to
1466 generate a string message. If the message does not end with a newline, then it
1467 will be extended with some indication of the current location in the code, as
1468 described for C<L</mess_sv>>.
1470 Normally, the resulting message is returned in a new mortal SV.
1471 But during global destruction a single SV may be shared between uses of
1474 The two forms differ only in that C<mess_nocontext> does not take a thread
1475 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1476 already have the thread context.
1481 #if defined(MULTIPLICITY)
1483 Perl_mess_nocontext(const char *pat, ...)
1488 PERL_ARGS_ASSERT_MESS_NOCONTEXT;
1489 va_start(args, pat);
1490 retval = vmess(pat, &args);
1494 #endif /* MULTIPLICITY */
1497 Perl_mess(pTHX_ const char *pat, ...)
1501 PERL_ARGS_ASSERT_MESS;
1502 va_start(args, pat);
1503 retval = vmess(pat, &args);
1509 Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop,
1512 /* Look for curop starting from o. cop is the last COP we've seen. */
1513 /* opnext means that curop is actually the ->op_next of the op we are
1516 PERL_ARGS_ASSERT_CLOSEST_COP;
1518 if (!o || !curop || (
1519 opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop
1523 if (o->op_flags & OPf_KIDS) {
1525 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1528 /* If the OP_NEXTSTATE has been optimised away we can still use it
1529 * the get the file and line number. */
1531 if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
1532 cop = (const COP *)kid;
1534 /* Keep searching, and return when we've found something. */
1536 new_cop = closest_cop(cop, kid, curop, opnext);
1542 /* Nothing found. */
1550 Expands a message, intended for the user, to include an indication of
1551 the current location in the code, if the message does not already appear
1554 C<basemsg> is the initial message or object. If it is a reference, it
1555 will be used as-is and will be the result of this function. Otherwise it
1556 is used as a string, and if it already ends with a newline, it is taken
1557 to be complete, and the result of this function will be the same string.
1558 If the message does not end with a newline, then a segment such as C<at
1559 foo.pl line 37> will be appended, and possibly other clauses indicating
1560 the current state of execution. The resulting message will end with a
1563 Normally, the resulting message is returned in a new mortal SV.
1564 During global destruction a single SV may be shared between uses of this
1565 function. If C<consume> is true, then the function is permitted (but not
1566 required) to modify and return C<basemsg> instead of allocating a new SV.
1572 Perl_mess_sv(pTHX_ SV *basemsg, bool consume)
1576 #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR)
1580 /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */
1581 if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR"))
1582 && grok_atoUV(ws, &wi, NULL)
1583 && wi <= PERL_INT_MAX
1585 Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1);
1590 PERL_ARGS_ASSERT_MESS_SV;
1592 if (SvROK(basemsg)) {
1598 sv_setsv(sv, basemsg);
1603 if (SvPOK(basemsg) && consume) {
1608 sv_copypv(sv, basemsg);
1611 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1613 * Try and find the file and line for PL_op. This will usually be
1614 * PL_curcop, but it might be a cop that has been optimised away. We
1615 * can try to find such a cop by searching through the optree starting
1616 * from the sibling of PL_curcop.
1621 closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE);
1626 Perl_sv_catpvf(aTHX_ sv, " at %s line %" LINE_Tf,
1627 OutCopFILE(cop), CopLINE(cop));
1630 /* Seems that GvIO() can be untrustworthy during global destruction. */
1631 if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO)
1632 && IoLINES(GvIOp(PL_last_in_gv)))
1635 const bool line_mode = (RsSIMPLE(PL_rs) &&
1636 *SvPV_const(PL_rs,l) == '\n' && l == 1);
1637 Perl_sv_catpvf(aTHX_ sv, ", <%" SVf "> %s %" IVdf,
1638 SVfARG(PL_last_in_gv == PL_argvgv
1640 : newSVhek_mortal(GvNAME_HEK(PL_last_in_gv))),
1641 line_mode ? "line" : "chunk",
1642 (IV)IoLINES(GvIOp(PL_last_in_gv)));
1644 if (PL_phase == PERL_PHASE_DESTRUCT)
1645 sv_catpvs(sv, " during global destruction");
1646 sv_catpvs(sv, ".\n");
1654 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1655 argument list, respectively. These are used to generate a string message. If
1657 message does not end with a newline, then it will be extended with
1658 some indication of the current location in the code, as described for
1661 Normally, the resulting message is returned in a new mortal SV.
1662 During global destruction a single SV may be shared between uses of
1669 Perl_vmess(pTHX_ const char *pat, va_list *args)
1671 SV * const sv = mess_alloc();
1673 PERL_ARGS_ASSERT_VMESS;
1675 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1676 return mess_sv(sv, 1);
1680 Perl_write_to_stderr(pTHX_ SV* msv)
1685 PERL_ARGS_ASSERT_WRITE_TO_STDERR;
1687 if (PL_stderrgv && SvREFCNT(PL_stderrgv)
1688 && (io = GvIO(PL_stderrgv))
1689 && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
1690 Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT),
1691 G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv);
1693 PerlIO * const serr = Perl_error_log;
1695 do_print(msv, serr);
1696 (void)PerlIO_flush(serr);
1701 =for apidoc_section $warning
1704 /* Common code used in dieing and warning */
1707 S_with_queued_errors(pTHX_ SV *ex)
1709 PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS;
1710 if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) {
1711 sv_catsv(PL_errors, ex);
1712 ex = sv_mortalcopy(PL_errors);
1713 SvCUR_set(PL_errors, 0);
1719 Perl_invoke_exception_hook(pTHX_ SV *ex, bool warn)
1724 SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1725 /* sv_2cv might call Perl_croak() or Perl_warner() */
1726 SV * const oldhook = *hook;
1728 if (!oldhook || oldhook == PERL_WARNHOOK_FATAL)
1734 cv = sv_2cv(oldhook, &stash, &gv, 0);
1736 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1746 exarg = newSVsv(ex);
1747 SvREADONLY_on(exarg);
1750 PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
1754 call_sv(MUTABLE_SV(cv), G_DISCARD);
1765 This behaves the same as L</croak_sv>, except for the return type.
1766 It should be used only where the C<OP *> return type is required.
1767 The function never actually returns.
1772 /* silence __declspec(noreturn) warnings */
1773 MSVC_DIAG_IGNORE(4646 4645)
1775 Perl_die_sv(pTHX_ SV *baseex)
1777 PERL_ARGS_ASSERT_DIE_SV;
1780 NORETURN_FUNCTION_END;
1786 =for apidoc_item die_nocontext
1788 These behave the same as L</croak>, except for the return type.
1789 They should be used only where the C<OP *> return type is required.
1790 They never actually return.
1792 The two forms differ only in that C<die_nocontext> does not take a thread
1793 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1794 already have the thread context.
1799 #if defined(MULTIPLICITY)
1801 /* silence __declspec(noreturn) warnings */
1802 MSVC_DIAG_IGNORE(4646 4645)
1804 Perl_die_nocontext(const char* pat, ...)
1808 va_start(args, pat);
1810 NOT_REACHED; /* NOTREACHED */
1812 NORETURN_FUNCTION_END;
1816 #endif /* MULTIPLICITY */
1818 /* silence __declspec(noreturn) warnings */
1819 MSVC_DIAG_IGNORE(4646 4645)
1821 Perl_die(pTHX_ const char* pat, ...)
1824 va_start(args, pat);
1826 NOT_REACHED; /* NOTREACHED */
1828 NORETURN_FUNCTION_END;
1833 =for apidoc croak_sv
1835 This is an XS interface to Perl's C<die> function.
1837 C<baseex> is the error message or object. If it is a reference, it
1838 will be used as-is. Otherwise it is used as a string, and if it does
1839 not end with a newline then it will be extended with some indication of
1840 the current location in the code, as described for L</mess_sv>.
1842 The error message or object will be used as an exception, by default
1843 returning control to the nearest enclosing C<eval>, but subject to
1844 modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv>
1845 function never returns normally.
1847 To die with a simple string message, the L</croak> function may be
1854 Perl_croak_sv(pTHX_ SV *baseex)
1856 SV *ex = with_queued_errors(mess_sv(baseex, 0));
1857 PERL_ARGS_ASSERT_CROAK_SV;
1858 invoke_exception_hook(ex, FALSE);
1865 This is an XS interface to Perl's C<die> function.
1867 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1868 argument list. These are used to generate a string message. If the
1869 message does not end with a newline, then it will be extended with
1870 some indication of the current location in the code, as described for
1873 The error message will be used as an exception, by default
1874 returning control to the nearest enclosing C<eval>, but subject to
1875 modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak>
1876 function never returns normally.
1878 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1879 (C<$@>) will be used as an error message or object instead of building an
1880 error message from arguments. If you want to throw a non-string object,
1881 or build an error message in an SV yourself, it is preferable to use
1882 the L</croak_sv> function, which does not involve clobbering C<ERRSV>.
1888 Perl_vcroak(pTHX_ const char* pat, va_list *args)
1890 SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0));
1891 invoke_exception_hook(ex, FALSE);
1897 =for apidoc_item croak_nocontext
1899 These are XS interfaces to Perl's C<die> function.
1901 They take a sprintf-style format pattern and argument list, which are used to
1902 generate a string message. If the message does not end with a newline, then it
1903 will be extended with some indication of the current location in the code, as
1904 described for C<L</mess_sv>>.
1906 The error message will be used as an exception, by default
1907 returning control to the nearest enclosing C<eval>, but subject to
1908 modification by a C<$SIG{__DIE__}> handler. In any case, these croak
1909 functions never return normally.
1911 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1912 (C<$@>) will be used as an error message or object instead of building an
1913 error message from arguments. If you want to throw a non-string object,
1914 or build an error message in an SV yourself, it is preferable to use
1915 the C<L</croak_sv>> function, which does not involve clobbering C<ERRSV>.
1917 The two forms differ only in that C<croak_nocontext> does not take a thread
1918 context (C<aTHX>) parameter. It is usually preferred as it takes up fewer
1919 bytes of code than plain C<Perl_croak>, and time is rarely a critical resource
1920 when you are about to throw an exception.
1925 #if defined(MULTIPLICITY)
1927 Perl_croak_nocontext(const char *pat, ...)
1931 va_start(args, pat);
1933 NOT_REACHED; /* NOTREACHED */
1936 #endif /* MULTIPLICITY */
1939 Perl_croak(pTHX_ const char *pat, ...)
1942 va_start(args, pat);
1944 NOT_REACHED; /* NOTREACHED */
1949 =for apidoc croak_no_modify
1951 This encapsulates a common reason for dying, generating terser object code than
1952 using the generic C<Perl_croak>. It is exactly equivalent to
1953 C<Perl_croak(aTHX_ "%s", PL_no_modify)> (which expands to something like
1954 "Modification of a read-only value attempted").
1956 Less code used on exception code paths reduces CPU cache pressure.
1962 Perl_croak_no_modify(void)
1964 Perl_croak_nocontext( "%s", PL_no_modify);
1967 /* does not return, used in util.c perlio.c and win32.c
1968 This is typically called when malloc returns NULL.
1971 Perl_croak_no_mem(void)
1975 int fd = PerlIO_fileno(Perl_error_log);
1977 SETERRNO(EBADF,RMS_IFI);
1979 /* Can't use PerlIO to write as it allocates memory */
1980 PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1));
1985 /* does not return, used only in POPSTACK */
1987 Perl_croak_popstack(void)
1990 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");
1997 This is an XS interface to Perl's C<warn> function.
1999 C<baseex> is the error message or object. If it is a reference, it
2000 will be used as-is. Otherwise it is used as a string, and if it does
2001 not end with a newline then it will be extended with some indication of
2002 the current location in the code, as described for L</mess_sv>.
2004 The error message or object will by default be written to standard error,
2005 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2007 To warn with a simple string message, the L</warn> function may be
2014 Perl_warn_sv(pTHX_ SV *baseex)
2016 SV *ex = mess_sv(baseex, 0);
2017 PERL_ARGS_ASSERT_WARN_SV;
2018 if (!invoke_exception_hook(ex, TRUE))
2019 write_to_stderr(ex);
2025 This is an XS interface to Perl's C<warn> function.
2027 This is like C<L</warn>>, but C<args> are an encapsulated
2030 Unlike with L</vcroak>, C<pat> is not permitted to be null.
2036 Perl_vwarn(pTHX_ const char* pat, va_list *args)
2038 SV *ex = vmess(pat, args);
2039 PERL_ARGS_ASSERT_VWARN;
2040 if (!invoke_exception_hook(ex, TRUE))
2041 write_to_stderr(ex);
2046 =for apidoc_item warn_nocontext
2048 These are XS interfaces to Perl's C<warn> function.
2050 They take a sprintf-style format pattern and argument list, which are used to
2051 generate a string message. If the message does not end with a newline, then it
2052 will be extended with some indication of the current location in the code, as
2053 described for C<L</mess_sv>>.
2055 The error message or object will by default be written to standard error,
2056 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2058 Unlike with C<L</croak>>, C<pat> is not permitted to be null.
2060 The two forms differ only in that C<warn_nocontext> does not take a thread
2061 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2062 already have the thread context.
2067 #if defined(MULTIPLICITY)
2069 Perl_warn_nocontext(const char *pat, ...)
2073 PERL_ARGS_ASSERT_WARN_NOCONTEXT;
2074 va_start(args, pat);
2078 #endif /* MULTIPLICITY */
2081 Perl_warn(pTHX_ const char *pat, ...)
2084 PERL_ARGS_ASSERT_WARN;
2085 va_start(args, pat);
2092 =for apidoc_item warner_nocontext
2094 These output a warning of the specified category (or categories) given by
2095 C<err>, using the sprintf-style format pattern C<pat>, and argument list.
2097 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2098 C<packWARN4> macros populated with the appropriate number of warning
2099 categories. If any of the warning categories they specify is fatal, a fatal
2100 exception is thrown.
2102 In any event a message is generated by the pattern and arguments. If the
2103 message does not end with a newline, then it will be extended with some
2104 indication of the current location in the code, as described for L</mess_sv>.
2106 The error message or object will by default be written to standard error,
2107 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2109 C<pat> is not permitted to be null.
2111 The two forms differ only in that C<warner_nocontext> does not take a thread
2112 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2113 already have the thread context.
2115 These functions differ from the similarly named C<L</warn>> functions, in that
2116 the latter are for XS code to unconditionally display a warning, whereas these
2117 are for code that may be compiling a perl program, and does extra checking to
2118 see if the warning should be fatal.
2120 =for apidoc ck_warner
2121 =for apidoc_item ck_warner_d
2122 If none of the warning categories given by C<err> are enabled, do nothing;
2123 otherwise call C<L</warner>> or C<L</warner_nocontext>> with the passed-in
2126 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2127 C<packWARN4> macros populated with the appropriate number of warning
2130 The two forms differ only in that C<ck_warner_d> should be used if warnings for
2131 any of the categories are by default enabled.
2134 This is like C<L</warner>>, but C<args> are an encapsulated argument list.
2139 #if defined(MULTIPLICITY)
2141 Perl_warner_nocontext(U32 err, const char *pat, ...)
2145 PERL_ARGS_ASSERT_WARNER_NOCONTEXT;
2146 va_start(args, pat);
2147 vwarner(err, pat, &args);
2150 #endif /* MULTIPLICITY */
2153 Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...)
2155 PERL_ARGS_ASSERT_CK_WARNER_D;
2157 if (Perl_ckwarn_d(aTHX_ err)) {
2159 va_start(args, pat);
2160 vwarner(err, pat, &args);
2166 Perl_ck_warner(pTHX_ U32 err, const char* pat, ...)
2168 PERL_ARGS_ASSERT_CK_WARNER;
2170 if (Perl_ckwarn(aTHX_ err)) {
2172 va_start(args, pat);
2173 vwarner(err, pat, &args);
2179 Perl_warner(pTHX_ U32 err, const char* pat,...)
2182 PERL_ARGS_ASSERT_WARNER;
2183 va_start(args, pat);
2184 vwarner(err, pat, &args);
2189 Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args)
2191 PERL_ARGS_ASSERT_VWARNER;
2193 (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) &&
2194 !(PL_in_eval & EVAL_KEEPERR)
2196 SV * const msv = vmess(pat, args);
2198 if (PL_parser && PL_parser->error_count) {
2202 invoke_exception_hook(msv, FALSE);
2207 Perl_vwarn(aTHX_ pat, args);
2211 /* implements the ckWARN? macros */
2214 Perl_ckwarn(pTHX_ U32 w)
2216 /* If lexical warnings have not been set, use $^W. */
2218 return PL_dowarn & G_WARN_ON;
2220 return ckwarn_common(w);
2223 /* implements the ckWARN?_d macro */
2226 Perl_ckwarn_d(pTHX_ U32 w)
2228 /* If lexical warnings have not been set then default classes warn. */
2232 return ckwarn_common(w);
2236 S_ckwarn_common(pTHX_ U32 w)
2238 if (PL_curcop->cop_warnings == pWARN_ALL)
2241 if (PL_curcop->cop_warnings == pWARN_NONE)
2244 /* Check the assumption that at least the first slot is non-zero. */
2245 assert(unpackWARN1(w));
2247 /* Check the assumption that it is valid to stop as soon as a zero slot is
2249 if (!unpackWARN2(w)) {
2250 assert(!unpackWARN3(w));
2251 assert(!unpackWARN4(w));
2252 } else if (!unpackWARN3(w)) {
2253 assert(!unpackWARN4(w));
2256 /* Right, dealt with all the special cases, which are implemented as non-
2257 pointers, so there is a pointer to a real warnings mask. */
2259 if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w)))
2261 } while (w >>= WARNshift);
2267 Perl_new_warnings_bitfield(pTHX_ char *buffer, const char *const bits,
2269 const MEM_SIZE len_wanted = (size > WARNsize ? size : WARNsize);
2270 PERL_UNUSED_CONTEXT;
2271 PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD;
2273 /* pass in null as the source string as we will do the
2274 * copy ourselves. */
2275 buffer = rcpv_new(NULL, len_wanted, RCPVf_NO_COPY);
2276 Copy(bits, buffer, size, char);
2277 if (size < WARNsize)
2278 Zero(buffer + size, WARNsize - size, char);
2282 /* since we've already done strlen() for both nam and val
2283 * we can use that info to make things faster than
2284 * sprintf(s, "%s=%s", nam, val)
2286 #define my_setenv_format(s, nam, nlen, val, vlen) \
2287 Copy(nam, s, nlen, char); \
2289 Copy(val, s+(nlen+1), vlen, char); \
2290 *(s+(nlen+1+vlen)) = '\0'
2294 #if defined(USE_ENVIRON_ARRAY) || defined(WIN32)
2295 /* NB: VMS' my_setenv() is in vms.c */
2297 /* small wrapper for use by Perl_my_setenv that mallocs, or reallocs if
2298 * 'current' is non-null, with up to three sizes that are added together.
2299 * It handles integer overflow.
2303 S_env_alloc(void *current, Size_t l1, Size_t l2, Size_t l3, Size_t size)
2306 Size_t sl, l = l1 + l2;
2318 ? safesysrealloc(current, sl)
2319 : safesysmalloc(sl);
2324 croak_memory_wrap();
2329 =for apidoc_section $utility
2330 =for apidoc my_setenv
2332 A wrapper for the C library L<setenv(3)>. Don't use the latter, as the perl
2333 version has desirable safeguards
2339 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2341 # if defined(USE_ITHREADS) && !defined(WIN32)
2342 /* only parent thread can modify process environment, so no need to use a
2344 if (PL_curinterp != aTHX)
2348 # if defined(HAS_SETENV) && defined(HAS_UNSETENV)
2352 setenv(nam, val, 1);
2355 # elif defined(HAS_UNSETENV)
2358 if (environ) /* old glibc can crash with null environ */
2361 const Size_t nlen = strlen(nam);
2362 const Size_t vlen = strlen(val);
2363 char * const new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2364 my_setenv_format(new_env, nam, nlen, val, vlen);
2368 # else /* ! HAS_UNSETENV */
2370 const Size_t nlen = strlen(nam);
2374 Size_t vlen = strlen(val);
2375 char *new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2376 /* all that work just for this */
2377 my_setenv_format(new_env, nam, nlen, val, vlen);
2381 PerlEnv_putenv(new_env);
2382 safesysfree(new_env);
2385 # endif /* HAS_SETENV */
2388 #endif /* USE_ENVIRON_ARRAY || WIN32 */
2390 #ifdef UNLINK_ALL_VERSIONS
2392 Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */
2396 PERL_ARGS_ASSERT_UNLNK;
2398 while (PerlLIO_unlink(f) >= 0)
2400 return retries ? 0 : -1;
2405 #if (__CHARSET_LIB == 1)
2406 static int chgfdccsid(int fd, unsigned short ccsid)
2409 memset(&attr, 0, sizeof(attr));
2410 attr.att_filetagchg = 1;
2411 attr.att_filetag.ft_ccsid = ccsid;
2412 if (ccsid != FT_BINARY) {
2413 attr.att_filetag.ft_txtflag = 1;
2415 return __fchattr(fd, &attr, sizeof(attr));
2421 =for apidoc my_popen_list
2423 Implementing function on some systems for PerlProc_popen_list()
2429 Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
2431 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2439 PERL_ARGS_ASSERT_MY_POPEN_LIST;
2441 PERL_FLUSHALL_FOR_CHILD;
2442 This = (*mode == 'w');
2446 taint_proper("Insecure %s%s", "EXEC");
2448 if (PerlProc_pipe_cloexec(p) < 0)
2450 /* Try for another pipe pair for error return */
2451 if (PerlProc_pipe_cloexec(pp) >= 0)
2453 while ((pid = PerlProc_fork()) < 0) {
2454 if (errno != EAGAIN) {
2455 PerlLIO_close(p[This]);
2456 PerlLIO_close(p[that]);
2458 PerlLIO_close(pp[0]);
2459 PerlLIO_close(pp[1]);
2463 Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2472 /* Close parent's end of error status pipe (if any) */
2474 PerlLIO_close(pp[0]);
2476 #if (__CHARSET_LIB == 1)
2477 chgfdccsid(p[THIS], 819);
2478 chgfdccsid(p[THAT], 819);
2481 /* Now dup our end of _the_ pipe to right position */
2482 if (p[THIS] != (*mode == 'r')) {
2483 PerlLIO_dup2(p[THIS], *mode == 'r');
2484 PerlLIO_close(p[THIS]);
2485 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2486 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
2489 setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2490 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
2492 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2493 /* No automatic close - do it by hand */
2500 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
2506 do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
2513 PerlLIO_close(pp[1]);
2514 /* Keep the lower of the two fd numbers */
2515 if (p[that] < p[This]) {
2516 PerlLIO_dup2_cloexec(p[This], p[that]);
2517 PerlLIO_close(p[This]);
2521 PerlLIO_close(p[that]); /* close child's end of pipe */
2523 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2524 SvUPGRADE(sv,SVt_IV);
2526 PL_forkprocess = pid;
2527 /* If we managed to get status pipe check for exec fail */
2528 if (did_pipes && pid > 0) {
2530 unsigned read_total = 0;
2532 while (read_total < sizeof(int)) {
2533 const SSize_t n1 = PerlLIO_read(pp[0],
2534 (void*)(((char*)&errkid)+read_total),
2535 (sizeof(int)) - read_total);
2540 PerlLIO_close(pp[0]);
2542 if (read_total) { /* Error */
2544 PerlLIO_close(p[This]);
2545 if (read_total != sizeof(int))
2546 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", read_total);
2548 pid2 = wait4pid(pid, &status, 0);
2549 } while (pid2 == -1 && errno == EINTR);
2550 errno = errkid; /* Propagate errno from kid */
2555 PerlLIO_close(pp[0]);
2557 #if (__CHARSET_LIB == 1)
2558 PerlIO* io = PerlIO_fdopen(p[This], mode);
2560 chgfdccsid(p[This], 819);
2564 return PerlIO_fdopen(p[This], mode);
2567 return PerlIO_fdopen(p[This], mode);
2571 # if defined(OS2) /* Same, without fork()ing and all extra overhead... */
2572 return my_syspopen4(aTHX_ NULL, mode, n, args);
2573 # elif defined(WIN32)
2574 return win32_popenlist(mode, n, args);
2576 Perl_croak(aTHX_ "List form of piped open not implemented");
2577 return (PerlIO *) NULL;
2582 /* VMS' my_popen() is in VMS.c, same with OS/2 and AmigaOS 4. */
2583 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2586 =for apidoc_section $io
2587 =for apidoc my_popen
2589 A wrapper for the C library L<popen(3)>. Don't use the latter, as the Perl
2590 version knows things that interact with the rest of the perl interpreter.
2596 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2602 const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
2606 PERL_ARGS_ASSERT_MY_POPEN;
2608 PERL_FLUSHALL_FOR_CHILD;
2611 return my_syspopen(aTHX_ cmd,mode);
2614 This = (*mode == 'w');
2616 if (doexec && TAINTING_get) {
2618 taint_proper("Insecure %s%s", "EXEC");
2620 if (PerlProc_pipe_cloexec(p) < 0)
2622 if (doexec && PerlProc_pipe_cloexec(pp) >= 0)
2624 while ((pid = PerlProc_fork()) < 0) {
2625 if (errno != EAGAIN) {
2626 PerlLIO_close(p[This]);
2627 PerlLIO_close(p[that]);
2629 PerlLIO_close(pp[0]);
2630 PerlLIO_close(pp[1]);
2633 Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno));
2636 Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2646 PerlLIO_close(pp[0]);
2648 #if (__CHARSET_LIB == 1)
2649 chgfdccsid(p[THIS], 819);
2650 chgfdccsid(p[THAT], 819);
2653 if (p[THIS] != (*mode == 'r')) {
2654 PerlLIO_dup2(p[THIS], *mode == 'r');
2655 PerlLIO_close(p[THIS]);
2656 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2657 PerlLIO_close(p[THAT]);
2660 setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2661 PerlLIO_close(p[THAT]);
2665 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2672 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2677 /* may or may not use the shell */
2678 do_exec3(cmd, pp[1], did_pipes);
2681 #endif /* defined OS2 */
2683 #ifdef PERLIO_USING_CRLF
2684 /* Since we circumvent IO layers when we manipulate low-level
2685 filedescriptors directly, need to manually switch to the
2686 default, binary, low-level mode; see PerlIOBuf_open(). */
2687 PerlLIO_setmode((*mode == 'r'), O_BINARY);
2690 #ifdef PERL_USES_PL_PIDSTATUS
2691 hv_clear(PL_pidstatus); /* we have no children */
2698 PerlLIO_close(pp[1]);
2699 if (p[that] < p[This]) {
2700 PerlLIO_dup2_cloexec(p[This], p[that]);
2701 PerlLIO_close(p[This]);
2705 PerlLIO_close(p[that]);
2707 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2708 SvUPGRADE(sv,SVt_IV);
2710 PL_forkprocess = pid;
2711 if (did_pipes && pid > 0) {
2715 while (n < sizeof(int)) {
2716 const SSize_t n1 = PerlLIO_read(pp[0],
2717 (void*)(((char*)&errkid)+n),
2723 PerlLIO_close(pp[0]);
2725 if (n) { /* Error */
2727 PerlLIO_close(p[This]);
2728 if (n != sizeof(int))
2729 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n);
2731 pid2 = wait4pid(pid, &status, 0);
2732 } while (pid2 == -1 && errno == EINTR);
2733 errno = errkid; /* Propagate errno from kid */
2738 PerlLIO_close(pp[0]);
2740 #if (__CHARSET_LIB == 1)
2741 PerlIO* io = PerlIO_fdopen(p[This], mode);
2743 chgfdccsid(p[This], 819);
2747 return PerlIO_fdopen(p[This], mode);
2750 return PerlIO_fdopen(p[This], mode);
2753 #elif defined(__LIBCATAMOUNT__)
2755 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2760 #endif /* !DOSISH */
2762 /* this is called in parent before the fork() */
2764 Perl_atfork_lock(void)
2765 #if defined(USE_ITHREADS)
2767 PERL_TSA_ACQUIRE(PL_perlio_mutex)
2770 PERL_TSA_ACQUIRE(PL_malloc_mutex)
2772 PERL_TSA_ACQUIRE(PL_op_mutex)
2775 #if defined(USE_ITHREADS)
2776 /* locks must be held in locking order (if any) */
2778 MUTEX_LOCK(&PL_perlio_mutex);
2781 MUTEX_LOCK(&PL_malloc_mutex);
2787 /* this is called in both parent and child after the fork() */
2789 Perl_atfork_unlock(void)
2790 #if defined(USE_ITHREADS)
2792 PERL_TSA_RELEASE(PL_perlio_mutex)
2795 PERL_TSA_RELEASE(PL_malloc_mutex)
2797 PERL_TSA_RELEASE(PL_op_mutex)
2800 #if defined(USE_ITHREADS)
2801 /* locks must be released in same order as in atfork_lock() */
2803 MUTEX_UNLOCK(&PL_perlio_mutex);
2806 MUTEX_UNLOCK(&PL_malloc_mutex);
2813 =for apidoc_section $concurrency
2816 This is for the use of C<PerlProc_fork> as a wrapper for the C library
2817 L<fork(2)> on some platforms to hide some platform quirks. It should not be
2818 used except through C<PerlProc_fork>.
2827 #if defined(HAS_FORK)
2829 #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
2834 /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2835 * handlers elsewhere in the code */
2839 #elif defined(__amigaos4__)
2840 return amigaos_fork();
2842 /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2843 Perl_croak_nocontext("fork() not available");
2845 #endif /* HAS_FORK */
2850 dup2(int oldfd, int newfd)
2852 #if defined(HAS_FCNTL) && defined(F_DUPFD)
2855 PerlLIO_close(newfd);
2856 return fcntl(oldfd, F_DUPFD, newfd);
2858 #define DUP2_MAX_FDS 256
2859 int fdtmp[DUP2_MAX_FDS];
2865 PerlLIO_close(newfd);
2866 /* good enough for low fd's... */
2867 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
2868 if (fdx >= DUP2_MAX_FDS) {
2876 PerlLIO_close(fdtmp[--fdx]);
2882 #ifdef HAS_SIGACTION
2885 =for apidoc_section $signals
2888 A wrapper for the C library functions L<sigaction(2)> or L<signal(2)>.
2889 Use this instead of those libc functions, as the Perl version gives the
2890 safest available implementation, and knows things that interact with the
2891 rest of the perl interpreter.
2897 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2899 struct sigaction act, oact;
2902 /* only "parent" interpreter can diddle signals */
2903 if (PL_curinterp != aTHX)
2904 return (Sighandler_t) SIG_ERR;
2907 act.sa_handler = handler;
2908 sigemptyset(&act.sa_mask);
2911 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2912 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2914 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
2915 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
2916 act.sa_flags |= SA_NOCLDWAIT;
2918 if (sigaction(signo, &act, &oact) == -1)
2919 return (Sighandler_t) SIG_ERR;
2921 return (Sighandler_t) oact.sa_handler;
2925 =for apidoc_section $signals
2926 =for apidoc rsignal_state
2928 Returns a the current signal handler for signal C<signo>.
2935 Perl_rsignal_state(pTHX_ int signo)
2937 struct sigaction oact;
2938 PERL_UNUSED_CONTEXT;
2940 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2941 return (Sighandler_t) SIG_ERR;
2943 return (Sighandler_t) oact.sa_handler;
2947 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
2949 struct sigaction act;
2951 PERL_ARGS_ASSERT_RSIGNAL_SAVE;
2954 /* only "parent" interpreter can diddle signals */
2955 if (PL_curinterp != aTHX)
2959 act.sa_handler = handler;
2960 sigemptyset(&act.sa_mask);
2963 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2964 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2966 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
2967 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
2968 act.sa_flags |= SA_NOCLDWAIT;
2970 return sigaction(signo, &act, save);
2974 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
2976 PERL_UNUSED_CONTEXT;
2978 /* only "parent" interpreter can diddle signals */
2979 if (PL_curinterp != aTHX)
2983 return sigaction(signo, save, (struct sigaction *)NULL);
2986 #else /* !HAS_SIGACTION */
2989 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2991 #if defined(USE_ITHREADS) && !defined(WIN32)
2992 /* only "parent" interpreter can diddle signals */
2993 if (PL_curinterp != aTHX)
2994 return (Sighandler_t) SIG_ERR;
2997 return PerlProc_signal(signo, handler);
3007 Perl_rsignal_state(pTHX_ int signo)
3009 Sighandler_t oldsig;
3011 #if defined(USE_ITHREADS) && !defined(WIN32)
3012 /* only "parent" interpreter can diddle signals */
3013 if (PL_curinterp != aTHX)
3014 return (Sighandler_t) SIG_ERR;
3018 oldsig = PerlProc_signal(signo, sig_trap);
3019 PerlProc_signal(signo, oldsig);
3021 PerlProc_kill(PerlProc_getpid(), signo);
3026 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3028 #if defined(USE_ITHREADS) && !defined(WIN32)
3029 /* only "parent" interpreter can diddle signals */
3030 if (PL_curinterp != aTHX)
3033 *save = PerlProc_signal(signo, handler);
3034 return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
3038 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3040 #if defined(USE_ITHREADS) && !defined(WIN32)
3041 /* only "parent" interpreter can diddle signals */
3042 if (PL_curinterp != aTHX)
3045 return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
3048 #endif /* !HAS_SIGACTION */
3050 /* VMS' my_pclose() is in VMS.c */
3053 =for apidoc_section $io
3054 =for apidoc my_pclose
3056 A wrapper for the C library L<pclose(3)>. Don't use the latter, as the Perl
3057 version knows things that interact with the rest of the perl interpreter.
3062 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
3064 Perl_my_pclose(pTHX_ PerlIO *ptr)
3072 const int fd = PerlIO_fileno(ptr);
3075 svp = av_fetch(PL_fdpid, fd, FALSE);
3077 pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
3084 #if defined(USE_PERLIO)
3085 /* Find out whether the refcount is low enough for us to wait for the
3086 child proc without blocking. */
3087 should_wait = PerlIOUnix_refcnt(fd) == 1 && pid > 0;
3089 should_wait = pid > 0;
3093 if (pid == -2) { /* Opened by popen. */
3094 return my_syspclose(ptr);
3097 close_failed = (PerlIO_close(ptr) == EOF);
3099 if (should_wait) do {
3100 pid2 = wait4pid(pid, &status, 0);
3101 } while (pid2 == -1 && errno == EINTR);
3108 ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)
3112 #elif defined(__LIBCATAMOUNT__)
3114 Perl_my_pclose(pTHX_ PerlIO *ptr)
3118 #endif /* !DOSISH */
3120 #if (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
3122 Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
3125 PERL_ARGS_ASSERT_WAIT4PID;
3126 #ifdef PERL_USES_PL_PIDSTATUS
3128 /* PERL_USES_PL_PIDSTATUS is only defined when neither
3129 waitpid() nor wait4() is available, or on OS/2, which
3130 doesn't appear to support waiting for a progress group
3131 member, so we can only treat a 0 pid as an unknown child.
3138 /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the
3139 pid, rather than a string form. */
3140 SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE);
3141 if (svp && *svp != &PL_sv_undef) {
3142 *statusp = SvIVX(*svp);
3143 (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),
3151 hv_iterinit(PL_pidstatus);
3152 if ((entry = hv_iternext(PL_pidstatus))) {
3153 SV * const sv = hv_iterval(PL_pidstatus,entry);
3155 const char * const spid = hv_iterkey(entry,&len);
3157 assert (len == sizeof(Pid_t));
3158 memcpy((char *)&pid, spid, len);
3159 *statusp = SvIVX(sv);
3160 /* The hash iterator is currently on this entry, so simply
3161 calling hv_delete would trigger the lazy delete, which on
3162 aggregate does more work, because next call to hv_iterinit()
3163 would spot the flag, and have to call the delete routine,
3164 while in the meantime any new entries can't re-use that
3166 hv_iterinit(PL_pidstatus);
3167 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
3174 # ifdef HAS_WAITPID_RUNTIME
3175 if (!HAS_WAITPID_RUNTIME)
3178 result = PerlProc_waitpid(pid,statusp,flags);
3181 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
3182 result = wait4(pid,statusp,flags,NULL);
3185 #ifdef PERL_USES_PL_PIDSTATUS
3186 #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
3191 Perl_croak(aTHX_ "Can't do waitpid with flags");
3193 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
3194 pidgone(result,*statusp);
3200 #if defined(HAS_WAITPID) || defined(HAS_WAIT4)
3203 if (result < 0 && errno == EINTR) {
3205 errno = EINTR; /* reset in case a signal handler changed $! */
3209 #endif /* !DOSISH || OS2 || WIN32 */
3211 #ifdef PERL_USES_PL_PIDSTATUS
3213 S_pidgone(pTHX_ Pid_t pid, int status)
3217 sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE);
3218 SvUPGRADE(sv,SVt_IV);
3219 SvIV_set(sv, status);
3227 int /* Cannot prototype with I32
3229 my_syspclose(PerlIO *ptr)
3232 Perl_my_pclose(pTHX_ PerlIO *ptr)
3235 /* Needs work for PerlIO ! */
3236 FILE * const f = PerlIO_findFILE(ptr);
3237 const I32 result = pclose(f);
3238 PerlIO_releaseFILE(ptr,f);
3244 =for apidoc repeatcpy
3246 Make C<count> copies of the C<len> bytes beginning at C<from>, placing them
3247 into memory beginning at C<to>, which must be big enough to accommodate them
3253 #define PERL_REPEATCPY_LINEAR 4
3255 Perl_repeatcpy(char *to, const char *from, SSize_t len, IV count)
3257 PERL_ARGS_ASSERT_REPEATCPY;
3262 croak_memory_wrap();
3265 memset(to, *from, count);
3268 IV items, linear, half;
3270 linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR;
3271 for (items = 0; items < linear; ++items) {
3272 const char *q = from;
3274 for (todo = len; todo > 0; todo--)
3279 while (items <= half) {
3280 IV size = items * len;
3281 memcpy(p, to, size);
3287 memcpy(p, to, (count - items) * len);
3293 Perl_same_dirent(pTHX_ const char *a, const char *b)
3295 char *fa = strrchr(a,'/');
3296 char *fb = strrchr(b,'/');
3299 SV * const tmpsv = sv_newmortal();
3301 PERL_ARGS_ASSERT_SAME_DIRENT;
3314 sv_setpvs(tmpsv, ".");
3316 sv_setpvn(tmpsv, a, fa - a);
3317 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
3320 sv_setpvs(tmpsv, ".");
3322 sv_setpvn(tmpsv, b, fb - b);
3323 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
3325 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
3326 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
3328 #endif /* !HAS_RENAME */
3331 Perl_find_script(pTHX_ const char *scriptname, bool dosearch,
3332 const char *const *const search_ext, I32 flags)
3334 const char *xfound = NULL;
3335 char *xfailed = NULL;
3336 char tmpbuf[MAXPATHLEN];
3341 #if defined(DOSISH) && !defined(OS2)
3342 # define SEARCH_EXTS ".bat", ".cmd", NULL
3343 # define MAX_EXT_LEN 4
3346 # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
3347 # define MAX_EXT_LEN 4
3350 # define SEARCH_EXTS ".pl", ".com", NULL
3351 # define MAX_EXT_LEN 4
3353 /* additional extensions to try in each dir if scriptname not found */
3355 static const char *const exts[] = { SEARCH_EXTS };
3356 const char *const *const ext = search_ext ? search_ext : exts;
3357 int extidx = 0, i = 0;
3358 const char *curext = NULL;
3360 PERL_UNUSED_ARG(search_ext);
3361 # define MAX_EXT_LEN 0
3364 PERL_ARGS_ASSERT_FIND_SCRIPT;
3367 * If dosearch is true and if scriptname does not contain path
3368 * delimiters, search the PATH for scriptname.
3370 * If SEARCH_EXTS is also defined, will look for each
3371 * scriptname{SEARCH_EXTS} whenever scriptname is not found
3372 * while searching the PATH.
3374 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
3375 * proceeds as follows:
3376 * If DOSISH or VMSISH:
3377 * + look for ./scriptname{,.foo,.bar}
3378 * + search the PATH for scriptname{,.foo,.bar}
3381 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
3382 * this will not look in '.' if it's not in the PATH)
3387 # ifdef ALWAYS_DEFTYPES
3388 len = strlen(scriptname);
3389 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
3390 int idx = 0, deftypes = 1;
3393 const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
3396 int idx = 0, deftypes = 1;
3399 const int hasdir = (strpbrk(scriptname,":[</") != NULL);
3401 /* The first time through, just add SEARCH_EXTS to whatever we
3402 * already have, so we can check for default file types. */
3404 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
3411 if ((strlen(tmpbuf) + strlen(scriptname)
3412 + MAX_EXT_LEN) >= sizeof tmpbuf)
3413 continue; /* don't search dir with too-long name */
3414 my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
3418 if (strEQ(scriptname, "-"))
3420 if (dosearch) { /* Look in '.' first. */
3421 const char *cur = scriptname;
3423 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
3425 if (strEQ(ext[i++],curext)) {
3426 extidx = -1; /* already has an ext */
3431 DEBUG_p(PerlIO_printf(Perl_debug_log,
3432 "Looking for %s\n",cur));
3435 if (PerlLIO_stat(cur,&statbuf) >= 0
3436 && !S_ISDIR(statbuf.st_mode)) {
3445 if (cur == scriptname) {
3446 len = strlen(scriptname);
3447 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
3449 my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
3452 } while (extidx >= 0 && ext[extidx] /* try an extension? */
3453 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len));
3458 if (dosearch && !strchr(scriptname, '/')
3460 && !strchr(scriptname, '\\')
3462 && (s = PerlEnv_getenv("PATH")))
3466 bufend = s + strlen(s);
3467 while (s < bufend) {
3471 && *s != ';'; len++, s++) {
3472 if (len < sizeof tmpbuf)
3475 if (len < sizeof tmpbuf)
3478 s = delimcpy_no_escape(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend,
3483 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
3484 continue; /* don't search dir with too-long name */
3487 && tmpbuf[len - 1] != '/'
3488 && tmpbuf[len - 1] != '\\'
3491 tmpbuf[len++] = '/';
3492 if (len == 2 && tmpbuf[0] == '.')
3494 (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
3498 len = strlen(tmpbuf);
3499 if (extidx > 0) /* reset after previous loop */
3503 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3504 retval = PerlLIO_stat(tmpbuf,&statbuf);
3505 if (S_ISDIR(statbuf.st_mode)) {
3509 } while ( retval < 0 /* not there */
3510 && extidx>=0 && ext[extidx] /* try an extension? */
3511 && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)
3516 if (S_ISREG(statbuf.st_mode)
3517 && cando(S_IRUSR,TRUE,&statbuf)
3518 #if !defined(DOSISH)
3519 && cando(S_IXUSR,TRUE,&statbuf)
3523 xfound = tmpbuf; /* bingo! */
3527 xfailed = savepv(tmpbuf);
3532 if (!xfound && !seen_dot && !xfailed &&
3533 (PerlLIO_stat(scriptname,&statbuf) < 0
3534 || S_ISDIR(statbuf.st_mode)))
3536 seen_dot = 1; /* Disable message. */
3541 if (flags & 1) { /* do or die? */
3542 /* diag_listed_as: Can't execute %s */
3543 Perl_croak(aTHX_ "Can't %s %s%s%s",
3544 (xfailed ? "execute" : "find"),
3545 (xfailed ? xfailed : scriptname),
3546 (xfailed ? "" : " on PATH"),
3547 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3552 scriptname = xfound;
3554 return (scriptname ? savepv(scriptname) : NULL);
3557 #ifndef PERL_GET_CONTEXT_DEFINED
3560 =for apidoc_section $embedding
3561 =for apidoc set_context
3563 Implements L<perlapi/C<PERL_SET_CONTEXT>>, which you should use instead.
3569 Perl_set_context(void *t)
3571 PERL_ARGS_ASSERT_SET_CONTEXT;
3572 #if defined(USE_ITHREADS)
3573 # ifdef PERL_USE_THREAD_LOCAL
3574 PL_current_context = t;
3576 # ifdef I_MACH_CTHREADS
3577 cthread_set_data(cthread_self(), t);
3579 /* We set thread-specific value always, as C++ code has to read it with
3580 * pthreads, because the declaration syntax for thread local storage for C11
3581 * is incompatible with C++, meaning that we can't expose the thread local
3582 * variable to C++ code. */
3584 const int error = pthread_setspecific(PL_thr_key, t);
3586 Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error);
3590 PERL_SET_NON_tTHX_CONTEXT(t);
3597 #endif /* !PERL_GET_CONTEXT_DEFINED */
3600 =for apidoc get_op_names
3602 Return a pointer to the array of all the names of the various OPs
3603 Given an opcode from the enum in F<opcodes.h>, C<PL_op_name[opcode]> returns a
3604 pointer to a C language string giving its name.
3610 Perl_get_op_names(pTHX)
3612 PERL_UNUSED_CONTEXT;
3613 return (char **)PL_op_name;
3617 =for apidoc get_op_descs
3619 Return a pointer to the array of all the descriptions of the various OPs
3620 Given an opcode from the enum in F<opcodes.h>, C<PL_op_desc[opcode]> returns a
3621 pointer to a C language string giving its description.
3627 Perl_get_op_descs(pTHX)
3629 PERL_UNUSED_CONTEXT;
3630 return (char **)PL_op_desc;
3634 Perl_get_no_modify(pTHX)
3636 PERL_UNUSED_CONTEXT;
3637 return PL_no_modify;
3641 Perl_get_opargs(pTHX)
3643 PERL_UNUSED_CONTEXT;
3644 return (U32 *)PL_opargs;
3648 Perl_get_ppaddr(pTHX)
3650 PERL_UNUSED_CONTEXT;
3651 return (PPADDR_t*)PL_ppaddr;
3654 #ifndef HAS_GETENV_LEN
3656 Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
3658 char * const env_trans = PerlEnv_getenv(env_elem);
3659 PERL_UNUSED_CONTEXT;
3660 PERL_ARGS_ASSERT_GETENV_LEN;
3662 *len = strlen(env_trans);
3668 =for apidoc_section $io
3669 =for apidoc my_fflush_all
3671 Implements C<PERL_FLUSHALL_FOR_CHILD> on some platforms.
3677 Perl_my_fflush_all(pTHX)
3679 #if defined(USE_PERLIO) || defined(FFLUSH_NULL)
3680 return PerlIO_flush(NULL);
3682 # if defined(HAS__FWALK)
3683 extern int fflush(FILE *);
3684 /* undocumented, unprototyped, but very useful BSDism */
3685 extern void _fwalk(int (*)(FILE *));
3689 # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3691 # ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3692 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3693 # elif defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3694 open_max = sysconf(_SC_OPEN_MAX);
3695 # elif defined(FOPEN_MAX)
3696 open_max = FOPEN_MAX;
3697 # elif defined(OPEN_MAX)
3698 open_max = OPEN_MAX;
3699 # elif defined(_NFILE)
3704 for (i = 0; i < open_max; i++)
3705 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3706 STDIO_STREAM_ARRAY[i]._file < open_max &&
3707 STDIO_STREAM_ARRAY[i]._flag)
3708 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3712 SETERRNO(EBADF,RMS_IFI);
3719 Perl_report_wrongway_fh(pTHX_ const GV *gv, const char have)
3721 if (ckWARN(WARN_IO)) {
3723 = gv && (isGV_with_GP(gv))
3726 const char * const direction = have == '>' ? "out" : "in";
3728 if (name && HEK_LEN(name))
3729 Perl_warner(aTHX_ packWARN(WARN_IO),
3730 "Filehandle %" HEKf " opened only for %sput",
3731 HEKfARG(name), direction);
3733 Perl_warner(aTHX_ packWARN(WARN_IO),
3734 "Filehandle opened only for %sput", direction);
3739 Perl_report_evil_fh(pTHX_ const GV *gv)
3741 const IO *io = gv ? GvIO(gv) : NULL;
3742 const PERL_BITFIELD16 op = PL_op->op_type;
3746 if (io && IoTYPE(io) == IoTYPE_CLOSED) {
3748 warn_type = WARN_CLOSED;
3752 warn_type = WARN_UNOPENED;
3755 if (ckWARN(warn_type)) {
3757 = gv && isGV_with_GP(gv) && GvENAMELEN(gv) ?
3758 newSVhek_mortal(GvENAME_HEK(gv)) : NULL;
3759 const char * const pars =
3760 (const char *)(OP_IS_FILETEST(op) ? "" : "()");
3761 const char * const func =
3763 (op == OP_READLINE || op == OP_RCATLINE
3764 ? "readline" : /* "<HANDLE>" not nice */
3765 op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */
3767 const char * const type =
3769 (OP_IS_SOCKET(op) || (io && IoTYPE(io) == IoTYPE_SOCKET)
3770 ? "socket" : "filehandle");
3771 const bool have_name = name && SvCUR(name);
3772 Perl_warner(aTHX_ packWARN(warn_type),
3773 "%s%s on %s %s%s%" SVf, func, pars, vile, type,
3774 have_name ? " " : "",
3775 SVfARG(have_name ? name : &PL_sv_no));
3776 if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3778 aTHX_ packWARN(warn_type),
3779 "\t(Are you trying to call %s%s on dirhandle%s%" SVf "?)\n",
3780 func, pars, have_name ? " " : "",
3781 SVfARG(have_name ? name : &PL_sv_no)
3786 /* To workaround core dumps from the uninitialised tm_zone we get the
3787 * system to give us a reasonable struct to copy. This fix means that
3788 * strftime uses the tm_zone and tm_gmtoff values returned by
3789 * localtime(time()). That should give the desired result most of the
3790 * time. But probably not always!
3792 * This does not address tzname aspects of NETaa14816.
3797 # ifndef STRUCT_TM_HASZONE
3798 # define STRUCT_TM_HASZONE
3802 #ifdef STRUCT_TM_HASZONE /* Backward compat */
3803 # ifndef HAS_TM_TM_ZONE
3804 # define HAS_TM_TM_ZONE
3809 Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */
3811 #ifdef HAS_TM_TM_ZONE
3813 const struct tm* my_tm;
3814 PERL_UNUSED_CONTEXT;
3815 PERL_ARGS_ASSERT_INIT_TM;
3819 my_tm = localtime(&now);
3821 Copy(my_tm, ptm, 1, struct tm);
3824 PERL_UNUSED_CONTEXT;
3825 PERL_ARGS_ASSERT_INIT_TM;
3826 PERL_UNUSED_ARG(ptm);
3831 =for apidoc_section $time
3832 =for apidoc mini_mktime
3833 normalise S<C<struct tm>> values without the localtime() semantics (and
3834 overhead) of mktime().
3839 Perl_mini_mktime(struct tm *ptm)
3843 int month, mday, year, jday;
3844 int odd_cent, odd_year;
3846 PERL_ARGS_ASSERT_MINI_MKTIME;
3848 #define DAYS_PER_YEAR 365
3849 #define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1)
3850 #define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1)
3851 #define DAYS_PER_QCENT (4*DAYS_PER_CENT+1)
3852 #define SECS_PER_HOUR (60*60)
3853 #define SECS_PER_DAY (24*SECS_PER_HOUR)
3854 /* parentheses deliberately absent on these two, otherwise they don't work */
3855 #define MONTH_TO_DAYS 153/5
3856 #define DAYS_TO_MONTH 5/153
3857 /* offset to bias by March (month 4) 1st between month/mday & year finding */
3858 #define YEAR_ADJUST (4*MONTH_TO_DAYS+1)
3859 /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */
3860 #define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */
3863 * Year/day algorithm notes:
3865 * With a suitable offset for numeric value of the month, one can find
3866 * an offset into the year by considering months to have 30.6 (153/5) days,
3867 * using integer arithmetic (i.e., with truncation). To avoid too much
3868 * messing about with leap days, we consider January and February to be
3869 * the 13th and 14th month of the previous year. After that transformation,
3870 * we need the month index we use to be high by 1 from 'normal human' usage,
3871 * so the month index values we use run from 4 through 15.
3873 * Given that, and the rules for the Gregorian calendar (leap years are those
3874 * divisible by 4 unless also divisible by 100, when they must be divisible
3875 * by 400 instead), we can simply calculate the number of days since some
3876 * arbitrary 'beginning of time' by futzing with the (adjusted) year number,
3877 * the days we derive from our month index, and adding in the day of the
3878 * month. The value used here is not adjusted for the actual origin which
3879 * it normally would use (1 January A.D. 1), since we're not exposing it.
3880 * We're only building the value so we can turn around and get the
3881 * normalised values for the year, month, day-of-month, and day-of-year.
3883 * For going backward, we need to bias the value we're using so that we find
3884 * the right year value. (Basically, we don't want the contribution of
3885 * March 1st to the number to apply while deriving the year). Having done
3886 * that, we 'count up' the contribution to the year number by accounting for
3887 * full quadracenturies (400-year periods) with their extra leap days, plus
3888 * the contribution from full centuries (to avoid counting in the lost leap
3889 * days), plus the contribution from full quad-years (to count in the normal
3890 * leap days), plus the leftover contribution from any non-leap years.
3891 * At this point, if we were working with an actual leap day, we'll have 0
3892 * days left over. This is also true for March 1st, however. So, we have
3893 * to special-case that result, and (earlier) keep track of the 'odd'
3894 * century and year contributions. If we got 4 extra centuries in a qcent,
3895 * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb.
3896 * Otherwise, we add back in the earlier bias we removed (the 123 from
3897 * figuring in March 1st), find the month index (integer division by 30.6),
3898 * and the remainder is the day-of-month. We then have to convert back to
3899 * 'real' months (including fixing January and February from being 14/15 in
3900 * the previous year to being in the proper year). After that, to get
3901 * tm_yday, we work with the normalised year and get a new yearday value for
3902 * January 1st, which we subtract from the yearday value we had earlier,
3903 * representing the date we've re-built. This is done from January 1
3904 * because tm_yday is 0-origin.
3906 * Since POSIX time routines are only guaranteed to work for times since the
3907 * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm
3908 * applies Gregorian calendar rules even to dates before the 16th century
3909 * doesn't bother me. Besides, you'd need cultural context for a given
3910 * date to know whether it was Julian or Gregorian calendar, and that's
3911 * outside the scope for this routine. Since we convert back based on the
3912 * same rules we used to build the yearday, you'll only get strange results
3913 * for input which needed normalising, or for the 'odd' century years which
3914 * were leap years in the Julian calendar but not in the Gregorian one.
3915 * I can live with that.
3917 * This algorithm also fails to handle years before A.D. 1 gracefully, but
3918 * that's still outside the scope for POSIX time manipulation, so I don't
3924 year = 1900 + ptm->tm_year;
3925 month = ptm->tm_mon;
3926 mday = ptm->tm_mday;
3932 yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400;
3933 yearday += month*MONTH_TO_DAYS + mday + jday;
3935 * Note that we don't know when leap-seconds were or will be,
3936 * so we have to trust the user if we get something which looks
3937 * like a sensible leap-second. Wild values for seconds will
3938 * be rationalised, however.
3940 if ((unsigned) ptm->tm_sec <= 60) {
3947 secs += 60 * ptm->tm_min;
3948 secs += SECS_PER_HOUR * ptm->tm_hour;
3950 if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) {
3951 /* got negative remainder, but need positive time */
3952 /* back off an extra day to compensate */
3953 yearday += (secs/SECS_PER_DAY)-1;
3954 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1);
3957 yearday += (secs/SECS_PER_DAY);
3958 secs -= SECS_PER_DAY * (secs/SECS_PER_DAY);
3961 else if (secs >= SECS_PER_DAY) {
3962 yearday += (secs/SECS_PER_DAY);
3963 secs %= SECS_PER_DAY;
3965 ptm->tm_hour = secs/SECS_PER_HOUR;
3966 secs %= SECS_PER_HOUR;
3967 ptm->tm_min = secs/60;
3969 ptm->tm_sec += secs;
3970 /* done with time of day effects */
3972 * The algorithm for yearday has (so far) left it high by 428.
3973 * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to
3974 * bias it by 123 while trying to figure out what year it
3975 * really represents. Even with this tweak, the reverse
3976 * translation fails for years before A.D. 0001.
3977 * It would still fail for Feb 29, but we catch that one below.
3979 jday = yearday; /* save for later fixup vis-a-vis Jan 1 */
3980 yearday -= YEAR_ADJUST;
3981 year = (yearday / DAYS_PER_QCENT) * 400;
3982 yearday %= DAYS_PER_QCENT;
3983 odd_cent = yearday / DAYS_PER_CENT;
3984 year += odd_cent * 100;
3985 yearday %= DAYS_PER_CENT;
3986 year += (yearday / DAYS_PER_QYEAR) * 4;
3987 yearday %= DAYS_PER_QYEAR;
3988 odd_year = yearday / DAYS_PER_YEAR;
3990 yearday %= DAYS_PER_YEAR;
3991 if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */
3996 yearday += YEAR_ADJUST; /* recover March 1st crock */
3997 month = yearday*DAYS_TO_MONTH;
3998 yearday -= month*MONTH_TO_DAYS;
3999 /* recover other leap-year adjustment */
4008 ptm->tm_year = year - 1900;
4010 ptm->tm_mday = yearday;
4011 ptm->tm_mon = month;
4015 ptm->tm_mon = month - 1;
4017 /* re-build yearday based on Jan 1 to get tm_yday */
4019 yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400;
4020 yearday += 14*MONTH_TO_DAYS + 1;
4021 ptm->tm_yday = jday - yearday;
4022 ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
4025 #define SV_CWD_RETURN_UNDEF \
4029 #define SV_CWD_ISDOT(dp) \
4030 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
4031 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
4034 =for apidoc_section $utility
4036 =for apidoc getcwd_sv
4038 Fill C<sv> with current working directory
4043 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
4044 * rewritten again by dougm, optimized for use with xs TARG, and to prefer
4045 * getcwd(3) if available
4046 * Comments from the original:
4047 * This is a faster version of getcwd. It's also more dangerous
4048 * because you might chdir out of a directory that you can't chdir
4052 Perl_getcwd_sv(pTHX_ SV *sv)
4056 PERL_ARGS_ASSERT_GETCWD_SV;
4060 char buf[MAXPATHLEN];
4062 /* Some getcwd()s automatically allocate a buffer of the given
4063 * size from the heap if they are given a NULL buffer pointer.
4064 * The problem is that this behaviour is not portable. */
4065 if (getcwd(buf, sizeof(buf) - 1)) {
4070 SV_CWD_RETURN_UNDEF;
4077 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
4081 SvUPGRADE(sv, SVt_PV);
4083 if (PerlLIO_lstat(".", &statbuf) < 0) {
4084 SV_CWD_RETURN_UNDEF;
4087 orig_cdev = statbuf.st_dev;
4088 orig_cino = statbuf.st_ino;
4098 if (PerlDir_chdir("..") < 0) {
4099 SV_CWD_RETURN_UNDEF;
4101 if (PerlLIO_stat(".", &statbuf) < 0) {
4102 SV_CWD_RETURN_UNDEF;
4105 cdev = statbuf.st_dev;
4106 cino = statbuf.st_ino;
4108 if (odev == cdev && oino == cino) {
4111 if (!(dir = PerlDir_open("."))) {
4112 SV_CWD_RETURN_UNDEF;
4115 while ((dp = PerlDir_read(dir)) != NULL) {
4117 namelen = dp->d_namlen;
4119 namelen = strlen(dp->d_name);
4122 if (SV_CWD_ISDOT(dp)) {
4126 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
4127 SV_CWD_RETURN_UNDEF;
4130 tdev = statbuf.st_dev;
4131 tino = statbuf.st_ino;
4132 if (tino == oino && tdev == odev) {
4138 SV_CWD_RETURN_UNDEF;
4141 if (pathlen + namelen + 1 >= MAXPATHLEN) {
4142 SV_CWD_RETURN_UNDEF;
4145 SvGROW(sv, pathlen + namelen + 1);
4149 Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char);
4152 /* prepend current directory to the front */
4154 Move(dp->d_name, SvPVX(sv)+1, namelen, char);
4155 pathlen += (namelen + 1);
4157 #ifdef VOID_CLOSEDIR
4160 if (PerlDir_close(dir) < 0) {
4161 SV_CWD_RETURN_UNDEF;
4167 SvCUR_set(sv, pathlen);
4171 if (PerlDir_chdir(SvPVX_const(sv)) < 0) {
4172 SV_CWD_RETURN_UNDEF;
4175 if (PerlLIO_stat(".", &statbuf) < 0) {
4176 SV_CWD_RETURN_UNDEF;
4179 cdev = statbuf.st_dev;
4180 cino = statbuf.st_ino;
4182 if (cdev != orig_cdev || cino != orig_cino) {
4183 Perl_croak(aTHX_ "Unstable directory path, "
4184 "current directory changed unexpectedly");
4194 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT)
4195 # define EMULATE_SOCKETPAIR_UDP
4198 #ifdef EMULATE_SOCKETPAIR_UDP
4200 S_socketpair_udp (int fd[2]) {
4202 /* Fake a datagram socketpair using UDP to localhost. */
4203 int sockets[2] = {-1, -1};
4204 struct sockaddr_in addresses[2];
4206 Sock_size_t size = sizeof(struct sockaddr_in);
4207 unsigned short port;
4210 memset(&addresses, 0, sizeof(addresses));
4213 sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET);
4214 if (sockets[i] == -1)
4215 goto tidy_up_and_fail;
4217 addresses[i].sin_family = AF_INET;
4218 addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4219 addresses[i].sin_port = 0; /* kernel chooses port. */
4220 if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i],
4221 sizeof(struct sockaddr_in)) == -1)
4222 goto tidy_up_and_fail;
4225 /* Now have 2 UDP sockets. Find out which port each is connected to, and
4226 for each connect the other socket to it. */
4229 if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i],
4231 goto tidy_up_and_fail;
4232 if (size != sizeof(struct sockaddr_in))
4233 goto abort_tidy_up_and_fail;
4234 /* !1 is 0, !0 is 1 */
4235 if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i],
4236 sizeof(struct sockaddr_in)) == -1)
4237 goto tidy_up_and_fail;
4240 /* Now we have 2 sockets connected to each other. I don't trust some other
4241 process not to have already sent a packet to us (by random) so send
4242 a packet from each to the other. */
4245 /* I'm going to send my own port number. As a short.
4246 (Who knows if someone somewhere has sin_port as a bitfield and needs
4247 this routine. (I'm assuming crays have socketpair)) */
4248 port = addresses[i].sin_port;
4249 got = PerlLIO_write(sockets[i], &port, sizeof(port));
4250 if (got != sizeof(port)) {
4252 goto tidy_up_and_fail;
4253 goto abort_tidy_up_and_fail;
4257 /* Packets sent. I don't trust them to have arrived though.
4258 (As I understand it Solaris TCP stack is multithreaded. Non-blocking
4259 connect to localhost will use a second kernel thread. In 2.6 the
4260 first thread running the connect() returns before the second completes,
4261 so EINPROGRESS> In 2.7 the improved stack is faster and connect()
4262 returns 0. Poor programs have tripped up. One poor program's authors'
4263 had a 50-1 reverse stock split. Not sure how connected these were.)
4264 So I don't trust someone not to have an unpredictable UDP stack.
4268 struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */
4269 int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0];
4273 FD_SET((unsigned int)sockets[0], &rset);
4274 FD_SET((unsigned int)sockets[1], &rset);
4276 got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor);
4277 if (got != 2 || !FD_ISSET(sockets[0], &rset)
4278 || !FD_ISSET(sockets[1], &rset)) {
4279 /* I hope this is portable and appropriate. */
4281 goto tidy_up_and_fail;
4282 goto abort_tidy_up_and_fail;
4286 /* And the paranoia department even now doesn't trust it to have arrive
4287 (hence MSG_DONTWAIT). Or that what arrives was sent by us. */
4289 struct sockaddr_in readfrom;
4290 unsigned short buffer[2];
4295 got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4296 sizeof(buffer), MSG_DONTWAIT,
4297 (struct sockaddr *) &readfrom, &size);
4299 got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4301 (struct sockaddr *) &readfrom, &size);
4305 goto tidy_up_and_fail;
4306 if (got != sizeof(port)
4307 || size != sizeof(struct sockaddr_in)
4308 /* Check other socket sent us its port. */
4309 || buffer[0] != (unsigned short) addresses[!i].sin_port
4310 /* Check kernel says we got the datagram from that socket */
4311 || readfrom.sin_family != addresses[!i].sin_family
4312 || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr
4313 || readfrom.sin_port != addresses[!i].sin_port)
4314 goto abort_tidy_up_and_fail;
4317 /* My caller (my_socketpair) has validated that this is non-NULL */
4320 /* I hereby declare this connection open. May God bless all who cross
4324 abort_tidy_up_and_fail:
4325 errno = ECONNABORTED;
4329 if (sockets[0] != -1)
4330 PerlLIO_close(sockets[0]);
4331 if (sockets[1] != -1)
4332 PerlLIO_close(sockets[1]);
4337 #endif /* EMULATE_SOCKETPAIR_UDP */
4339 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET)
4342 =for apidoc my_socketpair
4344 Emulates L<socketpair(2)> on systems that don't have it, but which do have
4345 enough functionality for the emulation.
4351 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4352 /* Stevens says that family must be AF_LOCAL, protocol 0.
4353 I'm going to enforce that, then ignore it, and use TCP (or UDP). */
4358 struct sockaddr_in listen_addr;
4359 struct sockaddr_in connect_addr;
4364 || family != AF_UNIX
4367 errno = EAFNOSUPPORT;
4376 type &= ~SOCK_CLOEXEC;
4379 #ifdef EMULATE_SOCKETPAIR_UDP
4380 if (type == SOCK_DGRAM)
4381 return S_socketpair_udp(fd);
4384 aTHXa(PERL_GET_THX);
4385 listener = PerlSock_socket(AF_INET, type, 0);
4388 memset(&listen_addr, 0, sizeof(listen_addr));
4389 listen_addr.sin_family = AF_INET;
4390 listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4391 listen_addr.sin_port = 0; /* kernel chooses port. */
4392 if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr,
4393 sizeof(listen_addr)) == -1)
4394 goto tidy_up_and_fail;
4395 if (PerlSock_listen(listener, 1) == -1)
4396 goto tidy_up_and_fail;
4398 connector = PerlSock_socket(AF_INET, type, 0);
4399 if (connector == -1)
4400 goto tidy_up_and_fail;
4401 /* We want to find out the port number to connect to. */
4402 size = sizeof(connect_addr);
4403 if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr,
4405 goto tidy_up_and_fail;
4406 if (size != sizeof(connect_addr))
4407 goto abort_tidy_up_and_fail;
4408 if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr,
4409 sizeof(connect_addr)) == -1)
4410 goto tidy_up_and_fail;
4412 size = sizeof(listen_addr);
4413 acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr,
4416 goto tidy_up_and_fail;
4417 if (size != sizeof(listen_addr))
4418 goto abort_tidy_up_and_fail;
4419 PerlLIO_close(listener);
4420 /* Now check we are talking to ourself by matching port and host on the
4422 if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr,
4424 goto tidy_up_and_fail;
4425 if (size != sizeof(connect_addr)
4426 || listen_addr.sin_family != connect_addr.sin_family
4427 || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
4428 || listen_addr.sin_port != connect_addr.sin_port) {
4429 goto abort_tidy_up_and_fail;
4435 abort_tidy_up_and_fail:
4437 errno = ECONNABORTED; /* This would be the standard thing to do. */
4438 #elif defined(ECONNREFUSED)
4439 errno = ECONNREFUSED; /* some OSes might not have ECONNABORTED. */
4441 errno = ETIMEDOUT; /* Desperation time. */
4447 PerlLIO_close(listener);
4448 if (connector != -1)
4449 PerlLIO_close(connector);
4451 PerlLIO_close(acceptor);
4457 /* In any case have a stub so that there's code corresponding
4458 * to the my_socketpair in embed.fnc. */
4460 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4461 #ifdef HAS_SOCKETPAIR
4462 return socketpair(family, type, protocol, fd);
4471 =for apidoc sv_nosharing
4473 Dummy routine which "shares" an SV when there is no sharing module present.
4474 Or "locks" it. Or "unlocks" it. In other
4475 words, ignores its single SV argument.
4476 Exists to avoid test for a C<NULL> function pointer and because it could
4477 potentially warn under some level of strict-ness.
4483 Perl_sv_nosharing(pTHX_ SV *sv)
4485 PERL_UNUSED_CONTEXT;
4486 PERL_UNUSED_ARG(sv);
4491 =for apidoc sv_destroyable
4493 Dummy routine which reports that object can be destroyed when there is no
4494 sharing module present. It ignores its single SV argument, and returns
4495 'true'. Exists to avoid test for a C<NULL> function pointer and because it
4496 could potentially warn under some level of strict-ness.
4502 Perl_sv_destroyable(pTHX_ SV *sv)
4504 PERL_UNUSED_CONTEXT;
4505 PERL_UNUSED_ARG(sv);
4510 Perl_parse_unicode_opts(pTHX_ const char **popt)
4512 const char *p = *popt;
4515 PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS;
4519 const char* endptr = p + strlen(p);
4521 if (grok_atoUV(p, &uv, &endptr) && uv <= U32_MAX) {
4524 if (p && *p && *p != '\n' && *p != '\r') {
4526 goto the_end_of_the_opts_parser;
4528 Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p);
4532 Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p);
4538 case PERL_UNICODE_STDIN:
4539 opt |= PERL_UNICODE_STDIN_FLAG; break;
4540 case PERL_UNICODE_STDOUT:
4541 opt |= PERL_UNICODE_STDOUT_FLAG; break;
4542 case PERL_UNICODE_STDERR:
4543 opt |= PERL_UNICODE_STDERR_FLAG; break;
4544 case PERL_UNICODE_STD:
4545 opt |= PERL_UNICODE_STD_FLAG; break;
4546 case PERL_UNICODE_IN:
4547 opt |= PERL_UNICODE_IN_FLAG; break;
4548 case PERL_UNICODE_OUT:
4549 opt |= PERL_UNICODE_OUT_FLAG; break;
4550 case PERL_UNICODE_INOUT:
4551 opt |= PERL_UNICODE_INOUT_FLAG; break;
4552 case PERL_UNICODE_LOCALE:
4553 opt |= PERL_UNICODE_LOCALE_FLAG; break;
4554 case PERL_UNICODE_ARGV:
4555 opt |= PERL_UNICODE_ARGV_FLAG; break;
4556 case PERL_UNICODE_UTF8CACHEASSERT:
4557 opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break;
4559 if (*p != '\n' && *p != '\r') {
4560 if(isSPACE(*p)) goto the_end_of_the_opts_parser;
4563 "Unknown Unicode option letter '%c'", *p);
4570 opt = PERL_UNICODE_DEFAULT_FLAGS;
4572 the_end_of_the_opts_parser:
4574 if (opt & ~PERL_UNICODE_ALL_FLAGS)
4575 Perl_croak(aTHX_ "Unknown Unicode option value %" UVuf,
4576 (UV) (opt & ~PERL_UNICODE_ALL_FLAGS));
4584 # include <starlet.h>
4587 /* hash a pointer and return a U32
4589 * this code was derived from Sereal, which was derived from autobox.
4592 PERL_STATIC_INLINE U32 S_ptr_hash(PTRV u) {
4595 * This is one of Thomas Wang's hash functions for 64-bit integers from:
4596 * http://www.concentric.net/~Ttwang/tech/inthash.htm
4598 u = (~u) + (u << 18);
4606 * This is one of Bob Jenkins' hash functions for 32-bit integers
4607 * from: http://burtleburtle.net/bob/hash/integer.html
4609 u = (u + 0x7ed55d16) + (u << 12);
4610 u = (u ^ 0xc761c23c) ^ (u >> 19);
4611 u = (u + 0x165667b1) + (u << 5);
4612 u = (u + 0xd3a2646c) ^ (u << 9);
4613 u = (u + 0xfd7046c5) + (u << 3);
4614 u = (u ^ 0xb55a4f09) ^ (u >> 16);
4624 * This is really just a quick hack which grabs various garbage
4625 * values. It really should be a real hash algorithm which
4626 * spreads the effect of every input bit onto every output bit,
4627 * if someone who knows about such things would bother to write it.
4628 * Might be a good idea to add that function to CORE as well.
4629 * No numbers below come from careful analysis or anything here,
4630 * except they are primes and SEED_C1 > 1E6 to get a full-width
4631 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
4632 * probably be bigger too.
4635 # define SEED_C1 1000003
4636 #define SEED_C4 73819
4638 # define SEED_C1 25747
4639 #define SEED_C4 20639
4643 #define SEED_C5 26107
4645 #ifndef PERL_NO_DEV_RANDOM
4649 #ifdef HAS_GETTIMEOFDAY
4650 struct timeval when;
4655 /* This test is an escape hatch, this symbol isn't set by Configure. */
4656 #ifndef PERL_NO_DEV_RANDOM
4657 #ifndef PERL_RANDOM_DEVICE
4658 /* /dev/random isn't used by default because reads from it will block
4659 * if there isn't enough entropy available. You can compile with
4660 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
4661 * is enough real entropy to fill the seed. */
4662 # ifdef __amigaos4__
4663 # define PERL_RANDOM_DEVICE "RANDOM:SIZE=4"
4665 # define PERL_RANDOM_DEVICE "/dev/urandom"
4668 fd = PerlLIO_open_cloexec(PERL_RANDOM_DEVICE, 0);
4670 if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u)
4678 #ifdef HAS_GETTIMEOFDAY
4679 PerlProc_gettimeofday(&when,NULL);
4680 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
4683 u = (U32)SEED_C1 * when;
4685 u += SEED_C3 * (U32)PerlProc_getpid();
4686 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
4687 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
4688 UV ptruv = PTR2UV(&when);
4689 u += SEED_C5 * ptr_hash(ptruv);
4695 Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
4697 #ifndef NO_PERL_HASH_ENV
4702 PERL_ARGS_ASSERT_GET_HASH_SEED;
4704 Zero(seed_buffer, PERL_HASH_SEED_BYTES, U8);
4705 Zero((U8*)PL_hash_state_w, PERL_HASH_STATE_BYTES, U8);
4707 #ifndef NO_PERL_HASH_ENV
4708 env_pv= PerlEnv_getenv("PERL_HASH_SEED");
4713 PerlIO_printf(Perl_debug_log,"Got PERL_HASH_SEED=<%s>\n", env_pv);
4714 /* ignore leading spaces */
4715 while (isSPACE(*env_pv))
4717 # ifdef USE_PERL_PERTURB_KEYS
4718 /* if they set it to "0" we disable key traversal randomization completely */
4719 if (strEQ(env_pv,"0")) {
4720 PL_hash_rand_bits_enabled= 0;
4722 /* otherwise switch to deterministic mode */
4723 PL_hash_rand_bits_enabled= 2;
4726 /* ignore a leading 0x... if it is there */
4727 if (env_pv[0] == '0' && env_pv[1] == 'x')
4730 for( i = 0; isXDIGIT(*env_pv) && i < PERL_HASH_SEED_BYTES; i++ ) {
4731 seed_buffer[i] = READ_XDIGIT(env_pv) << 4;
4732 if ( isXDIGIT(*env_pv)) {
4733 seed_buffer[i] |= READ_XDIGIT(env_pv);
4736 while (isSPACE(*env_pv))
4739 if (*env_pv && !isXDIGIT(*env_pv)) {
4740 Perl_warn(aTHX_ "perl: warning: Non hex character in '$ENV{PERL_HASH_SEED}', seed only partially set\n");
4742 /* should we check for unparsed crap? */
4743 /* should we warn about unused hex? */
4744 /* should we warn about insufficient hex? */
4747 #endif /* NO_PERL_HASH_ENV */
4749 for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
4750 seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
4753 #ifdef USE_PERL_PERTURB_KEYS
4754 # ifndef NO_PERL_HASH_ENV
4755 env_pv= PerlEnv_getenv("PERL_PERTURB_KEYS");
4758 PerlIO_printf(Perl_debug_log,
4759 "Got PERL_PERTURB_KEYS=<%s>\n", env_pv);
4760 if (strEQ(env_pv,"0") || strEQ(env_pv,"NO")) {
4761 PL_hash_rand_bits_enabled= 0;
4762 } else if (strEQ(env_pv,"1") || strEQ(env_pv,"RANDOM")) {
4763 PL_hash_rand_bits_enabled= 1;
4764 } else if (strEQ(env_pv,"2") || strEQ(env_pv,"DETERMINISTIC")) {
4765 PL_hash_rand_bits_enabled= 2;
4767 Perl_warn(aTHX_ "perl: warning: strange setting in '$ENV{PERL_PERTURB_KEYS}': '%s'\n", env_pv);
4771 { /* initialize PL_hash_rand_bits from the hash seed.
4772 * This value is highly volatile, it is updated every
4773 * hash insert, and is used as part of hash bucket chain
4774 * randomization and hash iterator randomization. */
4775 if (PL_hash_rand_bits_enabled == 1) {
4776 /* random mode initialize from seed() like we would our RNG() */
4777 PL_hash_rand_bits= seed();
4780 /* Use a constant */
4781 PL_hash_rand_bits= 0xbe49d17f; /* I just picked a number */
4782 /* and then mix in the leading bytes of the hash seed */
4783 for( i = 0; i < sizeof(UV) ; i++ ) {
4784 PL_hash_rand_bits ^= seed_buffer[i % PERL_HASH_SEED_BYTES];
4785 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,8);
4788 if (!PL_hash_rand_bits) {
4789 /* we use an XORSHIFT RNG to munge PL_hash_rand_bits,
4790 * which means it cannot be 0 or it will stay 0 for the
4791 * lifetime of the process, so if by some insane chance we
4792 * ended up with a 0 after the above initialization
4793 * then set it to this. This really should not happen, or
4794 * very very very rarely.
4796 PL_hash_rand_bits = 0x8110ba9d; /* a randomly chosen prime */
4803 Perl_debug_hash_seed(pTHX_ bool via_debug_h)
4805 PERL_ARGS_ASSERT_DEBUG_HASH_SEED;
4806 #if (defined(USE_HASH_SEED) || defined(USE_HASH_SEED_DEBUG)) && !defined(NO_PERL_HASH_SEED_DEBUG)
4808 const char * const s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
4809 bool via_env = cBOOL(s && strNE(s, "0") && strNE(s,""));
4811 if ( via_env != via_debug_h ) {
4812 const unsigned char *seed= PERL_HASH_SEED;
4813 const unsigned char *seed_end= PERL_HASH_SEED + PERL_HASH_SEED_BYTES;
4814 PerlIO_printf(Perl_debug_log, "HASH_FUNCTION = %s HASH_SEED = 0x", PERL_HASH_FUNC);
4815 while (seed < seed_end) {
4816 PerlIO_printf(Perl_debug_log, "%02x", *seed++);
4818 #ifdef PERL_HASH_RANDOMIZE_KEYS
4819 PerlIO_printf(Perl_debug_log, " PERTURB_KEYS = %d (%s)",
4820 PL_HASH_RAND_BITS_ENABLED,
4821 PL_HASH_RAND_BITS_ENABLED == 0 ? "NO" :
4822 PL_HASH_RAND_BITS_ENABLED == 1 ? "RANDOM"
4825 PerlIO_printf(Perl_debug_log,
4826 " RAND_BITS=0x%" UVxf, PL_hash_rand_bits);
4828 PerlIO_printf(Perl_debug_log, "\n");
4831 #endif /* #if (defined(USE_HASH_SEED) ... */
4839 /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including
4840 * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also
4841 * given, and you supply your own implementation.
4843 * The default implementation reads a single env var, PERL_MEM_LOG,
4844 * expecting one or more of the following:
4846 * \d+ - fd fd to write to : must be 1st (grok_atoUV)
4847 * 'm' - memlog was PERL_MEM_LOG=1
4848 * 's' - svlog was PERL_SV_LOG=1
4849 * 't' - timestamp was PERL_MEM_LOG_TIMESTAMP=1
4851 * This makes the logger controllable enough that it can reasonably be
4852 * added to the system perl.
4855 /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer
4856 * the Perl_mem_log_...() will use (either via sprintf or snprintf).
4858 #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 256
4860 /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...()
4861 * writes to. In the default logger, this is settable at runtime.
4863 #ifndef PERL_MEM_LOG_FD
4864 # define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */
4867 #ifndef PERL_MEM_LOG_NOIMPL
4869 # ifdef DEBUG_LEAKING_SCALARS
4870 # define SV_LOG_SERIAL_FMT " [%lu]"
4871 # define _SV_LOG_SERIAL_ARG(sv) , (unsigned long) (sv)->sv_debug_serial
4873 # define SV_LOG_SERIAL_FMT
4874 # define _SV_LOG_SERIAL_ARG(sv)
4878 S_mem_log_common(enum mem_log_type mlt, const UV n,
4879 const UV typesize, const char *type_name, const SV *sv,
4880 Malloc_t oldalloc, Malloc_t newalloc,
4881 const char *filename, const int linenumber,
4882 const char *funcname)
4887 PERL_ARGS_ASSERT_MEM_LOG_COMMON;
4889 PL_mem_log[0] |= 0x2; /* Flag that the call is from this code */
4890 pmlenv = PerlEnv_getenv("PERL_MEM_LOG");
4891 PL_mem_log[0] &= ~0x2;
4894 if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s'))
4896 /* We can't use SVs or PerlIO for obvious reasons,
4897 * so we'll use stdio and low-level IO instead. */
4898 char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE];
4900 # ifdef HAS_GETTIMEOFDAY
4901 # define MEM_LOG_TIME_FMT "%10d.%06d: "
4902 # define MEM_LOG_TIME_ARG (int)tv.tv_sec, (int)tv.tv_usec
4904 PerlProc_gettimeofday(&tv, 0);
4906 # define MEM_LOG_TIME_FMT "%10d: "
4907 # define MEM_LOG_TIME_ARG (int)when
4911 /* If there are other OS specific ways of hires time than
4912 * gettimeofday() (see dist/Time-HiRes), the easiest way is
4913 * probably that they would be used to fill in the struct
4917 const char* endptr = pmlenv + strlen(pmlenv);
4920 if (grok_atoUV(pmlenv, &uv, &endptr) /* Ignore endptr. */
4921 && uv && uv <= PERL_INT_MAX
4925 fd = PERL_MEM_LOG_FD;
4928 if (strchr(pmlenv, 't')) {
4929 len = my_snprintf(buf, sizeof(buf),
4930 MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG);
4931 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4935 len = my_snprintf(buf, sizeof(buf),
4936 "alloc: %s:%d:%s: %" IVdf " %" UVuf
4937 " %s = %" IVdf ": %" UVxf "\n",
4938 filename, linenumber, funcname, n, typesize,
4939 type_name, n * typesize, PTR2UV(newalloc));
4942 len = my_snprintf(buf, sizeof(buf),
4943 "realloc: %s:%d:%s: %" IVdf " %" UVuf
4944 " %s = %" IVdf ": %" UVxf " -> %" UVxf "\n",
4945 filename, linenumber, funcname, n, typesize,
4946 type_name, n * typesize, PTR2UV(oldalloc),
4950 len = my_snprintf(buf, sizeof(buf),
4951 "free: %s:%d:%s: %" UVxf "\n",
4952 filename, linenumber, funcname,
4957 len = my_snprintf(buf, sizeof(buf),
4958 "%s_SV: %s:%d:%s: %" UVxf SV_LOG_SERIAL_FMT "\n",
4959 mlt == MLT_NEW_SV ? "new" : "del",
4960 filename, linenumber, funcname,
4961 PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv));
4966 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4967 #ifdef USE_C_BACKTRACE
4968 if(strchr(pmlenv,'c') && (mlt == MLT_NEW_SV)) {
4969 len = my_snprintf(buf, sizeof(buf),
4970 " caller %s at %s line %" LINE_Tf "\n",
4971 /* CopSTASHPV can crash early on startup; use CopFILE to check */
4972 CopFILE(PL_curcop) ? CopSTASHPV(PL_curcop) : "<unknown>",
4973 CopFILE(PL_curcop), CopLINE(PL_curcop));
4974 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4976 Perl_c_backtrace *bt = Perl_get_c_backtrace(aTHX_ 3, 3);
4977 Perl_c_backtrace_frame *frame;
4979 for (i = 0, frame = bt->frame_info;
4980 i < bt->header.frame_count;
4982 len = my_snprintf(buf, sizeof(buf),
4983 " frame[%" UVuf "]: %p %s at %s +0x%lx\n",
4986 frame->symbol_name_size && frame->symbol_name_offset ? (char *)bt + frame->symbol_name_offset : "-",
4987 frame->object_name_size && frame->object_name_offset ? (char *)bt + frame->object_name_offset : "?",
4988 (char *)frame->addr - (char *)frame->object_base_addr);
4989 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4991 Perl_free_c_backtrace(bt);
4993 #endif /* USE_C_BACKTRACE */
4997 #endif /* !PERL_MEM_LOG_NOIMPL */
4999 #ifndef PERL_MEM_LOG_NOIMPL
5001 mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \
5002 mem_log_common (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm)
5004 /* this is suboptimal, but bug compatible. User is providing their
5005 own implementation, but is getting these functions anyway, and they
5006 do nothing. But _NOIMPL users should be able to cope or fix */
5008 mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \
5009 /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */
5013 Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name,
5015 const char *filename, const int linenumber,
5016 const char *funcname)
5018 PERL_ARGS_ASSERT_MEM_LOG_ALLOC;
5020 mem_log_common_if(MLT_ALLOC, n, typesize, type_name,
5021 NULL, NULL, newalloc,
5022 filename, linenumber, funcname);
5027 Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name,
5028 Malloc_t oldalloc, Malloc_t newalloc,
5029 const char *filename, const int linenumber,
5030 const char *funcname)
5032 PERL_ARGS_ASSERT_MEM_LOG_REALLOC;
5034 mem_log_common_if(MLT_REALLOC, n, typesize, type_name,
5035 NULL, oldalloc, newalloc,
5036 filename, linenumber, funcname);
5041 Perl_mem_log_free(Malloc_t oldalloc,
5042 const char *filename, const int linenumber,
5043 const char *funcname)
5045 PERL_ARGS_ASSERT_MEM_LOG_FREE;
5047 mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL,
5048 filename, linenumber, funcname);
5053 Perl_mem_log_new_sv(const SV *sv,
5054 const char *filename, const int linenumber,
5055 const char *funcname)
5057 PERL_ARGS_ASSERT_MEM_LOG_NEW_SV;
5059 mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL,
5060 filename, linenumber, funcname);
5064 Perl_mem_log_del_sv(const SV *sv,
5065 const char *filename, const int linenumber,
5066 const char *funcname)
5068 PERL_ARGS_ASSERT_MEM_LOG_DEL_SV;
5070 mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL,
5071 filename, linenumber, funcname);
5074 #endif /* PERL_MEM_LOG */
5077 =for apidoc_section $string
5078 =for apidoc quadmath_format_valid
5080 C<quadmath_snprintf()> is very strict about its C<format> string and will
5081 fail, returning -1, if the format is invalid. It accepts exactly
5084 C<quadmath_format_valid()> checks that the intended single spec looks
5085 sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>,
5086 and has C<Q> before it. This is not a full "printf syntax check",
5089 Returns true if it is valid, false if not.
5091 See also L</quadmath_format_needed>.
5097 Perl_quadmath_format_valid(const char* format)
5101 PERL_ARGS_ASSERT_QUADMATH_FORMAT_VALID;
5103 if (format[0] != '%' || strchr(format + 1, '%'))
5105 len = strlen(format);
5106 /* minimum length three: %Qg */
5107 if (len < 3 || memCHRs("efgaEFGA", format[len - 1]) == NULL)
5109 if (format[len - 2] != 'Q')
5116 =for apidoc quadmath_format_needed
5118 C<quadmath_format_needed()> returns true if the C<format> string seems to
5119 contain at least one non-Q-prefixed C<%[efgaEFGA]> format specifier,
5120 or returns false otherwise.
5122 The format specifier detection is not complete printf-syntax detection,
5123 but it should catch most common cases.
5125 If true is returned, those arguments B<should> in theory be processed
5126 with C<quadmath_snprintf()>, but in case there is more than one such
5127 format specifier (see L</quadmath_format_valid>), and if there is
5128 anything else beyond that one (even just a single byte), they
5129 B<cannot> be processed because C<quadmath_snprintf()> is very strict,
5130 accepting only one format spec, and nothing else.
5131 In this case, the code should probably fail.
5137 Perl_quadmath_format_needed(const char* format)
5139 const char *p = format;
5142 PERL_ARGS_ASSERT_QUADMATH_FORMAT_NEEDED;
5144 while ((q = strchr(p, '%'))) {
5146 if (*q == '+') /* plus */
5148 if (*q == '#') /* alt */
5150 if (*q == '*') /* width */
5154 while (isDIGIT(*q)) q++;
5157 if (*q == '.' && (q[1] == '*' || isDIGIT(q[1]))) { /* prec */
5162 while (isDIGIT(*q)) q++;
5164 if (memCHRs("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
5173 =for apidoc my_snprintf
5175 The C library C<snprintf> functionality, if available and
5176 standards-compliant (uses C<vsnprintf>, actually). However, if the
5177 C<vsnprintf> is not available, will unfortunately use the unsafe
5178 C<vsprintf> which can overrun the buffer (there is an overrun check,
5179 but that may be too late). Consider using C<sv_vcatpvf> instead, or
5180 getting C<vsnprintf>.
5186 Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...)
5192 PERL_ARGS_ASSERT_MY_SNPRINTF;
5193 #ifndef HAS_VSNPRINTF
5194 PERL_UNUSED_VAR(len);
5196 va_start(ap, format);
5199 bool quadmath_valid = FALSE;
5201 if (quadmath_format_valid(format)) {
5202 /* If the format looked promising, use it as quadmath. */
5203 WITH_LC_NUMERIC_SET_TO_NEEDED(
5204 retval = quadmath_snprintf(buffer, len, format, va_arg(ap, NV));
5207 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5209 quadmath_valid = TRUE;
5211 /* quadmath_format_single() will return false for example for
5212 * "foo = %g", or simply "%g". We could handle the %g by
5213 * using quadmath for the NV args. More complex cases of
5214 * course exist: "foo = %g, bar = %g", or "foo=%Qg" (otherwise
5215 * quadmath-valid but has stuff in front).
5217 * Handling the "Q-less" cases right would require walking
5218 * through the va_list and rewriting the format, calling
5219 * quadmath for the NVs, building a new va_list, and then
5220 * letting vsnprintf/vsprintf to take care of the other
5221 * arguments. This may be doable.
5223 * We do not attempt that now. But for paranoia, we here try
5224 * to detect some common (but not all) cases where the
5225 * "Q-less" %[efgaEFGA] formats are present, and die if
5226 * detected. This doesn't fix the problem, but it stops the
5227 * vsnprintf/vsprintf pulling doubles off the va_list when
5228 * __float128 NVs should be pulled off instead.
5230 * If quadmath_format_needed() returns false, we are reasonably
5231 * certain that we can call vnsprintf() or vsprintf() safely. */
5232 if (!quadmath_valid && quadmath_format_needed(format))
5233 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5239 #ifdef HAS_VSNPRINTF
5240 WITH_LC_NUMERIC_SET_TO_NEEDED(
5241 retval = vsnprintf(buffer, len, format, ap);
5244 WITH_LC_NUMERIC_SET_TO_NEEDED(
5245 retval = vsprintf(buffer, format, ap);
5252 /* vsprintf() shows failure with < 0 */
5254 #ifdef HAS_VSNPRINTF
5255 /* vsnprintf() shows failure with >= len */
5257 (len > 0 && (Size_t)retval >= len)
5260 Perl_croak_nocontext("panic: my_snprintf buffer overflow");
5265 =for apidoc my_vsnprintf
5267 The C library C<vsnprintf> if available and standards-compliant.
5268 However, if the C<vsnprintf> is not available, will unfortunately
5269 use the unsafe C<vsprintf> which can overrun the buffer (there is an
5270 overrun check, but that may be too late). Consider using
5271 C<sv_vcatpvf> instead, or getting C<vsnprintf>.
5277 Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap)
5280 PERL_UNUSED_ARG(buffer);
5281 PERL_UNUSED_ARG(len);
5282 PERL_UNUSED_ARG(format);
5283 /* the cast is to avoid gcc -Wsizeof-array-argument complaining */
5284 PERL_UNUSED_ARG((void*)ap);
5285 Perl_croak_nocontext("panic: my_vsnprintf not available with quadmath");
5291 # ifdef NEED_VA_COPY
5294 PERL_ARGS_ASSERT_MY_VSNPRINTF;
5295 Perl_va_copy(ap, apc);
5296 # ifdef HAS_VSNPRINTF
5298 WITH_LC_NUMERIC_SET_TO_NEEDED(
5299 retval = vsnprintf(buffer, len, format, apc);
5302 PERL_UNUSED_ARG(len);
5303 WITH_LC_NUMERIC_SET_TO_NEEDED(
5304 retval = vsprintf(buffer, format, apc);
5310 # ifdef HAS_VSNPRINTF
5311 WITH_LC_NUMERIC_SET_TO_NEEDED(
5312 retval = vsnprintf(buffer, len, format, ap);
5315 PERL_UNUSED_ARG(len);
5316 WITH_LC_NUMERIC_SET_TO_NEEDED(
5317 retval = vsprintf(buffer, format, ap);
5320 # endif /* #ifdef NEED_VA_COPY */
5322 /* vsprintf() shows failure with < 0 */
5324 # ifdef HAS_VSNPRINTF
5325 /* vsnprintf() shows failure with >= len */
5327 (len > 0 && (Size_t)retval >= len)
5330 Perl_croak_nocontext("panic: my_vsnprintf buffer overflow");
5337 Perl_my_clearenv(pTHX)
5339 # if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
5341 # else /* ! (PERL_IMPLICIT_SYS || WIN32) */
5342 # if defined(USE_ENVIRON_ARRAY)
5343 # if defined(USE_ITHREADS)
5344 /* only the parent thread can clobber the process environment, so no need
5346 if (PL_curinterp != aTHX)
5348 # endif /* USE_ITHREADS */
5349 # if defined(HAS_CLEARENV)
5351 # elif defined(HAS_UNSETENV)
5352 int bsiz = 80; /* Most envvar names will be shorter than this. */
5353 char *buf = (char*)safesysmalloc(bsiz);
5354 while (*environ != NULL) {
5355 char *e = strchr(*environ, '=');
5356 int l = e ? e - *environ : (int)strlen(*environ);
5359 bsiz = l + 1; /* + 1 for the \0. */
5360 buf = (char*)safesysmalloc(bsiz);
5362 memcpy(buf, *environ, l);
5367 # else /* ! HAS_CLEARENV && ! HAS_UNSETENV */
5368 /* Just null environ and accept the leakage. */
5370 # endif /* HAS_CLEARENV || HAS_UNSETENV */
5371 # endif /* USE_ENVIRON_ARRAY */
5372 # endif /* PERL_IMPLICIT_SYS || WIN32 */
5378 =for apidoc my_cxt_init
5380 Implements the L<perlxs/C<MY_CXT_INIT>> macro, which you should use instead.
5382 The first time a module is loaded, the global C<PL_my_cxt_index> is incremented,
5383 and that value is assigned to that module's static C<my_cxt_index> (whose
5384 address is passed as an arg). Then, for each interpreter this function is
5385 called for, it makes sure a C<void*> slot is available to hang the static data
5386 off, by allocating or extending the interpreter's C<PL_my_cxt_list> array
5392 Perl_my_cxt_init(pTHX_ int *indexp, size_t size)
5397 PERL_ARGS_ASSERT_MY_CXT_INIT;
5400 /* do initial check without locking.
5401 * -1: not allocated or another thread currently allocating
5402 * other: already allocated by another thread
5405 MUTEX_LOCK(&PL_my_ctx_mutex);
5406 /*now a stricter check with locking */
5409 /* this module hasn't been allocated an index yet */
5410 *indexp = PL_my_cxt_index++;
5412 MUTEX_UNLOCK(&PL_my_ctx_mutex);
5415 /* make sure the array is big enough */
5416 if (PL_my_cxt_size <= index) {
5417 if (PL_my_cxt_size) {
5418 IV new_size = PL_my_cxt_size;
5419 while (new_size <= index)
5421 Renew(PL_my_cxt_list, new_size, void *);
5422 PL_my_cxt_size = new_size;
5425 PL_my_cxt_size = 16;
5426 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
5429 /* newSV() allocates one more than needed */
5430 p = (void*)SvPVX(newSV(size-1));
5431 PL_my_cxt_list[index] = p;
5432 Zero(p, size, char);
5436 #endif /* MULTIPLICITY */
5439 /* Perl_xs_handshake():
5440 implement the various XS_*_BOOTCHECK macros, which are added to .c
5441 files by ExtUtils::ParseXS, to check that the perl the module was built
5442 with is binary compatible with the running perl.
5445 Perl_xs_handshake(U32 key, void * v_my_perl, const char * file,
5446 [U32 items, U32 ax], [char * api_version], [char * xs_version])
5448 The meaning of the varargs is determined the U32 key arg (which is not
5449 a format string). The fields of key are assembled by using HS_KEY().
5451 Under PERL_IMPLICIT_CONTEX, the v_my_perl arg is of type
5452 "PerlInterpreter *" and represents the callers context; otherwise it is
5453 of type "CV *", and is the boot xsub's CV.
5455 v_my_perl will catch where a threaded future perl526.dll calling IO.dll
5456 for example, and IO.dll was linked with threaded perl524.dll, and both
5457 perl526.dll and perl524.dll are in %PATH and the Win32 DLL loader
5458 successfully can load IO.dll into the process but simultaneously it
5459 loaded an interpreter of a different version into the process, and XS
5460 code will naturally pass SV*s created by perl524.dll for perl526.dll to
5461 use through perl526.dll's my_perl->Istack_base.
5463 v_my_perl cannot be the first arg, since then 'key' will be out of
5464 place in a threaded vs non-threaded mixup; and analyzing the key
5465 number's bitfields won't reveal the problem, since it will be a valid
5466 key (unthreaded perl) on interp side, but croak will report the XS mod's
5467 key as gibberish (it is really a my_perl ptr) (threaded XS mod); or if
5468 it's a threaded perl and an unthreaded XS module, threaded perl will
5469 look at an uninit C stack or an uninit register to get 'key'
5470 (remember that it assumes that the 1st arg is the interp cxt).
5472 'file' is the source filename of the caller.
5476 Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
5482 const char *stage = "first";
5490 PERL_ARGS_ASSERT_XS_HANDSHAKE;
5491 va_start(args, file);
5493 got = INT2PTR(void*, (UV)(key & HSm_KEY_MATCH));
5494 need = (void *)(HS_KEY(FALSE, FALSE, "", "") & HSm_KEY_MATCH);
5495 if (UNLIKELY(got != need))
5497 /* try to catch where a 2nd threaded perl interp DLL is loaded into a process
5498 by a XS DLL compiled against the wrong interl DLL b/c of bad @INC, and the
5499 2nd threaded perl interp DLL never initialized its TLS/PERL_SYS_INIT3 so
5500 dTHX call from 2nd interp DLL can't return the my_perl that pp_entersub
5501 passed to the XS DLL */
5503 xs_interp = (tTHX)v_my_perl;
5507 /* try to catch where an unthreaded perl interp DLL (for ex. perl522.dll) is
5508 loaded into a process by a XS DLL built by an unthreaded perl522.dll perl,
5509 but the DynaLoder/Perl that started the process and loaded the XS DLL is
5510 unthreaded perl524.dll, since unthreadeds don't pass my_perl (a unique *)
5511 through pp_entersub, use a unique value (which is a pointer to PL_stack_sp's
5512 location in the unthreaded perl binary) stored in CV * to figure out if this
5513 Perl_xs_handshake was called by the same pp_entersub */
5514 cv = (CV*)v_my_perl;
5515 xs_spp = (SV***)CvHSCXT(cv);
5517 need = &PL_stack_sp;
5520 if(UNLIKELY(got != need)) {
5521 bad_handshake:/* recycle branch and string from above */
5522 if(got != (void *)HSf_NOCHK)
5523 noperl_die("%s: loadable library and perl binaries are mismatched"
5524 " (got %s handshake key %p, needed %p)\n",
5525 file, stage, got, need);
5528 if(key & HSf_SETXSUBFN) { /* this might be called from a module bootstrap */
5529 SAVEPPTR(PL_xsubfilename);/* which was require'd from a XSUB BEGIN */
5530 PL_xsubfilename = file; /* so the old name must be restored for
5531 additional XSUBs to register themselves */
5532 /* XSUBs can't be perl lang/perl5db.pl debugged
5533 if (PERLDB_LINE_OR_SAVESRC)
5534 (void)gv_fetchfile(file); */
5537 if(key & HSf_POPMARK) {
5539 { SV **mark = PL_stack_base + ax++;
5541 items = (I32)(SP - MARK);
5545 items = va_arg(args, U32);
5546 ax = va_arg(args, U32);
5550 assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX);
5551 if((apiverlen = HS_GETAPIVERLEN(key))) {
5552 char * api_p = va_arg(args, char*);
5553 if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1
5554 || memNE(api_p, "v" PERL_API_VERSION_STRING,
5555 sizeof("v" PERL_API_VERSION_STRING)-1))
5556 Perl_croak_nocontext("Perl API version %s of %" SVf " does not match %s",
5557 api_p, SVfARG(PL_stack_base[ax + 0]),
5558 "v" PERL_API_VERSION_STRING);
5562 U32 xsverlen = HS_GETXSVERLEN(key);
5563 assert(xsverlen <= UCHAR_MAX && xsverlen <= HS_APIVERLEN_MAX);
5565 S_xs_version_bootcheck(aTHX_
5566 items, ax, va_arg(args, char*), xsverlen);
5574 S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p,
5578 const char *vn = NULL;
5579 SV *const module = PL_stack_base[ax];
5581 PERL_ARGS_ASSERT_XS_VERSION_BOOTCHECK;
5583 if (items >= 2) /* version supplied as bootstrap arg */
5584 sv = PL_stack_base[ax + 1];
5586 /* XXX GV_ADDWARN */
5588 sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5589 if (!sv || !SvOK(sv)) {
5591 sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5595 SV *xssv = Perl_newSVpvn_flags(aTHX_ xs_p, xs_len, SVs_TEMP);
5596 SV *pmsv = sv_isobject(sv) && sv_derived_from(sv, "version")
5597 ? sv : sv_2mortal(new_version(sv));
5598 xssv = upg_version(xssv, 0);
5599 if ( vcmp(pmsv,xssv) ) {
5600 SV *string = vstringify(xssv);
5601 SV *xpt = Perl_newSVpvf(aTHX_ "%" SVf " object version %" SVf
5602 " does not match ", SVfARG(module), SVfARG(string));
5604 SvREFCNT_dec(string);
5605 string = vstringify(pmsv);
5608 Perl_sv_catpvf(aTHX_ xpt, "$%" SVf "::%s %" SVf, SVfARG(module), vn,
5611 Perl_sv_catpvf(aTHX_ xpt, "bootstrap parameter %" SVf, SVfARG(string));
5613 SvREFCNT_dec(string);
5615 Perl_sv_2mortal(aTHX_ xpt);
5616 Perl_croak_sv(aTHX_ xpt);
5621 PERL_STATIC_INLINE bool
5622 S_gv_has_usable_name(pTHX_ GV *gv)
5626 && HvHasENAME(GvSTASH(gv))
5627 && (gvp = (GV **)hv_fetchhek(
5628 GvSTASH(gv), GvNAME_HEK(gv), 0
5634 Perl_get_db_sub(pTHX_ SV **svp, CV *cv)
5636 SV * const dbsv = GvSVn(PL_DBsub);
5637 const bool save_taint = TAINT_get;
5639 /* When we are called from pp_goto (svp is null),
5640 * we do not care about using dbsv to call CV;
5641 * it's for informational purposes only.
5644 PERL_ARGS_ASSERT_GET_DB_SUB;
5648 if (!PERLDB_SUB_NN) {
5651 if (!svp && !CvLEXICAL(cv)) {
5652 gv_efullname3(dbsv, gv, NULL);
5654 else if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) || CvLEXICAL(cv)
5655 || strEQ(GvNAME(gv), "END")
5656 || ( /* Could be imported, and old sub redefined. */
5657 (GvCV(gv) != cv || !S_gv_has_usable_name(aTHX_ gv))
5659 !( (SvTYPE(*svp) == SVt_PVGV)
5660 && (GvCV((const GV *)*svp) == cv)
5661 /* Use GV from the stack as a fallback. */
5662 && S_gv_has_usable_name(aTHX_ gv = (GV *)*svp)
5666 /* GV is potentially non-unique, or contain different CV. */
5667 SV * const tmp = newRV(MUTABLE_SV(cv));
5668 sv_setsv(dbsv, tmp);
5672 sv_sethek(dbsv, HvENAME_HEK(GvSTASH(gv)));
5673 sv_catpvs(dbsv, "::");
5674 sv_cathek(dbsv, GvNAME_HEK(gv));
5678 const int type = SvTYPE(dbsv);
5679 if (type < SVt_PVIV && type != SVt_IV)
5680 sv_upgrade(dbsv, SVt_PVIV);
5681 (void)SvIOK_on(dbsv);
5682 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
5685 TAINT_IF(save_taint);
5686 #ifdef NO_TAINT_SUPPORT
5687 PERL_UNUSED_VAR(save_taint);
5692 =for apidoc_section $io
5693 =for apidoc my_dirfd
5695 The C library C<L<dirfd(3)>> if available, or a Perl implementation of it, or die
5696 if not easily emulatable.
5702 Perl_my_dirfd(DIR * dir) {
5704 /* Most dirfd implementations have problems when passed NULL. */
5709 #elif defined(HAS_DIR_DD_FD)
5712 Perl_croak_nocontext(PL_no_func, "dirfd");
5713 NOT_REACHED; /* NOTREACHED */
5718 #if !defined(HAS_MKOSTEMP) || !defined(HAS_MKSTEMP)
5720 #define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
5721 #define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
5724 S_my_mkostemp(char *templte, int flags) {
5726 STRLEN len = strlen(templte);
5730 int delete_on_close = flags & O_VMS_DELETEONCLOSE;
5732 flags &= ~O_VMS_DELETEONCLOSE;
5736 templte[len-1] != 'X' || templte[len-2] != 'X' || templte[len-3] != 'X' ||
5737 templte[len-4] != 'X' || templte[len-5] != 'X' || templte[len-6] != 'X') {
5738 SETERRNO(EINVAL, LIB_INVARG);
5744 for (i = 1; i <= 6; ++i) {
5745 templte[len-i] = TEMP_FILE_CH[(int)(Perl_internal_drand48() * TEMP_FILE_CH_COUNT)];
5748 if (delete_on_close) {
5749 fd = open(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600, "fop=dlt");
5754 fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
5756 } while (fd == -1 && errno == EEXIST && ++attempts <= 100);
5763 #ifndef HAS_MKOSTEMP
5766 =for apidoc my_mkostemp
5768 The C library C<L<mkostemp(3)>> if available, or a Perl implementation of it.
5774 Perl_my_mkostemp(char *templte, int flags)
5776 PERL_ARGS_ASSERT_MY_MKOSTEMP;
5777 return S_my_mkostemp(templte, flags);
5784 =for apidoc my_mkstemp
5786 The C library C<L<mkstemp(3)>> if available, or a Perl implementation of it.
5792 Perl_my_mkstemp(char *templte)
5794 PERL_ARGS_ASSERT_MY_MKSTEMP;
5795 return S_my_mkostemp(templte, 0);
5800 Perl_get_re_arg(pTHX_ SV *sv) {
5806 sv = MUTABLE_SV(SvRV(sv));
5807 if (SvTYPE(sv) == SVt_REGEXP)
5808 return (REGEXP*) sv;
5815 * This code is derived from drand48() implementation from FreeBSD,
5816 * found in lib/libc/gen/_rand48.c.
5818 * The U64 implementation is original, based on the POSIX
5819 * specification for drand48().
5823 * Copyright (c) 1993 Martin Birgmeier
5824 * All rights reserved.
5826 * You may redistribute unmodified or modified versions of this source
5827 * code provided that the above copyright notice and this and the
5828 * following conditions are retained.
5830 * This software is provided ``as is'', and comes with no warranties
5831 * of any kind. I shall in no event be liable for anything that happens
5832 * to anyone/anything when using this software.
5835 #define FREEBSD_DRAND48_SEED_0 (0x330e)
5837 #ifdef PERL_DRAND48_QUAD
5839 #define DRAND48_MULT UINT64_C(0x5deece66d)
5840 #define DRAND48_ADD 0xb
5841 #define DRAND48_MASK UINT64_C(0xffffffffffff)
5845 #define FREEBSD_DRAND48_SEED_1 (0xabcd)
5846 #define FREEBSD_DRAND48_SEED_2 (0x1234)
5847 #define FREEBSD_DRAND48_MULT_0 (0xe66d)
5848 #define FREEBSD_DRAND48_MULT_1 (0xdeec)
5849 #define FREEBSD_DRAND48_MULT_2 (0x0005)
5850 #define FREEBSD_DRAND48_ADD (0x000b)
5852 const unsigned short _rand48_mult[3] = {
5853 FREEBSD_DRAND48_MULT_0,
5854 FREEBSD_DRAND48_MULT_1,
5855 FREEBSD_DRAND48_MULT_2
5857 const unsigned short _rand48_add = FREEBSD_DRAND48_ADD;
5862 Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed)
5864 PERL_ARGS_ASSERT_DRAND48_INIT_R;
5866 #ifdef PERL_DRAND48_QUAD
5867 *random_state = FREEBSD_DRAND48_SEED_0 + ((U64)seed << 16);
5869 random_state->seed[0] = FREEBSD_DRAND48_SEED_0;
5870 random_state->seed[1] = (U16) seed;
5871 random_state->seed[2] = (U16) (seed >> 16);
5876 Perl_drand48_r(perl_drand48_t *random_state)
5878 PERL_ARGS_ASSERT_DRAND48_R;
5880 #ifdef PERL_DRAND48_QUAD
5881 *random_state = (*random_state * DRAND48_MULT + DRAND48_ADD)
5884 return ldexp((double)*random_state, -48);
5890 accu = (U32) _rand48_mult[0] * (U32) random_state->seed[0]
5891 + (U32) _rand48_add;
5892 temp[0] = (U16) accu; /* lower 16 bits */
5893 accu >>= sizeof(U16) * 8;
5894 accu += (U32) _rand48_mult[0] * (U32) random_state->seed[1]
5895 + (U32) _rand48_mult[1] * (U32) random_state->seed[0];
5896 temp[1] = (U16) accu; /* middle 16 bits */
5897 accu >>= sizeof(U16) * 8;
5898 accu += _rand48_mult[0] * random_state->seed[2]
5899 + _rand48_mult[1] * random_state->seed[1]
5900 + _rand48_mult[2] * random_state->seed[0];
5901 random_state->seed[0] = temp[0];
5902 random_state->seed[1] = temp[1];
5903 random_state->seed[2] = (U16) accu;
5905 return ldexp((double) random_state->seed[0], -48) +
5906 ldexp((double) random_state->seed[1], -32) +
5907 ldexp((double) random_state->seed[2], -16);
5912 #ifdef USE_C_BACKTRACE
5914 /* Possibly move all this USE_C_BACKTRACE code into a new file. */
5919 /* abfd is the BFD handle. */
5921 /* bfd_syms is the BFD symbol table. */
5923 /* bfd_text is handle to the ".text" section of the object file. */
5925 /* Since opening the executable and scanning its symbols is quite
5926 * heavy operation, we remember the filename we used the last time,
5927 * and do the opening and scanning only if the filename changes.
5928 * This removes most (but not all) open+scan cycles. */
5929 const char* fname_prev;
5932 /* Given a dl_info, update the BFD context if necessary. */
5933 static void bfd_update(bfd_context* ctx, Dl_info* dl_info)
5935 /* BFD open and scan only if the filename changed. */
5936 if (ctx->fname_prev == NULL ||
5937 strNE(dl_info->dli_fname, ctx->fname_prev)) {
5939 bfd_close(ctx->abfd);
5941 ctx->abfd = bfd_openr(dl_info->dli_fname, 0);
5943 if (bfd_check_format(ctx->abfd, bfd_object)) {
5944 IV symbol_size = bfd_get_symtab_upper_bound(ctx->abfd);
5945 if (symbol_size > 0) {
5946 Safefree(ctx->bfd_syms);
5947 Newx(ctx->bfd_syms, symbol_size, asymbol*);
5949 bfd_get_section_by_name(ctx->abfd, ".text");
5957 ctx->fname_prev = dl_info->dli_fname;
5961 /* Given a raw frame, try to symbolize it and store
5962 * symbol information (source file, line number) away. */
5963 static void bfd_symbolize(bfd_context* ctx,
5966 STRLEN* symbol_name_size,
5968 STRLEN* source_name_size,
5969 STRLEN* source_line)
5971 *symbol_name = NULL;
5972 *symbol_name_size = 0;
5974 IV offset = PTR2IV(raw_frame) - PTR2IV(ctx->bfd_text->vma);
5976 bfd_canonicalize_symtab(ctx->abfd, ctx->bfd_syms) > 0) {
5979 unsigned int line = 0;
5980 if (bfd_find_nearest_line(ctx->abfd, ctx->bfd_text,
5981 ctx->bfd_syms, offset,
5982 &file, &func, &line) &&
5983 file && func && line > 0) {
5984 /* Size and copy the source file, use only
5985 * the basename of the source file.
5987 * NOTE: the basenames are fine for the
5988 * Perl source files, but may not always
5989 * be the best idea for XS files. */
5990 const char *p, *b = NULL;
5991 /* Look for the last slash. */
5992 for (p = file; *p; p++) {
5996 if (b == NULL || *b == 0) {
5999 *source_name_size = p - b + 1;
6000 Newx(*source_name, *source_name_size + 1, char);
6001 Copy(b, *source_name, *source_name_size + 1, char);
6003 *symbol_name_size = strlen(func);
6004 Newx(*symbol_name, *symbol_name_size + 1, char);
6005 Copy(func, *symbol_name, *symbol_name_size + 1, char);
6007 *source_line = line;
6013 #endif /* #ifdef USE_BFD */
6017 /* OS X has no public API for for 'symbolicating' (Apple official term)
6018 * stack addresses to {function_name, source_file, line_number}.
6019 * Good news: there is command line utility atos(1) which does that.
6020 * Bad news 1: it's a command line utility.
6021 * Bad news 2: one needs to have the Developer Tools installed.
6022 * Bad news 3: in newer releases it needs to be run as 'xcrun atos'.
6024 * To recap: we need to open a pipe for reading for a utility which
6025 * might not exist, or exists in different locations, and then parse
6026 * the output. And since this is all for a low-level API, we cannot
6027 * use high-level stuff. Thanks, Apple. */
6030 /* tool is set to the absolute pathname of the tool to use:
6033 /* format is set to a printf format string used for building
6034 * the external command to run. */
6036 /* unavail is set if e.g. xcrun cannot be found, or something
6037 * else happens that makes getting the backtrace dubious. Note,
6038 * however, that the context isn't persistent, the next call to
6039 * get_c_backtrace() will start from scratch. */
6041 /* fname is the current object file name. */
6043 /* object_base_addr is the base address of the shared object. */
6044 void* object_base_addr;
6047 /* Given |dl_info|, updates the context. If the context has been
6048 * marked unavailable, return immediately. If not but the tool has
6049 * not been set, set it to either "xcrun atos" or "atos" (also set the
6050 * format to use for creating commands for piping), or if neither is
6051 * unavailable (one needs the Developer Tools installed), mark the context
6052 * an unavailable. Finally, update the filename (object name),
6053 * and its base address. */
6055 static void atos_update(atos_context* ctx,
6060 if (ctx->tool == NULL) {
6061 const char* tools[] = {
6065 const char* formats[] = {
6066 "/usr/bin/xcrun atos -o '%s' -l %08x %08x 2>&1",
6067 "/usr/bin/atos -d -o '%s' -l %08x %08x 2>&1"
6071 for (i = 0; i < C_ARRAY_LENGTH(tools); i++) {
6072 if (stat(tools[i], &st) == 0 && S_ISREG(st.st_mode)) {
6073 ctx->tool = tools[i];
6074 ctx->format = formats[i];
6078 if (ctx->tool == NULL) {
6079 ctx->unavail = TRUE;
6083 if (ctx->fname == NULL ||
6084 strNE(dl_info->dli_fname, ctx->fname)) {
6085 ctx->fname = dl_info->dli_fname;
6086 ctx->object_base_addr = dl_info->dli_fbase;
6090 /* Given an output buffer end |p| and its |start|, matches
6091 * for the atos output, extracting the source code location
6092 * and returning non-NULL if possible, returning NULL otherwise. */
6093 static const char* atos_parse(const char* p,
6095 STRLEN* source_name_size,
6096 STRLEN* source_line) {
6097 /* atos() output is something like:
6098 * perl_parse (in miniperl) (perl.c:2314)\n\n".
6099 * We cannot use Perl regular expressions, because we need to
6100 * stay low-level. Therefore here we have a rolled-out version
6101 * of a state machine which matches _backwards_from_the_end_ and
6102 * if there's a success, returns the starts of the filename,
6103 * also setting the filename size and the source line number.
6104 * The matched regular expression is roughly "\(.*:\d+\)\s*$" */
6105 const char* source_number_start;
6106 const char* source_name_end;
6107 const char* source_line_end = start;
6108 const char* close_paren;
6111 /* Skip trailing whitespace. */
6112 while (p > start && isSPACE(*p)) p--;
6113 /* Now we should be at the close paren. */
6114 if (p == start || *p != ')')
6118 /* Now we should be in the line number. */
6119 if (p == start || !isDIGIT(*p))
6121 /* Skip over the digits. */
6122 while (p > start && isDIGIT(*p))
6124 /* Now we should be at the colon. */
6125 if (p == start || *p != ':')
6127 source_number_start = p + 1;
6128 source_name_end = p; /* Just beyond the end. */
6130 /* Look for the open paren. */
6131 while (p > start && *p != '(')
6136 *source_name_size = source_name_end - p;
6137 if (grok_atoUV(source_number_start, &uv, &source_line_end)
6138 && source_line_end == close_paren
6139 && uv <= PERL_INT_MAX
6141 *source_line = (STRLEN)uv;
6147 /* Given a raw frame, read a pipe from the symbolicator (that's the
6148 * technical term) atos, reads the result, and parses the source code
6149 * location. We must stay low-level, so we use snprintf(), pipe(),
6150 * and fread(), and then also parse the output ourselves. */
6151 static void atos_symbolize(atos_context* ctx,
6154 STRLEN* source_name_size,
6155 STRLEN* source_line)
6163 /* Simple security measure: if there's any funny business with
6164 * the object name (used as "-o '%s'" ), leave since at least
6165 * partially the user controls it. */
6166 for (p = ctx->fname; *p; p++) {
6167 if (*p == '\'' || isCNTRL(*p)) {
6168 ctx->unavail = TRUE;
6174 WITH_LC_NUMERIC_SET_TO_NEEDED(
6175 cnt = snprintf(cmd, sizeof(cmd), ctx->format,
6176 ctx->fname, ctx->object_base_addr, raw_frame);
6179 if (cnt < sizeof(cmd)) {
6180 /* Undo nostdio.h #defines that disable stdio.
6181 * This is somewhat naughty, but is used elsewhere
6182 * in the core, and affects only OS X. */
6187 FILE* fp = popen(cmd, "r");
6188 /* At the moment we open a new pipe for each stack frame.
6189 * This is naturally somewhat slow, but hopefully generating
6190 * stack traces is never going to in a performance critical path.
6192 * We could play tricks with atos by batching the stack
6193 * addresses to be resolved: atos can either take multiple
6194 * addresses from the command line, or read addresses from
6195 * a file (though the mess of creating temporary files would
6196 * probably negate much of any possible speedup).
6198 * Normally there are only two objects present in the backtrace:
6199 * perl itself, and the libdyld.dylib. (Note that the object
6200 * filenames contain the full pathname, so perl may not always
6201 * be in the same place.) Whenever the object in the
6202 * backtrace changes, the base address also changes.
6204 * The problem with batching the addresses, though, would be
6205 * matching the results with the addresses: the parsing of
6206 * the results is already painful enough with a single address. */
6209 UV cnt = fread(out, 1, sizeof(out), fp);
6210 if (cnt < sizeof(out)) {
6211 const char* p = atos_parse(out + cnt - 1, out,
6216 *source_name_size, char);
6217 Copy(p, *source_name,
6218 *source_name_size, char);
6226 #endif /* #ifdef PERL_DARWIN */
6229 =for apidoc_section $debugging
6230 =for apidoc get_c_backtrace
6232 Collects the backtrace (aka "stacktrace") into a single linear
6233 malloced buffer, which the caller B<must> C<Perl_free_c_backtrace()>.
6235 Scans the frames back by S<C<depth + skip>>, then drops the C<skip> innermost,
6236 returning at most C<depth> frames.
6242 Perl_get_c_backtrace(pTHX_ int depth, int skip)
6244 /* Note that here we must stay as low-level as possible: Newx(),
6245 * Copy(), Safefree(); since we may be called from anywhere,
6246 * so we should avoid higher level constructs like SVs or AVs.
6248 * Since we are using safesysmalloc() via Newx(), don't try
6249 * getting backtrace() there, unless you like deep recursion. */
6251 /* Currently only implemented with backtrace() and dladdr(),
6252 * for other platforms NULL is returned. */
6254 #if defined(HAS_BACKTRACE) && defined(HAS_DLADDR)
6255 /* backtrace() is available via <execinfo.h> in glibc and in most
6256 * modern BSDs; dladdr() is available via <dlfcn.h>. */
6258 /* We try fetching this many frames total, but then discard
6259 * the |skip| first ones. For the remaining ones we will try
6260 * retrieving more information with dladdr(). */
6261 int try_depth = skip + depth;
6263 /* The addresses (program counters) returned by backtrace(). */
6266 /* Retrieved with dladdr() from the addresses returned by backtrace(). */
6269 /* Sizes _including_ the terminating \0 of the object name
6270 * and symbol name strings. */
6271 STRLEN* object_name_sizes;
6272 STRLEN* symbol_name_sizes;
6275 /* The symbol names comes either from dli_sname,
6276 * or if using BFD, they can come from BFD. */
6277 char** symbol_names;
6280 /* The source code location information. Dug out with e.g. BFD. */
6281 char** source_names;
6282 STRLEN* source_name_sizes;
6283 STRLEN* source_lines;
6285 Perl_c_backtrace* bt = NULL; /* This is what will be returned. */
6286 int got_depth; /* How many frames were returned from backtrace(). */
6287 UV frame_count = 0; /* How many frames we return. */
6288 UV total_bytes = 0; /* The size of the whole returned backtrace. */
6291 bfd_context bfd_ctx;
6294 atos_context atos_ctx;
6297 /* Here are probably possibilities for optimizing. We could for
6298 * example have a struct that contains most of these and then
6299 * allocate |try_depth| of them, saving a bunch of malloc calls.
6300 * Note, however, that |frames| could not be part of that struct
6301 * because backtrace() will want an array of just them. Also be
6302 * careful about the name strings. */
6303 Newx(raw_frames, try_depth, void*);
6304 Newx(dl_infos, try_depth, Dl_info);
6305 Newx(object_name_sizes, try_depth, STRLEN);
6306 Newx(symbol_name_sizes, try_depth, STRLEN);
6307 Newx(source_names, try_depth, char*);
6308 Newx(source_name_sizes, try_depth, STRLEN);
6309 Newx(source_lines, try_depth, STRLEN);
6311 Newx(symbol_names, try_depth, char*);
6314 /* Get the raw frames. */
6315 got_depth = (int)backtrace(raw_frames, try_depth);
6317 /* We use dladdr() instead of backtrace_symbols() because we want
6318 * the full details instead of opaque strings. This is useful for
6319 * two reasons: () the details are needed for further symbolic
6320 * digging, for example in OS X (2) by having the details we fully
6321 * control the output, which in turn is useful when more platforms
6322 * are added: we can keep out output "portable". */
6324 /* We want a single linear allocation, which can then be freed
6325 * with a single swoop. We will do the usual trick of first
6326 * walking over the structure and seeing how much we need to
6327 * allocate, then allocating, and then walking over the structure
6328 * the second time and populating it. */
6330 /* First we must compute the total size of the buffer. */
6331 total_bytes = sizeof(Perl_c_backtrace_header);
6332 if (got_depth > skip) {
6335 bfd_init(); /* Is this safe to call multiple times? */
6336 Zero(&bfd_ctx, 1, bfd_context);
6339 Zero(&atos_ctx, 1, atos_context);
6341 for (i = skip; i < try_depth; i++) {
6342 Dl_info* dl_info = &dl_infos[i];
6344 object_name_sizes[i] = 0;
6345 source_names[i] = NULL;
6346 source_name_sizes[i] = 0;
6347 source_lines[i] = 0;
6349 /* Yes, zero from dladdr() is failure. */
6350 if (dladdr(raw_frames[i], dl_info)) {
6351 total_bytes += sizeof(Perl_c_backtrace_frame);
6353 object_name_sizes[i] =
6354 dl_info->dli_fname ? strlen(dl_info->dli_fname) : 0;
6355 symbol_name_sizes[i] =
6356 dl_info->dli_sname ? strlen(dl_info->dli_sname) : 0;
6358 bfd_update(&bfd_ctx, dl_info);
6359 bfd_symbolize(&bfd_ctx, raw_frames[i],
6361 &symbol_name_sizes[i],
6363 &source_name_sizes[i],
6367 atos_update(&atos_ctx, dl_info);
6368 atos_symbolize(&atos_ctx,
6371 &source_name_sizes[i],
6375 /* Plus ones for the terminating \0. */
6376 total_bytes += object_name_sizes[i] + 1;
6377 total_bytes += symbol_name_sizes[i] + 1;
6378 total_bytes += source_name_sizes[i] + 1;
6386 Safefree(bfd_ctx.bfd_syms);
6390 /* Now we can allocate and populate the result buffer. */
6391 Newxc(bt, total_bytes, char, Perl_c_backtrace);
6392 Zero(bt, total_bytes, char);
6393 bt->header.frame_count = frame_count;
6394 bt->header.total_bytes = total_bytes;
6395 if (frame_count > 0) {
6396 Perl_c_backtrace_frame* frame = bt->frame_info;
6397 char* name_base = (char *)(frame + frame_count);
6398 char* name_curr = name_base; /* Outputting the name strings here. */
6400 for (i = skip; i < skip + frame_count; i++) {
6401 Dl_info* dl_info = &dl_infos[i];
6403 frame->addr = raw_frames[i];
6404 frame->object_base_addr = dl_info->dli_fbase;
6405 frame->symbol_addr = dl_info->dli_saddr;
6407 /* Copies a string, including the \0, and advances the name_curr.
6408 * Also copies the start and the size to the frame. */
6409 #define PERL_C_BACKTRACE_STRCPY(frame, doffset, src, dsize, size) \
6411 Copy(src, name_curr, size, char); \
6412 frame->doffset = name_curr - (char*)bt; \
6413 frame->dsize = size; \
6414 name_curr += size; \
6417 PERL_C_BACKTRACE_STRCPY(frame, object_name_offset,
6419 object_name_size, object_name_sizes[i]);
6422 PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6424 symbol_name_size, symbol_name_sizes[i]);
6425 Safefree(symbol_names[i]);
6427 PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6429 symbol_name_size, symbol_name_sizes[i]);
6432 PERL_C_BACKTRACE_STRCPY(frame, source_name_offset,
6434 source_name_size, source_name_sizes[i]);
6435 Safefree(source_names[i]);
6437 #undef PERL_C_BACKTRACE_STRCPY
6439 frame->source_line_number = source_lines[i];
6443 assert(total_bytes ==
6444 (UV)(sizeof(Perl_c_backtrace_header) +
6445 frame_count * sizeof(Perl_c_backtrace_frame) +
6446 name_curr - name_base));
6449 Safefree(symbol_names);
6451 bfd_close(bfd_ctx.abfd);
6454 Safefree(source_lines);
6455 Safefree(source_name_sizes);
6456 Safefree(source_names);
6457 Safefree(symbol_name_sizes);
6458 Safefree(object_name_sizes);
6459 /* Assuming the strings returned by dladdr() are pointers
6460 * to read-only static memory (the object file), so that
6461 * they do not need freeing (and cannot be). */
6463 Safefree(raw_frames);
6466 PERL_UNUSED_ARG(depth);
6467 PERL_UNUSED_ARG(skip);
6473 =for apidoc free_c_backtrace
6475 Deallocates a backtrace received from get_c_backtrace.
6481 =for apidoc get_c_backtrace_dump
6483 Returns a SV containing a dump of C<depth> frames of the call stack, skipping
6484 the C<skip> innermost ones. C<depth> of 20 is usually enough.
6486 The appended output looks like:
6489 1 10e004812:0082 Perl_croak util.c:1716 /usr/bin/perl
6490 2 10df8d6d2:1d72 perl_parse perl.c:3975 /usr/bin/perl
6493 The fields are tab-separated. The first column is the depth (zero
6494 being the innermost non-skipped frame). In the hex:offset, the hex is
6495 where the program counter was in C<S_parse_body>, and the :offset (might
6496 be missing) tells how much inside the C<S_parse_body> the program counter was.
6498 The C<util.c:1716> is the source code file and line number.
6500 The F</usr/bin/perl> is obvious (hopefully).
6502 Unknowns are C<"-">. Unknowns can happen unfortunately quite easily:
6503 if the platform doesn't support retrieving the information;
6504 if the binary is missing the debug information;
6505 if the optimizer has transformed the code by for example inlining.
6511 Perl_get_c_backtrace_dump(pTHX_ int depth, int skip)
6513 Perl_c_backtrace* bt;
6515 bt = get_c_backtrace(depth, skip + 1 /* Hide ourselves. */);
6517 Perl_c_backtrace_frame* frame;
6518 SV* dsv = newSVpvs("");
6520 for (i = 0, frame = bt->frame_info;
6521 i < bt->header.frame_count; i++, frame++) {
6522 Perl_sv_catpvf(aTHX_ dsv, "%d", (int)i);
6523 Perl_sv_catpvf(aTHX_ dsv, "\t%p", frame->addr ? frame->addr : "-");
6524 /* Symbol (function) names might disappear without debug info.
6526 * The source code location might disappear in case of the
6527 * optimizer inlining or otherwise rearranging the code. */
6528 if (frame->symbol_addr) {
6529 Perl_sv_catpvf(aTHX_ dsv, ":%04x",
6531 ((char*)frame->addr - (char*)frame->symbol_addr));
6533 Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6534 frame->symbol_name_size &&
6535 frame->symbol_name_offset ?
6536 (char*)bt + frame->symbol_name_offset : "-");
6537 if (frame->source_name_size &&
6538 frame->source_name_offset &&
6539 frame->source_line_number) {
6540 Perl_sv_catpvf(aTHX_ dsv, "\t%s:%" UVuf,
6541 (char*)bt + frame->source_name_offset,
6542 (UV)frame->source_line_number);
6544 Perl_sv_catpvf(aTHX_ dsv, "\t-");
6546 Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6547 frame->object_name_size &&
6548 frame->object_name_offset ?
6549 (char*)bt + frame->object_name_offset : "-");
6550 /* The frame->object_base_addr is not output,
6551 * but it is used for symbolizing/symbolicating. */
6552 sv_catpvs(dsv, "\n");
6555 Perl_free_c_backtrace(bt);
6564 =for apidoc dump_c_backtrace
6566 Dumps the C backtrace to the given C<fp>.
6568 Returns true if a backtrace could be retrieved, false if not.
6574 Perl_dump_c_backtrace(pTHX_ PerlIO* fp, int depth, int skip)
6578 PERL_ARGS_ASSERT_DUMP_C_BACKTRACE;
6580 sv = Perl_get_c_backtrace_dump(aTHX_ depth, skip);
6583 PerlIO_printf(fp, "%s", SvPV_nolen(sv));
6589 #endif /* #ifdef USE_C_BACKTRACE */
6591 #if defined(USE_ITHREADS) && defined(I_PTHREAD)
6593 /* pthread_mutex_t and perl_mutex are typedef equivalent
6594 * so casting the pointers is fine. */
6596 int perl_tsa_mutex_lock(perl_mutex* mutex)
6598 return pthread_mutex_lock((pthread_mutex_t *) mutex);
6601 int perl_tsa_mutex_unlock(perl_mutex* mutex)
6603 return pthread_mutex_unlock((pthread_mutex_t *) mutex);
6606 int perl_tsa_mutex_destroy(perl_mutex* mutex)
6608 return pthread_mutex_destroy((pthread_mutex_t *) mutex);
6615 /* log a sub call or return */
6618 Perl_dtrace_probe_call(pTHX_ CV *cv, bool is_call)
6626 PERL_ARGS_ASSERT_DTRACE_PROBE_CALL;
6629 HEK *hek = CvNAME_HEK(cv);
6630 func = HEK_KEY(hek);
6636 start = (const COP *)CvSTART(cv);
6637 file = CopFILE(start);
6638 line = CopLINE(start);
6639 stash = CopSTASHPV(start);
6642 PERL_SUB_ENTRY(func, file, line, stash);
6645 PERL_SUB_RETURN(func, file, line, stash);
6650 /* log a require file loading/loaded */
6653 Perl_dtrace_probe_load(pTHX_ const char *name, bool is_loading)
6655 PERL_ARGS_ASSERT_DTRACE_PROBE_LOAD;
6658 PERL_LOADING_FILE(name);
6661 PERL_LOADED_FILE(name);
6666 /* log an op execution */
6669 Perl_dtrace_probe_op(pTHX_ const OP *op)
6671 PERL_ARGS_ASSERT_DTRACE_PROBE_OP;
6673 PERL_OP_ENTRY(OP_NAME(op));
6677 /* log a compile/run phase change */
6680 Perl_dtrace_probe_phase(pTHX_ enum perl_phase phase)
6682 const char *ph_old = PL_phase_names[PL_phase];
6683 const char *ph_new = PL_phase_names[phase];
6685 PERL_PHASE_CHANGE(ph_new, ph_old);
6691 * ex: set ts=8 sts=4 sw=4 et: