Bump version number.
[libsigsegv/ericb.git] / src / stackvma-freebsd.c
blobfd04ba8527b445ed688a01d4abe7a44ba7ae298d
1 /* Determine the virtual memory area of a given address. FreeBSD version.
2 Copyright (C) 2002-2003, 2006, 2008 Bruno Haible <bruno@clisp.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #include "stackvma.h"
19 #include <stdio.h>
21 #include "stackvma-simple.c"
22 #include "stackvma-rofile.c"
24 #if HAVE_MINCORE
25 # define sigsegv_get_vma mincore_get_vma
26 # define STATIC static
27 # include "stackvma-mincore.c"
28 # undef sigsegv_get_vma
29 #endif
31 int
32 sigsegv_get_vma (unsigned long address, struct vma_struct *vma)
34 struct rofile rof;
35 int c;
36 /* The stack appears as multiple adjacents segments, therefore we
37 merge adjacent segments. */
38 unsigned long next_start, next_end, curr_start, curr_end;
39 #if STACK_DIRECTION < 0
40 unsigned long prev_end;
41 #endif
43 /* Open the current process' maps file. It describes one VMA per line. */
44 if (rof_open (&rof, "/proc/curproc/map") < 0)
45 goto failed;
47 #if STACK_DIRECTION < 0
48 prev_end = 0;
49 #endif
50 for (curr_start = curr_end = 0; ;)
52 if (!(rof_getchar (&rof) == '0'
53 && rof_getchar (&rof) == 'x'
54 && rof_scanf_lx (&rof, &next_start) >= 0))
55 break;
56 while (c = rof_peekchar (&rof), c == ' ' || c == '\t')
57 rof_getchar (&rof);
58 if (!(rof_getchar (&rof) == '0'
59 && rof_getchar (&rof) == 'x'
60 && rof_scanf_lx (&rof, &next_end) >= 0))
61 break;
62 while (c = rof_getchar (&rof), c != -1 && c != '\n')
63 continue;
64 if (next_start == curr_end)
66 /* Merge adjacent segments. */
67 curr_end = next_end;
69 else
71 if (curr_start < curr_end
72 && address >= curr_start && address <= curr_end-1)
73 goto found;
74 #if STACK_DIRECTION < 0
75 prev_end = curr_end;
76 #endif
77 curr_start = next_start; curr_end = next_end;
80 if (address >= curr_start && address <= curr_end-1)
81 found:
83 vma->start = curr_start;
84 vma->end = curr_end;
85 #if STACK_DIRECTION < 0
86 vma->prev_end = prev_end;
87 #else
88 if (rof_getchar (&rof) == '0'
89 && rof_getchar (&rof) == 'x'
90 && rof_scanf_lx (&rof, &vma->next_start) >= 0)
92 while (c = rof_peekchar (&rof), c == ' ' || c == '\t')
93 rof_getchar (&rof);
94 if (rof_getchar (&rof) == '0'
95 && rof_getchar (&rof) == 'x'
96 && rof_scanf_lx (&rof, &next_end) >= 0)
98 else
99 vma->next_start = 0;
101 else
102 vma->next_start = 0;
103 #endif
104 rof_close (&rof);
105 vma->is_near_this = simple_is_near_this;
106 return 0;
108 rof_close (&rof);
109 failed:
110 #if HAVE_MINCORE
111 /* FreeBSD 6.[01] doesn't allow to distinguish unmapped pages from
112 mapped but swapped-out pages. See whether it's fixed. */
113 if (!is_mapped (0))
114 /* OK, mincore() appears to work as expected. */
115 return mincore_get_vma (address, vma);
116 #endif
117 return -1;