Improve NetBSD support: Allow VMA determination with fewer system calls.
[libsigsegv/ericb.git] / src / stackvma-linux.c
blob338422872703f5f034d53dacea39579d1cd9925f
1 /* Determine the virtual memory area of a given address. Linux version.
2 Copyright (C) 2002, 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 unsigned long start, end;
37 #if STACK_DIRECTION < 0
38 unsigned long prev;
39 #endif
41 /* Open the current process' maps file. It describes one VMA per line. */
42 if (rof_open (&rof, "/proc/self/maps") < 0)
43 goto failed;
45 #if STACK_DIRECTION < 0
46 prev = 0;
47 #endif
48 for (;;)
50 if (!(rof_scanf_lx (&rof, &start) >= 0
51 && rof_getchar (&rof) == '-'
52 && rof_scanf_lx (&rof, &end) >= 0))
53 break;
54 while (c = rof_getchar (&rof), c != -1 && c != '\n')
55 continue;
56 if (address >= start && address <= end - 1)
58 vma->start = start;
59 vma->end = end;
60 #if STACK_DIRECTION < 0
61 vma->prev_end = prev;
62 #else
63 if (!(rof_scanf_lx (&rof, &vma->next_start) >= 0
64 && rof_getchar (&rof) == '-'
65 && rof_scanf_lx (&rof, &end) >= 0))
66 vma->next_start = 0;
67 #endif
68 rof_close (&rof);
69 vma->is_near_this = simple_is_near_this;
70 return 0;
72 #if STACK_DIRECTION < 0
73 prev = end;
74 #endif
76 rof_close (&rof);
77 failed:
78 #if HAVE_MINCORE
79 return mincore_get_vma (address, vma);
80 #else
81 return -1;
82 #endif