2022-12-19 14:37:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
2023-02-02 11:15:11 +00:00
|
|
|
#include <span>
|
2022-12-19 14:37:51 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// Declare drgn structs and only refer to them by pointers to avoid exposing
|
|
|
|
// drgn.h.
|
|
|
|
struct drgn_error;
|
|
|
|
struct drgn_func_iterator;
|
2023-02-02 11:15:11 +00:00
|
|
|
struct drgn_program;
|
2022-12-19 14:37:51 +00:00
|
|
|
struct drgn_qualified_type;
|
2023-02-02 11:15:11 +00:00
|
|
|
struct drgn_symbol;
|
2023-02-17 15:16:25 +00:00
|
|
|
struct drgn_type;
|
2022-12-19 14:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace drgnplusplus {
|
|
|
|
|
|
|
|
class error : public std::exception {
|
|
|
|
public:
|
|
|
|
struct Deleter {
|
|
|
|
void operator()(drgn_error* err) noexcept;
|
|
|
|
};
|
2024-08-13 15:16:38 +01:00
|
|
|
error(drgn_error* err) : ptr(err) {};
|
2022-12-19 14:37:51 +00:00
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
return static_cast<bool>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* what() const noexcept final;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<drgn_error, Deleter> ptr;
|
|
|
|
};
|
|
|
|
|
2023-02-02 11:15:11 +00:00
|
|
|
struct SymbolsDeleter {
|
|
|
|
void operator()(std::span<drgn_symbol*>*) noexcept;
|
|
|
|
};
|
|
|
|
using symbols = std::unique_ptr<std::span<drgn_symbol*>, SymbolsDeleter>;
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
class program {
|
|
|
|
public:
|
|
|
|
struct Deleter {
|
|
|
|
void operator()(drgn_program* prog) noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
program();
|
2024-08-13 15:16:38 +01:00
|
|
|
program(drgn_program* prog) : ptr(prog) {};
|
2022-12-19 14:37:51 +00:00
|
|
|
|
2023-05-19 16:18:04 +01:00
|
|
|
symbols find_all_symbols();
|
2023-02-02 11:15:11 +00:00
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
drgn_program* get() {
|
|
|
|
return ptr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<drgn_program, Deleter> ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class func_iterator {
|
|
|
|
public:
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = drgn_qualified_type;
|
|
|
|
using pointer = drgn_qualified_type*;
|
|
|
|
using reference = drgn_qualified_type&;
|
|
|
|
|
|
|
|
struct Deleter {
|
|
|
|
void operator()(drgn_func_iterator* _iter) noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
func_iterator(drgn_program* prog);
|
|
|
|
func_iterator(program& prog);
|
|
|
|
func_iterator() = default;
|
|
|
|
func_iterator(drgn_func_iterator* _iter) : iter(_iter, Deleter()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
reference operator*() const {
|
|
|
|
return *current;
|
|
|
|
}
|
|
|
|
pointer operator->() {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
func_iterator& operator++();
|
|
|
|
friend bool operator==(const func_iterator& a, const func_iterator& b) {
|
|
|
|
return a.iter == b.iter;
|
|
|
|
};
|
|
|
|
friend bool operator!=(const func_iterator& a, const func_iterator& b) {
|
|
|
|
return !(a == b);
|
|
|
|
};
|
|
|
|
|
|
|
|
func_iterator begin() {
|
|
|
|
return ++(*this);
|
|
|
|
}
|
|
|
|
func_iterator end() {
|
|
|
|
return func_iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<drgn_func_iterator> iter = nullptr;
|
|
|
|
pointer current = nullptr;
|
|
|
|
};
|
|
|
|
|
2023-02-02 11:15:11 +00:00
|
|
|
namespace symbol {
|
|
|
|
const char* name(drgn_symbol*);
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:37:51 +00:00
|
|
|
} // namespace drgnplusplus
|
2023-02-17 15:16:25 +00:00
|
|
|
|
|
|
|
namespace drgn_utils {
|
|
|
|
/*
|
|
|
|
* These utils are not intended to be permanent. As part of the transition to
|
|
|
|
* type-graph based CodeGen, we need to break dependencies on legacy OICodeGen
|
|
|
|
* from other parts of OI.
|
|
|
|
*
|
|
|
|
* Parts of OICodeGen used by other parts of OI, but which only return drgn
|
|
|
|
* data can be moved here.
|
|
|
|
*/
|
|
|
|
|
2023-04-18 18:02:37 +01:00
|
|
|
void getDrgnArrayElementType(drgn_type* type,
|
|
|
|
drgn_type** outElemType,
|
2023-02-17 15:16:25 +00:00
|
|
|
size_t& outNumElems);
|
|
|
|
std::string typeToName(drgn_type* type);
|
2023-05-24 10:33:12 +01:00
|
|
|
bool isSizeComplete(struct drgn_type* type);
|
|
|
|
drgn_type* underlyingType(drgn_type* type);
|
2023-02-17 15:16:25 +00:00
|
|
|
|
|
|
|
} // namespace drgn_utils
|