annotate libbacktrace/read.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* read.c -- File views without mmap.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #include <errno.h>
kono
parents:
diff changeset
36 #include <stdlib.h>
kono
parents:
diff changeset
37 #include <sys/types.h>
kono
parents:
diff changeset
38 #include <unistd.h>
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 #include "backtrace.h"
kono
parents:
diff changeset
41 #include "internal.h"
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* This file implements file views when mmap is not available. */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. */
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 int
kono
parents:
diff changeset
48 backtrace_get_view (struct backtrace_state *state, int descriptor,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
49 off_t offset, uint64_t size,
111
kono
parents:
diff changeset
50 backtrace_error_callback error_callback,
kono
parents:
diff changeset
51 void *data, struct backtrace_view *view)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 ssize_t got;
kono
parents:
diff changeset
54
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
55 if ((uint64_t) (size_t) size != size)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
56 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
57 error_callback (data, "file size too large", 0);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
58 return 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
59 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
60
111
kono
parents:
diff changeset
61 if (lseek (descriptor, offset, SEEK_SET) < 0)
kono
parents:
diff changeset
62 {
kono
parents:
diff changeset
63 error_callback (data, "lseek", errno);
kono
parents:
diff changeset
64 return 0;
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 view->base = backtrace_alloc (state, size, error_callback, data);
kono
parents:
diff changeset
68 if (view->base == NULL)
kono
parents:
diff changeset
69 return 0;
kono
parents:
diff changeset
70 view->data = view->base;
kono
parents:
diff changeset
71 view->len = size;
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 got = read (descriptor, view->base, size);
kono
parents:
diff changeset
74 if (got < 0)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 error_callback (data, "read", errno);
kono
parents:
diff changeset
77 free (view->base);
kono
parents:
diff changeset
78 return 0;
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 if ((size_t) got < size)
kono
parents:
diff changeset
82 {
kono
parents:
diff changeset
83 error_callback (data, "file too short", 0);
kono
parents:
diff changeset
84 free (view->base);
kono
parents:
diff changeset
85 return 0;
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 return 1;
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 /* Release a view read by backtrace_get_view. */
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 void
kono
parents:
diff changeset
94 backtrace_release_view (struct backtrace_state *state,
kono
parents:
diff changeset
95 struct backtrace_view *view,
kono
parents:
diff changeset
96 backtrace_error_callback error_callback,
kono
parents:
diff changeset
97 void *data)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 backtrace_free (state, view->base, view->len, error_callback, data);
kono
parents:
diff changeset
100 view->data = NULL;
kono
parents:
diff changeset
101 view->base = NULL;
kono
parents:
diff changeset
102 }