summaryrefslogtreecommitdiff
path: root/src/os-linux.h
blob: 29aab5cf40ae47b7aeed0f3c6729c5828d348e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* libunwind - a platform-independent unwind library
   Copyright (C) 2003-2004 Hewlett-Packard Co
   Copyright (C) 2007 David Mosberger-Tang
        Contributed by David Mosberger-Tang <dmosberger@gmail.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#ifndef os_linux_h
#define os_linux_h

struct map_iterator
  {
    off_t offset;
    int fd;
    size_t buf_size;
    char *buf;
    char *buf_end;
    char *path;
  };

static inline char *
unw_ltoa (char *buf, long val)
{
  char *cp = buf, tmp;
  ssize_t i, len;

  do
    {
      *cp++ = '0' + (val % 10);
      val /= 10;
    }
  while (val);

  /* reverse the order of the digits: */
  len = cp - buf;
  --cp;
  for (i = 0; i < len / 2; ++i)
    {
      tmp = buf[i];
      buf[i] = cp[-i];
      cp[-i] = tmp;
    }
  return buf + len;
}

static inline int
maps_init (struct map_iterator *mi, pid_t pid)
{
  char path[sizeof ("/proc/0123456789/maps")], *cp;

  memcpy (path, "/proc/", 6);
  cp = unw_ltoa (path + 6, pid);
  assert (cp + 6 < path + sizeof (path));
  memcpy (cp, "/maps", 6);

  mi->fd = open (path, O_RDONLY);
  if (mi->fd >= 0)
    {
      /* Try to allocate a page-sized buffer.  */
      mi->buf_size = getpagesize ();
      cp = mmap (NULL, mi->buf_size, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
      if (cp == MAP_FAILED)
        {
          close(mi->fd);
          mi->fd = -1;
          return -1;
        }
      else
        {
          mi->offset = 0;
          mi->buf = mi->buf_end = cp + mi->buf_size;
          return 0;
        }
    }
  return -1;
}

static inline char *
skip_whitespace (char *cp)
{
  if (!cp)
    return NULL;

  while (*cp == ' ' || *cp == '\t')
    ++cp;
  return cp;
}

static inline char *
scan_hex (char *cp, unsigned long *valp)
{
  unsigned long num_digits = 0, digit, val = 0;

  cp = skip_whitespace (cp);
  if (!cp)
    return NULL;

  while (1)
    {
      digit = *cp;
      if ((digit - '0') <= 9)
        digit -= '0';
      else if ((digit - 'a') < 6)
        digit -= 'a' - 10;
      else if ((digit - 'A') < 6)
        digit -= 'A' - 10;
      else
        break;
      val = (val << 4) | digit;
      ++num_digits;
      ++cp;
    }
  if (!num_digits)
    return NULL;
  *valp = val;
  return cp;
}

static inline char *
scan_dec (char *cp, unsigned long *valp)
{
  unsigned long num_digits = 0, digit, val = 0;

  if (!(cp = skip_whitespace (cp)))
    return NULL;

  while (1)
    {
      digit = *cp;
      if ((digit - '0') <= 9)
        {
          digit -= '0';
          ++cp;
        }
      else
        break;
      val = (10 * val) + digit;
      ++num_digits;
    }
  if (!num_digits)
    return NULL;
  *valp = val;
  return cp;
}

static inline char *
scan_char (char *cp, char *valp)
{
  if (!cp)
    return NULL;

  *valp = *cp;

  /* don't step over NUL terminator */
  if (*cp)
    ++cp;
  return cp;
}

/* Scan a string delimited by white-space.  Fails on empty string or
   if string is doesn't fit in the specified buffer.  */
static inline char *
scan_string (char *cp, char *valp, size_t buf_size)
{
  size_t i = 0;

  if (!(cp = skip_whitespace (cp)))
    return NULL;

  while (*cp != ' ' && *cp != '\t' && *cp != '\0')
    {
      if ((valp != NULL) && (i < buf_size - 1))
        valp[i++] = *cp;
      ++cp;
    }
  if (i == 0 || i >= buf_size)
    return NULL;
  valp[i] = '\0';
  return cp;
}

static inline int
maps_next (struct map_iterator *mi,
           unsigned long *low, unsigned long *high, unsigned long *offset,
           unsigned long *flags)
{
  char perm[16], dash = 0, colon = 0, *cp;
  unsigned long major, minor, inum;
  ssize_t i, nread;

  if (mi->fd < 0)
    return 0;

  while (1)
    {
      ssize_t bytes_left = mi->buf_end - mi->buf;
      char *eol = NULL;

      for (i = 0; i < bytes_left; ++i)
        {
          if (mi->buf[i] == '\n')
            {
              eol = mi->buf + i;
              break;
            }
          else if (mi->buf[i] == '\0')
            break;
        }
      if (!eol)
        {
          /* copy down the remaining bytes, if any */
          if (bytes_left > 0)
            memmove (mi->buf_end - mi->buf_size, mi->buf, bytes_left);

          mi->buf = mi->buf_end - mi->buf_size;
          nread = read (mi->fd, mi->buf + bytes_left,
                        mi->buf_size - bytes_left);
          if (nread <= 0)
            return 0;
          else if ((size_t) (nread + bytes_left) < mi->buf_size)
            {
              /* Move contents to the end of the buffer so we
                 maintain the invariant that all bytes between
                 mi->buf and mi->buf_end are valid.  */
              memmove (mi->buf_end - nread - bytes_left, mi->buf,
                       nread + bytes_left);
              mi->buf = mi->buf_end - nread - bytes_left;
            }

          eol = mi->buf + bytes_left + nread - 1;

          for (i = bytes_left; i < bytes_left + nread; ++i)
            if (mi->buf[i] == '\n')
              {
                eol = mi->buf + i;
                break;
              }
        }
      cp = mi->buf;
      mi->buf = eol + 1;
      *eol = '\0';

      /* scan: "LOW-HIGH PERM OFFSET MAJOR:MINOR INUM PATH" */
      cp = scan_hex (cp, low);
      cp = scan_char (cp, &dash);
      cp = scan_hex (cp, high);
      cp = scan_string (cp, perm, sizeof (perm));
      cp = scan_hex (cp, offset);
      cp = scan_hex (cp, &major);
      cp = scan_char (cp, &colon);
      cp = scan_hex (cp, &minor);
      cp = scan_dec (cp, &inum);
      cp = mi->path = skip_whitespace (cp);
      if (!cp)
        continue;
      cp = scan_string (cp, NULL, 0);
      if (dash != '-' || colon != ':')
        continue;       /* skip line with unknown or bad format */
      if (flags)
        {
          *flags = 0;
          if (perm[0] == 'r')
            {
              *flags |= PROT_READ;
            }
          if (perm[1] == 'w')
            {
              *flags |= PROT_WRITE;
            }
          if (perm[2] == 'x')
            {
              *flags |= PROT_EXEC;
            }
        }
      return 1;
    }
  return 0;
}

static inline void
maps_close (struct map_iterator *mi)
{
  if (mi->fd < 0)
    return;
  close (mi->fd);
  mi->fd = -1;
  if (mi->buf)
    {
      munmap (mi->buf_end - mi->buf_size, mi->buf_size);
      mi->buf = mi->buf_end = NULL;
    }
}

#endif /* os_linux_h */