summaryrefslogtreecommitdiffstats
path: root/src/core/arm/debug.cpp
blob: 1fe37b8ee1980b11258cd68604071e9e98622d77 (plain) (blame)
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
345
346
347
348
349
350
351
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/demangle.h"
#include "core/arm/debug.h"
#include "core/arm/symbols.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_thread.h"
#include "core/memory.h"

namespace Core {

namespace {

std::optional<std::string> GetNameFromThreadType64(Core::Memory::Memory& memory,
                                                   const Kernel::KThread& thread) {
    // Read thread type from TLS
    const VAddr tls_thread_type{memory.Read64(thread.GetTlsAddress() + 0x1f8)};
    const VAddr argument_thread_type{thread.GetArgument()};

    if (argument_thread_type && tls_thread_type != argument_thread_type) {
        // Probably not created by nnsdk, no name available.
        return std::nullopt;
    }

    if (!tls_thread_type) {
        return std::nullopt;
    }

    const u16 version{memory.Read16(tls_thread_type + 0x46)};
    VAddr name_pointer{};
    if (version == 1) {
        name_pointer = memory.Read64(tls_thread_type + 0x1a0);
    } else {
        name_pointer = memory.Read64(tls_thread_type + 0x1a8);
    }

    if (!name_pointer) {
        // No name provided.
        return std::nullopt;
    }

    return memory.ReadCString(name_pointer, 256);
}

std::optional<std::string> GetNameFromThreadType32(Core::Memory::Memory& memory,
                                                   const Kernel::KThread& thread) {
    // Read thread type from TLS
    const VAddr tls_thread_type{memory.Read32(thread.GetTlsAddress() + 0x1fc)};
    const VAddr argument_thread_type{thread.GetArgument()};

    if (argument_thread_type && tls_thread_type != argument_thread_type) {
        // Probably not created by nnsdk, no name available.
        return std::nullopt;
    }

    if (!tls_thread_type) {
        return std::nullopt;
    }

    const u16 version{memory.Read16(tls_thread_type + 0x26)};
    VAddr name_pointer{};
    if (version == 1) {
        name_pointer = memory.Read32(tls_thread_type + 0xe4);
    } else {
        name_pointer = memory.Read32(tls_thread_type + 0xe8);
    }

    if (!name_pointer) {
        // No name provided.
        return std::nullopt;
    }

    return memory.ReadCString(name_pointer, 256);
}

constexpr std::array<u64, 2> SegmentBases{
    0x60000000ULL,
    0x7100000000ULL,
};

void SymbolicateBacktrace(const Kernel::KProcess* process, std::vector<BacktraceEntry>& out) {
    auto modules = FindModules(process);

    const bool is_64 = process->Is64Bit();

    std::map<std::string, Symbols::Symbols> symbols;
    for (const auto& module : modules) {
        symbols.insert_or_assign(module.second,
                                 Symbols::GetSymbols(module.first, process->GetMemory(), is_64));
    }

    for (auto& entry : out) {
        VAddr base = 0;
        for (auto iter = modules.rbegin(); iter != modules.rend(); ++iter) {
            const auto& module{*iter};
            if (entry.original_address >= module.first) {
                entry.module = module.second;
                base = module.first;
                break;
            }
        }

        entry.offset = entry.original_address - base;
        entry.address = SegmentBases[is_64] + entry.offset;

        if (entry.module.empty()) {
            entry.module = "unknown";
        }

        const auto symbol_set = symbols.find(entry.module);
        if (symbol_set != symbols.end()) {
            const auto symbol = Symbols::GetSymbolName(symbol_set->second, entry.offset);
            if (symbol) {
                entry.name = Common::DemangleSymbol(*symbol);
            }
        }
    }
}

std::vector<BacktraceEntry> GetAArch64Backtrace(const Kernel::KProcess* process,
                                                const Kernel::Svc::ThreadContext& ctx) {
    std::vector<BacktraceEntry> out;
    auto& memory = process->GetMemory();
    auto pc = ctx.pc, lr = ctx.lr, fp = ctx.fp;

    out.push_back({"", 0, pc, 0, ""});

    // fp (= x29) points to the previous frame record.
    // Frame records are two words long:
    // fp+0 : pointer to previous frame record
    // fp+8 : value of lr for frame
    for (size_t i = 0; i < 256; i++) {
        out.push_back({"", 0, lr, 0, ""});
        if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 16)) {
            break;
        }
        lr = memory.Read64(fp + 8);
        fp = memory.Read64(fp);
    }

    SymbolicateBacktrace(process, out);

    return out;
}

std::vector<BacktraceEntry> GetAArch32Backtrace(const Kernel::KProcess* process,
                                                const Kernel::Svc::ThreadContext& ctx) {
    std::vector<BacktraceEntry> out;
    auto& memory = process->GetMemory();
    auto pc = ctx.pc, lr = ctx.lr, fp = ctx.fp;

    out.push_back({"", 0, pc, 0, ""});

    // fp (= r11) points to the last frame record.
    // Frame records are two words long:
    // fp+0 : pointer to previous frame record
    // fp+4 : value of lr for frame
    for (size_t i = 0; i < 256; i++) {
        out.push_back({"", 0, lr, 0, ""});
        if (!fp || (fp % 4 != 0) || !memory.IsValidVirtualAddressRange(fp, 8)) {
            break;
        }
        lr = memory.Read32(fp + 4);
        fp = memory.Read32(fp);
    }

    SymbolicateBacktrace(process, out);

    return out;
}

} // namespace

std::optional<std::string> GetThreadName(const Kernel::KThread* thread) {
    const auto* process = thread->GetOwnerProcess();
    if (process->Is64Bit()) {
        return GetNameFromThreadType64(process->GetMemory(), *thread);
    } else {
        return GetNameFromThreadType32(process->GetMemory(), *thread);
    }
}

std::string_view GetThreadWaitReason(const Kernel::KThread* thread) {
    switch (thread->GetWaitReasonForDebugging()) {
    case Kernel::ThreadWaitReasonForDebugging::Sleep:
        return "Sleep";
    case Kernel::ThreadWaitReasonForDebugging::IPC:
        return "IPC";
    case Kernel::ThreadWaitReasonForDebugging::Synchronization:
        return "Synchronization";
    case Kernel::ThreadWaitReasonForDebugging::ConditionVar:
        return "ConditionVar";
    case Kernel::ThreadWaitReasonForDebugging::Arbitration:
        return "Arbitration";
    case Kernel::ThreadWaitReasonForDebugging::Suspended:
        return "Suspended";
    default:
        return "Unknown";
    }
}

std::string GetThreadState(const Kernel::KThread* thread) {
    switch (thread->GetState()) {
    case Kernel::ThreadState::Initialized:
        return "Initialized";
    case Kernel::ThreadState::Waiting:
        return fmt::format("Waiting ({})", GetThreadWaitReason(thread));
    case Kernel::ThreadState::Runnable:
        return "Runnable";
    case Kernel::ThreadState::Terminated:
        return "Terminated";
    default:
        return "Unknown";
    }
}

Kernel::KProcessAddress GetModuleEnd(const Kernel::KProcess* process,
                                     Kernel::KProcessAddress base) {
    Kernel::KMemoryInfo mem_info;
    Kernel::Svc::MemoryInfo svc_mem_info;
    Kernel::Svc::PageInfo page_info;
    VAddr cur_addr{GetInteger(base)};
    auto& page_table = process->GetPageTable();

    // Expect: r-x Code (.text)
    R_ASSERT(page_table.QueryInfo(std::addressof(mem_info), std::addressof(page_info), cur_addr));
    svc_mem_info = mem_info.GetSvcMemoryInfo();
    cur_addr = svc_mem_info.base_address + svc_mem_info.size;
    if (svc_mem_info.state != Kernel::Svc::MemoryState::Code ||
        svc_mem_info.permission != Kernel::Svc::MemoryPermission::ReadExecute) {
        return cur_addr - 1;
    }

    // Expect: r-- Code (.rodata)
    R_ASSERT(page_table.QueryInfo(std::addressof(mem_info), std::addressof(page_info), cur_addr));
    svc_mem_info = mem_info.GetSvcMemoryInfo();
    cur_addr = svc_mem_info.base_address + svc_mem_info.size;
    if (svc_mem_info.state != Kernel::Svc::MemoryState::Code ||
        svc_mem_info.permission != Kernel::Svc::MemoryPermission::Read) {
        return cur_addr - 1;
    }

    // Expect: rw- CodeData (.data)
    R_ASSERT(page_table.QueryInfo(std::addressof(mem_info), std::addressof(page_info), cur_addr));
    svc_mem_info = mem_info.GetSvcMemoryInfo();
    cur_addr = svc_mem_info.base_address + svc_mem_info.size;
    return cur_addr - 1;
}

Loader::AppLoader::Modules FindModules(const Kernel::KProcess* process) {
    Loader::AppLoader::Modules modules;

    auto& page_table = process->GetPageTable();
    auto& memory = process->GetMemory();
    VAddr cur_addr = 0;

    // Look for executable sections in Code or AliasCode regions.
    while (true) {
        Kernel::KMemoryInfo mem_info{};
        Kernel::Svc::PageInfo page_info{};
        R_ASSERT(
            page_table.QueryInfo(std::addressof(mem_info), std::addressof(page_info), cur_addr));
        auto svc_mem_info = mem_info.GetSvcMemoryInfo();

        if (svc_mem_info.permission == Kernel::Svc::MemoryPermission::ReadExecute &&
            (svc_mem_info.state == Kernel::Svc::MemoryState::Code ||
             svc_mem_info.state == Kernel::Svc::MemoryState::AliasCode)) {
            // Try to read the module name from its path.
            constexpr s32 PathLengthMax = 0x200;
            struct {
                u32 zero;
                s32 path_length;
                std::array<char, PathLengthMax> path;
            } module_path;

            if (memory.ReadBlock(svc_mem_info.base_address + svc_mem_info.size, &module_path,
                                 sizeof(module_path))) {
                if (module_path.zero == 0 && module_path.path_length > 0) {
                    // Truncate module name.
                    module_path.path[PathLengthMax - 1] = '\0';

                    // Ignore leading directories.
                    char* path_pointer = module_path.path.data();

                    for (s32 i = 0; i < std::min(PathLengthMax, module_path.path_length) &&
                                    module_path.path[i] != '\0';
                         i++) {
                        if (module_path.path[i] == '/' || module_path.path[i] == '\\') {
                            path_pointer = module_path.path.data() + i + 1;
                        }
                    }

                    // Insert output.
                    modules.emplace(svc_mem_info.base_address, path_pointer);
                }
            }
        }

        // Check if we're done.
        const uintptr_t next_address = svc_mem_info.base_address + svc_mem_info.size;
        if (next_address <= cur_addr) {
            break;
        }

        cur_addr = next_address;
    }

    return modules;
}

Kernel::KProcessAddress FindMainModuleEntrypoint(const Kernel::KProcess* process) {
    // Do we have any loaded executable sections?
    auto modules = FindModules(process);

    if (modules.size() >= 2) {
        // If we have two or more, the first one is rtld and the second is main.
        return std::next(modules.begin())->first;
    } else if (!modules.empty()) {
        // If we only have one, this is the main module.
        return modules.begin()->first;
    }

    // As a last resort, use the start of the code region.
    return GetInteger(process->GetPageTable().GetCodeRegionStart());
}

void InvalidateInstructionCacheRange(const Kernel::KProcess* process, u64 address, u64 size) {
    for (size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
        auto* interface = process->GetArmInterface(i);
        if (interface) {
            interface->InvalidateCacheRange(address, size);
        }
    }
}

std::vector<BacktraceEntry> GetBacktraceFromContext(const Kernel::KProcess* process,
                                                    const Kernel::Svc::ThreadContext& ctx) {
    if (process->Is64Bit()) {
        return GetAArch64Backtrace(process, ctx);
    } else {
        return GetAArch32Backtrace(process, ctx);
    }
}

std::vector<BacktraceEntry> GetBacktrace(const Kernel::KThread* thread) {
    Kernel::Svc::ThreadContext ctx = thread->GetContext();
    return GetBacktraceFromContext(thread->GetOwnerProcess(), ctx);
}

} // namespace Core