utils

package
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSymbolNotFound = errors.New("symbol not found")

Functions

func ClearBit added in v0.11.0

func ClearBit(n *uint64, offset uint)

func ClearBits added in v0.11.0

func ClearBits(n *uint64, mask uint64)

func CopyRegularFileByPath added in v0.8.2

func CopyRegularFileByPath(src, dst string) error

CopyRegularFileByPath copies a file from src to dst

func CopyRegularFileByRelativePath added in v0.8.2

func CopyRegularFileByRelativePath(srcName string, dstDir *os.File, dstName string) error

CopyRegularFileByRelativePath copies a file from src to dst, where destination is relative to a given directory. This function needs needed capabilities to be set before it is called.

func CreateAt added in v0.8.2

func CreateAt(dir *os.File, relativePath string) (*os.File, error)

CreateAt implements the same logic as os.Create using directory FD and relative path.

func Dup added in v0.8.2

func Dup(file *os.File) (*os.File, error)

Dup is a wrapper function to the `dup` syscall using golang types.

func GenerateRandomDuration added in v0.18.0

func GenerateRandomDuration(min, max int) time.Duration

GenerateRandomDuration returns a random duration between min and max, inclusive

func HasBit added in v0.11.0

func HasBit(n uint64, offset uint) bool

func HashTaskID added in v0.18.0

func HashTaskID(arg1 uint32, arg2 uint64) uint32

func HashU32AndU64 added in v0.18.0

func HashU32AndU64(arg1 uint32, arg2 uint64) uint32

HashU32AndU64 is a wrapper around Murmur32 making sure network byte order is used.

func IsDirEmpty added in v0.10.0

func IsDirEmpty(pathname string) (bool, error)

IsDirEmpty returns true if directory contains no files

func Max added in v0.11.0

func Max(x, y uint64) uint64

func Min added in v0.11.0

func Min(x, y uint64) uint64

func MkdirAt added in v0.8.2

func MkdirAt(dir *os.File, relativePath string, perm fs.FileMode) error

MkdirAt is a wrapper function to the `mkdirat` syscall using golang types.

func MkdirAtExist added in v0.8.2

func MkdirAtExist(dir *os.File, relativePath string, perm fs.FileMode) error

MkdirAtExist is a wrapper function to the `mkdirat` syscall using golang types, ignoring EEXIST error.

func Murmur32 added in v0.18.0

func Murmur32(key []byte) uint32

Murmur32 is a Murmur3 32-bit hash function implementation.

func OpenAt added in v0.8.2

func OpenAt(dir *os.File, relativePath string, flags int, perm fs.FileMode) (*os.File, error)

OpenAt is a wrapper function to the `openat` syscall using golang types.

func OpenExistingDir added in v0.8.2

func OpenExistingDir(p string) (*os.File, error)

OpenExistingDir open a directory with given path, and return the os.File of it.

func RemoveAt added in v0.14.1

func RemoveAt(dir *os.File, relativePath string, flags int) error

RemoveAt is a wrapper function to the `unlinkat` syscall using golang types.

func RenameAt added in v0.8.2

func RenameAt(olddir *os.File, oldpath string, newdir *os.File, newpath string) error

RenameAt is a wrapper function to the `renameat` syscall using golang types.

func ReverseString added in v0.23.0

func ReverseString(s string) string

func RoundToClosestN added in v0.18.0

func RoundToClosestN(val int, n int) int

RoundToClosestN rounds a number to the closest multiple of n.

func SetBit added in v0.11.0

func SetBit(n *uint64, offset uint)

func SymbolToOffset added in v0.21.0

func SymbolToOffset(path, symbol string) (uint32, error)

SymbolToOffset attempts to resolve a 'symbol' name in the binary found at 'path' to an offset. The offset can be used for attaching a u(ret)probe

Types

type Cloner added in v0.20.0

type Cloner[T any] interface {
	Clone() T
}

Cloner is a generic interface for objects that can clone themselves.

type Iterator added in v0.21.0

type Iterator[T any] interface {
	// HasNext returns true if there are more elements to iterate.
	HasNext() bool

	// Next returns the next element in the iteration.
	Next() T
}

Iterator is a generic interface for iterators.

type Symbol added in v0.23.0

type Symbol[T any] interface {
	// Name returns the symbol's name
	Name() string
	// Address returns the base address of the symbol
	Address() uint64
	// Contains returns whether a given address belongs to the symbol's
	// address range, which is defined by the symbol's implementation
	Contains(address uint64) bool
	Cloner[T]
}

The Symbol interface defines what is needed from a symbol implementation in order to facilitate the lookup functionalities provided by SymbolTable. Implementations of Symbol can hold various types of information relevant to the type of symbol they represent.

type SymbolTable added in v0.23.0

type SymbolTable[T Symbol[T]] struct {
	// contains filtered or unexported fields
}

SymbolTable is used to hold information about symbols (mapping from symbolic names used in code to their address) in a certain executable. It can be used to hold symbols from an ELF binary, or symbols of the entire kernel and its modules. It provides functions to lookup symbols by address and name.

func NewSymbolTable added in v0.23.0

func NewSymbolTable[T Symbol[T]](lazyNameLookup bool) *SymbolTable[T]

Creates a new SymbolTable. If lazyNameLookup is true, the mapping from name to symbol will be populated only when a failed lookup occurs. This reduces memory footprint at the cost of the time it takes to lookup a symbol name for the first time.

func (*SymbolTable[T]) AddSymbols added in v0.23.0

func (st *SymbolTable[T]) AddSymbols(symbols []*T)

Adds a slice of symbols to the symbol table.

func (*SymbolTable[T]) ForEachSymbol added in v0.23.0

func (st *SymbolTable[T]) ForEachSymbol(callback func(symbol *T))

func (*SymbolTable[T]) LookupByAddressContains added in v0.23.0

func (st *SymbolTable[T]) LookupByAddressContains(address uint64) (*T, error)

Find the symbol which contains the given address. If multiple symbols at different addresses contain the requested address, the symbol with the highest address will be returned. If multiple symbols at the same address contain the requested address, one of them will be returned, but there is no guarantee which one. This function assumes that symbols don't overlap in a way that a symbol with a smaller address contains the requested address while a symbol with a larger address (but still smaller that requested) doesn't contain it. For example, the following situation is assumed to be impossible:

        Requested Address
                 |
                 |
 +---------------+--+
 |Symbol 1       |  |
 +---------------+--+
    +--------+   |
    |Symbol 2|   |
    +--------+   v
<---------------------->

Smaller Larger Address Address

If the above situation happens, no symbol will be returned.

func (*SymbolTable[T]) LookupByAddressExact added in v0.23.0

func (st *SymbolTable[T]) LookupByAddressExact(address uint64) ([]*T, error)

Lookup a symbol in the table by its exact address. Because there may be multiple symbols at the same address, a slice of all matching symbols is returned.

func (*SymbolTable[T]) LookupByName added in v0.23.0

func (st *SymbolTable[T]) LookupByName(name string) ([]*T, error)

Lookup a symbol in the table by its name. Because there may be multiple symbols with the same name, a slice of all matching symbols is returned.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳