Improve NetBSD support: Allow VMA determination with fewer system calls.
[libsigsegv/ericb.git] / src / stackvma-netbsd.c
blob4d74eae20cfa4b0e31d57752776c61c0a72070c6
1 /* Determine the virtual memory area of a given address. NetBSD version.
2 Copyright (C) 2002-2003, 2006, 2008, 2011 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 There are two such files:
45 - /proc/curproc/map in FreeBSD syntax,
46 - /proc/curproc/maps in Linux syntax.
47 Cf. <http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/miscfs/procfs/procfs_map.c?rev=HEAD> */
48 if (rof_open (&rof, "/proc/curproc/map") < 0)
49 goto failed;
51 #if STACK_DIRECTION < 0
52 prev_end = 0;
53 #endif
54 for (curr_start = curr_end = 0; ;)
56 if (!(rof_getchar (&rof) == '0'
57 && rof_getchar (&rof) == 'x'
58 && rof_scanf_lx (&rof, &next_start) >= 0))
59 break;
60 while (c = rof_peekchar (&rof), c == ' ' || c == '\t')
61 rof_getchar (&rof);
62 if (!(rof_getchar (&rof) == '0'
63 && rof_getchar (&rof) == 'x'
64 && rof_scanf_lx (&rof, &next_end) >= 0))
65 break;
66 while (c = rof_getchar (&rof), c != -1 && c != '\n')
67 continue;
68 if (next_start == curr_end)
70 /* Merge adjacent segments. */
71 curr_end = next_end;
73 else
75 if (curr_start < curr_end
76 && address >= curr_start && address <= curr_end-1)
77 goto found;
78 #if STACK_DIRECTION < 0
79 prev_end = curr_end;
80 #endif
81 curr_start = next_start; curr_end = next_end;
84 if (address >= curr_start && address <= curr_end-1)
85 found:
87 vma->start = curr_start;
88 vma->end = curr_end;
89 #if STACK_DIRECTION < 0
90 vma->prev_end = prev_end;
91 #else
92 if (rof_getchar (&rof) == '0'
93 && rof_getchar (&rof) == 'x'
94 && rof_scanf_lx (&rof, &vma->next_start) >= 0)
96 while (c = rof_peekchar (&rof), c == ' ' || c == '\t')
97 rof_getchar (&rof);
98 if (rof_getchar (&rof) == '0'
99 && rof_getchar (&rof) == 'x'
100 && rof_scanf_lx (&rof, &next_end) >= 0)
102 else
103 vma->next_start = 0;
105 else
106 vma->next_start = 0;
107 #endif
108 rof_close (&rof);
109 vma->is_near_this = simple_is_near_this;
110 return 0;
112 rof_close (&rof);
113 failed:
114 #if HAVE_MINCORE
115 return mincore_get_vma (address, vma);
116 #endif
117 return -1;