MCPcopy Create free account
hub / github.com/bloomberg/pystack / getDataFromNoteSection

Function getDataFromNoteSection

src/pystack/_pystack/elf_common.cpp:299–349  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

297}
298
299static std::vector<NoteData>
300getDataFromNoteSection(
301 Elf* elf,
302 Elf64_Word note_type,
303 Elf_Type note_data_type,
304 const GElf_Phdr* program_header,
305 Elf_Data* data)
306{
307 size_t note_offset = 0;
308 size_t name_offset = 0;
309 size_t desc_offset = 0;
310 GElf_Nhdr note_contents;
311 auto is_note_of_desired_type = [](const char* name, Elf64_Word type, GElf_Nhdr nhdr) -> bool {
312 return nhdr.n_type == type && (nhdr.n_namesz == 4 || (nhdr.n_namesz == 5 && name[4] == '\0'))
313 && !::memcmp(name, "CORE", 4);
314 };
315
316 std::vector<NoteData> result;
317
318 while (note_offset < data->d_size) {
319 note_offset = gelf_getnote(data, note_offset, &note_contents, &name_offset, &desc_offset);
320 if (note_offset <= 0) {
321 break;
322 }
323
324 const char* note_name = note_contents.n_namesz == 0 ? "" : (char*)(data->d_buf) + name_offset;
325 if (!is_note_of_desired_type(note_name, note_type, note_contents)) {
326 LOG(DEBUG) << "Skipping NOTE segment with name " << note_name << " and type "
327 << note_contents.n_type;
328 continue;
329 }
330
331 const GElf_Word descr_size = note_contents.n_descsz;
332 const GElf_Off descr_location = program_header->p_offset + desc_offset;
333 Elf_Data* note_data = elf_getdata_rawchunk(elf, descr_location, descr_size, note_data_type);
334 if (note_data == nullptr) {
335 LOG(WARNING) << "Invalid auxiliary NOTE data found in core file";
336 // There may be some other note that has valid data, so we need to continue.
337 continue;
338 }
339
340 LOG(DEBUG) << "Found NOTE of type " << note_type << " with name '" << note_name
341 << "' at position " << std::hex << std::showbase << descr_location;
342
343 result.emplace_back(NoteData{elf, note_data, descr_size, desc_offset, note_contents});
344 }
345 if (result.empty()) {
346 LOG(DEBUG) << "Failed to locate NOTE of type " << note_type << " in the core file";
347 }
348 return result;
349}
350
351std::vector<NoteData>
352getNoteData(Elf* elf, Elf64_Word note_type, Elf_Type note_data_type)

Callers 1

getNoteDataFunction · 0.85

Calls 1

LOGClass · 0.85

Tested by

no test coverage detected