summaryrefslogtreecommitdiff
path: root/src/coredump/_UCD_create.c
blob: e5ccd356058d36599894b3897c92ea086bbcf765 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* libunwind - a platform-independent unwind library

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.  */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* Endian detection */
#include <limits.h>
#if defined(HAVE_BYTESWAP_H)
#include <byteswap.h>
#endif

#if defined(HAVE_ELF_H)
# include <elf.h>
#elif defined(HAVE_SYS_ELF_H)
# include <sys/elf.h>
#endif
#include <sys/procfs.h> /* struct elf_prstatus */

#include "_UCD_lib.h"
#include "_UCD_internal.h"


struct UCD_info *
_UCD_create(const char *filename)
{
  union
    {
      Elf32_Ehdr h32;
      Elf64_Ehdr h64;
    } elf_header;
#define elf_header32 elf_header.h32
#define elf_header64 elf_header.h64
  bool _64bits;

  struct UCD_info *ui = memset(malloc(sizeof(*ui)), 0, sizeof(*ui));
  ui->edi.di_cache.format = -1;
  ui->edi.di_debug.format = -1;
#if UNW_TARGET_IA64
  ui->edi.ktab.format = -1;
#endif

  int fd = ui->coredump_fd = open(filename, O_RDONLY);
  if (fd < 0)
    goto err;
  ui->coredump_filename = strdup(filename);

  /* No sane ELF32 file is going to be smaller then ELF64 _header_,
   * so let's just read 64-bit sized one.
   */
  if (read(fd, &elf_header64, sizeof(elf_header64)) != sizeof(elf_header64))
    {
      Debug(0, "'%s' is not an ELF file\n", filename);
      goto err;
    }

  if (memcmp(&elf_header32, ELFMAG, SELFMAG) != 0)
    {
      Debug(0, "'%s' is not an ELF file\n", filename);
      goto err;
    }

  if (elf_header32.e_ident[EI_CLASS] != ELFCLASS32
   && elf_header32.e_ident[EI_CLASS] != ELFCLASS64)
    {
      Debug(0, "'%s' is not a 32/64 bit ELF file\n", filename);
      goto err;
    }

  if (target_is_big_endian() && (elf_header32.e_ident[EI_DATA] == ELFDATA2LSB))
    {
      Debug(0, "'%s' is endian-incompatible\n", filename);
      goto err;
    }

  _64bits = (elf_header32.e_ident[EI_CLASS] == ELFCLASS64);
  if (_64bits && sizeof(elf_header64.e_entry) > sizeof(off_t))
    {
      Debug(0, "Can't process '%s': 64-bit file "
               "while only %ld bits are supported",
            filename, 8L * sizeof(off_t));
      goto err;
    }

  /* paranoia check */
  if (_64bits
            ? 0 /* todo: (elf_header64.e_ehsize != NN || elf_header64.e_phentsize != NN) */
            : (elf_header32.e_ehsize != 52 || elf_header32.e_phentsize != 32)
  )
    {
      Debug(0, "'%s' has wrong e_ehsize or e_phentsize\n", filename);
      goto err;
    }

  off_t ofs = (_64bits ? elf_header64.e_phoff : elf_header32.e_phoff);
  if (lseek(fd, ofs, SEEK_SET) != ofs)
    {
      Debug(0, "Can't read phdrs from '%s'\n", filename);
      goto err;
    }
  unsigned size = ui->phdrs_count = (_64bits ? elf_header64.e_phnum : elf_header32.e_phnum);
  coredump_phdr_t *phdrs = ui->phdrs = memset(malloc(size * sizeof(phdrs[0])), 0, size * sizeof(phdrs[0]));
  if (_64bits)
    {
      coredump_phdr_t *cur = phdrs;
      unsigned i = 0;
      while (i < size)
        {
          Elf64_Phdr hdr64;
          if (read(fd, &hdr64, sizeof(hdr64)) != sizeof(hdr64))
            {
              Debug(0, "Can't read phdrs from '%s'\n", filename);
              goto err;
            }
          cur->p_type   = hdr64.p_type  ;
          cur->p_flags  = hdr64.p_flags ;
          cur->p_offset = hdr64.p_offset;
          cur->p_vaddr  = hdr64.p_vaddr ;
          /*cur->p_paddr  = hdr32.p_paddr ; always 0 */
//TODO: check that and abort if it isn't?
          cur->p_filesz = hdr64.p_filesz;
          cur->p_memsz  = hdr64.p_memsz ;
          cur->p_align  = hdr64.p_align ;
          /* cur->backing_filename = NULL; - done by memset */
          cur->backing_fd = -1;
          cur->backing_filesize = hdr64.p_filesz;
          i++;
          cur++;
        }
    } else {
      coredump_phdr_t *cur = phdrs;
      unsigned i = 0;
      while (i < size)
        {
          Elf32_Phdr hdr32;
          if (read(fd, &hdr32, sizeof(hdr32)) != sizeof(hdr32))
            {
              Debug(0, "Can't read phdrs from '%s'\n", filename);
              goto err;
            }
          cur->p_type   = hdr32.p_type  ;
          cur->p_flags  = hdr32.p_flags ;
          cur->p_offset = hdr32.p_offset;
          cur->p_vaddr  = hdr32.p_vaddr ;
          /*cur->p_paddr  = hdr32.p_paddr ; always 0 */
          cur->p_filesz = hdr32.p_filesz;
          cur->p_memsz  = hdr32.p_memsz ;
          cur->p_align  = hdr32.p_align ;
          /* cur->backing_filename = NULL; - done by memset */
          cur->backing_fd = -1;
          cur->backing_filesize = hdr32.p_memsz;
          i++;
          cur++;
        }
    }

    int ret = _UCD_get_threadinfo(ui, phdrs, size);
    if (ret != UNW_ESUCCESS) {
		Debug(0, "failure retrieving thread info from core file\n");
		goto err;
	}

    ret = _UCD_get_mapinfo(ui, phdrs, size);
    if (ret != UNW_ESUCCESS) {
		Debug(0, "failure retrieving file mapping from core file\n");
		goto err;
	}

	coredump_phdr_t *cur = phdrs;
	for (unsigned i = 0; i < size; ++i)
	  {
		if (cur->p_type == PT_LOAD)
		  {
			Debug(2, " ofs:%08llx va:%08llx filesize:%08llx memsize:%08llx flg:%x",
								(unsigned long long) cur->p_offset,
								(unsigned long long) cur->p_vaddr,
								(unsigned long long) cur->p_filesz,
								(unsigned long long) cur->p_memsz,
								cur->p_flags
			);
			if (cur->p_filesz < cur->p_memsz)
			  {
				Debug(2, " partial");
			  }
			if (cur->p_flags & PF_X)
			  {
				Debug(2, " executable");
			  }
		  }
		Debug(2, "\n");
		cur++;
	  }

    if (ui->n_threads == 0)
      {
        Debug(0, "No NT_PRSTATUS note found in '%s'\n", filename);
        goto err;
      }

    ui->prstatus = &ui->threads[0].prstatus;
#ifdef HAVE_ELF_FPREGSET_T
    ui->fpregset = &ui->threads[0].fpregset;
#endif

  return ui;

 err:
  _UCD_destroy(ui);
  return NULL;
}

int _UCD_get_num_threads(struct UCD_info *ui)
{
  return ui->n_threads;
}

void _UCD_select_thread(struct UCD_info *ui, int n)
{
  if (n >= 0 && n < ui->n_threads) {
    ui->prstatus = &ui->threads[n].prstatus;
#ifdef HAVE_ELF_FPREGSET_T
    ui->fpregset = &ui->threads[n].fpregset;
#endif
  }
}

pid_t _UCD_get_pid(struct UCD_info *ui)
{
#if defined(HAVE_PROCFS_STATUS)
  return ui->prstatus->pid;
#else
  return ui->prstatus->pr_pid;
#endif
}

int _UCD_get_cursig(struct UCD_info *ui)
{
#if defined(HAVE_PROCFS_STATUS)
  return 0;
#else
  return ui->prstatus->pr_cursig;
#endif
}

int _UCD_add_backing_file_at_segment(struct UCD_info *ui, int phdr_no, const char *filename)
{
  if ((unsigned)phdr_no >= ui->phdrs_count)
    {
      Debug(0, "There is no segment %d in this coredump\n", phdr_no);
      return -1;
    }

  struct coredump_phdr *phdr = &ui->phdrs[phdr_no];
  if (phdr->backing_filename)
    {
      Debug(0, "Backing file already added to segment %d\n", phdr_no);
      return -1;
    }

  int fd = open(filename, O_RDONLY);
  if (fd < 0)
    {
      Debug(0, "Can't open '%s'\n", filename);
      return -1;
    }

  phdr->backing_fd = fd;
  phdr->backing_filename = strdup(filename);

  struct stat statbuf;
  if (fstat(fd, &statbuf) != 0)
    {
      Debug(0, "Can't stat '%s'\n", filename);
      goto err;
    }
  phdr->backing_filesize = (uoff_t)statbuf.st_size;

  if (phdr->p_flags != (PF_X | PF_R))
    {
      Debug(1, "Note: phdr[%u] is not r-x: flags are 0x%x\n",
                        phdr_no, phdr->p_flags);
    }

  if (phdr->backing_filesize > phdr->p_memsz)
    {
      /* This is expected */
      Debug(2, "Note: phdr[%u] is %lld bytes, file is larger: %lld bytes\n",
                        phdr_no,
                        (unsigned long long)phdr->p_memsz,
                        (unsigned long long)phdr->backing_filesize
      );
    }
//TODO: else loudly complain? Maybe even fail?

  /* Success */
  return 0;

 err:
  if (phdr->backing_fd >= 0)
    {
      close(phdr->backing_fd);
      phdr->backing_fd = -1;
    }
  free(phdr->backing_filename);
  phdr->backing_filename = NULL;
  return -1;
}

int _UCD_add_backing_file_at_vaddr(struct UCD_info *ui,
                                   unsigned long vaddr,
                                   const char *filename)
{
  unsigned i;
  for (i = 0; i < ui->phdrs_count; i++)
    {
      struct coredump_phdr *phdr = &ui->phdrs[i];
      if (phdr->p_vaddr != vaddr)
        continue;
      /* It seems to match. Add it. */
      return _UCD_add_backing_file_at_segment(ui, i, filename);
    }
  return -1;
}