• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Extract threadinfo from a coredump (targets with NT_PRSTATUS)
3  */
4 /*
5  This file is part of libunwind.
6 
7  Permission is hereby granted, free of charge, to any person obtaining a copy of
8  this software and associated documentation files (the "Software"), to deal in
9  the Software without restriction, including without limitation the rights to
10  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11  of the Software, and to permit persons to whom the Software is furnished to do
12  so, subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included in all
15  copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  SOFTWARE.
24 */
25 #include "_UCD_internal.h"
26 
27 #if defined(HAVE_ELF_H)
28 # include <elf.h>
29 #elif defined(HAVE_SYS_ELF_H)
30 # include <sys/elf.h>
31 #endif
32 
33 
34 /**
35  * Accumulate a count of the number of thread notes
36  *
37  * This _UCD_elf_visit_notes() callback just increments a count for each
38  * NT_PRSTATUS note seen.
39  */
40 static int
_count_thread_notes(uint32_t n_namesz,uint32_t n_descsz,uint32_t n_type,char * name,uint8_t * desc,void * arg)41 _count_thread_notes(uint32_t n_namesz, uint32_t n_descsz, uint32_t n_type, char *name, uint8_t *desc, void *arg)
42 {
43   size_t *thread_count = (size_t *)arg;
44   if (n_type == NT_PRSTATUS)
45   {
46     ++*thread_count;
47   }
48   return UNW_ESUCCESS;
49 }
50 
51 
52 /**
53  * Save a thread note to the unwind-coredump context
54  *
55  * This _UCD_elf_visit_notes() callback just copies the actual data structure
56  * from any NT_PRSTATUS note seen into an array of such structures and
57  * increments the count.
58  */
59 static int
_save_thread_notes(uint32_t n_namesz,uint32_t n_descsz,uint32_t n_type,char * name,uint8_t * desc,void * arg)60 _save_thread_notes(uint32_t n_namesz, uint32_t n_descsz, uint32_t n_type, char *name, uint8_t *desc, void *arg)
61 {
62   struct UCD_info *ui = (struct UCD_info *)arg;
63   if (n_type == NT_PRSTATUS)
64   {
65     memcpy(&ui->threads[ui->n_threads], desc, sizeof(struct PRSTATUS_STRUCT));
66     ++ui->n_threads;
67   }
68   return UNW_ESUCCESS;
69 }
70 
71 
72 /**
73  * Get thread info from core file
74  *
75  * On Linux threads are emulated by cloned processes sharing an address space
76  * and the process information is described by a note in the core file of type
77  * NT_PRSTATUS.  In fact, on Linux, the state of a thread is described by a
78  * CPU-dependent group of notes but right now we're only going to care about the
79  * one process-status note.  This statement is also true for FreeBSD.
80  *
81  * Depending on how the core file is created, there may be one PT_NOTE segment
82  * with multiple NT_PRSTATUS notes in it, or multiple PT_NOTE segments.  Just to
83  * be safe, it's better to assume there are multiple PT_NOTE segments each with
84  * multiple NT_PRSTATUS notes, as that covers all the cases.
85  */
86 int
_UCD_get_threadinfo(struct UCD_info * ui,coredump_phdr_t * phdrs,unsigned phdr_size)87 _UCD_get_threadinfo(struct UCD_info *ui, coredump_phdr_t *phdrs, unsigned phdr_size)
88 {
89   int ret = -UNW_ENOINFO;
90 
91   for (unsigned i = 0; i < phdr_size; ++i)
92   {
93     Debug(8, "phdr[%03d]: type:%d", i, phdrs[i].p_type);
94     if (phdrs[i].p_type == PT_NOTE)
95     {
96       size_t thread_count = 0;
97       uint8_t *segment;
98       size_t segment_size;
99       ret = _UCD_elf_read_segment(ui, &phdrs[i], &segment, &segment_size);
100       if (ret == UNW_ESUCCESS)
101       {
102       	_UCD_elf_visit_notes(segment, segment_size, _count_thread_notes, &thread_count);
103 	Debug(2, "found %zu threads\n", thread_count);
104 
105       	size_t new_size = sizeof(struct PRSTATUS_STRUCT) * (ui->n_threads + thread_count);
106       	ui->threads = realloc(ui->threads, new_size);
107       	if (ui->threads == NULL)
108 	{
109 	  Debug(0, "error allocating %zu bytes of memory \n", new_size);
110       	  free(segment);
111 	  return -UNW_EUNSPEC;
112 	}
113       	_UCD_elf_visit_notes(segment, segment_size, _save_thread_notes, ui);
114 
115       	free(segment);
116       }
117     }
118   }
119 
120   return ret;
121 }
122