forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_drgn.pyi
2414 lines (2021 loc) · 78.1 KB
/
_drgn.pyi
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later
"""
libdrgn bindings
Don't use this module directly. Instead, use the drgn package.
"""
import enum
import os
import sys
from typing import (
Any,
Callable,
ClassVar,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Sequence,
Tuple,
Union,
overload,
)
if sys.version_info < (3, 8):
from typing_extensions import Final, Protocol
else:
from typing import Final, Protocol
if sys.version_info < (3, 10):
from typing_extensions import TypeAlias
else:
from typing import TypeAlias
# This is effectively typing.SupportsIndex without @typing.runtime_checkable
# (both of which are only available since Python 3.8), with a more
# self-explanatory name.
class IntegerLike(Protocol):
"""
An :class:`int` or integer-like object.
Parameters annotated with this type expect an integer which may be given as
a Python :class:`int` or an :class:`Object` with integer type.
"""
def __index__(self) -> int: ...
Path: TypeAlias = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
"""
Filesystem path.
Parameters annotated with this type accept a filesystem path as :class:`str`,
:class:`bytes`, or :class:`os.PathLike`.
"""
class Program:
"""
A ``Program`` represents a crashed or running program. It can be used to
lookup type definitions, access variables, and read arbitrary memory.
The main functionality of a ``Program`` is looking up objects (i.e.,
variables, constants, or functions). This is usually done with the
:meth:`[] <.__getitem__>` operator.
"""
def __init__(self, platform: Optional[Platform] = None) -> None:
"""
Create a ``Program`` with no target program. It is usually more
convenient to use one of the :ref:`api-program-constructors`.
:param platform: The platform of the program, or ``None`` if it should
be determined automatically when a core dump or symbol file is
added.
"""
...
flags: ProgramFlags
"""Flags which apply to this program."""
platform: Optional[Platform]
"""
Platform that this program runs on, or ``None`` if it has not been
determined yet.
"""
language: Language
"""
Default programming language of the program.
This is used for interpreting the type name given to :meth:`type()` and
when creating an :class:`Object` without an explicit type.
For the Linux kernel, this defaults to :attr:`Language.C`. For userspace
programs, this defaults to the language of ``main`` in the program, falling
back to :attr:`Language.C`. This heuristic may change in the future.
This can be explicitly set to a different language (e.g., if the heuristic
was incorrect).
"""
def __getitem__(self, name: str) -> Object:
"""
Implement ``self[name]``. Get the object (variable, constant, or
function) with the given name.
This is equivalent to ``prog.object(name)`` except that this raises
:exc:`KeyError` instead of :exc:`LookupError` if no objects with the
given name are found.
If there are multiple objects with the same name, one is returned
arbitrarily. In this case, the :meth:`variable()`, :meth:`constant()`,
:meth:`function()`, or :meth:`object()` methods can be used instead.
>>> prog['jiffies']
Object(prog, 'volatile unsigned long', address=0xffffffff94c05000)
:param name: Object name.
"""
...
def __contains__(self, name: str) -> bool:
"""
Implement ``name in self``. Return whether an object (variable,
constant, or function) with the given name exists in the program.
:param name: Object name.
"""
...
def variable(self, name: str, filename: Optional[str] = None) -> Object:
"""
Get the variable with the given name.
>>> prog.variable('jiffies')
Object(prog, 'volatile unsigned long', address=0xffffffff94c05000)
This is equivalent to ``prog.object(name, FindObjectFlags.VARIABLE,
filename)``.
:param name: The variable name.
:param filename: The source code file that contains the definition. See
:ref:`api-filenames`.
:raises LookupError: if no variables with the given name are found in
the given file
"""
...
def constant(self, name: str, filename: Optional[str] = None) -> Object:
"""
Get the constant (e.g., enumeration constant) with the given name.
Note that support for macro constants is not yet implemented for DWARF
files, and most compilers don't generate macro debugging information by
default anyways.
>>> prog.constant('PIDTYPE_MAX')
Object(prog, 'enum pid_type', value=4)
This is equivalent to ``prog.object(name, FindObjectFlags.CONSTANT,
filename)``.
:param name: The constant name.
:param filename: The source code file that contains the definition. See
:ref:`api-filenames`.
:raises LookupError: if no constants with the given name are found in
the given file
"""
...
def function(self, name: str, filename: Optional[str] = None) -> Object:
"""
Get the function with the given name.
>>> prog.function('schedule')
Object(prog, 'void (void)', address=0xffffffff94392370)
This is equivalent to ``prog.object(name, FindObjectFlags.FUNCTION,
filename)``.
:param name: The function name.
:param filename: The source code file that contains the definition. See
:ref:`api-filenames`.
:raises LookupError: if no functions with the given name are found in
the given file
"""
...
def object(
self,
name: str,
flags: FindObjectFlags = FindObjectFlags.ANY,
filename: Optional[str] = None,
) -> Object:
"""
Get the object (variable, constant, or function) with the given name.
When debugging the Linux kernel, this can look up certain special
objects documented in :ref:`kernel-special-objects`, sometimes without
any debugging information loaded.
:param name: The object name.
:param flags: Flags indicating what kind of object to look for.
:param filename: The source code file that contains the definition. See
:ref:`api-filenames`.
:raises LookupError: if no objects with the given name are found in
the given file
"""
...
def symbol(self, address_or_name: Union[IntegerLike, str]) -> Symbol:
"""
Get a symbol containing the given address, or a symbol with the given
name.
Global symbols are preferred over weak symbols, and weak symbols are
preferred over other symbols. In other words: if a matching
:attr:`SymbolBinding.GLOBAL` or :attr:`SymbolBinding.UNIQUE` symbol is
found, it is returned. Otherwise, if a matching
:attr:`SymbolBinding.WEAK` symbol is found, it is returned. Otherwise,
any matching symbol (e.g., :attr:`SymbolBinding.LOCAL`) is returned. If
there are multiple matching symbols with the same binding, one is
returned arbitrarily. To retrieve all matching symbols, use
:meth:`symbols()`.
:param address_or_name: Address or name to search for. This parameter
is positional-only.
:raises LookupError: if no symbol contains the given address or matches
the given name
"""
...
def symbols(
self,
address_or_name: Union[None, IntegerLike, str] = None,
) -> List[Symbol]:
"""
Get a list of global and local symbols, optionally matching a name or
address.
If a string argument is given, this returns all symbols matching that
name. If an integer-like argument given, this returns a list of all
symbols containing that address. If no argument is given, all symbols
in the program are returned. In all cases, the symbols are returned in
an unspecified order.
:param address_or_name: Address or name to search for. This parameter
is positional-only.
"""
...
def stack_trace(
self,
# Object is already IntegerLike, but this explicitly documents that it
# can take non-integer Objects.
thread: Union[Object, IntegerLike],
) -> StackTrace:
"""
Get the stack trace for the given thread in the program.
``thread`` may be a thread ID (as defined by :manpage:`gettid(2)`), in
which case this will unwind the stack for the thread with that ID. The
ID may be a Python ``int`` or an integer :class:`Object`
``thread`` may also be a ``struct pt_regs`` or ``struct pt_regs *``
object, in which case the initial register values will be fetched from
that object.
Finally, if debugging the Linux kernel, ``thread`` may be a ``struct
task_struct *`` object, in which case this will unwind the stack for
that task. See :func:`drgn.helpers.linux.pid.find_task()`.
This is implemented for the Linux kernel (both live and core dumps) as
well as userspace core dumps; it is not yet implemented for live
userspace processes.
:param thread: Thread ID, ``struct pt_regs`` object, or
``struct task_struct *`` object.
"""
...
@overload
def type(self, name: str, filename: Optional[str] = None) -> Type:
"""
Get the type with the given name.
>>> prog.type('long')
prog.int_type(name='long', size=8, is_signed=True)
:param name: The type name.
:param filename: The source code file that contains the definition. See
:ref:`api-filenames`.
:raises LookupError: if no types with the given name are found in
the given file
"""
...
@overload
# type is positional-only.
def type(self, type: Type) -> Type:
"""
Return the given type.
This is mainly useful so that helpers can use ``prog.type()`` to get a
:class:`Type` regardless of whether they were given a :class:`str` or a
:class:`Type`. For example:
.. code-block:: python3
def my_helper(obj: Object, type: Union[str, Type]) -> bool:
# type may be str or Type.
type = obj.prog_.type(type)
# type is now always Type.
return sizeof(obj) > sizeof(type)
:param type: Type.
:return: The exact same type.
"""
...
def threads(self) -> Iterator[Thread]:
"""Get an iterator over all of the threads in the program."""
...
def thread(self, tid: IntegerLike) -> Thread:
"""
Get the thread with the given thread ID.
:param tid: Thread ID (as defined by :manpage:`gettid(2)`).
:raises LookupError: if no thread has the given thread ID
"""
...
def main_thread(self) -> Thread:
"""
Get the main thread of the program.
This is only defined for userspace programs.
:raises ValueError: if the program is the Linux kernel
"""
...
def crashed_thread(self) -> Thread:
"""
Get the thread that caused the program to crash.
For userspace programs, this is the thread that received the fatal
signal (e.g., ``SIGSEGV`` or ``SIGQUIT``).
For the kernel, this is the thread that panicked (either directly or as
a result of an oops, ``BUG_ON()``, etc.).
:raises ValueError: if the program is live (i.e., not a core dump)
"""
...
def read(
self, address: IntegerLike, size: IntegerLike, physical: bool = False
) -> bytes:
"""
Read *size* bytes of memory starting at *address* in the program. The
address may be virtual (the default) or physical if the program
supports it.
>>> prog.read(0xffffffffbe012b40, 16)
b'swapper/0\x00\x00\x00\x00\x00\x00\x00'
:param address: The starting address.
:param size: The number of bytes to read.
:param physical: Whether *address* is a physical memory address. If
``False``, then it is a virtual memory address. Physical memory can
usually only be read when the program is an operating system
kernel.
:raises FaultError: if the address range is invalid or the type of
address (physical or virtual) is not supported by the program
:raises ValueError: if *size* is negative
"""
...
def read_u8(self, address: IntegerLike, physical: bool = False) -> int:
""" """
...
def read_u16(self, address: IntegerLike, physical: bool = False) -> int:
""" """
...
def read_u32(self, address: IntegerLike, physical: bool = False) -> int:
""" """
...
def read_u64(self, address: IntegerLike, physical: bool = False) -> int:
""" """
...
def read_word(self, address: IntegerLike, physical: bool = False) -> int:
"""
Read an unsigned integer from the program's memory in the program's
byte order.
:meth:`read_u8()`, :meth:`read_u16()`, :meth:`read_u32()`, and
:meth:`read_u64()` read an 8-, 16-, 32-, or 64-bit unsigned integer,
respectively. :meth:`read_word()` reads a program word-sized unsigned
integer.
For signed integers, alternate byte order, or other formats, you can
use :meth:`read()` and :meth:`int.from_bytes()` or the :mod:`struct`
module.
:param address: Address of the integer.
:param physical: Whether *address* is a physical memory address; see
:meth:`read()`.
:raises FaultError: if the address is invalid; see :meth:`read()`
"""
...
def add_memory_segment(
self,
address: IntegerLike,
size: IntegerLike,
read_fn: Callable[[int, int, int, bool], bytes],
physical: bool = False,
) -> None:
"""
Define a region of memory in the program.
If it overlaps a previously registered segment, the new segment takes
precedence.
:param address: Address of the segment.
:param size: Size of the segment in bytes.
:param physical: Whether to add a physical memory segment. If
``False``, then this adds a virtual memory segment.
:param read_fn: Callable to call to read memory from the segment. It is
passed the address being read from, the number of bytes to read,
the offset in bytes from the beginning of the segment, and whether
the address is physical: ``(address, count, offset, physical)``. It
should return the requested number of bytes as :class:`bytes` or
another :ref:`buffer <python:binaryseq>` type.
"""
...
def add_type_finder(
self, fn: Callable[[TypeKind, str, Optional[str]], Optional[Type]]
) -> None:
"""
Register a callback for finding types in the program.
Callbacks are called in reverse order of the order they were added
until the type is found. So, more recently added callbacks take
precedence.
:param fn: Callable taking a :class:`TypeKind`, name, and filename:
``(kind, name, filename)``. The filename should be matched with
:func:`filename_matches()`. This should return a :class:`Type`
or ``None`` if not found.
"""
...
def add_object_finder(
self,
fn: Callable[[Program, str, FindObjectFlags, Optional[str]], Optional[Object]],
) -> None:
"""
Register a callback for finding objects in the program.
Callbacks are called in reverse order of the order they were added
until the object is found. So, more recently added callbacks take
precedence.
:param fn: Callable taking a program, name, :class:`FindObjectFlags`,
and filename: ``(prog, name, flags, filename)``. The filename
should be matched with :func:`filename_matches()`. This should
return an :class:`Object` or ``None`` if not found.
"""
...
def set_core_dump(self, path: Union[Path, int]) -> None:
"""
Set the program to a core dump.
This loads the memory segments from the core dump and determines the
mapped executable and libraries. It does not load any debugging
symbols; see :meth:`load_default_debug_info()`.
:param path: Core dump file path or open file descriptor.
"""
...
def set_kernel(self) -> None:
"""
Set the program to the running operating system kernel.
This loads the memory of the running kernel and thus requires root
privileges. It does not load any debugging symbols; see
:meth:`load_default_debug_info()`.
"""
...
def set_pid(self, pid: int) -> None:
"""
Set the program to a running process.
This loads the memory of the process and determines the mapped
executable and libraries. It does not load any debugging symbols; see
:meth:`load_default_debug_info()`.
:param pid: Process ID.
"""
...
def load_debug_info(
self,
paths: Optional[Iterable[Path]] = None,
default: bool = False,
main: bool = False,
) -> None:
"""
Load debugging information for a list of executable or library files.
Note that this is parallelized, so it is usually faster to load
multiple files at once rather than one by one.
:param paths: Paths of binary files.
:param default: Also load debugging information which can automatically
be determined from the program.
For the Linux kernel, this tries to load ``vmlinux`` and any loaded
kernel modules from a few standard locations.
For userspace programs, this tries to load the executable and any
loaded libraries.
This implies ``main=True``.
:param main: Also load debugging information for the main executable.
For the Linux kernel, this tries to load ``vmlinux``.
This is currently ignored for userspace programs.
:raises MissingDebugInfoError: if debugging information was not
available for some files; other files with debugging information
are still loaded
"""
...
def load_default_debug_info(self) -> None:
"""
Load debugging information which can automatically be determined from
the program.
This is equivalent to ``load_debug_info(None, True)``.
"""
...
cache: Dict[Any, Any]
"""
Dictionary for caching program metadata.
This isn't used by drgn itself. It is intended to be used by helpers to
cache metadata about the program. For example, if a helper for a program
depends on the program version or an optional feature, the helper can
detect it and cache it for subsequent invocations:
.. code-block:: python3
def my_helper(prog):
try:
have_foo = prog.cache['have_foo']
except KeyError:
have_foo = detect_foo_feature(prog)
prog.cache['have_foo'] = have_foo
if have_foo:
return prog['foo']
else:
return prog['bar']
"""
def void_type(
self,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new void type. It has kind :attr:`TypeKind.VOID`.
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def int_type(
self,
name: str,
size: IntegerLike,
is_signed: bool,
byteorder: Optional[str] = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new integer type. It has kind :attr:`TypeKind.INT`.
:param name: :attr:`Type.name`
:param size: :attr:`Type.size`
:param is_signed: :attr:`Type.is_signed`
:param byteorder: :attr:`Type.byteorder`, or ``None`` to use the
program's default byte order.
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def bool_type(
self,
name: str,
size: IntegerLike,
byteorder: Optional[str] = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new boolean type. It has kind :attr:`TypeKind.BOOL`.
:param name: :attr:`Type.name`
:param size: :attr:`Type.size`
:param byteorder: :attr:`Type.byteorder`, or ``None`` to use the
program's default byte order.
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def float_type(
self,
name: str,
size: IntegerLike,
byteorder: Optional[str] = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new floating-point type. It has kind :attr:`TypeKind.FLOAT`.
:param name: :attr:`Type.name`
:param size: :attr:`Type.size`
:param byteorder: :attr:`Type.byteorder`, or ``None`` to use the
program's default byte order.
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
@overload
def struct_type(
self,
tag: Optional[str],
size: IntegerLike,
members: Sequence[TypeMember],
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new structure type. It has kind :attr:`TypeKind.STRUCT`.
:param tag: :attr:`Type.tag`
:param size: :attr:`Type.size`
:param members: :attr:`Type.members`
:param template_parameters: :attr:`Type.template_parameters`
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
@overload
def struct_type(
self,
tag: Optional[str],
size: None = None,
members: None = None,
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""Create a new incomplete structure type."""
...
@overload
def union_type(
self,
tag: Optional[str],
size: IntegerLike,
members: Sequence[TypeMember],
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new union type. It has kind :attr:`TypeKind.UNION`. Otherwise,
this is the same as as :meth:`struct_type()`.
"""
...
@overload
def union_type(
self,
tag: Optional[str],
size: None = None,
members: None = None,
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""Create a new incomplete union type."""
...
@overload
def class_type(
self,
tag: Optional[str],
size: IntegerLike,
members: Sequence[TypeMember],
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new class type. It has kind :attr:`TypeKind.CLASS`. Otherwise,
this is the same as as :meth:`struct_type()`.
"""
...
@overload
def class_type(
self,
tag: Optional[str],
size: None = None,
members: None = None,
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""Create a new incomplete class type."""
...
@overload
def enum_type(
self,
tag: Optional[str],
type: Type,
enumerators: Sequence[TypeEnumerator],
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new enumerated type. It has kind :attr:`TypeKind.ENUM`.
:param tag: :attr:`Type.tag`
:param type: The compatible integer type (:attr:`Type.type`)
:param enumerators: :attr:`Type.enumerators`
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
@overload
def enum_type(
self,
tag: Optional[str],
type: None = None,
enumerators: None = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""Create a new incomplete enumerated type."""
...
def typedef_type(
self,
name: str,
type: Type,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new typedef type. It has kind :attr:`TypeKind.TYPEDEF`.
:param name: :attr:`Type.name`
:param type: The aliased type (:attr:`Type.type`)
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def pointer_type(
self,
type: Type,
size: Optional[int] = None,
byteorder: Optional[str] = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new pointer type. It has kind :attr:`TypeKind.POINTER`,
:param type: The referenced type (:attr:`Type.type`)
:param size: :attr:`Type.size`, or ``None`` to use the program's
default pointer size.
:param byteorder: :attr:`Type.byteorder`, or ``None`` to use the
program's default byte order.
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def array_type(
self,
type: Type,
length: Optional[int] = None,
*,
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new array type. It has kind :attr:`TypeKind.ARRAY`.
:param type: The element type (:attr:`Type.type`)
:param length: :attr:`Type.length`
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
def function_type(
self,
type: Type,
parameters: Sequence[TypeParameter],
is_variadic: bool = False,
*,
template_parameters: Sequence[TypeTemplateParameter] = (),
qualifiers: Qualifiers = Qualifiers.NONE,
language: Optional[Language] = None,
) -> Type:
"""
Create a new function type. It has kind :attr:`TypeKind.FUNCTION`.
:param type: The return type (:attr:`Type.type`)
:param parameters: :attr:`Type.parameters`
:param is_variadic: :attr:`Type.is_variadic`
:param template_parameters: :attr:`Type.template_parameters`
:param qualifiers: :attr:`Type.qualifiers`
:param lang: :attr:`Type.language`
"""
...
class ProgramFlags(enum.Flag):
"""
``ProgramFlags`` are flags that can apply to a :class:`Program` (e.g.,
about what kind of program it is).
"""
IS_LINUX_KERNEL = ...
"""The program is the Linux kernel."""
IS_LIVE = ...
"""
The program is currently running (e.g., it is the running operating system
kernel or a running process).
"""
IS_LOCAL = ...
"""
The program is running on the local machine.
"""
class FindObjectFlags(enum.Flag):
"""
``FindObjectFlags`` are flags for :meth:`Program.object()`. These can be
combined to search for multiple kinds of objects at once.
"""
CONSTANT = ...
""
FUNCTION = ...
""
VARIABLE = ...
""
ANY = ...
""
def get_default_prog() -> Program:
"""
Get the default program for the current thread.
:raises NoDefaultProgramError: if the default program is not set
"""
...
# prog is positional-only.
def set_default_prog(prog: Optional[Program]) -> None:
"""
Set the default program for the current thread.
:param prog: Program to set as the default, or ``None`` to unset it.
"""
...
class NoDefaultProgramError(Exception):
"""
Error raised when trying to use the default program when it is not set.
"""
...
class Thread:
"""A thread in a program."""
tid: Final[int]
"""Thread ID (as defined by :manpage:`gettid(2)`)."""
object: Final[Object]
"""
If the program is the Linux kernel, the ``struct task_struct *`` object for
this thread. Otherwise, not defined.
"""
def stack_trace(self) -> StackTrace:
"""
Get the stack trace for this thread.
This is equivalent to ``prog.stack_trace(thread.tid)``. See
:meth:`Program.stack_trace()`.
"""
...
def filename_matches(haystack: Optional[str], needle: Optional[str]) -> bool:
"""
Return whether a filename containing a definition (*haystack*) matches a
filename being searched for (*needle*).
The filename is matched from right to left, so ``'stdio.h'``,
``'include/stdio.h'``, ``'usr/include/stdio.h'``, and
``'/usr/include/stdio.h'`` would all match a definition in
``/usr/include/stdio.h``. If *needle* is ``None`` or empty, it matches any
definition. If *haystack* is ``None`` or empty, it only matches if *needle*
is also ``None`` or empty.
:param haystack: Path of file containing definition.
:param needle: Filename to match.
"""
...
def program_from_core_dump(path: Union[Path, int]) -> Program:
"""
Create a :class:`Program` from a core dump file. The type of program (e.g.,
userspace or kernel) is determined automatically.
:param path: Core dump file path or open file descriptor.
"""
...
def program_from_kernel() -> Program:
"""
Create a :class:`Program` from the running operating system kernel. This
requires root privileges.
"""
...
def program_from_pid(pid: int) -> Program:
"""
Create a :class:`Program` from a running program with the given PID. This
requires appropriate permissions (on Linux, :manpage:`ptrace(2)` attach
permissions).
:param pid: Process ID of the program to debug.
"""
...
class Platform:
"""
A ``Platform`` represents the environment (i.e., architecture and ABI) that
a program runs on.
"""
def __init__(
self, arch: Architecture, flags: Optional[PlatformFlags] = None
) -> None:
"""
Create a ``Platform``.
:param arch: :attr:`Platform.arch`
:param flags: :attr:`Platform.flags`; if ``None``, default flags for
the architecture are used.
"""
...
arch: Final[Architecture]
"""Instruction set architecture of this platform."""
flags: Final[PlatformFlags]
"""Flags which apply to this platform."""
registers: Final[Sequence[Register]]
"""Processor registers on this platform."""
class Architecture(enum.Enum):
"""An ``Architecture`` represents an instruction set architecture."""
X86_64 = ...
"""The x86-64 architecture, a.k.a. AMD64 or Intel 64."""
I386 = ...
"""The 32-bit x86 architecture, a.k.a. i386 or IA-32."""
AARCH64 = ...
"""The AArch64 architecture, a.k.a. ARM64."""
ARM = ...
"""The 32-bit Arm architecture."""
PPC64 = ...
"""The 64-bit PowerPC architecture."""
RISCV64 = ...
"""The 64-bit RISC-V architecture."""
RISCV32 = ...
"""The 32-bit RISC-V architecture."""
S390X = ...
"""The s390x architecture, a.k.a. IBM Z or z/Architecture."""