forked from osandov/drgn
-
Notifications
You must be signed in to change notification settings - Fork 6
/
program.c
1644 lines (1476 loc) · 46.2 KB
/
program.c
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
#include "drgnpy.h"
#include "../bitops.h"
#include "../error.h"
#include "../hash_table.h"
#include "../linux_kernel.h"
#include "../log.h"
#include "../program.h"
#include "../string_builder.h"
#include "../symbol.h"
#include "../util.h"
#include "../vector.h"
DEFINE_HASH_SET_FUNCTIONS(pyobjectp_set, ptr_key_hash_pair, scalar_key_eq);
static PyObject *percent_s;
static PyObject *logger;
static PyObject *logger_log;
static void drgnpy_log_fn(struct drgn_program *prog, void *arg,
enum drgn_log_level level, const char *format,
va_list ap, struct drgn_error *err)
{
STRING_BUILDER(sb);
if (!string_builder_vappendf(&sb, format, ap))
return;
if (err && !string_builder_append_error(&sb, err))
return;
PyGILState_guard();
PyObject *ret = PyObject_CallFunction(logger_log, "iOs#",
(level + 1) * 10, percent_s,
sb.str ? sb.str : "",
(Py_ssize_t)sb.len);
if (ret)
Py_DECREF(ret);
else
PyErr_WriteUnraisable(logger_log);
}
static int get_log_level(void)
{
// We don't use getEffectiveLevel() because that doesn't take
// logging.disable() into account.
int level;
for (level = 0; level < DRGN_LOG_NONE; level++) {
_cleanup_pydecref_ PyObject *enabled =
PyObject_CallMethod(logger, "isEnabledFor", "i",
(level + 1) * 10);
if (!enabled)
return -1;
int ret = PyObject_IsTrue(enabled);
if (ret < 0)
return -1;
if (ret)
break;
}
return level;
}
// This is slightly heinous. We need to sync the Python log level with the
// libdrgn log level, but the Python log level can change at any time, and there
// is no API to be notified of this. So, we monkey patch logger._cache.clear()
// to update the log level on every live program. This only works since CPython
// commit 78c18a9b9a14 ("bpo-30962: Added caching to Logger.isEnabledFor()
// (GH-2752)") (in v3.7), though. Before that, the best we can do is sync the
// level at the time that the program is created.
#if PY_VERSION_HEX >= 0x030700a1
static int cached_log_level;
static struct pyobjectp_set programs = HASH_TABLE_INIT;
static int cache_log_level(void)
{
int level = get_log_level();
if (level < 0)
return level;
cached_log_level = level;
return 0;
}
static PyObject *LoggerCacheWrapper_clear(PyObject *self)
{
PyDict_Clear(self);
if (cache_log_level())
return NULL;
for (struct pyobjectp_set_iterator it = pyobjectp_set_first(&programs);
it.entry; it = pyobjectp_set_next(it)) {
Program *prog = (Program *)*it.entry;
drgn_program_set_log_level(&prog->prog, cached_log_level);
}
Py_RETURN_NONE;
}
static PyMethodDef LoggerCacheWrapper_methods[] = {
{"clear", (PyCFunction)LoggerCacheWrapper_clear, METH_NOARGS},
{},
};
static PyTypeObject LoggerCacheWrapper_type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_drgn._LoggerCacheWrapper",
.tp_methods = LoggerCacheWrapper_methods,
};
static int init_logger_cache_wrapper(void)
{
LoggerCacheWrapper_type.tp_base = &PyDict_Type;
if (PyType_Ready(&LoggerCacheWrapper_type))
return -1;
_cleanup_pydecref_ PyObject *cache_wrapper =
PyObject_CallFunction((PyObject *)&LoggerCacheWrapper_type,
NULL);
if (!cache_wrapper)
return -1;
if (PyObject_SetAttrString(logger, "_cache", cache_wrapper))
return -1;
return cache_log_level();
}
static int Program_init_logging(Program *prog)
{
PyObject *obj = (PyObject *)prog;
if (pyobjectp_set_insert(&programs, &obj, NULL) < 0)
return -1;
drgn_program_set_log_callback(&prog->prog, drgnpy_log_fn, NULL);
drgn_program_set_log_level(&prog->prog, cached_log_level);
return 0;
}
static void Program_deinit_logging(Program *prog)
{
PyObject *obj = (PyObject *)prog;
pyobjectp_set_delete(&programs, &obj);
}
#else
static int init_logger_cache_wrapper(void) { return 0; }
static int Program_init_logging(Program *prog)
{
int level = get_log_level();
if (level < 0)
return level;
drgn_program_set_log_callback(&prog->prog, drgnpy_log_fn, NULL);
drgn_program_set_log_level(&prog->prog, level);
return 0;
}
static void Program_deinit_logging(Program *prog) {}
#endif
int init_logging(void)
{
percent_s = PyUnicode_InternFromString("%s");
if (!percent_s)
return -1;
_cleanup_pydecref_ PyObject *logging = PyImport_ImportModule("logging");
if (!logging)
return -1;
logger = PyObject_CallMethod(logging, "getLogger", "s", "drgn");
if (!logger)
return -1;
logger_log = PyObject_GetAttrString(logger, "log");
if (!logger_log)
return -1;
return init_logger_cache_wrapper();
}
int Program_hold_object(Program *prog, PyObject *obj)
{
int ret = pyobjectp_set_insert(&prog->objects, &obj, NULL);
if (ret > 0) {
Py_INCREF(obj);
ret = 0;
} else if (ret < 0) {
PyErr_NoMemory();
}
return ret;
}
bool Program_hold_reserve(Program *prog, size_t n)
{
if (!pyobjectp_set_reserve(&prog->objects,
pyobjectp_set_size(&prog->objects) + n)) {
PyErr_NoMemory();
return false;
}
return true;
}
int Program_type_arg(Program *prog, PyObject *type_obj, bool can_be_none,
struct drgn_qualified_type *ret)
{
struct drgn_error *err;
if (PyObject_TypeCheck(type_obj, &DrgnType_type)) {
if (DrgnType_prog((DrgnType *)type_obj) != prog) {
PyErr_SetString(PyExc_ValueError,
"type is from different program");
return -1;
}
ret->type = ((DrgnType *)type_obj)->type;
ret->qualifiers = ((DrgnType *)type_obj)->qualifiers;
} else if (PyUnicode_Check(type_obj)) {
const char *name;
name = PyUnicode_AsUTF8(type_obj);
if (!name)
return -1;
err = drgn_program_find_type(&prog->prog, name, NULL, ret);
if (err) {
set_drgn_error(err);
return -1;
}
} else if (can_be_none && type_obj == Py_None) {
ret->type = NULL;
ret->qualifiers = 0;
} else {
PyErr_SetString(PyExc_TypeError,
can_be_none ?
"type must be Type, str, or None" :
"type must be Type or str");
return -1;
}
return 0;
}
static void *drgnpy_begin_blocking(struct drgn_program *prog, void *arg)
{
PyThreadState *state = PyThreadState_GetUnchecked();
if (state)
PyEval_ReleaseThread(state);
return state;
}
static void drgnpy_end_blocking(struct drgn_program *prog, void *arg, void *state)
{
if (state)
PyEval_RestoreThread(state);
}
static Program *Program_new(PyTypeObject *subtype, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = { "platform", "vmcoreinfo", NULL };
PyObject *platform_obj = NULL;
const char *vmcoreinfo = NULL;
Py_ssize_t vmcoreinfo_size;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O$z#:Program", keywords,
&platform_obj, &vmcoreinfo,
&vmcoreinfo_size))
return NULL;
struct drgn_platform *platform;
if (!platform_obj || platform_obj == Py_None) {
platform = NULL;
} else if (PyObject_TypeCheck(platform_obj, &Platform_type)) {
platform = ((Platform *)platform_obj)->platform;
} else {
PyErr_SetString(PyExc_TypeError,
"platform must be Platform or None");
return NULL;
}
_cleanup_pydecref_ PyObject *cache = PyDict_New();
if (!cache)
return NULL;
_cleanup_pydecref_ Program *prog = call_tp_alloc(Program);
if (!prog)
return NULL;
prog->cache = no_cleanup_ptr(cache);
pyobjectp_set_init(&prog->objects);
drgn_program_init(&prog->prog, platform);
drgn_program_set_blocking_callback(&prog->prog, drgnpy_begin_blocking,
drgnpy_end_blocking, NULL);
if (vmcoreinfo) {
struct drgn_error *err = drgn_program_parse_vmcoreinfo(
&prog->prog, vmcoreinfo, vmcoreinfo_size);
if (err)
return set_drgn_error(err);
}
if (Program_init_logging(prog))
return NULL;
return_ptr(prog);
}
static void Program_dealloc(Program *self)
{
Program_deinit_logging(self);
drgn_program_deinit(&self->prog);
for (struct pyobjectp_set_iterator it =
pyobjectp_set_first(&self->objects); it.entry;
it = pyobjectp_set_next(it))
Py_DECREF(*it.entry);
pyobjectp_set_deinit(&self->objects);
Py_XDECREF(self->cache);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static int Program_traverse(Program *self, visitproc visit, void *arg)
{
for (struct pyobjectp_set_iterator it =
pyobjectp_set_first(&self->objects); it.entry;
it = pyobjectp_set_next(it))
Py_VISIT(*it.entry);
Py_VISIT(self->cache);
return 0;
}
static int Program_clear(Program *self)
{
for (struct pyobjectp_set_iterator it =
pyobjectp_set_first(&self->objects); it.entry;
it = pyobjectp_set_next(it))
Py_DECREF(*it.entry);
pyobjectp_set_deinit(&self->objects);
pyobjectp_set_init(&self->objects);
Py_CLEAR(self->cache);
return 0;
}
static struct drgn_error *py_memory_read_fn(void *buf, uint64_t address,
size_t count, uint64_t offset,
void *arg, bool physical)
{
struct drgn_error *err;
PyGILState_guard();
_cleanup_pydecref_ PyObject *ret =
PyObject_CallFunction(arg, "KKKO", (unsigned long long)address,
(unsigned long long)count,
(unsigned long long)offset,
physical ? Py_True : Py_False);
if (!ret)
return drgn_error_from_python();
Py_buffer view;
if (PyObject_GetBuffer(ret, &view, PyBUF_SIMPLE) == -1)
return drgn_error_from_python();
if (view.len != count) {
PyErr_Format(PyExc_ValueError,
"memory read callback returned buffer of length %zd (expected %zu)",
view.len, count);
err = drgn_error_from_python();
goto out;
}
memcpy(buf, view.buf, count);
err = NULL;
out:
PyBuffer_Release(&view);
return err;
}
static PyObject *Program_add_memory_segment(Program *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {
"address", "size", "read_fn", "physical", NULL,
};
struct drgn_error *err;
struct index_arg address = {};
struct index_arg size = {};
PyObject *read_fn;
int physical = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&O&O|p:add_memory_segment", keywords,
index_converter, &address,
index_converter, &size, &read_fn,
&physical))
return NULL;
if (!PyCallable_Check(read_fn)) {
PyErr_SetString(PyExc_TypeError, "read_fn must be callable");
return NULL;
}
if (Program_hold_object(self, read_fn) == -1)
return NULL;
err = drgn_program_add_memory_segment(&self->prog, address.uvalue,
size.uvalue, py_memory_read_fn,
read_fn, physical);
if (err)
return set_drgn_error(err);
Py_RETURN_NONE;
}
static inline struct drgn_error *
py_type_find_fn_common(PyObject *type_obj, void *arg,
struct drgn_qualified_type *ret)
{
if (!PyObject_TypeCheck(type_obj, &DrgnType_type)) {
PyErr_SetString(PyExc_TypeError,
"type find callback must return Type or None");
return drgn_error_from_python();
}
// This check is also done in libdrgn, but we need it here because if
// the type isn't from this program, then there's no guarantee that it
// will remain valid after we decrement its reference count.
if (DrgnType_prog((DrgnType *)type_obj)
!= (Program *)PyTuple_GET_ITEM(arg, 0)) {
PyErr_SetString(PyExc_ValueError,
"type find callback returned type from wrong program");
return drgn_error_from_python();
}
ret->type = ((DrgnType *)type_obj)->type;
ret->qualifiers = ((DrgnType *)type_obj)->qualifiers;
return NULL;
}
static struct drgn_error *py_type_find_fn(uint64_t kinds, const char *name,
size_t name_len, const char *filename,
void *arg,
struct drgn_qualified_type *ret)
{
PyGILState_guard();
_cleanup_pydecref_ PyObject *name_obj =
PyUnicode_FromStringAndSize(name, name_len);
if (!name_obj)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *kinds_obj = TypeKindSet_wrap(kinds);
if (!kinds_obj)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *type_obj =
PyObject_CallFunction(PyTuple_GET_ITEM(arg, 1), "OOOs",
PyTuple_GET_ITEM(arg, 0), kinds_obj,
name_obj, filename);
if (!type_obj)
return drgn_error_from_python();
if (type_obj == Py_None)
return &drgn_not_found;
return py_type_find_fn_common(type_obj, arg, ret);
}
// Old version for add_type_finder().
static struct drgn_error *py_type_find_fn_old(uint64_t kinds, const char *name,
size_t name_len,
const char *filename, void *arg,
struct drgn_qualified_type *ret)
{
PyGILState_guard();
_cleanup_pydecref_ PyObject *name_obj =
PyUnicode_FromStringAndSize(name, name_len);
if (!name_obj)
return drgn_error_from_python();
int kind;
for_each_bit(kind, kinds) {
_cleanup_pydecref_ PyObject *
kind_obj = PyObject_CallFunction(TypeKind_class, "i",
kind);
if (!kind_obj)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *type_obj =
PyObject_CallFunction(PyTuple_GET_ITEM(arg, 1), "OOs",
kind_obj, name_obj, filename);
if (!type_obj)
return drgn_error_from_python();
if (type_obj == Py_None)
continue;
return py_type_find_fn_common(type_obj, arg, ret);
}
return &drgn_not_found;
}
static struct drgn_error *py_object_find_fn(const char *name, size_t name_len,
const char *filename,
enum drgn_find_object_flags flags,
void *arg, struct drgn_object *ret)
{
PyGILState_guard();
_cleanup_pydecref_ PyObject *name_obj =
PyUnicode_FromStringAndSize(name, name_len);
if (!name_obj)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *flags_obj =
PyObject_CallFunction(FindObjectFlags_class, "i", (int)flags);
if (!flags_obj)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *obj =
PyObject_CallFunction(arg, "OOOs",
container_of(drgn_object_program(ret), Program, prog),
name_obj, flags_obj, filename);
if (!obj)
return drgn_error_from_python();
if (obj == Py_None)
return &drgn_not_found;
if (!PyObject_TypeCheck(obj, &DrgnObject_type)) {
PyErr_SetString(PyExc_TypeError,
"object find callback must return Object or None");
return drgn_error_from_python();
}
return drgn_object_copy(ret, &((DrgnObject *)obj)->obj);
}
static struct drgn_error *
py_symbol_find_fn(const char *name, uint64_t addr,
enum drgn_find_symbol_flags flags, void *arg,
struct drgn_symbol_result_builder *builder)
{
// Fast path for SymbolIndex: don't bother converting to and from Python
// types, as this is a C finder. Use Py_TYPE and pointer comparison
// directly here to avoid needing to take the GIL for
// PyObject_TypeCheck(). SymbolIndex cannot be subclassed, so the logic
// for subclass checking is unnecessary anyway.
if (Py_TYPE(PyTuple_GET_ITEM(arg, 1)) == &SymbolIndex_type) {
SymbolIndex *ix = (SymbolIndex *)PyTuple_GET_ITEM(arg, 1);
return drgn_symbol_index_find(name, addr, flags, &ix->index, builder);
}
PyGILState_guard();
_cleanup_pydecref_ PyObject *name_obj = NULL;
if (flags & DRGN_FIND_SYMBOL_NAME) {
name_obj = PyUnicode_FromString(name);
if (!name_obj)
return drgn_error_from_python();
} else {
name_obj = Py_None;
Py_INCREF(name_obj);
}
_cleanup_pydecref_ PyObject *address_obj = NULL;
if (flags & DRGN_FIND_SYMBOL_ADDR) {
address_obj = PyLong_FromUnsignedLong(addr);
if (!address_obj)
return drgn_error_from_python();
} else {
address_obj = Py_None;
Py_INCREF(address_obj);
}
_cleanup_pydecref_ PyObject *one_obj = PyBool_FromLong(flags & DRGN_FIND_SYMBOL_ONE);
_cleanup_pydecref_ PyObject *tmp =
PyObject_CallFunction(PyTuple_GET_ITEM(arg, 1), "OOOO",
PyTuple_GET_ITEM(arg, 0), name_obj,
address_obj, one_obj);
if (!tmp)
return drgn_error_from_python();
_cleanup_pydecref_ PyObject *obj =
PySequence_Fast(tmp, "symbol finder must return a sequence");
if (!obj)
return drgn_error_from_python();
size_t len = PySequence_Fast_GET_SIZE(obj);
if (len > 1 && (flags & DRGN_FIND_SYMBOL_ONE)) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"symbol finder returned multiple elements, but one was requested");
}
for (size_t i = 0; i < len; i++) {
PyObject *item = PySequence_Fast_GET_ITEM(obj, i);
if (!PyObject_TypeCheck(item, &Symbol_type))
return drgn_error_create(DRGN_ERROR_TYPE,
"symbol finder results must be of type Symbol");
_cleanup_free_ struct drgn_symbol *sym = malloc(sizeof(*sym));
if (!sym)
return &drgn_enomem;
struct drgn_error *err = drgn_symbol_copy(sym, ((Symbol *)item)->sym);
if (err)
return err;
if (!drgn_symbol_result_builder_add(builder, sym))
return &drgn_enomem;
sym = NULL; // owned by the builder now
}
return NULL;
}
#define type_finder_arg(self, fn) \
_cleanup_pydecref_ PyObject *arg = Py_BuildValue("OO", self, fn); \
if (!arg) \
return NULL;
#define object_finder_arg(self, fn) PyObject *arg = fn;
#define symbol_finder_arg type_finder_arg
#define DEFINE_PROGRAM_FINDER_METHODS(which) \
static PyObject *Program_register_##which##_finder(Program *self, \
PyObject *args, \
PyObject *kwds) \
{ \
struct drgn_error *err; \
static char *keywords[] = {"name", "fn", "enable_index", NULL}; \
const char *name; \
PyObject *fn; \
PyObject *enable_index_obj = Py_None; \
if (!PyArg_ParseTupleAndKeywords(args, kwds, \
"sO|$O:register_" #which "_finder", \
keywords, &name, &fn, \
&enable_index_obj)) \
return NULL; \
\
if (!PyCallable_Check(fn)) { \
PyErr_SetString(PyExc_TypeError, "fn must be callable"); \
return NULL; \
} \
\
size_t enable_index; \
if (enable_index_obj == Py_None) { \
enable_index = DRGN_HANDLER_REGISTER_DONT_ENABLE; \
} else { \
_cleanup_pydecref_ PyObject *negative_one = PyLong_FromLong(-1);\
if (!negative_one) \
return NULL; \
int eq = PyObject_RichCompareBool(enable_index_obj, \
negative_one, Py_EQ); \
if (eq < 0) \
return NULL; \
if (eq) { \
enable_index = DRGN_HANDLER_REGISTER_ENABLE_LAST; \
} else { \
enable_index = PyLong_AsSize_t(enable_index_obj); \
if (enable_index == (size_t)-1 && PyErr_Occurred()) \
return NULL; \
/* \
* If the index happens to be the \
* DRGN_HANDLER_REGISTER_DONT_ENABLE sentinel \
* (SIZE_MAX - 1), set it to something else; it's \
* impossible to have this many finders anyways. \
*/ \
if (enable_index == DRGN_HANDLER_REGISTER_DONT_ENABLE) \
enable_index--; \
} \
} \
\
which##_finder_arg(self, fn) \
if (!Program_hold_reserve(self, 1)) \
return NULL; \
const struct drgn_##which##_finder_ops ops = { \
.find = py_##which##_find_fn, \
}; \
err = drgn_program_register_##which##_finder(&self->prog, name, &ops, \
arg, enable_index); \
if (err) \
return set_drgn_error(err); \
Program_hold_object(self, arg); \
Py_RETURN_NONE; \
\
} \
\
static PyObject *Program_registered_##which##_finders(Program *self) \
{ \
struct drgn_error *err; \
_cleanup_free_ const char **names = NULL; \
size_t count; \
err = drgn_program_registered_##which##_finders(&self->prog, &names, \
&count); \
if (err) \
return set_drgn_error(err); \
_cleanup_pydecref_ PyObject *res = PySet_New(NULL); \
if (!res) \
return NULL; \
for (size_t i = 0; i < count; i++) { \
_cleanup_pydecref_ PyObject *name = \
PyUnicode_FromString(names[i]); \
if (!name) \
return NULL; \
if (PySet_Add(res, name)) \
return NULL; \
} \
return_ptr(res); \
} \
\
static PyObject *Program_set_enabled_##which##_finders(Program *self, \
PyObject *args, \
PyObject *kwds) \
{ \
struct drgn_error *err; \
static char *keywords[] = {"names", NULL}; \
PyObject *names_obj; \
if (!PyArg_ParseTupleAndKeywords(args, kwds, \
"O:set_enabled_" #which "_finders", \
keywords, &names_obj)) \
return NULL; \
_cleanup_pydecref_ PyObject *names_seq = \
PySequence_Fast(names_obj, "names must be sequence"); \
if (!names_seq) \
return NULL; \
size_t count = PySequence_Fast_GET_SIZE(names_seq); \
_cleanup_free_ const char **names = \
malloc_array(count, sizeof(names[0])); \
if (!names) \
return NULL; \
for (size_t i = 0; i < count; i++) { \
names[i] = PyUnicode_AsUTF8(PySequence_Fast_GET_ITEM(names_seq, i));\
if (!names[i]) \
return NULL; \
} \
err = drgn_program_set_enabled_##which##_finders(&self->prog, names, \
count); \
if (err) \
return set_drgn_error(err); \
Py_RETURN_NONE; \
} \
\
static PyObject *Program_enabled_##which##_finders(Program *self) \
{ \
struct drgn_error *err; \
_cleanup_free_ const char **names = NULL; \
size_t count; \
err = drgn_program_enabled_##which##_finders(&self->prog, &names, \
&count); \
if (err) \
return set_drgn_error(err); \
_cleanup_pydecref_ PyObject *res = PyList_New(count); \
if (!res) \
return NULL; \
for (size_t i = 0; i < count; i++) { \
PyObject *name = PyUnicode_FromString(names[i]); \
if (!name) \
return NULL; \
PyList_SET_ITEM(res, i, name); \
} \
return_ptr(res); \
}
DEFINE_PROGRAM_FINDER_METHODS(type)
DEFINE_PROGRAM_FINDER_METHODS(object)
DEFINE_PROGRAM_FINDER_METHODS(symbol)
static PyObject *deprecated_finder_name_obj(PyObject *fn)
{
_cleanup_pydecref_ PyObject *name_attr_obj =
PyObject_GetAttrString(fn, "__name__");
if (name_attr_obj) {
return PyUnicode_FromFormat("%S_%lu", name_attr_obj, random());
} else if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
return PyUnicode_FromFormat("%lu", random());
} else {
return NULL;
}
}
static PyObject *Program_add_type_finder(Program *self, PyObject *args,
PyObject *kwds)
{
struct drgn_error *err;
static char *keywords[] = {"fn", NULL};
PyObject *fn;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:add_type_finder",
keywords, &fn))
return NULL;
if (!PyCallable_Check(fn)) {
PyErr_SetString(PyExc_TypeError, "fn must be callable");
return NULL;
}
_cleanup_pydecref_ PyObject *arg = Py_BuildValue("OO", self, fn);
if (!arg)
return NULL;
_cleanup_pydecref_ PyObject *name_obj = deprecated_finder_name_obj(fn);
if (!name_obj)
return NULL;
const char *name = PyUnicode_AsUTF8(name_obj);
if (!name)
return NULL;
if (!Program_hold_reserve(self, 1))
return NULL;
const struct drgn_type_finder_ops ops = {
.find = py_type_find_fn_old,
};
err = drgn_program_register_type_finder(&self->prog, name, &ops, arg,
0);
if (err)
return set_drgn_error(err);
Program_hold_object(self, arg);
Py_RETURN_NONE;
}
static PyObject *Program_add_object_finder(Program *self, PyObject *args,
PyObject *kwds)
{
struct drgn_error *err;
static char *keywords[] = {"fn", NULL};
PyObject *fn;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:add_object_finder",
keywords, &fn))
return NULL;
if (!PyCallable_Check(fn)) {
PyErr_SetString(PyExc_TypeError, "fn must be callable");
return NULL;
}
_cleanup_pydecref_ PyObject *name_obj = deprecated_finder_name_obj(fn);
if (!name_obj)
return NULL;
const char *name = PyUnicode_AsUTF8(name_obj);
if (!name)
return NULL;
if (!Program_hold_reserve(self, 1))
return NULL;
const struct drgn_object_finder_ops ops = {
.find = py_object_find_fn,
};
err = drgn_program_register_object_finder(&self->prog, name, &ops, fn,
0);
if (err)
return set_drgn_error(err);
Program_hold_object(self, fn);
Py_RETURN_NONE;
}
static PyObject *Program_set_core_dump(Program *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct drgn_error *err;
PATH_ARG(path, .allow_fd = true);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:set_core_dump",
keywords, path_converter, &path))
return NULL;
if (path.fd >= 0)
err = drgn_program_set_core_dump_fd(&self->prog, path.fd);
else
err = drgn_program_set_core_dump(&self->prog, path.path);
if (err)
return set_drgn_error(err);
Py_RETURN_NONE;
}
static PyObject *Program_set_kernel(Program *self)
{
struct drgn_error *err;
err = drgn_program_set_kernel(&self->prog);
if (err)
return set_drgn_error(err);
Py_RETURN_NONE;
}
static PyObject *Program_set_pid(Program *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"pid", NULL};
struct drgn_error *err;
int pid;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:set_pid", keywords,
&pid))
return NULL;
err = drgn_program_set_pid(&self->prog, pid);
if (err)
return set_drgn_error(err);
Py_RETURN_NONE;
}
DEFINE_VECTOR(path_arg_vector, struct path_arg);
static void path_arg_vector_cleanup(struct path_arg_vector *path_args)
{
vector_for_each(path_arg_vector, path_arg, path_args)
path_cleanup(path_arg);
path_arg_vector_deinit(path_args);
}
static PyObject *Program_load_debug_info(Program *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {"paths", "default", "main", NULL};
struct drgn_error *err;
PyObject *paths_obj = Py_None;
int load_default = 0;
int load_main = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Opp:load_debug_info",
keywords, &paths_obj, &load_default,
&load_main))
return NULL;
_cleanup_(path_arg_vector_cleanup)
struct path_arg_vector path_args = VECTOR_INIT;
_cleanup_free_ const char **paths = NULL;
if (paths_obj != Py_None) {
_cleanup_pydecref_ PyObject *it = PyObject_GetIter(paths_obj);
if (!it)
return NULL;
Py_ssize_t length_hint = PyObject_LengthHint(paths_obj, 1);
if (length_hint == -1)
return NULL;
if (!path_arg_vector_reserve(&path_args, length_hint)) {
PyErr_NoMemory();
return NULL;
}
for (;;) {
_cleanup_pydecref_ PyObject *item = PyIter_Next(it);
if (!item)
break;
struct path_arg *path_arg =
path_arg_vector_append_entry(&path_args);
if (!path_arg) {
PyErr_NoMemory();
return NULL;
}
memset(path_arg, 0, sizeof(*path_arg));
if (!path_converter(item, path_arg)) {
path_arg_vector_pop(&path_args);
return NULL;
}
}
if (PyErr_Occurred())
return NULL;
paths = malloc_array(path_arg_vector_size(&path_args),
sizeof(*paths));
if (!paths) {
PyErr_NoMemory();
return NULL;
}
for (size_t i = 0; i < path_arg_vector_size(&path_args); i++)
paths[i] = path_arg_vector_at(&path_args, i)->path;
}
err = drgn_program_load_debug_info(&self->prog, paths,
path_arg_vector_size(&path_args),
load_default, load_main);
if (err) {
set_drgn_error(err);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *Program_load_default_debug_info(Program *self)
{
struct drgn_error *err;
err = drgn_program_load_debug_info(&self->prog, NULL, 0, true, true);
if (err)
return set_drgn_error(err);
Py_RETURN_NONE;
}
static PyObject *Program_read(Program *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"address", "size", "physical", NULL};
struct drgn_error *err;
struct index_arg address = {};
Py_ssize_t size;
int physical = 0;
bool clear;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&n|p:read", keywords,
index_converter, &address, &size,
&physical))
return NULL;
if (size < 0) {
PyErr_SetString(PyExc_ValueError, "negative size");
return NULL;
}
_cleanup_pydecref_ PyObject *buf =
PyBytes_FromStringAndSize(NULL, size);
if (!buf)
return NULL;
clear = set_drgn_in_python();
err = drgn_program_read_memory(&self->prog, PyBytes_AS_STRING(buf),
address.uvalue, size, physical);
if (clear)
clear_drgn_in_python();
if (err)
return set_drgn_error(err);
return_ptr(buf);
}
#define METHOD_READ(x, type) \
static PyObject *Program_read_##x(Program *self, PyObject *args, \
PyObject *kwds) \
{ \
static char *keywords[] = {"address", "physical", NULL}; \
struct drgn_error *err; \
struct index_arg address = {}; \
int physical = 0; \
type tmp; \
\
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|p:read_"#x, keywords, \
index_converter, &address, &physical)) \
return NULL; \
\