| 63 | }; |
| 64 | |
| 65 | KCore::KCore() |
| 66 | : m_offsets( 16 ) |
| 67 | { |
| 68 | m_fd = open( "/proc/kcore", O_RDONLY ); |
| 69 | if( m_fd == -1 ) return; |
| 70 | |
| 71 | elf_ehdr ehdr; |
| 72 | if( read( m_fd, &ehdr, sizeof( ehdr ) ) != sizeof( ehdr ) ) goto err; |
| 73 | |
| 74 | assert( ehdr.e_phentsize == sizeof( elf_phdr ) ); |
| 75 | |
| 76 | for( elf_half i=0; i<ehdr.e_phnum; i++ ) |
| 77 | { |
| 78 | elf_phdr phdr; |
| 79 | if( lseek( m_fd, ehdr.e_phoff + i * ehdr.e_phentsize, SEEK_SET ) == -1 ) goto err; |
| 80 | if( read( m_fd, &phdr, sizeof( phdr ) ) != sizeof( phdr ) ) goto err; |
| 81 | if( phdr.p_type != 1 ) continue; |
| 82 | |
| 83 | auto ptr = m_offsets.push_next(); |
| 84 | ptr->start = phdr.p_vaddr; |
| 85 | ptr->size = phdr.p_memsz; |
| 86 | ptr->offset = phdr.p_offset; |
| 87 | } |
| 88 | |
| 89 | std::sort( m_offsets.begin(), m_offsets.end(), []( const Offset& lhs, const Offset& rhs ) { return lhs.start < rhs.start; } ); |
| 90 | TracyDebug( "KCore: %zu segments found\n", m_offsets.size() ); |
| 91 | return; |
| 92 | |
| 93 | err: |
| 94 | close( m_fd ); |
| 95 | m_fd = -1; |
| 96 | } |
| 97 | |
| 98 | KCore::~KCore() |
| 99 | { |