ProcessElfCore.cpp revision 258054
1//===-- ProcessElfCore.cpp --------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// C Includes
11#include <stdlib.h>
12
13// Other libraries and framework includes
14#include "lldb/Core/PluginManager.h"
15#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleSpec.h"
17#include "lldb/Core/Section.h"
18#include "lldb/Core/State.h"
19#include "lldb/Core/DataBufferHeap.h"
20#include "lldb/Target/Target.h"
21#include "lldb/Target/DynamicLoader.h"
22#include "ProcessPOSIXLog.h"
23
24#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
25#include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
26
27// Project includes
28#include "ProcessElfCore.h"
29#include "ThreadElfCore.h"
30
31using namespace lldb_private;
32
33ConstString
34ProcessElfCore::GetPluginNameStatic()
35{
36    static ConstString g_name("elf-core");
37    return g_name;
38}
39
40const char *
41ProcessElfCore::GetPluginDescriptionStatic()
42{
43    return "ELF core dump plug-in.";
44}
45
46void
47ProcessElfCore::Terminate()
48{
49    PluginManager::UnregisterPlugin (ProcessElfCore::CreateInstance);
50}
51
52
53lldb::ProcessSP
54ProcessElfCore::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file)
55{
56    lldb::ProcessSP process_sp;
57    if (crash_file)
58        process_sp.reset(new ProcessElfCore (target, listener, *crash_file));
59    return process_sp;
60}
61
62bool
63ProcessElfCore::CanDebug(Target &target, bool plugin_specified_by_name)
64{
65    // For now we are just making sure the file exists for a given module
66    if (!m_core_module_sp && m_core_file.Exists())
67    {
68        ModuleSpec core_module_spec(m_core_file, target.GetArchitecture());
69        Error error (ModuleList::GetSharedModule (core_module_spec, m_core_module_sp,
70                                                  NULL, NULL, NULL));
71        if (m_core_module_sp)
72        {
73            ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
74            if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile)
75                return true;
76        }
77    }
78    return false;
79}
80
81//----------------------------------------------------------------------
82// ProcessElfCore constructor
83//----------------------------------------------------------------------
84ProcessElfCore::ProcessElfCore(Target& target, Listener &listener,
85                               const FileSpec &core_file) :
86    Process (target, listener),
87    m_core_module_sp (),
88    m_core_file (core_file),
89    m_dyld_plugin_name (),
90    m_thread_data_valid(false),
91    m_thread_data(),
92    m_core_aranges ()
93{
94}
95
96//----------------------------------------------------------------------
97// Destructor
98//----------------------------------------------------------------------
99ProcessElfCore::~ProcessElfCore()
100{
101    Clear();
102    // We need to call finalize on the process before destroying ourselves
103    // to make sure all of the broadcaster cleanup goes as planned. If we
104    // destruct this class, then Process::~Process() might have problems
105    // trying to fully destroy the broadcaster.
106    Finalize();
107}
108
109//----------------------------------------------------------------------
110// PluginInterface
111//----------------------------------------------------------------------
112ConstString
113ProcessElfCore::GetPluginName()
114{
115    return GetPluginNameStatic();
116}
117
118uint32_t
119ProcessElfCore::GetPluginVersion()
120{
121    return 1;
122}
123
124lldb::addr_t
125ProcessElfCore::AddAddressRangeFromLoadSegment(const elf::ELFProgramHeader *header)
126{
127    lldb::addr_t addr = header->p_vaddr;
128    FileRange file_range (header->p_offset, header->p_filesz);
129    VMRangeToFileOffset::Entry range_entry(addr, header->p_memsz, file_range);
130
131    VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back();
132    if (last_entry &&
133        last_entry->GetRangeEnd() == range_entry.GetRangeBase() &&
134        last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase())
135    {
136        last_entry->SetRangeEnd (range_entry.GetRangeEnd());
137        last_entry->data.SetRangeEnd (range_entry.data.GetRangeEnd());
138    }
139    else
140    {
141        m_core_aranges.Append(range_entry);
142    }
143
144    return addr;
145}
146
147//----------------------------------------------------------------------
148// Process Control
149//----------------------------------------------------------------------
150Error
151ProcessElfCore::DoLoadCore ()
152{
153    Error error;
154    if (!m_core_module_sp)
155    {
156        error.SetErrorString ("invalid core module");
157        return error;
158    }
159
160    ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
161    if (core == NULL)
162    {
163        error.SetErrorString ("invalid core object file");
164        return error;
165    }
166
167    const uint32_t num_segments = core->GetProgramHeaderCount();
168    if (num_segments == 0)
169    {
170        error.SetErrorString ("core file has no sections");
171        return error;
172    }
173
174    SetCanJIT(false);
175
176    m_thread_data_valid = true;
177
178    bool ranges_are_sorted = true;
179    lldb::addr_t vm_addr = 0;
180    /// Walk through segments and Thread and Address Map information.
181    /// PT_NOTE - Contains Thread and Register information
182    /// PT_LOAD - Contains a contiguous range of Process Address Space
183    for(uint32_t i = 1; i <= num_segments; i++)
184    {
185        const elf::ELFProgramHeader *header = core->GetProgramHeaderByIndex(i);
186        assert(header != NULL);
187
188        DataExtractor data = core->GetSegmentDataByIndex(i);
189
190        // Parse thread contexts and auxv structure
191        if (header->p_type == llvm::ELF::PT_NOTE)
192            ParseThreadContextsFromNoteSegment(header, data);
193
194        // PT_LOAD segments contains address map
195        if (header->p_type == llvm::ELF::PT_LOAD)
196        {
197            lldb::addr_t last_addr = AddAddressRangeFromLoadSegment(header);
198            if (vm_addr > last_addr)
199                ranges_are_sorted = false;
200            vm_addr = last_addr;
201        }
202    }
203
204    if (!ranges_are_sorted)
205        m_core_aranges.Sort();
206
207    // Even if the architecture is set in the target, we need to override
208    // it to match the core file which is always single arch.
209    ArchSpec arch (m_core_module_sp->GetArchitecture());
210    if (arch.IsValid())
211        m_target.SetArchitecture(arch);
212
213    return error;
214}
215
216lldb_private::DynamicLoader *
217ProcessElfCore::GetDynamicLoader ()
218{
219    if (m_dyld_ap.get() == NULL)
220        m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderPOSIXDYLD::GetPluginNameStatic().GetCString()));
221    return m_dyld_ap.get();
222}
223
224bool
225ProcessElfCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
226{
227    const uint32_t num_threads = GetNumThreadContexts ();
228    if (!m_thread_data_valid)
229        return false;
230
231    for (lldb::tid_t tid = 0; tid < num_threads; ++tid)
232    {
233        const ThreadData &td = m_thread_data[tid];
234        lldb::ThreadSP thread_sp(new ThreadElfCore (*this, tid, td));
235        new_thread_list.AddThread (thread_sp);
236    }
237    return new_thread_list.GetSize(false) > 0;
238}
239
240void
241ProcessElfCore::RefreshStateAfterStop ()
242{
243}
244
245Error
246ProcessElfCore::DoDestroy ()
247{
248    return Error();
249}
250
251//------------------------------------------------------------------
252// Process Queries
253//------------------------------------------------------------------
254
255bool
256ProcessElfCore::IsAlive ()
257{
258    return true;
259}
260
261//------------------------------------------------------------------
262// Process Memory
263//------------------------------------------------------------------
264size_t
265ProcessElfCore::ReadMemory (lldb::addr_t addr, void *buf, size_t size, Error &error)
266{
267    // Don't allow the caching that lldb_private::Process::ReadMemory does
268    // since in core files we have it all cached our our core file anyway.
269    return DoReadMemory (addr, buf, size, error);
270}
271
272size_t
273ProcessElfCore::DoReadMemory (lldb::addr_t addr, void *buf, size_t size, Error &error)
274{
275    ObjectFile *core_objfile = m_core_module_sp->GetObjectFile();
276
277    if (core_objfile == NULL)
278        return 0;
279
280    // Get the address range
281    const VMRangeToFileOffset::Entry *address_range = m_core_aranges.FindEntryThatContains (addr);
282    if (address_range == NULL || address_range->GetRangeEnd() < addr)
283    {
284        error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr);
285        return 0;
286    }
287
288    // Convert the address into core file offset
289    const lldb::addr_t offset = addr - address_range->GetRangeBase();
290    const lldb::addr_t file_start = address_range->data.GetRangeBase();
291    const lldb::addr_t file_end = address_range->data.GetRangeEnd();
292    size_t bytes_to_read = size; // Number of bytes to read from the core file
293    size_t bytes_copied = 0;     // Number of bytes actually read from the core file
294    size_t zero_fill_size = 0;   // Padding
295    lldb::addr_t bytes_left = 0; // Number of bytes available in the core file from the given address
296
297    if (file_end > offset)
298        bytes_left = file_end - offset;
299
300    if (bytes_to_read > bytes_left)
301    {
302        zero_fill_size = bytes_to_read - bytes_left;
303        bytes_to_read = bytes_left;
304    }
305
306    // If there is data available on the core file read it
307    if (bytes_to_read)
308        bytes_copied = core_objfile->CopyData(offset + file_start, bytes_to_read, buf);
309
310    assert(zero_fill_size <= size);
311    // Pad remaining bytes
312    if (zero_fill_size)
313        memset(((char *)buf) + bytes_copied, 0, zero_fill_size);
314
315    return bytes_copied + zero_fill_size;
316}
317
318void
319ProcessElfCore::Clear()
320{
321    m_thread_list.Clear();
322}
323
324void
325ProcessElfCore::Initialize()
326{
327    static bool g_initialized = false;
328
329    if (g_initialized == false)
330    {
331        g_initialized = true;
332        PluginManager::RegisterPlugin (GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance);
333    }
334}
335
336lldb::addr_t
337ProcessElfCore::GetImageInfoAddress()
338{
339    Target *target = &GetTarget();
340    ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
341    Address addr = obj_file->GetImageInfoAddress(target);
342
343    if (addr.IsValid())
344        return addr.GetLoadAddress(target);
345    return LLDB_INVALID_ADDRESS;
346}
347
348/// Core files PT_NOTE segment descriptor types
349enum {
350    NT_PRSTATUS     = 1,
351    NT_FPREGSET,
352    NT_PRPSINFO,
353    NT_TASKSTRUCT,
354    NT_PLATFORM,
355    NT_AUXV
356};
357
358enum {
359    NT_FREEBSD_PRSTATUS      = 1,
360    NT_FREEBSD_FPREGSET,
361    NT_FREEBSD_PRPSINFO,
362    NT_FREEBSD_THRMISC       = 7,
363    NT_FREEBSD_PROCSTAT_AUXV = 16
364};
365
366/// Note Structure found in ELF core dumps.
367/// This is PT_NOTE type program/segments in the core file.
368struct ELFNote
369{
370    elf::elf_word n_namesz;
371    elf::elf_word n_descsz;
372    elf::elf_word n_type;
373
374    std::string n_name;
375
376    ELFNote() : n_namesz(0), n_descsz(0), n_type(0)
377    {
378    }
379
380    /// Parse an ELFNote entry from the given DataExtractor starting at position
381    /// \p offset.
382    ///
383    /// @param[in] data
384    ///    The DataExtractor to read from.
385    ///
386    /// @param[in,out] offset
387    ///    Pointer to an offset in the data.  On return the offset will be
388    ///    advanced by the number of bytes read.
389    ///
390    /// @return
391    ///    True if the ELFRel entry was successfully read and false otherwise.
392    bool
393    Parse(const DataExtractor &data, lldb::offset_t *offset)
394    {
395        // Read all fields.
396        if (data.GetU32(offset, &n_namesz, 3) == NULL)
397            return false;
398
399        // The name field is required to be nul-terminated, and n_namesz
400        // includes the terminating nul in observed implementations (contrary
401        // to the ELF-64 spec).  A special case is needed for cores generated
402        // by some older Linux versions, which write a note named "CORE"
403        // without a nul terminator and n_namesz = 4.
404        if (n_namesz == 4)
405        {
406            char buf[4];
407            if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4)
408                return false;
409            if (strncmp (buf, "CORE", 4) == 0)
410            {
411                n_name = "CORE";
412                *offset += 4;
413                return true;
414            }
415        }
416
417        const char *cstr = data.GetCStr(offset, llvm::RoundUpToAlignment(n_namesz, 4));
418        if (cstr == NULL)
419        {
420            Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
421            if (log)
422                log->Printf("Failed to parse note name lacking nul terminator");
423
424            return false;
425        }
426        n_name = cstr;
427        return true;
428    }
429};
430
431// Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
432static void
433ParseFreeBSDPrStatus(ThreadData *thread_data, DataExtractor &data,
434                     ArchSpec &arch)
435{
436    lldb::offset_t offset = 0;
437    bool have_padding = (arch.GetMachine() == llvm::Triple::mips64 ||
438                         arch.GetMachine() == llvm::Triple::x86_64);
439    int pr_version = data.GetU32(&offset);
440
441    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
442    if (log)
443    {
444        if (pr_version > 1)
445            log->Printf("FreeBSD PRSTATUS unexpected version %d", pr_version);
446    }
447
448    if (have_padding)
449        offset += 4;
450    offset += 28;       // pr_statussz, pr_gregsetsz, pr_fpregsetsz, pr_osreldate
451    thread_data->signo = data.GetU32(&offset); // pr_cursig
452    offset += 4;        // pr_pid
453    if (have_padding)
454        offset += 4;
455
456    size_t len = data.GetByteSize() - offset;
457    thread_data->gpregset = DataExtractor(data, offset, len);
458}
459
460static void
461ParseFreeBSDThrMisc(ThreadData *thread_data, DataExtractor &data)
462{
463    lldb::offset_t offset = 0;
464    thread_data->name = data.GetCStr(&offset, 20);
465}
466
467/// Parse Thread context from PT_NOTE segment and store it in the thread list
468/// Notes:
469/// 1) A PT_NOTE segment is composed of one or more NOTE entries.
470/// 2) NOTE Entry contains a standard header followed by variable size data.
471///   (see ELFNote structure)
472/// 3) A Thread Context in a core file usually described by 3 NOTE entries.
473///    a) NT_PRSTATUS - Register context
474///    b) NT_PRPSINFO - Process info(pid..)
475///    c) NT_FPREGSET - Floating point registers
476/// 4) The NOTE entries can be in any order
477/// 5) If a core file contains multiple thread contexts then there is two data forms
478///    a) Each thread context(2 or more NOTE entries) contained in its own segment (PT_NOTE)
479///    b) All thread context is stored in a single segment(PT_NOTE).
480///        This case is little tricker since while parsing we have to find where the
481///        new thread starts. The current implementation marks beginning of
482///        new thread when it finds NT_PRSTATUS or NT_PRPSINFO NOTE entry.
483///    For case (b) there may be either one NT_PRPSINFO per thread, or a single
484///    one that applies to all threads (depending on the platform type).
485void
486ProcessElfCore::ParseThreadContextsFromNoteSegment(const elf::ELFProgramHeader *segment_header,
487                                                   DataExtractor segment_data)
488{
489    assert(segment_header && segment_header->p_type == llvm::ELF::PT_NOTE);
490
491    lldb::offset_t offset = 0;
492    ThreadData *thread_data = new ThreadData();
493    bool have_prstatus = false;
494    bool have_prpsinfo = false;
495
496    ArchSpec arch = GetArchitecture();
497    ELFLinuxPrPsInfo prpsinfo;
498    ELFLinuxPrStatus prstatus;
499    size_t header_size;
500    size_t len;
501
502    // Loop through the NOTE entires in the segment
503    while (offset < segment_header->p_filesz)
504    {
505        ELFNote note = ELFNote();
506        note.Parse(segment_data, &offset);
507
508        // Beginning of new thread
509        if ((note.n_type == NT_PRSTATUS && have_prstatus) ||
510            (note.n_type == NT_PRPSINFO && have_prpsinfo))
511        {
512            assert(thread_data->gpregset.GetByteSize() > 0);
513            // Add the new thread to thread list
514            m_thread_data.push_back(*thread_data);
515            thread_data = new ThreadData();
516            have_prstatus = false;
517            have_prpsinfo = false;
518        }
519
520        size_t note_start, note_size;
521        note_start = offset;
522        note_size = llvm::RoundUpToAlignment(note.n_descsz, 4);
523
524        // Store the NOTE information in the current thread
525        DataExtractor note_data (segment_data, note_start, note_size);
526        if (note.n_name == "FreeBSD")
527        {
528            switch (note.n_type)
529            {
530                case NT_FREEBSD_PRSTATUS:
531                    have_prstatus = true;
532                    ParseFreeBSDPrStatus(thread_data, note_data, arch);
533                    break;
534                case NT_FREEBSD_FPREGSET:
535                    thread_data->fpregset = note_data;
536                    break;
537                case NT_FREEBSD_PRPSINFO:
538                    have_prpsinfo = true;
539                    break;
540                case NT_FREEBSD_THRMISC:
541                    ParseFreeBSDThrMisc(thread_data, note_data);
542                    break;
543                case NT_FREEBSD_PROCSTAT_AUXV:
544                    // FIXME: FreeBSD sticks an int at the beginning of the note
545                    m_auxv = DataExtractor(segment_data, note_start + 4, note_size - 4);
546                    break;
547                default:
548                    break;
549            }
550        }
551        else
552        {
553            switch (note.n_type)
554            {
555                case NT_PRSTATUS:
556                    have_prstatus = true;
557                    prstatus.Parse(note_data, arch);
558                    thread_data->signo = prstatus.pr_cursig;
559                    header_size = ELFLinuxPrStatus::GetSize(arch);
560                    len = note_data.GetByteSize() - header_size;
561                    thread_data->gpregset = DataExtractor(note_data, header_size, len);
562                    break;
563                case NT_FPREGSET:
564                    thread_data->fpregset = note_data;
565                    break;
566                case NT_PRPSINFO:
567                    have_prpsinfo = true;
568                    prpsinfo.Parse(note_data, arch);
569                    thread_data->name = prpsinfo.pr_fname;
570                    break;
571                case NT_AUXV:
572                    m_auxv = DataExtractor(note_data);
573                    break;
574                default:
575                    break;
576            }
577        }
578
579        offset += note_size;
580    }
581    // Add last entry in the note section
582    if (thread_data && thread_data->gpregset.GetByteSize() > 0)
583    {
584        m_thread_data.push_back(*thread_data);
585    }
586}
587
588uint32_t
589ProcessElfCore::GetNumThreadContexts ()
590{
591    if (!m_thread_data_valid)
592        DoLoadCore();
593    return m_thread_data.size();
594}
595
596ArchSpec
597ProcessElfCore::GetArchitecture()
598{
599    ObjectFileELF *core_file = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
600    ArchSpec arch;
601    core_file->GetArchitecture(arch);
602    return arch;
603}
604
605const lldb::DataBufferSP
606ProcessElfCore::GetAuxvData()
607{
608    const uint8_t *start = m_auxv.GetDataStart();
609    size_t len = m_auxv.GetByteSize();
610    lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(start, len));
611    return buffer;
612}
613
614