forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_drgn.pyi
2221 lines (1867 loc) · 71.3 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: GPL-3.0-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,
Dict,
Iterable,
Iterator,
Mapping,
Optional,
Sequence,
Tuple,
Union,
overload,
)
if sys.version_info < (3, 8):
from typing_extensions import Protocol
else:
from typing import Protocol
# 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 = 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 is :attr:`Language.C`. For userspace programs,
this is determined from the language of ``main`` in the program, falling
back to :attr:`Language.C`. This heuristic may change in the future.
"""
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.
: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
"""
...
# address_or_name is positional-only.
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.
:param address_or_name: Address or name.
:raises LookupError: if no symbol contains the given address or matches
the given name
"""
...
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 `gettid(2)
<http://man7.org/linux/man-pages/man2/gettid.2.html>`_), 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.
"""
...
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
"""
...
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]], 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`.
"""
...
def add_object_finder(
self, fn: Callable[[Program, str, FindObjectFlags, Optional[str]], 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`.
"""
...
def set_core_dump(self, path: Path) -> 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.
"""
...
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).
"""
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 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: Path) -> 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.
"""
...
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: Architecture
"""Instruction set architecture of this platform."""
flags: PlatformFlags
"""Flags which apply to this platform."""
registers: 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."""
PPC64 = ...
"""The 64-bit PowerPC architecture."""
UNKNOWN = ...
"""
An architecture which is not known to drgn. Certain features are not
available when the architecture is unknown, but most of drgn will still
work.
"""
class PlatformFlags(enum.Flag):
"""``PlatformFlags`` are flags describing a :class:`Platform`."""
IS_64_BIT = ...
"""Platform is 64-bit."""
IS_LITTLE_ENDIAN = ...
"""Platform is little-endian."""
class Register:
"""A ``Register`` represents information about a processor register."""
names: Sequence[str]
"""Names of this register."""
host_platform: Platform
"""The platform of the host which is running drgn."""
class Language:
"""
A ``Language`` represents a programming language supported by drgn.
This class cannot be constructed; there are singletons for the supported
languages.
"""
name: str
"""Name of the programming language."""
C: Language
"""The C programming language."""
class Object:
"""
An ``Object`` represents a symbol or value in a program. An object may
exist in the memory of the program (a *reference*), it may be a constant or
temporary computed value (a *value*), or it may be absent entirely (an
*absent* object).
All instances of this class have two attributes: :attr:`prog_`, the program
that the object is from; and :attr:`type_`, the type of the object.
Reference objects also have an :attr:`address_` and a :attr:`bit_offset_`.
Objects may also have a :attr:`bit_field_size_`.
:func:`repr()` of an object returns a Python representation of the object:
>>> print(repr(prog['jiffies']))
Object(prog, 'volatile unsigned long', address=0xffffffffbf005000)
:class:`str() <str>` returns a "pretty" representation of the object in
programming language syntax:
>>> print(prog['jiffies'])
(volatile unsigned long)4326237045
The output format of ``str()`` can be modified by using the
:meth:`format_()` method instead:
>>> sysname = prog['init_uts_ns'].name.sysname
>>> print(sysname)
(char [65])"Linux"
>>> print(sysname.format_(type_name=False))
"Linux"
>>> print(sysname.format_(string=False))
(char [65]){ 76, 105, 110, 117, 120 }
.. note::
The drgn CLI is set up so that objects are displayed in the "pretty"
format instead of with ``repr()`` (the latter is the default behavior
of Python's interactive mode). Therefore, it's usually not necessary to
call ``print()`` in the drgn CLI.
Objects support the following operators:
* Arithmetic operators: ``+``, ``-``, ``*``, ``/``, ``%``
* Bitwise operators: ``<<``, ``>>``, ``&``, ``|``, ``^``, ``~``
* Relational operators: ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=``
* Subscripting: :meth:`[] <__getitem__>` (Python does not have a unary
``*`` operator, so pointers are dereferenced with ``ptr[0]``)
* Member access: :meth:`. <__getattribute__>` (Python does not have a
``->`` operator, so ``.`` is also used to access members of pointers to
structures)
* The address-of operator: :meth:`drgn.Object.address_of_()` (this is a
method because Python does not have a ``&`` operator)
* Array length: :meth:`len() <__len__>`
These operators all have the semantics of the program's programming
language. For example, adding two objects from a program written in C
results in an object with a type and value according to the rules of C:
>>> Object(prog, 'unsigned long', 2**64 - 1) + Object(prog, 'int', 1)
Object(prog, 'unsigned long', value=0)
If only one operand to a binary operator is an object, the other operand
will be converted to an object according to the language's rules for
literals:
>>> Object(prog, 'char', 0) - 1
Object(prog, 'int', value=-1)
The standard :class:`int() <int>`, :class:`float() <float>`, and
:class:`bool() <bool>` functions convert an object to that Python type.
Conversion to ``bool`` uses the programming language's notion of
"truthiness". Additionally, certain Python functions will automatically
coerce an object to the appropriate Python type (e.g., :func:`hex()`,
:func:`round()`, and :meth:`list subscripting <object.__getitem__>`).
Object attributes and methods are named with a trailing underscore to avoid
conflicting with structure, union, or class members. The attributes and
methods always take precedence; use :meth:`member_()` if there is a
conflict.
Objects are usually obtained directly from a :class:`Program`, but they can
be constructed manually, as well (for example, if you got a variable
address from a log file).
"""
@overload
def __init__(
self,
prog: Program,
type: Union[str, Type],
# This should use numbers.Number, but mypy doesn't support it yet; see
# python/mypy#3186. Additionally, once mypy supports recursive types,
# we can make the Mapping and Sequence item types stricter; see
# python/mypy#731.
value: Union[IntegerLike, float, bool, Mapping[str, Any], Sequence[Any]],
*,
bit_field_size: Optional[IntegerLike] = None,
) -> None:
"""
Create a value object given its type and value.
:param prog: Program to create the object in.
:param type: Type of the object.
:param value: Value of the object. See :meth:`value_()`.
:param bit_field_size: Size in bits of the object if it is a bit field.
The default is ``None``, which means the object is not a bit field.
"""
...
@overload
def __init__(self, prog: Program, *, value: Union[int, float, bool]) -> None:
"""
Create a value object from a "literal".
This is used to emulate a literal number in the source code of the
program. The type is deduced from *value* according to the language's
rules for literals.