File: | usr/include/python3.9/object.h |
Warning: | line 128, column 12 Access to field 'ob_type' results in a dereference of a null pointer (loaded from variable 'ob') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * ompdModule.c | |||
3 | */ | |||
4 | ||||
5 | //===----------------------------------------------------------------------===// | |||
6 | // | |||
7 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
8 | // See https://llvm.org/LICENSE.txt for license information. | |||
9 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include <Python.h> | |||
14 | #include <omp-tools.h> | |||
15 | // #include <ompd.h> | |||
16 | #include <dlfcn.h> | |||
17 | #include <errno(*__errno_location ()).h> | |||
18 | #include <pthread.h> | |||
19 | #include <stdio.h> | |||
20 | #include <stdlib.h> | |||
21 | #include <string.h> | |||
22 | ||||
23 | void *ompd_library; | |||
24 | ||||
25 | #define OMPD_WEAK_ATTR__attribute__((weak)) __attribute__((weak)) | |||
26 | ||||
27 | struct _ompd_aspace_cont { | |||
28 | int id; | |||
29 | }; | |||
30 | struct _ompd_thread_cont { | |||
31 | int id; | |||
32 | }; | |||
33 | ompd_address_space_context_t acontext = {42}; | |||
34 | ||||
35 | PyObject *pModule; | |||
36 | ||||
37 | ompd_rc_t _print(const char *str, int category); | |||
38 | ||||
39 | // NOTE: implement functions to check parameters of OMPD API functions for | |||
40 | // correctness | |||
41 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_api_version(ompd_word_t *addr) { | |||
42 | static ompd_rc_t (*my_get_api_version)(ompd_word_t *) = NULL((void*)0); | |||
43 | if (!my_get_api_version) { | |||
44 | my_get_api_version = dlsym(ompd_library, "ompd_get_api_version"); | |||
45 | if (dlerror()) { | |||
46 | return ompd_rc_error; | |||
47 | } | |||
48 | } | |||
49 | return my_get_api_version(addr); | |||
50 | } | |||
51 | ||||
52 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_version_string(const char **string) { | |||
53 | static ompd_rc_t (*my_get_version_string)(const char **) = NULL((void*)0); | |||
54 | if (!my_get_version_string) { | |||
55 | my_get_version_string = dlsym(ompd_library, "ompd_get_version_string"); | |||
56 | if (dlerror()) { | |||
57 | return ompd_rc_error; | |||
58 | } | |||
59 | } | |||
60 | return my_get_version_string(string); | |||
61 | } | |||
62 | ||||
63 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_finalize(void) { | |||
64 | static ompd_rc_t (*my_ompd_finalize)(void) = NULL((void*)0); | |||
65 | if (!my_ompd_finalize) { | |||
66 | my_ompd_finalize = dlsym(ompd_library, "ompd_finalize"); | |||
67 | if (dlerror()) { | |||
68 | return ompd_rc_error; | |||
69 | } | |||
70 | } | |||
71 | return my_ompd_finalize(); | |||
72 | } | |||
73 | ||||
74 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
75 | ompd_process_initialize(ompd_address_space_context_t *context, | |||
76 | ompd_address_space_handle_t **handle) { | |||
77 | static ompd_rc_t (*my_ompd_process_initialize)( | |||
78 | ompd_address_space_context_t *, ompd_address_space_handle_t **) = NULL((void*)0); | |||
79 | if (!my_ompd_process_initialize) { | |||
80 | my_ompd_process_initialize = dlsym(ompd_library, "ompd_process_initialize"); | |||
81 | if (dlerror()) { | |||
82 | return ompd_rc_error; | |||
83 | } | |||
84 | } | |||
85 | return my_ompd_process_initialize(context, handle); | |||
86 | } | |||
87 | ||||
88 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_omp_version( | |||
89 | ompd_address_space_handle_t *address_space, ompd_word_t *omp_version) { | |||
90 | static ompd_rc_t (*my_ompd_get_omp_version)(ompd_address_space_handle_t *, | |||
91 | ompd_word_t *) = NULL((void*)0); | |||
92 | if (!my_ompd_get_omp_version) { | |||
93 | my_ompd_get_omp_version = dlsym(ompd_library, "ompd_get_omp_version"); | |||
94 | if (dlerror()) { | |||
95 | return ompd_rc_error; | |||
96 | } | |||
97 | } | |||
98 | return my_ompd_get_omp_version(address_space, omp_version); | |||
99 | } | |||
100 | ||||
101 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_omp_version_string( | |||
102 | ompd_address_space_handle_t *address_space, const char **string) { | |||
103 | static ompd_rc_t (*my_ompd_get_omp_version_string)( | |||
104 | ompd_address_space_handle_t *, const char **) = NULL((void*)0); | |||
105 | if (!my_ompd_get_omp_version_string) { | |||
106 | my_ompd_get_omp_version_string = | |||
107 | dlsym(ompd_library, "ompd_get_omp_version_string"); | |||
108 | if (dlerror()) { | |||
109 | return ompd_rc_error; | |||
110 | } | |||
111 | } | |||
112 | return my_ompd_get_omp_version_string(address_space, string); | |||
113 | } | |||
114 | ||||
115 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_thread_handle( | |||
116 | ompd_address_space_handle_t *handle, ompd_thread_id_t kind, | |||
117 | ompd_size_t tidSize, const void *tid, ompd_thread_handle_t **threadHandle) { | |||
118 | static ompd_rc_t (*my_get_thread_handle)( | |||
119 | ompd_address_space_handle_t *, ompd_thread_id_t, ompd_size_t, | |||
120 | const void *, ompd_thread_handle_t **) = NULL((void*)0); | |||
121 | if (!my_get_thread_handle) { | |||
122 | my_get_thread_handle = dlsym(ompd_library, "ompd_get_thread_handle"); | |||
123 | if (dlerror()) { | |||
124 | return ompd_rc_error; | |||
125 | } | |||
126 | } | |||
127 | return my_get_thread_handle(handle, kind, tidSize, tid, threadHandle); | |||
128 | } | |||
129 | ||||
130 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_thread_in_parallel( | |||
131 | ompd_parallel_handle_t *parallelHandle, int threadNum, | |||
132 | ompd_thread_handle_t **threadHandle) { | |||
133 | static ompd_rc_t (*my_get_thread_in_parallel)(ompd_parallel_handle_t *, int, | |||
134 | ompd_thread_handle_t **) = NULL((void*)0); | |||
135 | if (!my_get_thread_in_parallel) { | |||
136 | my_get_thread_in_parallel = | |||
137 | dlsym(ompd_library, "ompd_get_thread_in_parallel"); | |||
138 | if (dlerror()) { | |||
139 | return ompd_rc_error; | |||
140 | } | |||
141 | } | |||
142 | return my_get_thread_in_parallel(parallelHandle, threadNum, threadHandle); | |||
143 | } | |||
144 | ||||
145 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_thread_handle_compare( | |||
146 | ompd_thread_handle_t *thread_handle1, ompd_thread_handle_t *thread_handle2, | |||
147 | int *cmp_value) { | |||
148 | static ompd_rc_t (*my_thread_handle_compare)( | |||
149 | ompd_thread_handle_t *, ompd_thread_handle_t *, int *) = NULL((void*)0); | |||
150 | if (!my_thread_handle_compare) { | |||
151 | my_thread_handle_compare = | |||
152 | dlsym(ompd_library, "ompd_thread_handle_compare"); | |||
153 | if (dlerror()) { | |||
154 | return ompd_rc_error; | |||
155 | } | |||
156 | } | |||
157 | return my_thread_handle_compare(thread_handle1, thread_handle2, cmp_value); | |||
158 | } | |||
159 | ||||
160 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
161 | ompd_get_curr_parallel_handle(ompd_thread_handle_t *threadHandle, | |||
162 | ompd_parallel_handle_t **parallelHandle) { | |||
163 | static ompd_rc_t (*my_get_current_parallel_handle)( | |||
164 | ompd_thread_handle_t *, ompd_parallel_handle_t **) = NULL((void*)0); | |||
165 | if (!my_get_current_parallel_handle) { | |||
166 | my_get_current_parallel_handle = | |||
167 | dlsym(ompd_library, "ompd_get_curr_parallel_handle"); | |||
168 | if (dlerror()) { | |||
169 | return ompd_rc_error; | |||
170 | } | |||
171 | } | |||
172 | return my_get_current_parallel_handle(threadHandle, parallelHandle); | |||
173 | } | |||
174 | ||||
175 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_parallel_handle_compare( | |||
176 | ompd_parallel_handle_t *parallel_handle_1, | |||
177 | ompd_parallel_handle_t *parallel_handle_2, int *cmp_value) { | |||
178 | static ompd_rc_t (*my_parallel_handle_compare)( | |||
179 | ompd_parallel_handle_t *, ompd_parallel_handle_t *, int *) = NULL((void*)0); | |||
180 | if (!my_parallel_handle_compare) { | |||
181 | my_parallel_handle_compare = | |||
182 | dlsym(ompd_library, "ompd_parallel_handle_compare"); | |||
183 | if (dlerror()) { | |||
184 | return ompd_rc_error; | |||
185 | } | |||
186 | } | |||
187 | return my_parallel_handle_compare(parallel_handle_1, parallel_handle_2, | |||
188 | cmp_value); | |||
189 | } | |||
190 | ||||
191 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
192 | ompd_get_enclosing_parallel_handle(ompd_parallel_handle_t *parallelHandle, | |||
193 | ompd_parallel_handle_t **enclosing) { | |||
194 | static ompd_rc_t (*my_get_enclosing_parallel_handle)( | |||
195 | ompd_parallel_handle_t *, ompd_parallel_handle_t **) = NULL((void*)0); | |||
196 | if (!my_get_enclosing_parallel_handle) { | |||
197 | my_get_enclosing_parallel_handle = | |||
198 | dlsym(ompd_library, "ompd_get_enclosing_parallel_handle"); | |||
199 | if (dlerror()) { | |||
200 | return ompd_rc_error; | |||
201 | } | |||
202 | } | |||
203 | return my_get_enclosing_parallel_handle(parallelHandle, enclosing); | |||
204 | } | |||
205 | ||||
206 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
207 | ompd_get_task_parallel_handle(ompd_task_handle_t *taskHandle, | |||
208 | ompd_parallel_handle_t **taskParallelHandle) { | |||
209 | static ompd_rc_t (*my_get_task_parallel_handle)( | |||
210 | ompd_task_handle_t *, ompd_parallel_handle_t **) = NULL((void*)0); | |||
211 | if (!my_get_task_parallel_handle) { | |||
212 | my_get_task_parallel_handle = | |||
213 | dlsym(ompd_library, "ompd_get_task_parallel_handle"); | |||
214 | if (dlerror()) { | |||
215 | return ompd_rc_error; | |||
216 | } | |||
217 | } | |||
218 | return my_get_task_parallel_handle(taskHandle, taskParallelHandle); | |||
219 | } | |||
220 | ||||
221 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_curr_task_handle( | |||
222 | ompd_thread_handle_t *threadHandle, ompd_task_handle_t **taskHandle) { | |||
223 | static ompd_rc_t (*my_get_current_task_handle)(ompd_thread_handle_t *, | |||
224 | ompd_task_handle_t **) = NULL((void*)0); | |||
225 | if (!my_get_current_task_handle) { | |||
226 | my_get_current_task_handle = | |||
227 | dlsym(ompd_library, "ompd_get_curr_task_handle"); | |||
228 | if (dlerror()) { | |||
229 | return ompd_rc_error; | |||
230 | } | |||
231 | } | |||
232 | return my_get_current_task_handle(threadHandle, taskHandle); | |||
233 | } | |||
234 | ||||
235 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_generating_task_handle( | |||
236 | ompd_task_handle_t *taskHandle, ompd_task_handle_t **generating) { | |||
237 | static ompd_rc_t (*my_get_generating_task_handle)( | |||
238 | ompd_task_handle_t *, ompd_task_handle_t **) = NULL((void*)0); | |||
239 | if (!my_get_generating_task_handle) { | |||
240 | my_get_generating_task_handle = | |||
241 | dlsym(ompd_library, "ompd_get_generating_task_handle"); | |||
242 | if (dlerror()) { | |||
243 | return ompd_rc_error; | |||
244 | } | |||
245 | } | |||
246 | return my_get_generating_task_handle(taskHandle, generating); | |||
247 | } | |||
248 | ||||
249 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_scheduling_task_handle( | |||
250 | ompd_task_handle_t *taskHandle, ompd_task_handle_t **scheduling) { | |||
251 | static ompd_rc_t (*my_get_scheduling_task_handle)( | |||
252 | ompd_task_handle_t *, ompd_task_handle_t **) = NULL((void*)0); | |||
253 | if (!my_get_scheduling_task_handle) { | |||
254 | my_get_scheduling_task_handle = | |||
255 | dlsym(ompd_library, "ompd_get_scheduling_task_handle"); | |||
256 | if (dlerror()) { | |||
257 | return ompd_rc_error; | |||
258 | } | |||
259 | } | |||
260 | return my_get_scheduling_task_handle(taskHandle, scheduling); | |||
261 | } | |||
262 | ||||
263 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
264 | ompd_get_task_in_parallel(ompd_parallel_handle_t *parallelHandle, int threadNum, | |||
265 | ompd_task_handle_t **taskHandle) { | |||
266 | static ompd_rc_t (*my_get_task_in_parallel)(ompd_parallel_handle_t *, int, | |||
267 | ompd_task_handle_t **) = NULL((void*)0); | |||
268 | if (!my_get_task_in_parallel) { | |||
269 | my_get_task_in_parallel = dlsym(ompd_library, "ompd_get_task_in_parallel"); | |||
270 | if (dlerror()) { | |||
271 | return ompd_rc_error; | |||
272 | } | |||
273 | } | |||
274 | return my_get_task_in_parallel(parallelHandle, threadNum, taskHandle); | |||
275 | } | |||
276 | ||||
277 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_task_frame(ompd_task_handle_t *taskHandle, | |||
278 | ompd_frame_info_t *exitFrame, | |||
279 | ompd_frame_info_t *enterFrame) { | |||
280 | static ompd_rc_t (*my_get_task_frame)( | |||
281 | ompd_task_handle_t *, ompd_frame_info_t *, ompd_frame_info_t *) = NULL((void*)0); | |||
282 | if (!my_get_task_frame) { | |||
283 | my_get_task_frame = dlsym(ompd_library, "ompd_get_task_frame"); | |||
284 | if (dlerror()) { | |||
285 | return ompd_rc_error; | |||
286 | } | |||
287 | } | |||
288 | return my_get_task_frame(taskHandle, exitFrame, enterFrame); | |||
289 | } | |||
290 | ||||
291 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_icv_from_scope(void *handle, | |||
292 | ompd_scope_t scope, | |||
293 | ompd_icv_id_t icvId, | |||
294 | ompd_word_t *icvValue) { | |||
295 | static ompd_rc_t (*my_get_icv_from_scope)(void *, ompd_scope_t, ompd_icv_id_t, | |||
296 | ompd_word_t *) = NULL((void*)0); | |||
297 | if (!my_get_icv_from_scope) { | |||
298 | my_get_icv_from_scope = dlsym(ompd_library, "ompd_get_icv_from_scope"); | |||
299 | if (dlerror()) { | |||
300 | return ompd_rc_error; | |||
301 | } | |||
302 | } | |||
303 | return my_get_icv_from_scope(handle, scope, icvId, icvValue); | |||
304 | } | |||
305 | ||||
306 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
307 | ompd_enumerate_icvs(ompd_address_space_handle_t *handle, ompd_icv_id_t current, | |||
308 | ompd_icv_id_t *next, const char **nextIcvName, | |||
309 | ompd_scope_t *nextScope, int *more) { | |||
310 | static ompd_rc_t (*my_enumerate_icvs)( | |||
311 | ompd_address_space_handle_t *, ompd_icv_id_t, ompd_icv_id_t *, | |||
312 | const char **, ompd_scope_t *, int *) = NULL((void*)0); | |||
313 | if (!my_enumerate_icvs) { | |||
314 | my_enumerate_icvs = dlsym(ompd_library, "ompd_enumerate_icvs"); | |||
315 | if (dlerror()) { | |||
316 | return ompd_rc_error; | |||
317 | } | |||
318 | } | |||
319 | return my_enumerate_icvs(handle, current, next, nextIcvName, nextScope, more); | |||
320 | } | |||
321 | ||||
322 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
323 | ompd_enumerate_states(ompd_address_space_handle_t *addrSpaceHandle, | |||
324 | ompd_word_t currentState, ompd_word_t *nextState, | |||
325 | const char **nextStateName, ompd_word_t *moreEnums) { | |||
326 | static ompd_rc_t (*my_enumerate_states)(ompd_address_space_handle_t *, | |||
327 | ompd_word_t, ompd_word_t *, | |||
328 | const char **, ompd_word_t *) = NULL((void*)0); | |||
329 | if (!my_enumerate_states) { | |||
330 | my_enumerate_states = dlsym(ompd_library, "ompd_enumerate_states"); | |||
331 | if (dlerror()) { | |||
332 | return ompd_rc_error; | |||
333 | } | |||
334 | } | |||
335 | return my_enumerate_states(addrSpaceHandle, currentState, nextState, | |||
336 | nextStateName, moreEnums); | |||
337 | } | |||
338 | ||||
339 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_state(ompd_thread_handle_t *threadHandle, | |||
340 | ompd_word_t *state, | |||
341 | ompd_wait_id_t *waitId) { | |||
342 | static ompd_rc_t (*my_get_state)(ompd_thread_handle_t *, ompd_word_t *, | |||
343 | ompd_wait_id_t *) = NULL((void*)0); | |||
344 | if (!my_get_state) { | |||
345 | my_get_state = dlsym(ompd_library, "ompd_get_state"); | |||
346 | if (dlerror()) { | |||
347 | return ompd_rc_error; | |||
348 | } | |||
349 | } | |||
350 | return my_get_state(threadHandle, state, waitId); | |||
351 | } | |||
352 | ||||
353 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_task_function(ompd_task_handle_t *taskHandle, | |||
354 | ompd_address_t *entryPoint) { | |||
355 | static ompd_rc_t (*my_get_task_function)(ompd_task_handle_t *, | |||
356 | ompd_address_t *) = NULL((void*)0); | |||
357 | if (!my_get_task_function) { | |||
358 | my_get_task_function = dlsym(ompd_library, "ompd_get_task_function"); | |||
359 | if (dlerror()) { | |||
360 | return ompd_rc_error; | |||
361 | } | |||
362 | } | |||
363 | return my_get_task_function(taskHandle, entryPoint); | |||
364 | } | |||
365 | ||||
366 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_thread_id(ompd_thread_handle_t *threadHandle, | |||
367 | ompd_thread_id_t kind, | |||
368 | ompd_size_t tidSize, void *tid) { | |||
369 | static ompd_rc_t (*my_get_thread_id)(ompd_thread_handle_t *, ompd_thread_id_t, | |||
370 | ompd_size_t, void *) = NULL((void*)0); | |||
371 | if (!my_get_thread_id) { | |||
372 | my_get_thread_id = dlsym(ompd_library, "ompd_get_thread_id"); | |||
373 | if (dlerror()) { | |||
374 | return ompd_rc_error; | |||
375 | } | |||
376 | } | |||
377 | return my_get_thread_id(threadHandle, kind, tidSize, tid); | |||
378 | } | |||
379 | ||||
380 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_get_tool_data(void *handle, ompd_scope_t scope, | |||
381 | ompd_word_t *value, | |||
382 | ompd_address_t *ptr) { | |||
383 | static ompd_rc_t (*my_get_tool_data)(void *, ompd_scope_t, ompd_word_t *, | |||
384 | ompd_address_t *) = NULL((void*)0); | |||
385 | if (!my_get_tool_data) { | |||
386 | my_get_tool_data = dlsym(ompd_library, "ompd_get_tool_data"); | |||
387 | if (dlerror()) { | |||
388 | return ompd_rc_error; | |||
389 | } | |||
390 | } | |||
391 | return my_get_tool_data(handle, scope, value, ptr); | |||
392 | } | |||
393 | ||||
394 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
395 | ompd_get_icv_string_from_scope(void *handle, ompd_scope_t scope, | |||
396 | ompd_icv_id_t icvId, const char **icvString) { | |||
397 | static ompd_rc_t (*my_get_icv_string_from_scope)( | |||
398 | void *, ompd_scope_t, ompd_icv_id_t, const char **) = NULL((void*)0); | |||
399 | if (!my_get_icv_string_from_scope) { | |||
400 | my_get_icv_string_from_scope = | |||
401 | dlsym(ompd_library, "ompd_get_icv_string_from_scope"); | |||
402 | if (dlerror()) { | |||
403 | return ompd_rc_error; | |||
404 | } | |||
405 | } | |||
406 | return my_get_icv_string_from_scope(handle, scope, icvId, icvString); | |||
407 | } | |||
408 | ||||
409 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
410 | ompd_rel_thread_handle(ompd_thread_handle_t *threadHandle) { | |||
411 | static ompd_rc_t (*my_release_thread_handle)(ompd_thread_handle_t *) = NULL((void*)0); | |||
412 | if (!my_release_thread_handle) { | |||
413 | my_release_thread_handle = dlsym(ompd_library, "ompd_rel_thread_handle"); | |||
414 | if (dlerror()) { | |||
415 | return ompd_rc_error; | |||
416 | } | |||
417 | } | |||
418 | return my_release_thread_handle(threadHandle); | |||
419 | } | |||
420 | ||||
421 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
422 | ompd_rel_parallel_handle(ompd_parallel_handle_t *parallelHandle) { | |||
423 | static ompd_rc_t (*my_release_parallel_handle)(ompd_parallel_handle_t *) = | |||
424 | NULL((void*)0); | |||
425 | if (!my_release_parallel_handle) { | |||
426 | my_release_parallel_handle = | |||
427 | dlsym(ompd_library, "ompd_rel_parallel_handle"); | |||
428 | if (dlerror()) { | |||
429 | return ompd_rc_error; | |||
430 | } | |||
431 | } | |||
432 | return my_release_parallel_handle(parallelHandle); | |||
433 | } | |||
434 | ||||
435 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t ompd_rel_task_handle(ompd_task_handle_t *taskHandle) { | |||
436 | static ompd_rc_t (*my_release_task_handle)(ompd_task_handle_t *) = NULL((void*)0); | |||
437 | if (!my_release_task_handle) { | |||
438 | my_release_task_handle = dlsym(ompd_library, "ompd_rel_task_handle"); | |||
439 | if (dlerror()) { | |||
440 | return ompd_rc_error; | |||
441 | } | |||
442 | } | |||
443 | return my_release_task_handle(taskHandle); | |||
444 | } | |||
445 | ||||
446 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
447 | ompd_task_handle_compare(ompd_task_handle_t *task_handle_1, | |||
448 | ompd_task_handle_t *task_handle_2, int *cmp_value) { | |||
449 | static ompd_rc_t (*my_task_handle_compare)( | |||
450 | ompd_task_handle_t *, ompd_task_handle_t *, int *) = NULL((void*)0); | |||
451 | if (!my_task_handle_compare) { | |||
452 | my_task_handle_compare = dlsym(ompd_library, "ompd_task_handle_compare"); | |||
453 | if (dlerror()) { | |||
454 | return ompd_rc_error; | |||
455 | } | |||
456 | } | |||
457 | return my_task_handle_compare(task_handle_1, task_handle_2, cmp_value); | |||
458 | } | |||
459 | ||||
460 | OMPD_WEAK_ATTR__attribute__((weak)) ompd_rc_t | |||
461 | ompd_get_display_control_vars(ompd_address_space_handle_t *address_space_handle, | |||
462 | const char *const **control_vars) { | |||
463 | static ompd_rc_t (*my_ompd_get_display_control_vars)( | |||
464 | ompd_address_space_handle_t *, const char *const **) = NULL((void*)0); | |||
465 | if (!my_ompd_get_display_control_vars) { | |||
466 | my_ompd_get_display_control_vars = | |||
467 | dlsym(ompd_library, "ompd_get_display_control_vars"); | |||
468 | if (dlerror()) { | |||
469 | return ompd_rc_error; | |||
470 | } | |||
471 | } | |||
472 | return my_ompd_get_display_control_vars(address_space_handle, control_vars); | |||
473 | } | |||
474 | ||||
475 | /** | |||
476 | * Loads the OMPD library (libompd.so). Returns an integer with the version if | |||
477 | * the OMPD library could be loaded successfully. Error codes: -1: argument | |||
478 | * could not be converted to string -2: error when calling dlopen -3: error when | |||
479 | * fetching version of OMPD API else: see ompd return codes | |||
480 | */ | |||
481 | static PyObject *ompd_open(PyObject *self, PyObject *args) { | |||
482 | const char *name, *dlerr; | |||
483 | dlerror(); | |||
484 | if (!PyArg_ParseTuple(args, "s", &name)) { | |||
485 | return Py_BuildValue("i", -1); | |||
486 | } | |||
487 | ompd_library = dlopen(name, RTLD_LAZY0x00001); | |||
488 | if ((dlerr = dlerror())) { | |||
489 | return Py_BuildValue("i", -2); | |||
490 | } | |||
491 | if (dlerror()) { | |||
492 | return Py_BuildValue("i", -3); | |||
493 | } | |||
494 | ompd_word_t version; | |||
495 | ompd_rc_t rc = ompd_get_api_version(&version); | |||
496 | if (rc != ompd_rc_ok) | |||
497 | return Py_BuildValue("l", -10 - rc); | |||
498 | ||||
499 | int returnValue = version; | |||
500 | return Py_BuildValue("i", returnValue); | |||
501 | } | |||
502 | ||||
503 | /** | |||
504 | * Have the debugger print a string. | |||
505 | */ | |||
506 | ompd_rc_t _print(const char *str, int category) { | |||
507 | PyObject *pFunc = PyObject_GetAttrString(pModule, "_print"); | |||
508 | if (pFunc && PyCallable_Check(pFunc)) { | |||
509 | PyObject *pArgs = PyTuple_New(1); | |||
510 | PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", str)); | |||
511 | PyObject_CallObject(pFunc, pArgs); | |||
512 | Py_XDECREF(pArgs)_Py_XDECREF(((PyObject*)(pArgs))); | |||
513 | } | |||
514 | Py_XDECREF(pFunc)_Py_XDECREF(((PyObject*)(pFunc))); | |||
515 | return ompd_rc_ok; | |||
516 | } | |||
517 | ||||
518 | void _printf(const char *format, ...) { | |||
519 | va_list args; | |||
520 | va_start(args, format)__builtin_va_start(args, format); | |||
521 | char output[1024]; | |||
522 | vsnprintf(output, 1024, format, args); | |||
523 | va_end(args)__builtin_va_end(args); | |||
524 | _print(output, 0); | |||
525 | } | |||
526 | ||||
527 | /** | |||
528 | * Capsule destructors for thread, parallel and task handles. | |||
529 | */ | |||
530 | static void call_ompd_rel_thread_handle_temp(PyObject *capsule) { | |||
531 | ompd_thread_handle_t *threadHandle = | |||
532 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(capsule, "ThreadHandle")); | |||
533 | ||||
534 | ompd_rc_t retVal = ompd_rel_thread_handle(threadHandle); | |||
535 | if (retVal != ompd_rc_ok) { | |||
536 | _printf( | |||
537 | "An error occurred when calling ompd_rel_thread_handle! Error code: %d", | |||
538 | retVal); | |||
539 | } | |||
540 | } | |||
541 | ||||
542 | static void destroyThreadCapsule(PyObject *capsule) { | |||
543 | call_ompd_rel_thread_handle_temp(capsule); | |||
544 | } | |||
545 | static void (*my_thread_capsule_destructor)(PyObject *) = destroyThreadCapsule; | |||
546 | ||||
547 | static void call_ompd_rel_parallel_handle_temp(PyObject *capsule) { | |||
548 | ompd_parallel_handle_t *parallelHandle = | |||
549 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(capsule, | |||
550 | "ParallelHandle")); | |||
551 | ||||
552 | ompd_rc_t retVal = ompd_rel_parallel_handle(parallelHandle); | |||
553 | if (retVal != ompd_rc_ok) { | |||
554 | _printf("An error occurred when calling ompd_rel_parallel_handle! Error " | |||
555 | "code: %d", | |||
556 | retVal); | |||
557 | } | |||
558 | } | |||
559 | ||||
560 | static void destroyParallelCapsule(PyObject *capsule) { | |||
561 | call_ompd_rel_parallel_handle_temp(capsule); | |||
562 | } | |||
563 | static void (*my_parallel_capsule_destructor)(PyObject *) = | |||
564 | destroyParallelCapsule; | |||
565 | ||||
566 | static void call_ompd_rel_task_handle_temp(PyObject *capsule) { | |||
567 | ompd_task_handle_t *taskHandle = | |||
568 | (ompd_task_handle_t *)(PyCapsule_GetPointer(capsule, "TaskHandle")); | |||
569 | ||||
570 | ompd_rc_t retVal = ompd_rel_task_handle(taskHandle); | |||
571 | if (retVal != ompd_rc_ok) { | |||
572 | _printf("An error occurred when calling ompd_rel_task_handle!\n"); | |||
573 | } | |||
574 | } | |||
575 | ||||
576 | static void destroyTaskCapsule(PyObject *capsule) { | |||
577 | call_ompd_rel_task_handle_temp(capsule); | |||
578 | } | |||
579 | static void (*my_task_capsule_destructor)(PyObject *) = destroyTaskCapsule; | |||
580 | ||||
581 | /** | |||
582 | * Release thread handle. Called inside destructor for Python thread_handle | |||
583 | * object. | |||
584 | */ | |||
585 | static PyObject *call_ompd_rel_thread_handle(PyObject *self, PyObject *args) { | |||
586 | PyObject *threadHandlePy = PyTuple_GetItem(args, 0); | |||
587 | ompd_thread_handle_t *threadHandle = | |||
588 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(threadHandlePy, | |||
589 | "ThreadHandle")); | |||
590 | ||||
591 | ompd_rc_t retVal = ompd_rel_thread_handle(threadHandle); | |||
592 | if (retVal != ompd_rc_ok) { | |||
593 | _printf( | |||
594 | "An error occurred when calling ompd_rel_thread_handle! Error code: %d", | |||
595 | retVal); | |||
596 | } | |||
597 | return Py_BuildValue("l", retVal); | |||
598 | } | |||
599 | ||||
600 | /** | |||
601 | * Allocate memory in the debugger's address space. | |||
602 | */ | |||
603 | ompd_rc_t _alloc(ompd_size_t bytes, void **ptr) { | |||
604 | if (ptr == NULL((void*)0)) { | |||
605 | return ompd_rc_bad_input; | |||
606 | } | |||
607 | *ptr = malloc(bytes); | |||
608 | return ompd_rc_ok; | |||
609 | } | |||
610 | ||||
611 | /** | |||
612 | * Free memory in the debugger's address space. | |||
613 | */ | |||
614 | ompd_rc_t _free(void *ptr) { | |||
615 | free(ptr); | |||
616 | return ompd_rc_ok; | |||
617 | } | |||
618 | ||||
619 | /** | |||
620 | * Look up the sizes of primitive types in the target. | |||
621 | */ | |||
622 | ompd_rc_t _sizes(ompd_address_space_context_t *_acontext, /* IN */ | |||
623 | ompd_device_type_sizes_t *sizes) /* OUT */ | |||
624 | { | |||
625 | if (acontext.id != _acontext->id) | |||
626 | return ompd_rc_stale_handle; | |||
627 | ompd_device_type_sizes_t mysizes = { | |||
628 | (uint8_t)sizeof(char), (uint8_t)sizeof(short), | |||
629 | (uint8_t)sizeof(int), (uint8_t)sizeof(long), | |||
630 | (uint8_t)sizeof(long long), (uint8_t)sizeof(void *)}; | |||
631 | *sizes = mysizes; | |||
632 | return ompd_rc_ok; | |||
633 | } | |||
634 | ||||
635 | /** | |||
636 | * Look up the address of a global symbol in the target. | |||
637 | */ | |||
638 | ompd_rc_t _sym_addr(ompd_address_space_context_t *context, /* IN */ | |||
639 | ompd_thread_context_t *tcontext, /* IN */ | |||
640 | const char *symbol_name, /* IN */ | |||
641 | ompd_address_t *symbol_addr, /* OUT */ | |||
642 | const char *file_name) /* IN */ | |||
643 | { | |||
644 | int thread_id = -1; | |||
645 | PyObject *symbolAddress; | |||
646 | if (tcontext != NULL((void*)0)) { | |||
647 | thread_id = tcontext->id; | |||
648 | } | |||
649 | PyObject *pFunc = PyObject_GetAttrString(pModule, "_sym_addr"); | |||
650 | if (pFunc && PyCallable_Check(pFunc)) { | |||
651 | PyObject *pArgs = PyTuple_New(2); | |||
652 | PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", thread_id)); | |||
653 | PyTuple_SetItem(pArgs, 1, Py_BuildValue("s", symbol_name)); | |||
654 | symbolAddress = PyObject_CallObject(pFunc, pArgs); | |||
655 | if (symbolAddress == NULL((void*)0)) { | |||
656 | PyErr_Print(); | |||
657 | } | |||
658 | symbol_addr->address = PyLong_AsLong(symbolAddress); | |||
659 | Py_XDECREF(pArgs)_Py_XDECREF(((PyObject*)(pArgs))); | |||
660 | Py_XDECREF(symbolAddress)_Py_XDECREF(((PyObject*)(symbolAddress))); | |||
661 | } | |||
662 | Py_XDECREF(pFunc)_Py_XDECREF(((PyObject*)(pFunc))); | |||
663 | return ompd_rc_ok; | |||
664 | } | |||
665 | ||||
666 | /** | |||
667 | * Read memory from the target. | |||
668 | */ | |||
669 | ompd_rc_t _read(ompd_address_space_context_t *context, /* IN */ | |||
670 | ompd_thread_context_t *tcontext, /* IN */ | |||
671 | const ompd_address_t *addr, /* IN */ | |||
672 | ompd_size_t nbytes, /* IN */ | |||
673 | void *buffer) /* OUT */ | |||
674 | { | |||
675 | uint64_t readMem = (uint64_t)addr->address; | |||
676 | PyObject *pFunc = PyObject_GetAttrString(pModule, "_read"); | |||
677 | if (pFunc && PyCallable_Check(pFunc)) { | |||
| ||||
678 | PyObject *pArgs = PyTuple_New(2); | |||
679 | PyTuple_SetItem(pArgs, 0, Py_BuildValue("l", readMem)); | |||
680 | PyTuple_SetItem(pArgs, 1, Py_BuildValue("l", nbytes)); | |||
681 | PyObject *retArray = PyObject_CallObject(pFunc, pArgs); | |||
682 | Py_XDECREF(pArgs)_Py_XDECREF(((PyObject*)(pArgs))); | |||
683 | if (retArray == NULL((void*)0)) { | |||
684 | PyErr_Print(); | |||
685 | } | |||
686 | if (!PyByteArray_Check(retArray)(_Py_IS_TYPE(((const PyObject*)(retArray)), &PyByteArray_Type ) || PyType_IsSubtype((((PyObject*)(retArray))->ob_type), ( &PyByteArray_Type)))) { | |||
687 | return ompd_rc_error; | |||
688 | } | |||
689 | Py_ssize_t retSize = PyByteArray_Size(retArray); | |||
690 | const char *strBuf = PyByteArray_AsString(retArray); | |||
691 | if ((ompd_size_t)retSize != nbytes) { | |||
692 | return ompd_rc_error; | |||
693 | } | |||
694 | memcpy(buffer, strBuf, nbytes); | |||
695 | Py_XDECREF(retArray)_Py_XDECREF(((PyObject*)(retArray))); | |||
696 | } | |||
697 | Py_XDECREF(pFunc)_Py_XDECREF(((PyObject*)(pFunc))); | |||
698 | return ompd_rc_ok; | |||
699 | } | |||
700 | ||||
701 | /** | |||
702 | * Reads string from target. | |||
703 | */ | |||
704 | ompd_rc_t _read_string(ompd_address_space_context_t *context, /* IN */ | |||
705 | ompd_thread_context_t *tcontext, /* IN */ | |||
706 | const ompd_address_t *addr, /* IN */ | |||
707 | ompd_size_t nbytes, /* IN */ | |||
708 | void *buffer) /* OUT */ | |||
709 | { | |||
710 | ompd_rc_t retVal = ompd_rc_ok; | |||
711 | uint64_t readMem = (uint64_t)addr->address; | |||
712 | PyObject *pFunc = PyObject_GetAttrString(pModule, "_read_string"); | |||
713 | PyObject *pArgs = PyTuple_New(1); | |||
714 | PyTuple_SetItem(pArgs, 0, Py_BuildValue("l", readMem)); | |||
715 | PyObject *retString = PyObject_CallObject(pFunc, pArgs); | |||
716 | Py_XDECREF(pArgs)_Py_XDECREF(((PyObject*)(pArgs))); | |||
717 | if (!PyUnicode_Check(retString)PyType_HasFeature((((PyObject*)(retString))->ob_type), (1UL << 28))) { | |||
718 | return ompd_rc_error; | |||
719 | } | |||
720 | Py_ssize_t retSize; | |||
721 | const char *strbuffer = PyUnicode_AsUTF8AndSize(retString, &retSize); | |||
722 | if ((ompd_size_t)retSize + 1 >= nbytes) { | |||
723 | retVal = ompd_rc_incomplete; | |||
724 | } | |||
725 | strncpy(buffer, strbuffer, nbytes); | |||
726 | ((char *)buffer)[nbytes - 1] = '\0'; | |||
727 | return retVal; | |||
728 | } | |||
729 | ||||
730 | /** | |||
731 | * Write memory from the target. | |||
732 | */ | |||
733 | ompd_rc_t | |||
734 | _endianess(ompd_address_space_context_t *address_space_context, /* IN */ | |||
735 | const void *input, /* IN */ | |||
736 | ompd_size_t unit_size, /* IN */ | |||
737 | ompd_size_t count, /* IN: number of primitive type */ | |||
738 | void *output) { | |||
739 | if (acontext.id != address_space_context->id) | |||
740 | return ompd_rc_stale_handle; | |||
741 | memmove(output, input, count * unit_size); | |||
742 | return ompd_rc_ok; | |||
743 | } | |||
744 | ||||
745 | /** | |||
746 | * Returns thread context for thread id; helper function for _thread_context | |||
747 | * callback. | |||
748 | */ | |||
749 | ompd_thread_context_t *get_thread_context(int id) { | |||
750 | static ompd_thread_context_t *tc = NULL((void*)0); | |||
751 | static int size = 0; | |||
752 | int i; | |||
753 | if (id < 1) | |||
754 | return NULL((void*)0); | |||
755 | if (tc == NULL((void*)0)) { | |||
756 | size = 16; | |||
757 | tc = malloc(size * sizeof(ompd_thread_context_t)); | |||
758 | for (i = 0; i < size; i++) | |||
759 | tc[i].id = i + 1; | |||
760 | } | |||
761 | if (id - 1 >= size) { | |||
762 | size += 16; | |||
763 | tc = realloc(tc, size * sizeof(ompd_thread_context_t)); | |||
764 | for (i = 0; i < size; i++) | |||
765 | tc[i].id = i + 1; | |||
766 | } | |||
767 | return tc + id - 1; | |||
768 | } | |||
769 | ||||
770 | /** | |||
771 | * Get thread specific context. | |||
772 | */ | |||
773 | ompd_rc_t | |||
774 | _thread_context(ompd_address_space_context_t *context, /* IN */ | |||
775 | ompd_thread_id_t kind, /* IN, 0 for pthread, 1 for lwp */ | |||
776 | ompd_size_t sizeof_thread_id, /* IN */ | |||
777 | const void *thread_id, /* IN */ | |||
778 | ompd_thread_context_t **thread_context) /* OUT */ | |||
779 | { | |||
780 | if (acontext.id != context->id) | |||
781 | return ompd_rc_stale_handle; | |||
782 | if (kind != 0 && kind != 1) | |||
783 | return ompd_rc_unsupported; | |||
784 | long int tid; | |||
785 | if (sizeof(long int) >= 8 && sizeof_thread_id == 8) | |||
786 | tid = *(const uint64_t *)thread_id; | |||
787 | else if (sizeof(long int) >= 4 && sizeof_thread_id == 4) | |||
788 | tid = *(const uint32_t *)thread_id; | |||
789 | else if (sizeof(long int) >= 2 && sizeof_thread_id == 2) | |||
790 | tid = *(const uint16_t *)thread_id; | |||
791 | else | |||
792 | return ompd_rc_bad_input; | |||
793 | PyObject *pFunc = PyObject_GetAttrString(pModule, "_thread_context"); | |||
794 | if (pFunc && PyCallable_Check(pFunc)) { | |||
795 | PyObject *pArgs = PyTuple_New(2); | |||
796 | PyTuple_SetItem(pArgs, 0, Py_BuildValue("l", kind)); | |||
797 | PyTuple_SetItem(pArgs, 1, Py_BuildValue("l", tid)); | |||
798 | PyObject *res = PyObject_CallObject(pFunc, pArgs); | |||
799 | int resAsInt = (int)PyLong_AsLong(res); | |||
800 | if (resAsInt == -1) { | |||
801 | // NOTE: could not find match for thread_id | |||
802 | return ompd_rc_unavailable; | |||
803 | } | |||
804 | (*thread_context) = get_thread_context(resAsInt); | |||
805 | Py_XDECREF(pArgs)_Py_XDECREF(((PyObject*)(pArgs))); | |||
806 | Py_XDECREF(res)_Py_XDECREF(((PyObject*)(res))); | |||
807 | Py_XDECREF(pFunc)_Py_XDECREF(((PyObject*)(pFunc))); | |||
808 | if (*thread_context == NULL((void*)0)) { | |||
809 | return ompd_rc_bad_input; | |||
810 | } | |||
811 | return ompd_rc_ok; | |||
812 | } | |||
813 | Py_XDECREF(pFunc)_Py_XDECREF(((PyObject*)(pFunc))); | |||
814 | return ompd_rc_error; | |||
815 | } | |||
816 | ||||
817 | /** | |||
818 | * Calls ompd_process_initialize; returns pointer to ompd_address_space_handle. | |||
819 | */ | |||
820 | static PyObject *call_ompd_initialize(PyObject *self, PyObject *noargs) { | |||
821 | pModule = PyImport_Import(PyUnicode_FromString("ompd_callbacks")); | |||
822 | ||||
823 | static ompd_callbacks_t table = { | |||
824 | _alloc, _free, _print, _sizes, _sym_addr, _read, | |||
825 | NULL((void*)0), _read_string, _endianess, _endianess, _thread_context}; | |||
826 | ||||
827 | ompd_rc_t (*my_ompd_init)(ompd_word_t version, ompd_callbacks_t *) = | |||
828 | dlsym(ompd_library, "ompd_initialize"); | |||
829 | ompd_rc_t returnInit = my_ompd_init(201811, &table); | |||
830 | if (returnInit != ompd_rc_ok) { | |||
831 | _printf("An error occurred when calling ompd_initialize! Error code: %d", | |||
832 | returnInit); | |||
833 | } | |||
834 | ompd_address_space_handle_t *addr_space = NULL((void*)0); | |||
835 | ompd_rc_t (*my_proc_init)(ompd_address_space_context_t *, | |||
836 | ompd_address_space_handle_t **) = | |||
837 | dlsym(ompd_library, "ompd_process_initialize"); | |||
838 | ompd_rc_t retProcInit = my_proc_init(&acontext, &addr_space); | |||
839 | if (retProcInit != ompd_rc_ok) { | |||
840 | _printf("An error occurred when calling ompd_process_initialize! Error " | |||
841 | "code: %d", | |||
842 | retProcInit); | |||
843 | } | |||
844 | return PyCapsule_New(addr_space, "AddressSpace", NULL((void*)0)); | |||
845 | } | |||
846 | ||||
847 | /** | |||
848 | * Returns a PyCapsule pointer to thread handle for thread with the given id. | |||
849 | */ | |||
850 | static PyObject *get_thread_handle(PyObject *self, PyObject *args) { | |||
851 | PyObject *threadIdTup = PyTuple_GetItem(args, 0); | |||
852 | uint64_t threadId = (uint64_t)PyLong_AsLong(threadIdTup); | |||
853 | // NOTE: compiler does not know what thread handle looks like, so no memory | |||
854 | // is allocated automatically in the debugger's memory space | |||
855 | ||||
856 | PyObject *addrSpaceTup = PyTuple_GetItem(args, 1); | |||
857 | ompd_thread_handle_t *threadHandle; | |||
858 | ompd_address_space_handle_t *addrSpace = | |||
859 | (ompd_address_space_handle_t *)PyCapsule_GetPointer(addrSpaceTup, | |||
860 | "AddressSpace"); | |||
861 | ||||
862 | ompd_size_t sizeof_tid = (ompd_size_t)sizeof(uint64_t); | |||
863 | ompd_rc_t retVal = ompd_get_thread_handle(addrSpace, 1, sizeof_tid, &threadId, | |||
864 | &threadHandle); | |||
865 | ||||
866 | if (retVal == ompd_rc_unavailable) { | |||
867 | return Py_BuildValue("i", -1); | |||
868 | } else if (retVal != ompd_rc_ok) { | |||
869 | _printf( | |||
870 | "An error occured when calling ompd_get_thread_handle! Error code: %d", | |||
871 | retVal); | |||
872 | return Py_BuildValue("l", retVal); | |||
873 | } | |||
874 | return PyCapsule_New(threadHandle, "ThreadHandle", | |||
875 | my_thread_capsule_destructor); | |||
876 | } | |||
877 | ||||
878 | /** | |||
879 | * Returns a PyCapsule pointer to a thread handle for a specific thread id in | |||
880 | * the current parallel context. | |||
881 | */ | |||
882 | static PyObject *call_ompd_get_thread_in_parallel(PyObject *self, | |||
883 | PyObject *args) { | |||
884 | PyObject *parallelHandlePy = PyTuple_GetItem(args, 0); | |||
885 | int threadNum = (int)PyLong_AsLong(PyTuple_GetItem(args, 1)); | |||
886 | ompd_parallel_handle_t *parallelHandle = | |||
887 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(parallelHandlePy, | |||
888 | "ParallelHandle")); | |||
889 | ompd_thread_handle_t *threadHandle; | |||
890 | ||||
891 | ompd_rc_t retVal = | |||
892 | ompd_get_thread_in_parallel(parallelHandle, threadNum, &threadHandle); | |||
893 | ||||
894 | if (retVal != ompd_rc_ok) { | |||
895 | _printf("An error occurred when calling ompd_get_thread_in_parallel! Error " | |||
896 | "code: %d", | |||
897 | retVal); | |||
898 | return Py_BuildValue("l", retVal); | |||
899 | } | |||
900 | return PyCapsule_New(threadHandle, "ThreadHandle", | |||
901 | my_thread_capsule_destructor); | |||
902 | } | |||
903 | ||||
904 | /** | |||
905 | * Returns a PyCapsule pointer to the parallel handle of the current parallel | |||
906 | * region associated with a thread. | |||
907 | */ | |||
908 | static PyObject *call_ompd_get_curr_parallel_handle(PyObject *self, | |||
909 | PyObject *args) { | |||
910 | PyObject *threadHandlePy = PyTuple_GetItem(args, 0); | |||
911 | ompd_thread_handle_t *threadHandle = | |||
912 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(threadHandlePy, | |||
913 | "ThreadHandle")); | |||
914 | ompd_parallel_handle_t *parallelHandle; | |||
915 | ||||
916 | ompd_rc_t retVal = | |||
917 | ompd_get_curr_parallel_handle(threadHandle, ¶llelHandle); | |||
918 | ||||
919 | if (retVal != ompd_rc_ok) { | |||
920 | _printf("An error occurred when calling ompd_get_curr_parallel_handle! " | |||
921 | "Error code: %d", | |||
922 | retVal); | |||
923 | return Py_BuildValue("l", retVal); | |||
924 | } | |||
925 | return PyCapsule_New(parallelHandle, "ParallelHandle", | |||
926 | my_parallel_capsule_destructor); | |||
927 | } | |||
928 | ||||
929 | /** | |||
930 | * Returns a PyCapsule pointer to the parallel handle for the parallel region | |||
931 | * enclosing the parallel region specified by parallel_handle. | |||
932 | */ | |||
933 | static PyObject *call_ompd_get_enclosing_parallel_handle(PyObject *self, | |||
934 | PyObject *args) { | |||
935 | PyObject *parallelHandlePy = PyTuple_GetItem(args, 0); | |||
936 | ompd_parallel_handle_t *parallelHandle = | |||
937 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(parallelHandlePy, | |||
938 | "ParallelHandle")); | |||
939 | ompd_parallel_handle_t *enclosingParallelHandle; | |||
940 | ||||
941 | ompd_rc_t retVal = ompd_get_enclosing_parallel_handle( | |||
942 | parallelHandle, &enclosingParallelHandle); | |||
943 | ||||
944 | if (retVal != ompd_rc_ok) { | |||
945 | _printf("An error occurred when calling " | |||
946 | "ompd_get_enclosing_parallel_handle! Error code: %d", | |||
947 | retVal); | |||
948 | return Py_BuildValue("l", retVal); | |||
949 | } | |||
950 | return PyCapsule_New(enclosingParallelHandle, "ParallelHandle", | |||
951 | my_parallel_capsule_destructor); | |||
952 | } | |||
953 | ||||
954 | /** | |||
955 | * Returns a PyCapsule pointer to the parallel handle for the parallel region | |||
956 | * enclosing the task specified. | |||
957 | */ | |||
958 | static PyObject *call_ompd_get_task_parallel_handle(PyObject *self, | |||
959 | PyObject *args) { | |||
960 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
961 | ompd_task_handle_t *taskHandle = | |||
962 | PyCapsule_GetPointer(taskHandlePy, "TaskHandle"); | |||
963 | ompd_parallel_handle_t *taskParallelHandle; | |||
964 | ||||
965 | ompd_rc_t retVal = | |||
966 | ompd_get_task_parallel_handle(taskHandle, &taskParallelHandle); | |||
967 | ||||
968 | if (retVal != ompd_rc_ok) { | |||
969 | _printf("An error occurred when calling ompd_get_task_parallel_handle! " | |||
970 | "Error code: %d"); | |||
971 | return Py_BuildValue("l", retVal); | |||
972 | } | |||
973 | return PyCapsule_New(taskParallelHandle, "ParallelHandle", | |||
974 | my_parallel_capsule_destructor); | |||
975 | } | |||
976 | ||||
977 | /** | |||
978 | * Releases a parallel handle; is called in by the destructor of a Python | |||
979 | * parallel_handle object. | |||
980 | */ | |||
981 | static PyObject *call_ompd_rel_parallel_handle(PyObject *self, PyObject *args) { | |||
982 | PyObject *parallelHandlePy = PyTuple_GetItem(args, 0); | |||
983 | ompd_parallel_handle_t *parallelHandle = | |||
984 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(parallelHandlePy, | |||
985 | "ParallelHandle")); | |||
986 | ||||
987 | ompd_rc_t retVal = ompd_rel_parallel_handle(parallelHandle); | |||
988 | if (retVal != ompd_rc_ok) { | |||
989 | _printf("An error occurred when calling ompd_rel_parallel_handle! Error " | |||
990 | "code: %d", | |||
991 | retVal); | |||
992 | } | |||
993 | return Py_BuildValue("l", retVal); | |||
994 | } | |||
995 | ||||
996 | /** | |||
997 | * Returns a PyCapsule pointer to the task handle of the current task region | |||
998 | * associated with a thread. | |||
999 | */ | |||
1000 | static PyObject *call_ompd_get_curr_task_handle(PyObject *self, | |||
1001 | PyObject *args) { | |||
1002 | PyObject *threadHandlePy = PyTuple_GetItem(args, 0); | |||
1003 | ompd_thread_handle_t *threadHandle = | |||
1004 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(threadHandlePy, | |||
1005 | "ThreadHandle")); | |||
1006 | ompd_task_handle_t *taskHandle; | |||
1007 | ||||
1008 | ompd_rc_t retVal = ompd_get_curr_task_handle(threadHandle, &taskHandle); | |||
1009 | ||||
1010 | if (retVal != ompd_rc_ok) { | |||
1011 | _printf("An error occurred when calling ompd_get_curr_task_handle! Error " | |||
1012 | "code: %d", | |||
1013 | retVal); | |||
1014 | return Py_BuildValue("l", retVal); | |||
1015 | } | |||
1016 | return PyCapsule_New(taskHandle, "TaskHandle", my_task_capsule_destructor); | |||
1017 | } | |||
1018 | ||||
1019 | /** | |||
1020 | * Returns a task handle for the task that created the task specified. | |||
1021 | */ | |||
1022 | static PyObject *call_ompd_get_generating_task_handle(PyObject *self, | |||
1023 | PyObject *args) { | |||
1024 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
1025 | ompd_task_handle_t *taskHandle = | |||
1026 | (ompd_task_handle_t *)(PyCapsule_GetPointer(taskHandlePy, "TaskHandle")); | |||
1027 | ompd_task_handle_t *generatingTaskHandle; | |||
1028 | ||||
1029 | ompd_rc_t retVal = | |||
1030 | ompd_get_generating_task_handle(taskHandle, &generatingTaskHandle); | |||
1031 | ||||
1032 | if (retVal != ompd_rc_ok) { | |||
1033 | _printf("An error occurred when calling ompd_get_generating_task_handle! " | |||
1034 | "Error code: %d", | |||
1035 | retVal); | |||
1036 | return Py_BuildValue("l", retVal); | |||
1037 | } | |||
1038 | return PyCapsule_New(generatingTaskHandle, "TaskHandle", | |||
1039 | my_task_capsule_destructor); | |||
1040 | } | |||
1041 | ||||
1042 | /** | |||
1043 | * Returns the task handle for the task that scheduled the task specified. | |||
1044 | */ | |||
1045 | static PyObject *call_ompd_get_scheduling_task_handle(PyObject *self, | |||
1046 | PyObject *args) { | |||
1047 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
1048 | ompd_task_handle_t *taskHandle = | |||
1049 | (ompd_task_handle_t *)(PyCapsule_GetPointer(taskHandlePy, "TaskHandle")); | |||
1050 | ompd_task_handle_t *schedulingTaskHandle; | |||
1051 | ||||
1052 | ompd_rc_t retVal = | |||
1053 | ompd_get_scheduling_task_handle(taskHandle, &schedulingTaskHandle); | |||
1054 | ||||
1055 | if (retVal == ompd_rc_unavailable) { | |||
1056 | return Py_None(&_Py_NoneStruct); | |||
1057 | } else if (retVal != ompd_rc_ok) { | |||
1058 | _printf("An error occurred when calling ompd_get_scheduling_task_handle! " | |||
1059 | "Error code: %d", | |||
1060 | retVal); | |||
1061 | return Py_BuildValue("l", retVal); | |||
1062 | } | |||
1063 | return PyCapsule_New(schedulingTaskHandle, "TaskHandle", | |||
1064 | my_task_capsule_destructor); | |||
1065 | } | |||
1066 | ||||
1067 | /** | |||
1068 | * Returns task handles for the implicit tasks associated with a parallel | |||
1069 | * region. | |||
1070 | */ | |||
1071 | static PyObject *call_ompd_get_task_in_parallel(PyObject *self, | |||
1072 | PyObject *args) { | |||
1073 | PyObject *parallelHandlePy = PyTuple_GetItem(args, 0); | |||
1074 | int threadNum = (int)PyLong_AsLong(PyTuple_GetItem(args, 1)); | |||
1075 | ompd_parallel_handle_t *parallelHandle = | |||
1076 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(parallelHandlePy, | |||
1077 | "ParallelHandle")); | |||
1078 | ompd_task_handle_t *taskHandle; | |||
1079 | ||||
1080 | ompd_rc_t retVal = | |||
1081 | ompd_get_task_in_parallel(parallelHandle, threadNum, &taskHandle); | |||
1082 | ||||
1083 | if (retVal != ompd_rc_ok) { | |||
1084 | _printf("An error occurred when calling ompd_get_task_in_parallel! Error " | |||
1085 | "code: %d", | |||
1086 | retVal); | |||
1087 | return Py_BuildValue("l", retVal); | |||
1088 | } | |||
1089 | return PyCapsule_New(taskHandle, "TaskHandle", my_task_capsule_destructor); | |||
1090 | } | |||
1091 | ||||
1092 | /** | |||
1093 | * Releases a task handle; is called by the destructor of a Python task_handle | |||
1094 | * object. | |||
1095 | */ | |||
1096 | static PyObject *call_ompd_rel_task_handle(PyObject *self, PyObject *args) { | |||
1097 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
1098 | ompd_task_handle_t *taskHandle = | |||
1099 | (ompd_task_handle_t *)(PyCapsule_GetPointer(taskHandlePy, "TaskHandle")); | |||
1100 | ||||
1101 | ompd_rc_t retVal = ompd_rel_task_handle(taskHandle); | |||
1102 | if (retVal != ompd_rc_ok) { | |||
1103 | _printf( | |||
1104 | "An error occurred when calling ompd_rel_task_handle! Error code: %d", | |||
1105 | retVal); | |||
1106 | } | |||
1107 | return Py_BuildValue("l", retVal); | |||
1108 | } | |||
1109 | ||||
1110 | /** | |||
1111 | * Calls ompd_get_task_frame and returns a PyCapsule for the enter frame of the | |||
1112 | * given task. | |||
1113 | */ | |||
1114 | static PyObject *call_ompd_get_task_frame(PyObject *self, PyObject *args) { | |||
1115 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
1116 | ompd_task_handle_t *taskHandle = | |||
1117 | (ompd_task_handle_t *)PyCapsule_GetPointer(taskHandlePy, "TaskHandle"); | |||
1118 | ompd_frame_info_t exitFrameInfo; | |||
1119 | ompd_frame_info_t enterFrameInfo; | |||
1120 | ||||
1121 | ompd_rc_t retVal = | |||
1122 | ompd_get_task_frame(taskHandle, &exitFrameInfo, &enterFrameInfo); | |||
1123 | ||||
1124 | if (retVal != ompd_rc_ok) { | |||
1125 | _printf( | |||
1126 | "An error occurred when calling ompd_get_task_frame! Error code: %d", | |||
1127 | retVal); | |||
1128 | return Py_BuildValue("l", retVal); | |||
1129 | } | |||
1130 | ||||
1131 | PyObject *result = PyTuple_New(4); | |||
1132 | PyTuple_SetItem( | |||
1133 | result, 0, PyLong_FromUnsignedLong(enterFrameInfo.frame_address.address)); | |||
1134 | PyTuple_SetItem(result, 1, | |||
1135 | PyLong_FromUnsignedLong(enterFrameInfo.frame_flag)); | |||
1136 | PyTuple_SetItem(result, 2, | |||
1137 | PyLong_FromUnsignedLong(exitFrameInfo.frame_address.address)); | |||
1138 | PyTuple_SetItem(result, 3, PyLong_FromUnsignedLong(exitFrameInfo.frame_flag)); | |||
1139 | return result; | |||
1140 | } | |||
1141 | ||||
1142 | /** | |||
1143 | * Calls ompd_get_icv_from_scope. | |||
1144 | */ | |||
1145 | static PyObject *call_ompd_get_icv_from_scope(PyObject *self, PyObject *args) { | |||
1146 | PyObject *addrSpaceHandlePy = PyTuple_GetItem(args, 0); | |||
1147 | PyObject *scopePy = PyTuple_GetItem(args, 1); | |||
1148 | PyObject *icvIdPy = PyTuple_GetItem(args, 2); | |||
1149 | ||||
1150 | ompd_scope_t scope = (ompd_scope_t)PyLong_AsLong(scopePy); | |||
1151 | ompd_address_space_handle_t *addrSpaceHandle; | |||
1152 | switch (scope) { | |||
1153 | case ompd_scope_thread: | |||
1154 | addrSpaceHandle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1155 | addrSpaceHandlePy, "ThreadHandle"); | |||
1156 | break; | |||
1157 | case ompd_scope_parallel: | |||
1158 | addrSpaceHandle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1159 | addrSpaceHandlePy, "ParallelHandle"); | |||
1160 | break; | |||
1161 | case ompd_scope_implicit_task: | |||
1162 | addrSpaceHandle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1163 | addrSpaceHandlePy, "TaskHandle"); | |||
1164 | break; | |||
1165 | case ompd_scope_task: | |||
1166 | addrSpaceHandle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1167 | addrSpaceHandlePy, "TaskHandle"); | |||
1168 | break; | |||
1169 | default: | |||
1170 | addrSpaceHandle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1171 | addrSpaceHandlePy, "AddressSpace"); | |||
1172 | break; | |||
1173 | } | |||
1174 | ||||
1175 | ompd_icv_id_t icvId = (ompd_icv_id_t)PyLong_AsLong(icvIdPy); | |||
1176 | ompd_word_t icvValue; | |||
1177 | ||||
1178 | ompd_rc_t retVal = | |||
1179 | ompd_get_icv_from_scope(addrSpaceHandle, scope, icvId, &icvValue); | |||
1180 | ||||
1181 | if (retVal != ompd_rc_ok) { | |||
1182 | if (retVal != ompd_rc_incomplete) { | |||
1183 | _printf("An error occurred when calling ompd_get_icv_from_scope(%i, %i): " | |||
1184 | "Error code: %d", | |||
1185 | scope, icvId, retVal); | |||
1186 | } | |||
1187 | return Py_None(&_Py_NoneStruct); | |||
1188 | } | |||
1189 | return PyLong_FromLong(icvValue); | |||
1190 | } | |||
1191 | ||||
1192 | /** | |||
1193 | * Calls ompd_enumerate_icvs. | |||
1194 | */ | |||
1195 | static PyObject *call_ompd_enumerate_icvs(PyObject *self, PyObject *args) { | |||
1196 | PyObject *addrSpaceHandlePy = PyTuple_GetItem(args, 0); | |||
1197 | PyObject *currentPy = PyTuple_GetItem(args, 1); | |||
1198 | ompd_icv_id_t current = (ompd_icv_id_t)(PyLong_AsLong(currentPy)); | |||
1199 | ompd_address_space_handle_t *addrSpaceHandle = | |||
1200 | (ompd_address_space_handle_t *)PyCapsule_GetPointer(addrSpaceHandlePy, | |||
1201 | "AddressSpace"); | |||
1202 | ||||
1203 | const char *nextIcv; | |||
1204 | ompd_scope_t nextScope; | |||
1205 | int more; | |||
1206 | ompd_icv_id_t nextId; | |||
1207 | ||||
1208 | ompd_rc_t retVal = ompd_enumerate_icvs(addrSpaceHandle, current, &nextId, | |||
1209 | &nextIcv, &nextScope, &more); | |||
1210 | ||||
1211 | if (retVal != ompd_rc_ok) { | |||
1212 | _printf( | |||
1213 | "An error occurred when calling ompd_enumerate_icvs! Error code: %d", | |||
1214 | retVal); | |||
1215 | return Py_None(&_Py_NoneStruct); | |||
1216 | } | |||
1217 | PyObject *retTuple = PyTuple_New(4); | |||
1218 | PyTuple_SetItem(retTuple, 0, PyLong_FromUnsignedLong(nextId)); | |||
1219 | PyTuple_SetItem(retTuple, 1, PyUnicode_FromString(nextIcv)); | |||
1220 | PyTuple_SetItem(retTuple, 2, PyLong_FromUnsignedLong(nextScope)); | |||
1221 | PyTuple_SetItem(retTuple, 3, PyLong_FromLong(more)); | |||
1222 | return retTuple; | |||
1223 | } | |||
1224 | ||||
1225 | /** | |||
1226 | * Calls ompd_enumerate_states. | |||
1227 | */ | |||
1228 | static PyObject *call_ompd_enumerate_states(PyObject *self, PyObject *args) { | |||
1229 | PyObject *addrSpaceHandlePy = PyTuple_GetItem(args, 0); | |||
1230 | ompd_address_space_handle_t *addrSpaceHandle = | |||
1231 | (ompd_address_space_handle_t *)PyCapsule_GetPointer(addrSpaceHandlePy, | |||
1232 | "AddressSpace"); | |||
1233 | ompd_word_t currentState = | |||
1234 | (ompd_word_t)PyLong_AsLong(PyTuple_GetItem(args, 1)); | |||
1235 | ||||
1236 | ompd_word_t nextState; | |||
1237 | const char *nextStateName; | |||
1238 | ompd_word_t moreEnums; | |||
1239 | ||||
1240 | ompd_rc_t retVal = ompd_enumerate_states( | |||
1241 | addrSpaceHandle, currentState, &nextState, &nextStateName, &moreEnums); | |||
1242 | ||||
1243 | if (retVal != ompd_rc_ok) { | |||
1244 | _printf( | |||
1245 | "An error occurred when calling ompd_enumerate_states! Error code: %d", | |||
1246 | retVal); | |||
1247 | return Py_None(&_Py_NoneStruct); | |||
1248 | } | |||
1249 | PyObject *retTuple = PyTuple_New(3); | |||
1250 | PyTuple_SetItem(retTuple, 0, PyLong_FromLong(nextState)); | |||
1251 | PyTuple_SetItem(retTuple, 1, PyUnicode_FromString(nextStateName)); | |||
1252 | PyTuple_SetItem(retTuple, 2, PyLong_FromLong(moreEnums)); | |||
1253 | return retTuple; | |||
1254 | } | |||
1255 | ||||
1256 | /** | |||
1257 | * Calls ompd_get_state. | |||
1258 | */ | |||
1259 | static PyObject *call_ompd_get_state(PyObject *self, PyObject *args) { | |||
1260 | PyObject *threadHandlePy = PyTuple_GetItem(args, 0); | |||
1261 | ompd_thread_handle_t *threadHandle = | |||
1262 | (ompd_thread_handle_t *)PyCapsule_GetPointer(threadHandlePy, | |||
1263 | "ThreadHandle"); | |||
1264 | ompd_word_t state; | |||
1265 | ompd_wait_id_t waitId; | |||
1266 | ||||
1267 | ompd_rc_t retVal = ompd_get_state(threadHandle, &state, &waitId); | |||
1268 | ||||
1269 | if (retVal != ompd_rc_ok) { | |||
1270 | _printf("An error occurred when calling ompd_get_state! Error code: %d", | |||
1271 | retVal); | |||
1272 | return Py_None(&_Py_NoneStruct); | |||
1273 | } | |||
1274 | PyObject *retTuple = PyTuple_New(2); | |||
1275 | PyTuple_SetItem(retTuple, 0, PyLong_FromLong(state)); | |||
1276 | PyTuple_SetItem(retTuple, 1, PyLong_FromUnsignedLong(waitId)); | |||
1277 | return retTuple; | |||
1278 | } | |||
1279 | ||||
1280 | /** | |||
1281 | * Calls ompd_get_task_function and returns entry point of the code that | |||
1282 | * corresponds to the code executed by the task. | |||
1283 | */ | |||
1284 | static PyObject *call_ompd_get_task_function(PyObject *self, PyObject *args) { | |||
1285 | PyObject *taskHandlePy = PyTuple_GetItem(args, 0); | |||
1286 | ompd_task_handle_t *taskHandle = | |||
1287 | (ompd_task_handle_t *)PyCapsule_GetPointer(taskHandlePy, "TaskHandle"); | |||
1288 | ompd_address_t entryPoint; | |||
1289 | ||||
1290 | ompd_rc_t retVal = ompd_get_task_function(taskHandle, &entryPoint); | |||
1291 | ||||
1292 | if (retVal != ompd_rc_ok) { | |||
1293 | _printf( | |||
1294 | "An error occurred when calling ompd_get_task_function! Error code: %d", | |||
1295 | retVal); | |||
1296 | return Py_None(&_Py_NoneStruct); | |||
1297 | } | |||
1298 | return PyLong_FromLong((long)entryPoint.address); | |||
1299 | } | |||
1300 | ||||
1301 | /** | |||
1302 | * Prints pointer stored inside PyCapusle. | |||
1303 | */ | |||
1304 | static PyObject *print_capsule(PyObject *self, PyObject *args) { | |||
1305 | PyObject *capsule = PyTuple_GetItem(args, 0); | |||
1306 | PyObject *name = PyTuple_GetItem(args, 1); | |||
1307 | void *pointer = | |||
1308 | PyCapsule_GetPointer(capsule, PyUnicode_AsUTF8AndSize(name, NULL((void*)0))); | |||
1309 | _printf("Capsule pointer: %p", pointer); | |||
1310 | return Py_None(&_Py_NoneStruct); | |||
1311 | } | |||
1312 | ||||
1313 | /** | |||
1314 | * Calls ompd_get_thread_id for given handle and returns the thread id as a | |||
1315 | * long. | |||
1316 | */ | |||
1317 | static PyObject *call_ompd_get_thread_id(PyObject *self, PyObject *args) { | |||
1318 | PyObject *threadHandlePy = PyTuple_GetItem(args, 0); | |||
1319 | ompd_thread_handle_t *threadHandle = | |||
1320 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(threadHandlePy, | |||
1321 | "ThreadHandle")); | |||
1322 | ompd_thread_id_t kind = 0; // OMPD_THREAD_ID_PTHREAD | |||
1323 | ompd_size_t sizeOfId = (ompd_size_t)sizeof(pthread_t); | |||
1324 | ||||
1325 | uint64_t thread; | |||
1326 | ompd_rc_t retVal = ompd_get_thread_id(threadHandle, kind, sizeOfId, &thread); | |||
1327 | ||||
1328 | if (retVal != ompd_rc_ok) { | |||
1329 | kind = 1; // OMPD_THREAD_ID_LWP | |||
1330 | retVal = ompd_get_thread_id(threadHandle, kind, sizeOfId, &thread); | |||
1331 | if (retVal != ompd_rc_ok) { | |||
1332 | _printf( | |||
1333 | "An error occurred when calling ompd_get_thread_id! Error code: %d", | |||
1334 | retVal); | |||
1335 | return Py_None(&_Py_NoneStruct); | |||
1336 | } | |||
1337 | } | |||
1338 | return PyLong_FromLong(thread); | |||
1339 | } | |||
1340 | ||||
1341 | /** | |||
1342 | * Calls ompd_get_tool_data and returns a tuple containing the value and pointer | |||
1343 | * of the ompt_data_t union for the selected scope. | |||
1344 | */ | |||
1345 | static PyObject *call_ompd_get_tool_data(PyObject *self, PyObject *args) { | |||
1346 | PyObject *scopePy = PyTuple_GetItem(args, 0); | |||
1347 | ompd_scope_t scope = (ompd_scope_t)(PyLong_AsLong(scopePy)); | |||
1348 | PyObject *handlePy = PyTuple_GetItem(args, 1); | |||
1349 | void *handle = NULL((void*)0); | |||
1350 | ||||
1351 | if (scope == 3) { | |||
1352 | ompd_thread_handle_t *threadHandle = | |||
1353 | (ompd_thread_handle_t *)(PyCapsule_GetPointer(handlePy, | |||
1354 | "ThreadHandle")); | |||
1355 | handle = threadHandle; | |||
1356 | } else if (scope == 4) { | |||
1357 | ompd_parallel_handle_t *parallelHandle = | |||
1358 | (ompd_parallel_handle_t *)(PyCapsule_GetPointer(handlePy, | |||
1359 | "ParallelHandle")); | |||
1360 | handle = parallelHandle; | |||
1361 | } else if (scope == 5 || scope == 6) { | |||
1362 | ompd_task_handle_t *taskHandle = | |||
1363 | (ompd_task_handle_t *)(PyCapsule_GetPointer(handlePy, "TaskHandle")); | |||
1364 | handle = taskHandle; | |||
1365 | } else { | |||
1366 | _printf("An error occured when calling ompd_get_tool_data! Scope type not " | |||
1367 | "supported."); | |||
1368 | return Py_None(&_Py_NoneStruct); | |||
1369 | } | |||
1370 | ||||
1371 | ompd_word_t value; | |||
1372 | ompd_address_t ptr; | |||
1373 | ||||
1374 | ompd_rc_t retVal = ompd_get_tool_data(handle, scope, &value, &ptr); | |||
1375 | ||||
1376 | if (retVal != ompd_rc_ok) { | |||
1377 | _printf("An error occured when calling ompd_get_tool_data! Error code: %d", | |||
1378 | retVal); | |||
1379 | return Py_None(&_Py_NoneStruct); | |||
1380 | } | |||
1381 | ||||
1382 | PyObject *retTuple = PyTuple_New(2); | |||
1383 | PyTuple_SetItem(retTuple, 0, PyLong_FromLong(value)); | |||
1384 | PyTuple_SetItem(retTuple, 1, PyLong_FromLong(ptr.address)); | |||
1385 | return retTuple; | |||
1386 | } | |||
1387 | ||||
1388 | /** Calls ompd_get_icv_string_from_scope. | |||
1389 | */ | |||
1390 | static PyObject *call_ompd_get_icv_string_from_scope(PyObject *self, | |||
1391 | PyObject *args) { | |||
1392 | PyObject *handlePy = PyTuple_GetItem(args, 0); | |||
1393 | PyObject *scopePy = PyTuple_GetItem(args, 1); | |||
1394 | PyObject *icvIdPy = PyTuple_GetItem(args, 2); | |||
1395 | ||||
1396 | ompd_scope_t scope = (ompd_scope_t)PyLong_AsLong(scopePy); | |||
1397 | void *handle = NULL((void*)0); | |||
1398 | switch (scope) { | |||
1399 | case ompd_scope_thread: | |||
1400 | handle = | |||
1401 | (ompd_thread_handle_t *)PyCapsule_GetPointer(handlePy, "ThreadHandle"); | |||
1402 | break; | |||
1403 | case ompd_scope_parallel: | |||
1404 | handle = (ompd_parallel_handle_t *)PyCapsule_GetPointer(handlePy, | |||
1405 | "ParallelHandle"); | |||
1406 | break; | |||
1407 | case ompd_scope_implicit_task: | |||
1408 | handle = (ompd_task_handle_t *)PyCapsule_GetPointer(handlePy, "TaskHandle"); | |||
1409 | break; | |||
1410 | case ompd_scope_task: | |||
1411 | handle = (ompd_task_handle_t *)PyCapsule_GetPointer(handlePy, "TaskHandle"); | |||
1412 | break; | |||
1413 | default: | |||
1414 | handle = (ompd_address_space_handle_t *)PyCapsule_GetPointer( | |||
1415 | handlePy, "AddressSpace"); | |||
1416 | break; | |||
1417 | } | |||
1418 | ||||
1419 | ompd_icv_id_t icvId = (ompd_icv_id_t)PyLong_AsLong(icvIdPy); | |||
1420 | const char *icvString; | |||
1421 | ||||
1422 | ompd_rc_t retVal = | |||
1423 | ompd_get_icv_string_from_scope(handle, scope, icvId, &icvString); | |||
1424 | ||||
1425 | if (retVal != ompd_rc_ok) { | |||
1426 | _printf("An error occurred when calling ompd_get_icv_string_from_scope! " | |||
1427 | "Error code: %d", | |||
1428 | retVal); | |||
1429 | return Py_None(&_Py_NoneStruct); | |||
1430 | } | |||
1431 | return PyUnicode_FromString(icvString); | |||
1432 | } | |||
1433 | ||||
1434 | // Prototypes of API test functions. | |||
1435 | PyObject *test_ompd_get_thread_handle(PyObject *self, PyObject *args); | |||
1436 | PyObject *test_ompd_get_curr_parallel_handle(PyObject *self, PyObject *args); | |||
1437 | PyObject *test_ompd_get_thread_in_parallel(PyObject *self, PyObject *args); | |||
1438 | PyObject *test_ompd_thread_handle_compare(PyObject *self, PyObject *args); | |||
1439 | PyObject *test_ompd_get_thread_id(PyObject *self, PyObject *args); | |||
1440 | PyObject *test_ompd_rel_thread_handle(PyObject *self, PyObject *args); | |||
1441 | PyObject *test_ompd_get_enclosing_parallel_handle(PyObject *self, | |||
1442 | PyObject *args); | |||
1443 | PyObject *test_ompd_parallel_handle_compare(PyObject *self, PyObject *args); | |||
1444 | PyObject *test_ompd_rel_parallel_handle(PyObject *self, PyObject *args); | |||
1445 | PyObject *test_ompd_initialize(PyObject *self, PyObject *noargs); | |||
1446 | PyObject *test_ompd_get_api_version(PyObject *self, PyObject *noargs); | |||
1447 | PyObject *test_ompd_get_version_string(PyObject *self, PyObject *noargs); | |||
1448 | PyObject *test_ompd_finalize(PyObject *self, PyObject *noargs); | |||
1449 | PyObject *test_ompd_process_initialize(PyObject *self, PyObject *noargs); | |||
1450 | PyObject *test_ompd_device_initialize(PyObject *self, PyObject *noargs); | |||
1451 | PyObject *test_ompd_rel_address_space_handle(PyObject *self, PyObject *noargs); | |||
1452 | PyObject *test_ompd_get_omp_version(PyObject *self, PyObject *args); | |||
1453 | PyObject *test_ompd_get_omp_version_string(PyObject *self, PyObject *args); | |||
1454 | PyObject *test_ompd_get_curr_task_handle(PyObject *self, PyObject *args); | |||
1455 | PyObject *test_ompd_get_task_parallel_handle(PyObject *self, PyObject *args); | |||
1456 | PyObject *test_ompd_get_generating_task_handle(PyObject *self, PyObject *args); | |||
1457 | PyObject *test_ompd_get_scheduling_task_handle(PyObject *self, PyObject *args); | |||
1458 | PyObject *test_ompd_get_task_in_parallel(PyObject *self, PyObject *args); | |||
1459 | PyObject *test_ompd_rel_task_handle(PyObject *self, PyObject *noargs); | |||
1460 | PyObject *test_ompd_task_handle_compare(PyObject *self, PyObject *args); | |||
1461 | PyObject *test_ompd_get_task_function(PyObject *self, PyObject *args); | |||
1462 | PyObject *test_ompd_get_task_frame(PyObject *self, PyObject *args); | |||
1463 | PyObject *test_ompd_get_state(PyObject *self, PyObject *args); | |||
1464 | PyObject *test_ompd_get_display_control_vars(PyObject *self, PyObject *args); | |||
1465 | PyObject *test_ompd_rel_display_control_vars(PyObject *self, PyObject *noargs); | |||
1466 | PyObject *test_ompd_enumerate_icvs(PyObject *self, PyObject *noargs); | |||
1467 | PyObject *test_ompd_get_icv_from_scope_with_addr_handle(PyObject *self, | |||
1468 | PyObject *noargs); | |||
1469 | PyObject *test_ompd_get_icv_from_scope_with_thread_handle(PyObject *self, | |||
1470 | PyObject *noargs); | |||
1471 | PyObject *test_ompd_get_icv_from_scope_with_parallel_handle(PyObject *self, | |||
1472 | PyObject *noargs); | |||
1473 | PyObject *test_ompd_get_icv_from_scope_with_task_handle(PyObject *self, | |||
1474 | PyObject *noargs); | |||
1475 | PyObject *test_ompd_get_icv_string_from_scope(PyObject *self, PyObject *noargs); | |||
1476 | PyObject *test_ompd_get_tool_data(PyObject *self, PyObject *noargs); | |||
1477 | PyObject *test_ompd_enumerate_states(PyObject *self, PyObject *noargs); | |||
1478 | /** | |||
1479 | * Binds Python function names to C functions. | |||
1480 | */ | |||
1481 | static PyMethodDef ompdModule_methods[] = { | |||
1482 | {"ompd_open", ompd_open, METH_VARARGS0x0001, | |||
1483 | "Execute dlopen, return OMPD version."}, | |||
1484 | {"call_ompd_initialize", call_ompd_initialize, METH_NOARGS0x0004, | |||
1485 | "Initializes OMPD environment and callbacks."}, | |||
1486 | {"call_ompd_rel_thread_handle", call_ompd_rel_thread_handle, METH_VARARGS0x0001, | |||
1487 | "Releases a thread handle."}, | |||
1488 | {"get_thread_handle", get_thread_handle, METH_VARARGS0x0001, | |||
1489 | "Collects information on threads."}, | |||
1490 | {"call_ompd_get_thread_in_parallel", call_ompd_get_thread_in_parallel, | |||
1491 | METH_VARARGS0x0001, | |||
1492 | "Obtains handle for a certain thread within parallel region."}, | |||
1493 | {"call_ompd_get_curr_parallel_handle", call_ompd_get_curr_parallel_handle, | |||
1494 | METH_VARARGS0x0001, | |||
1495 | "Obtains a pointer to the parallel handle for the current parallel " | |||
1496 | "region."}, | |||
1497 | {"call_ompd_get_enclosing_parallel_handle", | |||
1498 | call_ompd_get_enclosing_parallel_handle, METH_VARARGS0x0001, | |||
1499 | "Obtains a pointer to the parallel handle for the parallel region " | |||
1500 | "enclosing the parallel region specified."}, | |||
1501 | {"call_ompd_get_task_parallel_handle", call_ompd_get_task_parallel_handle, | |||
1502 | METH_VARARGS0x0001, | |||
1503 | "Obtains a pointer to the parallel handle for the parallel region " | |||
1504 | "enclosing the task region specified."}, | |||
1505 | {"call_ompd_rel_parallel_handle", call_ompd_rel_parallel_handle, | |||
1506 | METH_VARARGS0x0001, "Releases a parallel region handle."}, | |||
1507 | {"call_ompd_get_curr_task_handle", call_ompd_get_curr_task_handle, | |||
1508 | METH_VARARGS0x0001, | |||
1509 | "Obtains a pointer to the task handle for the current task region " | |||
1510 | "associated with an OpenMP thread."}, | |||
1511 | {"call_ompd_get_generating_task_handle", | |||
1512 | call_ompd_get_generating_task_handle, METH_VARARGS0x0001, | |||
1513 | "Obtains a pointer to the task handle for the task that was created when " | |||
1514 | "the task handle specified was encountered."}, | |||
1515 | {"call_ompd_get_scheduling_task_handle", | |||
1516 | call_ompd_get_scheduling_task_handle, METH_VARARGS0x0001, | |||
1517 | "Obtains a pointer to the task handle for the task that scheduled the " | |||
1518 | "task specified."}, | |||
1519 | {"call_ompd_get_task_in_parallel", call_ompd_get_task_in_parallel, | |||
1520 | METH_VARARGS0x0001, | |||
1521 | "Obtains the handle for implicit tasks associated with a parallel " | |||
1522 | "region."}, | |||
1523 | {"call_ompd_rel_task_handle", call_ompd_rel_task_handle, METH_VARARGS0x0001, | |||
1524 | "Releases a task handle."}, | |||
1525 | {"call_ompd_get_task_frame", call_ompd_get_task_frame, METH_VARARGS0x0001, | |||
1526 | "Returns a pointer to the enter and exit frame address and flag of the " | |||
1527 | "given task."}, | |||
1528 | {"call_ompd_enumerate_icvs", call_ompd_enumerate_icvs, METH_VARARGS0x0001, | |||
1529 | "Saves ICVs in map."}, | |||
1530 | {"call_ompd_get_icv_from_scope", call_ompd_get_icv_from_scope, METH_VARARGS0x0001, | |||
1531 | "Gets ICVs from scope."}, | |||
1532 | {"call_ompd_enumerate_states", call_ompd_enumerate_states, METH_VARARGS0x0001, | |||
1533 | "Enumerates OMP states."}, | |||
1534 | {"call_ompd_get_state", call_ompd_get_state, METH_VARARGS0x0001, | |||
1535 | "Returns state for given thread handle."}, | |||
1536 | {"call_ompd_get_task_function", call_ompd_get_task_function, METH_VARARGS0x0001, | |||
1537 | "Returns point of code where task starts executing."}, | |||
1538 | {"print_capsule", print_capsule, METH_VARARGS0x0001, "Print capsule content"}, | |||
1539 | {"call_ompd_get_thread_id", call_ompd_get_thread_id, METH_VARARGS0x0001, | |||
1540 | "Maps an OMPD thread handle to a native thread."}, | |||
1541 | {"call_ompd_get_tool_data", call_ompd_get_tool_data, METH_VARARGS0x0001, | |||
1542 | "Returns value and pointer of ompd_data_t for given scope and handle."}, | |||
1543 | {"call_ompd_get_icv_string_from_scope", call_ompd_get_icv_string_from_scope, | |||
1544 | METH_VARARGS0x0001, "Gets ICV string representation from scope."}, | |||
1545 | ||||
1546 | {"test_ompd_get_thread_handle", test_ompd_get_thread_handle, METH_VARARGS0x0001, | |||
1547 | "Test API ompd_get_thread_handle."}, | |||
1548 | {"test_ompd_get_curr_parallel_handle", test_ompd_get_curr_parallel_handle, | |||
1549 | METH_VARARGS0x0001, "Test API test_ompd_get_curr_parallel_handle."}, | |||
1550 | {"test_ompd_get_thread_in_parallel", test_ompd_get_thread_in_parallel, | |||
1551 | METH_VARARGS0x0001, "Test API ompd_get_thread_in_parallel."}, | |||
1552 | {"test_ompd_thread_handle_compare", test_ompd_thread_handle_compare, | |||
1553 | METH_VARARGS0x0001, "Test API ompd_thread_handle_compare."}, | |||
1554 | {"test_ompd_get_thread_id", test_ompd_get_thread_id, METH_VARARGS0x0001, | |||
1555 | "Test API ompd_get_thread_id."}, | |||
1556 | {"test_ompd_rel_thread_handle", test_ompd_rel_thread_handle, METH_VARARGS0x0001, | |||
1557 | "Test API ompd_rel_thread_handle."}, | |||
1558 | {"test_ompd_get_enclosing_parallel_handle", | |||
1559 | test_ompd_get_enclosing_parallel_handle, METH_VARARGS0x0001, | |||
1560 | "Test API ompd_get_enclosing_parallel_handle."}, | |||
1561 | {"test_ompd_parallel_handle_compare", test_ompd_parallel_handle_compare, | |||
1562 | METH_VARARGS0x0001, "Test API test_ompd_parallel_handle_compare."}, | |||
1563 | {"test_ompd_rel_parallel_handle", test_ompd_rel_parallel_handle, | |||
1564 | METH_VARARGS0x0001, "Test API ompd_rel_parallel_handle."}, | |||
1565 | ||||
1566 | {"test_ompd_initialize", test_ompd_initialize, METH_VARARGS0x0001, | |||
1567 | "Test API ompd_initialize."}, | |||
1568 | {"test_ompd_get_api_version", test_ompd_get_api_version, METH_VARARGS0x0001, | |||
1569 | "Test API ompd_get_api_version."}, | |||
1570 | {"test_ompd_get_version_string", test_ompd_get_version_string, METH_VARARGS0x0001, | |||
1571 | "Test API ompd_get_version_string."}, | |||
1572 | {"test_ompd_finalize", test_ompd_finalize, METH_VARARGS0x0001, | |||
1573 | "Test API ompd_finalize."}, | |||
1574 | {"test_ompd_process_initialize", test_ompd_process_initialize, METH_VARARGS0x0001, | |||
1575 | "Test API ompd_process_initialize. "}, | |||
1576 | {"test_ompd_device_initialize", test_ompd_device_initialize, METH_VARARGS0x0001, | |||
1577 | "Test API ompd_device_initialize."}, | |||
1578 | {"test_ompd_rel_address_space_handle", test_ompd_rel_address_space_handle, | |||
1579 | METH_VARARGS0x0001, "Test API ompd_rel_address_space_handle."}, | |||
1580 | {"test_ompd_get_omp_version", test_ompd_get_omp_version, METH_VARARGS0x0001, | |||
1581 | "Test API ompd_get_omp_version."}, | |||
1582 | {"test_ompd_get_omp_version_string", test_ompd_get_omp_version_string, | |||
1583 | METH_VARARGS0x0001, "Test API ompd_get_omp_version_string."}, | |||
1584 | ||||
1585 | {"test_ompd_get_curr_task_handle", test_ompd_get_curr_task_handle, | |||
1586 | METH_VARARGS0x0001, "Test API ompd_get_curr_task_handle."}, | |||
1587 | {"test_ompd_get_task_parallel_handle", test_ompd_get_task_parallel_handle, | |||
1588 | METH_VARARGS0x0001, "Test API ompd_get_task_parallel_handle."}, | |||
1589 | {"test_ompd_get_generating_task_handle", | |||
1590 | test_ompd_get_generating_task_handle, METH_VARARGS0x0001, | |||
1591 | "Test API ompd_get_generating_task_handle."}, | |||
1592 | {"test_ompd_get_scheduling_task_handle", | |||
1593 | test_ompd_get_scheduling_task_handle, METH_VARARGS0x0001, | |||
1594 | "Test API ompd_get_scheduling_task_handle."}, | |||
1595 | {"test_ompd_get_task_in_parallel", test_ompd_get_task_in_parallel, | |||
1596 | METH_VARARGS0x0001, "Test API ompd_get_task_in_parallel."}, | |||
1597 | {"test_ompd_rel_task_handle", test_ompd_rel_task_handle, METH_VARARGS0x0001, | |||
1598 | "Test API ompd_rel_task_handle."}, | |||
1599 | {"test_ompd_task_handle_compare", test_ompd_task_handle_compare, | |||
1600 | METH_VARARGS0x0001, "Test API ompd_task_handle_compare."}, | |||
1601 | {"test_ompd_get_task_function", test_ompd_get_task_function, METH_VARARGS0x0001, | |||
1602 | "Test API ompd_get_task_function."}, | |||
1603 | {"test_ompd_get_task_frame", test_ompd_get_task_frame, METH_VARARGS0x0001, | |||
1604 | "Test API ompd_get_task_frame."}, | |||
1605 | {"test_ompd_get_state", test_ompd_get_state, METH_VARARGS0x0001, | |||
1606 | "Test API ompd_get_state."}, | |||
1607 | {"test_ompd_get_display_control_vars", test_ompd_get_display_control_vars, | |||
1608 | METH_VARARGS0x0001, "Test API ompd_get_display_control_vars."}, | |||
1609 | {"test_ompd_rel_display_control_vars", test_ompd_rel_display_control_vars, | |||
1610 | METH_VARARGS0x0001, "Test API ompd_rel_display_control_vars."}, | |||
1611 | {"test_ompd_enumerate_icvs", test_ompd_enumerate_icvs, METH_VARARGS0x0001, | |||
1612 | "Test API ompd_enumerate_icvs."}, | |||
1613 | {"test_ompd_get_icv_from_scope_with_addr_handle", | |||
1614 | test_ompd_get_icv_from_scope_with_addr_handle, METH_VARARGS0x0001, | |||
1615 | "Test API ompd_get_icv_from_scope with addr_handle."}, | |||
1616 | {"test_ompd_get_icv_from_scope_with_thread_handle", | |||
1617 | test_ompd_get_icv_from_scope_with_thread_handle, METH_VARARGS0x0001, | |||
1618 | "Test API ompd_get_icv_from_scope with thread_handle."}, | |||
1619 | {"test_ompd_get_icv_from_scope_with_parallel_handle", | |||
1620 | test_ompd_get_icv_from_scope_with_parallel_handle, METH_VARARGS0x0001, | |||
1621 | "Test API ompd_get_icv_from_scope with parallel_handle."}, | |||
1622 | {"test_ompd_get_icv_from_scope_with_task_handle", | |||
1623 | test_ompd_get_icv_from_scope_with_task_handle, METH_VARARGS0x0001, | |||
1624 | "Test API ompd_get_icv_from_scope with task_handle."}, | |||
1625 | {"test_ompd_get_icv_string_from_scope", test_ompd_get_icv_string_from_scope, | |||
1626 | METH_VARARGS0x0001, "Test API ompd_get_icv_string_from_scope."}, | |||
1627 | {"test_ompd_get_tool_data", test_ompd_get_tool_data, METH_VARARGS0x0001, | |||
1628 | "Test API ompd_get_tool_data."}, | |||
1629 | {"test_ompd_enumerate_states", test_ompd_enumerate_states, METH_VARARGS0x0001, | |||
1630 | "Test API ompd_enumerate_states."}, | |||
1631 | {NULL((void*)0), NULL((void*)0), 0, NULL((void*)0)}}; | |||
1632 | ||||
1633 | /** | |||
1634 | * Lets Python initialize module. | |||
1635 | */ | |||
1636 | #if PY_MAJOR_VERSION3 >= 3 | |||
1637 | static struct PyModuleDef moduledef = { | |||
1638 | PyModuleDef_HEAD_INIT{ { 1, ((void*)0) }, ((void*)0), 0, ((void*)0), }, | |||
1639 | "ompdModule", /* m_name */ | |||
1640 | "This is a module", /* m_doc */ | |||
1641 | -1, /* m_size */ | |||
1642 | ompdModule_methods, /* m_methods */ | |||
1643 | NULL((void*)0), /* m_reload */ | |||
1644 | NULL((void*)0), /* m_traverse */ | |||
1645 | NULL((void*)0), /* m_clear */ | |||
1646 | NULL((void*)0), /* m_free */ | |||
1647 | }; | |||
1648 | #endif | |||
1649 | void PyInit_ompdModule(void) { | |||
1650 | // (void) Py_InitModule("ompdModule", ompdModule_methods); | |||
1651 | PyModule_Create(&moduledef)PyModule_Create2(&moduledef, 1013); | |||
1652 | } |
1 | #ifndef Py_OBJECT_H | |||
2 | #define Py_OBJECT_H | |||
3 | ||||
4 | #ifdef __cplusplus | |||
5 | extern "C" { | |||
6 | #endif | |||
7 | ||||
8 | ||||
9 | /* Object and type object interface */ | |||
10 | ||||
11 | /* | |||
12 | Objects are structures allocated on the heap. Special rules apply to | |||
13 | the use of objects to ensure they are properly garbage-collected. | |||
14 | Objects are never allocated statically or on the stack; they must be | |||
15 | accessed through special macros and functions only. (Type objects are | |||
16 | exceptions to the first rule; the standard types are represented by | |||
17 | statically initialized type objects, although work on type/class unification | |||
18 | for Python 2.2 made it possible to have heap-allocated type objects too). | |||
19 | ||||
20 | An object has a 'reference count' that is increased or decreased when a | |||
21 | pointer to the object is copied or deleted; when the reference count | |||
22 | reaches zero there are no references to the object left and it can be | |||
23 | removed from the heap. | |||
24 | ||||
25 | An object has a 'type' that determines what it represents and what kind | |||
26 | of data it contains. An object's type is fixed when it is created. | |||
27 | Types themselves are represented as objects; an object contains a | |||
28 | pointer to the corresponding type object. The type itself has a type | |||
29 | pointer pointing to the object representing the type 'type', which | |||
30 | contains a pointer to itself!. | |||
31 | ||||
32 | Objects do not float around in memory; once allocated an object keeps | |||
33 | the same size and address. Objects that must hold variable-size data | |||
34 | can contain pointers to variable-size parts of the object. Not all | |||
35 | objects of the same type have the same size; but the size cannot change | |||
36 | after allocation. (These restrictions are made so a reference to an | |||
37 | object can be simply a pointer -- moving an object would require | |||
38 | updating all the pointers, and changing an object's size would require | |||
39 | moving it if there was another object right next to it.) | |||
40 | ||||
41 | Objects are always accessed through pointers of the type 'PyObject *'. | |||
42 | The type 'PyObject' is a structure that only contains the reference count | |||
43 | and the type pointer. The actual memory allocated for an object | |||
44 | contains other data that can only be accessed after casting the pointer | |||
45 | to a pointer to a longer structure type. This longer type must start | |||
46 | with the reference count and type fields; the macro PyObject_HEAD should be | |||
47 | used for this (to accommodate for future changes). The implementation | |||
48 | of a particular object type can cast the object pointer to the proper | |||
49 | type and back. | |||
50 | ||||
51 | A standard interface exists for objects that contain an array of items | |||
52 | whose size is determined when the object is allocated. | |||
53 | */ | |||
54 | ||||
55 | /* Py_DEBUG implies Py_REF_DEBUG. */ | |||
56 | #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) | |||
57 | #define Py_REF_DEBUG | |||
58 | #endif | |||
59 | ||||
60 | #if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG) | |||
61 | #error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG | |||
62 | #endif | |||
63 | ||||
64 | /* PyTypeObject structure is defined in cpython/object.h. | |||
65 | In Py_LIMITED_API, PyTypeObject is an opaque structure. */ | |||
66 | typedef struct _typeobject PyTypeObject; | |||
67 | ||||
68 | #ifdef Py_TRACE_REFS | |||
69 | /* Define pointers to support a doubly-linked list of all live heap objects. */ | |||
70 | #define _PyObject_HEAD_EXTRA \ | |||
71 | struct _object *_ob_next; \ | |||
72 | struct _object *_ob_prev; | |||
73 | ||||
74 | #define _PyObject_EXTRA_INIT 0, 0, | |||
75 | ||||
76 | #else | |||
77 | #define _PyObject_HEAD_EXTRA | |||
78 | #define _PyObject_EXTRA_INIT | |||
79 | #endif | |||
80 | ||||
81 | /* PyObject_HEAD defines the initial segment of every PyObject. */ | |||
82 | #define PyObject_HEADPyObject ob_base; PyObject ob_base; | |||
83 | ||||
84 | #define PyObject_HEAD_INIT(type){ 1, type }, \ | |||
85 | { _PyObject_EXTRA_INIT \ | |||
86 | 1, type }, | |||
87 | ||||
88 | #define PyVarObject_HEAD_INIT(type, size){ { 1, type }, size }, \ | |||
89 | { PyObject_HEAD_INIT(type){ 1, type }, size }, | |||
90 | ||||
91 | /* PyObject_VAR_HEAD defines the initial segment of all variable-size | |||
92 | * container objects. These end with a declaration of an array with 1 | |||
93 | * element, but enough space is malloc'ed so that the array actually | |||
94 | * has room for ob_size elements. Note that ob_size is an element count, | |||
95 | * not necessarily a byte count. | |||
96 | */ | |||
97 | #define PyObject_VAR_HEADPyVarObject ob_base; PyVarObject ob_base; | |||
98 | #define Py_INVALID_SIZE(Py_ssize_t)-1 (Py_ssize_t)-1 | |||
99 | ||||
100 | /* Nothing is actually declared to be a PyObject, but every pointer to | |||
101 | * a Python object can be cast to a PyObject*. This is inheritance built | |||
102 | * by hand. Similarly every pointer to a variable-size Python object can, | |||
103 | * in addition, be cast to PyVarObject*. | |||
104 | */ | |||
105 | typedef struct _object { | |||
106 | _PyObject_HEAD_EXTRA | |||
107 | Py_ssize_t ob_refcnt; | |||
108 | PyTypeObject *ob_type; | |||
109 | } PyObject; | |||
110 | ||||
111 | /* Cast argument to PyObject* type. */ | |||
112 | #define _PyObject_CAST(op)((PyObject*)(op)) ((PyObject*)(op)) | |||
113 | #define _PyObject_CAST_CONST(op)((const PyObject*)(op)) ((const PyObject*)(op)) | |||
114 | ||||
115 | typedef struct { | |||
116 | PyObject ob_base; | |||
117 | Py_ssize_t ob_size; /* Number of items in variable part */ | |||
118 | } PyVarObject; | |||
119 | ||||
120 | /* Cast argument to PyVarObject* type. */ | |||
121 | #define _PyVarObject_CAST(op)((PyVarObject*)(op)) ((PyVarObject*)(op)) | |||
122 | ||||
123 | #define Py_REFCNT(ob)(((PyObject*)(ob))->ob_refcnt) (_PyObject_CAST(ob)((PyObject*)(ob))->ob_refcnt) | |||
124 | #define Py_TYPE(ob)(((PyObject*)(ob))->ob_type) (_PyObject_CAST(ob)((PyObject*)(ob))->ob_type) | |||
125 | #define Py_SIZE(ob)(((PyVarObject*)(ob))->ob_size) (_PyVarObject_CAST(ob)((PyVarObject*)(ob))->ob_size) | |||
126 | ||||
127 | static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) { | |||
128 | return ob->ob_type == type; | |||
| ||||
129 | } | |||
130 | #define Py_IS_TYPE(ob, type)_Py_IS_TYPE(((const PyObject*)(ob)), type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob)((const PyObject*)(ob)), type) | |||
131 | ||||
132 | static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { | |||
133 | ob->ob_refcnt = refcnt; | |||
134 | } | |||
135 | #define Py_SET_REFCNT(ob, refcnt)_Py_SET_REFCNT(((PyObject*)(ob)), refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob)((PyObject*)(ob)), refcnt) | |||
136 | ||||
137 | static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { | |||
138 | ob->ob_type = type; | |||
139 | } | |||
140 | #define Py_SET_TYPE(ob, type)_Py_SET_TYPE(((PyObject*)(ob)), type) _Py_SET_TYPE(_PyObject_CAST(ob)((PyObject*)(ob)), type) | |||
141 | ||||
142 | static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { | |||
143 | ob->ob_size = size; | |||
144 | } | |||
145 | #define Py_SET_SIZE(ob, size)_Py_SET_SIZE(((PyVarObject*)(ob)), size) _Py_SET_SIZE(_PyVarObject_CAST(ob)((PyVarObject*)(ob)), size) | |||
146 | ||||
147 | ||||
148 | /* | |||
149 | Type objects contain a string containing the type name (to help somewhat | |||
150 | in debugging), the allocation parameters (see PyObject_New() and | |||
151 | PyObject_NewVar()), | |||
152 | and methods for accessing objects of the type. Methods are optional, a | |||
153 | nil pointer meaning that particular kind of access is not available for | |||
154 | this type. The Py_DECREF() macro uses the tp_dealloc method without | |||
155 | checking for a nil pointer; it should always be implemented except if | |||
156 | the implementation can guarantee that the reference count will never | |||
157 | reach zero (e.g., for statically allocated type objects). | |||
158 | ||||
159 | NB: the methods for certain type groups are now contained in separate | |||
160 | method blocks. | |||
161 | */ | |||
162 | ||||
163 | typedef PyObject * (*unaryfunc)(PyObject *); | |||
164 | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); | |||
165 | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); | |||
166 | typedef int (*inquiry)(PyObject *); | |||
167 | typedef Py_ssize_t (*lenfunc)(PyObject *); | |||
168 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); | |||
169 | typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); | |||
170 | typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); | |||
171 | typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); | |||
172 | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); | |||
173 | ||||
174 | typedef int (*objobjproc)(PyObject *, PyObject *); | |||
175 | typedef int (*visitproc)(PyObject *, void *); | |||
176 | typedef int (*traverseproc)(PyObject *, visitproc, void *); | |||
177 | ||||
178 | ||||
179 | typedef void (*freefunc)(void *); | |||
180 | typedef void (*destructor)(PyObject *); | |||
181 | typedef PyObject *(*getattrfunc)(PyObject *, char *); | |||
182 | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); | |||
183 | typedef int (*setattrfunc)(PyObject *, char *, PyObject *); | |||
184 | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); | |||
185 | typedef PyObject *(*reprfunc)(PyObject *); | |||
186 | typedef Py_hash_t (*hashfunc)(PyObject *); | |||
187 | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); | |||
188 | typedef PyObject *(*getiterfunc) (PyObject *); | |||
189 | typedef PyObject *(*iternextfunc) (PyObject *); | |||
190 | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); | |||
191 | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); | |||
192 | typedef int (*initproc)(PyObject *, PyObject *, PyObject *); | |||
193 | typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *); | |||
194 | typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t); | |||
195 | ||||
196 | typedef struct{ | |||
197 | int slot; /* slot id, see below */ | |||
198 | void *pfunc; /* function pointer */ | |||
199 | } PyType_Slot; | |||
200 | ||||
201 | typedef struct{ | |||
202 | const char* name; | |||
203 | int basicsize; | |||
204 | int itemsize; | |||
205 | unsigned int flags; | |||
206 | PyType_Slot *slots; /* terminated by slot==0. */ | |||
207 | } PyType_Spec; | |||
208 | ||||
209 | PyAPI_FUNC(PyObject*)__attribute__ ((visibility ("default"))) PyObject* PyType_FromSpec(PyType_Spec*); | |||
210 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 | |||
211 | PyAPI_FUNC(PyObject*)__attribute__ ((visibility ("default"))) PyObject* PyType_FromSpecWithBases(PyType_Spec*, PyObject*); | |||
212 | #endif | |||
213 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 | |||
214 | PyAPI_FUNC(void*)__attribute__ ((visibility ("default"))) void* PyType_GetSlot(PyTypeObject*, int); | |||
215 | #endif | |||
216 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 | |||
217 | PyAPI_FUNC(PyObject*)__attribute__ ((visibility ("default"))) PyObject* PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *); | |||
218 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyType_GetModule(struct _typeobject *); | |||
219 | PyAPI_FUNC(void *)__attribute__ ((visibility ("default"))) void * PyType_GetModuleState(struct _typeobject *); | |||
220 | #endif | |||
221 | ||||
222 | /* Generic type check */ | |||
223 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyType_IsSubtype(PyTypeObject *, PyTypeObject *); | |||
224 | #define PyObject_TypeCheck(ob, tp)(_Py_IS_TYPE(((const PyObject*)(ob)), tp) || PyType_IsSubtype ((((PyObject*)(ob))->ob_type), (tp))) \ | |||
225 | (Py_IS_TYPE(ob, tp)_Py_IS_TYPE(((const PyObject*)(ob)), tp) || PyType_IsSubtype(Py_TYPE(ob)(((PyObject*)(ob))->ob_type), (tp))) | |||
226 | ||||
227 | PyAPI_DATA(PyTypeObject)extern __attribute__ ((visibility ("default"))) PyTypeObject PyType_Type; /* built-in 'type' */ | |||
228 | PyAPI_DATA(PyTypeObject)extern __attribute__ ((visibility ("default"))) PyTypeObject PyBaseObject_Type; /* built-in 'object' */ | |||
229 | PyAPI_DATA(PyTypeObject)extern __attribute__ ((visibility ("default"))) PyTypeObject PySuper_Type; /* built-in 'super' */ | |||
230 | ||||
231 | PyAPI_FUNC(unsigned long)__attribute__ ((visibility ("default"))) unsigned long PyType_GetFlags(PyTypeObject*); | |||
232 | ||||
233 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyType_Ready(PyTypeObject *); | |||
234 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyType_GenericAlloc(PyTypeObject *, Py_ssize_t); | |||
235 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyType_GenericNew(PyTypeObject *, | |||
236 | PyObject *, PyObject *); | |||
237 | PyAPI_FUNC(unsigned int)__attribute__ ((visibility ("default"))) unsigned int PyType_ClearCache(void); | |||
238 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void PyType_Modified(PyTypeObject *); | |||
239 | ||||
240 | /* Generic operations on objects */ | |||
241 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_Repr(PyObject *); | |||
242 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_Str(PyObject *); | |||
243 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_ASCII(PyObject *); | |||
244 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_Bytes(PyObject *); | |||
245 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_RichCompare(PyObject *, PyObject *, int); | |||
246 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_RichCompareBool(PyObject *, PyObject *, int); | |||
247 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_GetAttrString(PyObject *, const char *); | |||
248 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_SetAttrString(PyObject *, const char *, PyObject *); | |||
249 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_HasAttrString(PyObject *, const char *); | |||
250 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_GetAttr(PyObject *, PyObject *); | |||
251 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_SetAttr(PyObject *, PyObject *, PyObject *); | |||
252 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_HasAttr(PyObject *, PyObject *); | |||
253 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_SelfIter(PyObject *); | |||
254 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_GenericGetAttr(PyObject *, PyObject *); | |||
255 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *); | |||
256 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 | |||
257 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_GenericSetDict(PyObject *, PyObject *, void *); | |||
258 | #endif | |||
259 | PyAPI_FUNC(Py_hash_t)__attribute__ ((visibility ("default"))) Py_hash_t PyObject_Hash(PyObject *); | |||
260 | PyAPI_FUNC(Py_hash_t)__attribute__ ((visibility ("default"))) Py_hash_t PyObject_HashNotImplemented(PyObject *); | |||
261 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_IsTrue(PyObject *); | |||
262 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyObject_Not(PyObject *); | |||
263 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int PyCallable_Check(PyObject *); | |||
264 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void PyObject_ClearWeakRefs(PyObject *); | |||
265 | ||||
266 | /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a | |||
267 | list of strings. PyObject_Dir(NULL) is like builtins.dir(), | |||
268 | returning the names of the current locals. In this case, if there are | |||
269 | no current locals, NULL is returned, and PyErr_Occurred() is false. | |||
270 | */ | |||
271 | PyAPI_FUNC(PyObject *)__attribute__ ((visibility ("default"))) PyObject * PyObject_Dir(PyObject *); | |||
272 | ||||
273 | ||||
274 | /* Helpers for printing recursive container types */ | |||
275 | PyAPI_FUNC(int)__attribute__ ((visibility ("default"))) int Py_ReprEnter(PyObject *); | |||
276 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void Py_ReprLeave(PyObject *); | |||
277 | ||||
278 | /* Flag bits for printing: */ | |||
279 | #define Py_PRINT_RAW1 1 /* No string quotes etc. */ | |||
280 | ||||
281 | /* | |||
282 | Type flags (tp_flags) | |||
283 | ||||
284 | These flags are used to change expected features and behavior for a | |||
285 | particular type. | |||
286 | ||||
287 | Arbitration of the flag bit positions will need to be coordinated among | |||
288 | all extension writers who publicly release their extensions (this will | |||
289 | be fewer than you might expect!). | |||
290 | ||||
291 | Most flags were removed as of Python 3.0 to make room for new flags. (Some | |||
292 | flags are not for backwards compatibility but to indicate the presence of an | |||
293 | optional feature; these flags remain of course.) | |||
294 | ||||
295 | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. | |||
296 | ||||
297 | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the | |||
298 | given type object has a specified feature. | |||
299 | */ | |||
300 | ||||
301 | /* Set if the type object is dynamically allocated */ | |||
302 | #define Py_TPFLAGS_HEAPTYPE(1UL << 9) (1UL << 9) | |||
303 | ||||
304 | /* Set if the type allows subclassing */ | |||
305 | #define Py_TPFLAGS_BASETYPE(1UL << 10) (1UL << 10) | |||
306 | ||||
307 | /* Set if the type implements the vectorcall protocol (PEP 590) */ | |||
308 | #ifndef Py_LIMITED_API | |||
309 | #define Py_TPFLAGS_HAVE_VECTORCALL(1UL << 11) (1UL << 11) | |||
310 | // Backwards compatibility alias for API that was provisional in Python 3.8 | |||
311 | #define _Py_TPFLAGS_HAVE_VECTORCALL(1UL << 11) Py_TPFLAGS_HAVE_VECTORCALL(1UL << 11) | |||
312 | #endif | |||
313 | ||||
314 | /* Set if the type is 'ready' -- fully initialized */ | |||
315 | #define Py_TPFLAGS_READY(1UL << 12) (1UL << 12) | |||
316 | ||||
317 | /* Set while the type is being 'readied', to prevent recursive ready calls */ | |||
318 | #define Py_TPFLAGS_READYING(1UL << 13) (1UL << 13) | |||
319 | ||||
320 | /* Objects support garbage collection (see objimpl.h) */ | |||
321 | #define Py_TPFLAGS_HAVE_GC(1UL << 14) (1UL << 14) | |||
322 | ||||
323 | /* These two bits are preserved for Stackless Python, next after this is 17 */ | |||
324 | #ifdef STACKLESS | |||
325 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION0 (3UL << 15) | |||
326 | #else | |||
327 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION0 0 | |||
328 | #endif | |||
329 | ||||
330 | /* Objects behave like an unbound method */ | |||
331 | #define Py_TPFLAGS_METHOD_DESCRIPTOR(1UL << 17) (1UL << 17) | |||
332 | ||||
333 | /* Objects support type attribute cache */ | |||
334 | #define Py_TPFLAGS_HAVE_VERSION_TAG(1UL << 18) (1UL << 18) | |||
335 | #define Py_TPFLAGS_VALID_VERSION_TAG(1UL << 19) (1UL << 19) | |||
336 | ||||
337 | /* Type is abstract and cannot be instantiated */ | |||
338 | #define Py_TPFLAGS_IS_ABSTRACT(1UL << 20) (1UL << 20) | |||
339 | ||||
340 | /* These flags are used to determine if a type is a subclass. */ | |||
341 | #define Py_TPFLAGS_LONG_SUBCLASS(1UL << 24) (1UL << 24) | |||
342 | #define Py_TPFLAGS_LIST_SUBCLASS(1UL << 25) (1UL << 25) | |||
343 | #define Py_TPFLAGS_TUPLE_SUBCLASS(1UL << 26) (1UL << 26) | |||
344 | #define Py_TPFLAGS_BYTES_SUBCLASS(1UL << 27) (1UL << 27) | |||
345 | #define Py_TPFLAGS_UNICODE_SUBCLASS(1UL << 28) (1UL << 28) | |||
346 | #define Py_TPFLAGS_DICT_SUBCLASS(1UL << 29) (1UL << 29) | |||
347 | #define Py_TPFLAGS_BASE_EXC_SUBCLASS(1UL << 30) (1UL << 30) | |||
348 | #define Py_TPFLAGS_TYPE_SUBCLASS(1UL << 31) (1UL << 31) | |||
349 | ||||
350 | #define Py_TPFLAGS_DEFAULT( 0 | (1UL << 18) | 0) ( \ | |||
351 | Py_TPFLAGS_HAVE_STACKLESS_EXTENSION0 | \ | |||
352 | Py_TPFLAGS_HAVE_VERSION_TAG(1UL << 18) | \ | |||
353 | 0) | |||
354 | ||||
355 | /* NOTE: The following flags reuse lower bits (removed as part of the | |||
356 | * Python 3.0 transition). */ | |||
357 | ||||
358 | /* The following flag is kept for compatibility. Starting with 3.8, | |||
359 | * binary compatibility of C extensions across feature releases of | |||
360 | * Python is not supported anymore, except when using the stable ABI. | |||
361 | */ | |||
362 | ||||
363 | /* Type structure has tp_finalize member (3.4) */ | |||
364 | #define Py_TPFLAGS_HAVE_FINALIZE(1UL << 0) (1UL << 0) | |||
365 | ||||
366 | ||||
367 | /* | |||
368 | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement | |||
369 | reference counts. Py_DECREF calls the object's deallocator function when | |||
370 | the refcount falls to 0; for | |||
371 | objects that don't contain references to other objects or heap memory | |||
372 | this can be the standard function free(). Both macros can be used | |||
373 | wherever a void expression is allowed. The argument must not be a | |||
374 | NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead. | |||
375 | The macro _Py_NewReference(op) initialize reference counts to 1, and | |||
376 | in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional | |||
377 | bookkeeping appropriate to the special build. | |||
378 | ||||
379 | We assume that the reference count field can never overflow; this can | |||
380 | be proven when the size of the field is the same as the pointer size, so | |||
381 | we ignore the possibility. Provided a C int is at least 32 bits (which | |||
382 | is implicitly assumed in many parts of this code), that's enough for | |||
383 | about 2**31 references to an object. | |||
384 | ||||
385 | XXX The following became out of date in Python 2.2, but I'm not sure | |||
386 | XXX what the full truth is now. Certainly, heap-allocated type objects | |||
387 | XXX can and should be deallocated. | |||
388 | Type objects should never be deallocated; the type pointer in an object | |||
389 | is not considered to be a reference to the type object, to save | |||
390 | complications in the deallocation function. (This is actually a | |||
391 | decision that's up to the implementer of each new type so if you want, | |||
392 | you can count such references to the type object.) | |||
393 | */ | |||
394 | ||||
395 | #ifdef Py_REF_DEBUG | |||
396 | PyAPI_DATA(Py_ssize_t)extern __attribute__ ((visibility ("default"))) Py_ssize_t _Py_RefTotal; | |||
397 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void _Py_NegativeRefcount(const char *filename, int lineno, | |||
398 | PyObject *op); | |||
399 | #endif /* Py_REF_DEBUG */ | |||
400 | ||||
401 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void _Py_Dealloc(PyObject *); | |||
402 | ||||
403 | static inline void _Py_INCREF(PyObject *op) | |||
404 | { | |||
405 | #ifdef Py_REF_DEBUG | |||
406 | _Py_RefTotal++; | |||
407 | #endif | |||
408 | op->ob_refcnt++; | |||
409 | } | |||
410 | ||||
411 | #define Py_INCREF(op)_Py_INCREF(((PyObject*)(op))) _Py_INCREF(_PyObject_CAST(op)((PyObject*)(op))) | |||
412 | ||||
413 | static inline void _Py_DECREF( | |||
414 | #ifdef Py_REF_DEBUG | |||
415 | const char *filename, int lineno, | |||
416 | #endif | |||
417 | PyObject *op) | |||
418 | { | |||
419 | #ifdef Py_REF_DEBUG | |||
420 | _Py_RefTotal--; | |||
421 | #endif | |||
422 | if (--op->ob_refcnt != 0) { | |||
423 | #ifdef Py_REF_DEBUG | |||
424 | if (op->ob_refcnt < 0) { | |||
425 | _Py_NegativeRefcount(filename, lineno, op); | |||
426 | } | |||
427 | #endif | |||
428 | } | |||
429 | else { | |||
430 | _Py_Dealloc(op); | |||
431 | } | |||
432 | } | |||
433 | ||||
434 | #ifdef Py_REF_DEBUG | |||
435 | # define Py_DECREF(op)_Py_DECREF(((PyObject*)(op))) _Py_DECREF(__FILE__"/usr/include/python3.9/object.h", __LINE__435, _PyObject_CAST(op)((PyObject*)(op))) | |||
436 | #else | |||
437 | # define Py_DECREF(op)_Py_DECREF(((PyObject*)(op))) _Py_DECREF(_PyObject_CAST(op)((PyObject*)(op))) | |||
438 | #endif | |||
439 | ||||
440 | ||||
441 | /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear | |||
442 | * and tp_dealloc implementations. | |||
443 | * | |||
444 | * Note that "the obvious" code can be deadly: | |||
445 | * | |||
446 | * Py_XDECREF(op); | |||
447 | * op = NULL; | |||
448 | * | |||
449 | * Typically, `op` is something like self->containee, and `self` is done | |||
450 | * using its `containee` member. In the code sequence above, suppose | |||
451 | * `containee` is non-NULL with a refcount of 1. Its refcount falls to | |||
452 | * 0 on the first line, which can trigger an arbitrary amount of code, | |||
453 | * possibly including finalizers (like __del__ methods or weakref callbacks) | |||
454 | * coded in Python, which in turn can release the GIL and allow other threads | |||
455 | * to run, etc. Such code may even invoke methods of `self` again, or cause | |||
456 | * cyclic gc to trigger, but-- oops! --self->containee still points to the | |||
457 | * object being torn down, and it may be in an insane state while being torn | |||
458 | * down. This has in fact been a rich historic source of miserable (rare & | |||
459 | * hard-to-diagnose) segfaulting (and other) bugs. | |||
460 | * | |||
461 | * The safe way is: | |||
462 | * | |||
463 | * Py_CLEAR(op); | |||
464 | * | |||
465 | * That arranges to set `op` to NULL _before_ decref'ing, so that any code | |||
466 | * triggered as a side-effect of `op` getting torn down no longer believes | |||
467 | * `op` points to a valid object. | |||
468 | * | |||
469 | * There are cases where it's safe to use the naive code, but they're brittle. | |||
470 | * For example, if `op` points to a Python integer, you know that destroying | |||
471 | * one of those can't cause problems -- but in part that relies on that | |||
472 | * Python integers aren't currently weakly referencable. Best practice is | |||
473 | * to use Py_CLEAR() even if you can't think of a reason for why you need to. | |||
474 | */ | |||
475 | #define Py_CLEAR(op)do { PyObject *_py_tmp = ((PyObject*)(op)); if (_py_tmp != (( void*)0)) { (op) = ((void*)0); _Py_DECREF(((PyObject*)(_py_tmp ))); } } while (0) \ | |||
476 | do { \ | |||
477 | PyObject *_py_tmp = _PyObject_CAST(op)((PyObject*)(op)); \ | |||
478 | if (_py_tmp != NULL((void*)0)) { \ | |||
479 | (op) = NULL((void*)0); \ | |||
480 | Py_DECREF(_py_tmp)_Py_DECREF(((PyObject*)(_py_tmp))); \ | |||
481 | } \ | |||
482 | } while (0) | |||
483 | ||||
484 | /* Function to use in case the object pointer can be NULL: */ | |||
485 | static inline void _Py_XINCREF(PyObject *op) | |||
486 | { | |||
487 | if (op != NULL((void*)0)) { | |||
488 | Py_INCREF(op)_Py_INCREF(((PyObject*)(op))); | |||
489 | } | |||
490 | } | |||
491 | ||||
492 | #define Py_XINCREF(op)_Py_XINCREF(((PyObject*)(op))) _Py_XINCREF(_PyObject_CAST(op)((PyObject*)(op))) | |||
493 | ||||
494 | static inline void _Py_XDECREF(PyObject *op) | |||
495 | { | |||
496 | if (op != NULL((void*)0)) { | |||
497 | Py_DECREF(op)_Py_DECREF(((PyObject*)(op))); | |||
498 | } | |||
499 | } | |||
500 | ||||
501 | #define Py_XDECREF(op)_Py_XDECREF(((PyObject*)(op))) _Py_XDECREF(_PyObject_CAST(op)((PyObject*)(op))) | |||
502 | ||||
503 | /* | |||
504 | These are provided as conveniences to Python runtime embedders, so that | |||
505 | they can have object code that is not dependent on Python compilation flags. | |||
506 | */ | |||
507 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void Py_IncRef(PyObject *); | |||
508 | PyAPI_FUNC(void)__attribute__ ((visibility ("default"))) void Py_DecRef(PyObject *); | |||
509 | ||||
510 | /* | |||
511 | _Py_NoneStruct is an object of undefined type which can be used in contexts | |||
512 | where NULL (nil) is not suitable (since NULL often means 'error'). | |||
513 | ||||
514 | Don't forget to apply Py_INCREF() when returning this value!!! | |||
515 | */ | |||
516 | PyAPI_DATA(PyObject)extern __attribute__ ((visibility ("default"))) PyObject _Py_NoneStruct; /* Don't use this directly */ | |||
517 | #define Py_None(&_Py_NoneStruct) (&_Py_NoneStruct) | |||
518 | ||||
519 | /* Macro for returning Py_None from a function */ | |||
520 | #define Py_RETURN_NONEreturn _Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), (& _Py_NoneStruct) return Py_INCREF(Py_None)_Py_INCREF(((PyObject*)((&_Py_NoneStruct)))), Py_None(&_Py_NoneStruct) | |||
521 | ||||
522 | /* | |||
523 | Py_NotImplemented is a singleton used to signal that an operation is | |||
524 | not implemented for a given type combination. | |||
525 | */ | |||
526 | PyAPI_DATA(PyObject)extern __attribute__ ((visibility ("default"))) PyObject _Py_NotImplementedStruct; /* Don't use this directly */ | |||
527 | #define Py_NotImplemented(&_Py_NotImplementedStruct) (&_Py_NotImplementedStruct) | |||
528 | ||||
529 | /* Macro for returning Py_NotImplemented from a function */ | |||
530 | #define Py_RETURN_NOTIMPLEMENTEDreturn _Py_INCREF(((PyObject*)((&_Py_NotImplementedStruct )))), (&_Py_NotImplementedStruct) \ | |||
531 | return Py_INCREF(Py_NotImplemented)_Py_INCREF(((PyObject*)((&_Py_NotImplementedStruct)))), Py_NotImplemented(&_Py_NotImplementedStruct) | |||
532 | ||||
533 | /* Rich comparison opcodes */ | |||
534 | #define Py_LT0 0 | |||
535 | #define Py_LE1 1 | |||
536 | #define Py_EQ2 2 | |||
537 | #define Py_NE3 3 | |||
538 | #define Py_GT4 4 | |||
539 | #define Py_GE5 5 | |||
540 | ||||
541 | /* | |||
542 | * Macro for implementing rich comparisons | |||
543 | * | |||
544 | * Needs to be a macro because any C-comparable type can be used. | |||
545 | */ | |||
546 | #define Py_RETURN_RICHCOMPARE(val1, val2, op)do { switch (op) { case 2: if ((val1) == (val2)) return _Py_INCREF (((PyObject*)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) &_Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); case 3: if ((val1) != (val2)) return _Py_INCREF(((PyObject *)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) & _Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); case 0: if ((val1) < (val2)) return _Py_INCREF(((PyObject *)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) & _Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); case 4: if ((val1) > (val2)) return _Py_INCREF(((PyObject *)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) & _Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); case 1: if ((val1) <= (val2)) return _Py_INCREF(((PyObject *)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) & _Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); case 5: if ((val1) >= (val2)) return _Py_INCREF(((PyObject *)(((PyObject *) &_Py_TrueStruct)))), ((PyObject *) & _Py_TrueStruct); return _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct)))), ((PyObject *) &_Py_FalseStruct ); default: __builtin_unreachable(); } } while (0) \ | |||
547 | do { \ | |||
548 | switch (op) { \ | |||
549 | case Py_EQ2: if ((val1) == (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
550 | case Py_NE3: if ((val1) != (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
551 | case Py_LT0: if ((val1) < (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
552 | case Py_GT4: if ((val1) > (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
553 | case Py_LE1: if ((val1) <= (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
554 | case Py_GE5: if ((val1) >= (val2)) Py_RETURN_TRUEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_TrueStruct )))), ((PyObject *) &_Py_TrueStruct); Py_RETURN_FALSEreturn _Py_INCREF(((PyObject*)(((PyObject *) &_Py_FalseStruct )))), ((PyObject *) &_Py_FalseStruct); \ | |||
555 | default: \ | |||
556 | Py_UNREACHABLE()__builtin_unreachable(); \ | |||
557 | } \ | |||
558 | } while (0) | |||
559 | ||||
560 | ||||
561 | /* | |||
562 | More conventions | |||
563 | ================ | |||
564 | ||||
565 | Argument Checking | |||
566 | ----------------- | |||
567 | ||||
568 | Functions that take objects as arguments normally don't check for nil | |||
569 | arguments, but they do check the type of the argument, and return an | |||
570 | error if the function doesn't apply to the type. | |||
571 | ||||
572 | Failure Modes | |||
573 | ------------- | |||
574 | ||||
575 | Functions may fail for a variety of reasons, including running out of | |||
576 | memory. This is communicated to the caller in two ways: an error string | |||
577 | is set (see errors.h), and the function result differs: functions that | |||
578 | normally return a pointer return NULL for failure, functions returning | |||
579 | an integer return -1 (which could be a legal return value too!), and | |||
580 | other functions return 0 for success and -1 for failure. | |||
581 | Callers should always check for errors before using the result. If | |||
582 | an error was set, the caller must either explicitly clear it, or pass | |||
583 | the error on to its caller. | |||
584 | ||||
585 | Reference Counts | |||
586 | ---------------- | |||
587 | ||||
588 | It takes a while to get used to the proper usage of reference counts. | |||
589 | ||||
590 | Functions that create an object set the reference count to 1; such new | |||
591 | objects must be stored somewhere or destroyed again with Py_DECREF(). | |||
592 | Some functions that 'store' objects, such as PyTuple_SetItem() and | |||
593 | PyList_SetItem(), | |||
594 | don't increment the reference count of the object, since the most | |||
595 | frequent use is to store a fresh object. Functions that 'retrieve' | |||
596 | objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also | |||
597 | don't increment | |||
598 | the reference count, since most frequently the object is only looked at | |||
599 | quickly. Thus, to retrieve an object and store it again, the caller | |||
600 | must call Py_INCREF() explicitly. | |||
601 | ||||
602 | NOTE: functions that 'consume' a reference count, like | |||
603 | PyList_SetItem(), consume the reference even if the object wasn't | |||
604 | successfully stored, to simplify error handling. | |||
605 | ||||
606 | It seems attractive to make other functions that take an object as | |||
607 | argument consume a reference count; however, this may quickly get | |||
608 | confusing (even the current practice is already confusing). Consider | |||
609 | it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at | |||
610 | times. | |||
611 | */ | |||
612 | ||||
613 | #ifndef Py_LIMITED_API | |||
614 | # define Py_CPYTHON_OBJECT_H | |||
615 | # include "cpython/object.h" | |||
616 | # undef Py_CPYTHON_OBJECT_H | |||
617 | #endif | |||
618 | ||||
619 | ||||
620 | static inline int | |||
621 | PyType_HasFeature(PyTypeObject *type, unsigned long feature) | |||
622 | { | |||
623 | unsigned long flags; | |||
624 | #ifdef Py_LIMITED_API | |||
625 | // PyTypeObject is opaque in the limited C API | |||
626 | flags = PyType_GetFlags(type); | |||
627 | #else | |||
628 | flags = type->tp_flags; | |||
629 | #endif | |||
630 | return ((flags & feature) != 0); | |||
631 | } | |||
632 | ||||
633 | #define PyType_FastSubclass(type, flag)PyType_HasFeature(type, flag) PyType_HasFeature(type, flag) | |||
634 | ||||
635 | static inline int _PyType_Check(PyObject *op) { | |||
636 | return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)PyType_HasFeature((((PyObject*)(op))->ob_type), (1UL << 31)); | |||
637 | } | |||
638 | #define PyType_Check(op)_PyType_Check(((PyObject*)(op))) _PyType_Check(_PyObject_CAST(op)((PyObject*)(op))) | |||
639 | ||||
640 | static inline int _PyType_CheckExact(PyObject *op) { | |||
641 | return Py_IS_TYPE(op, &PyType_Type)_Py_IS_TYPE(((const PyObject*)(op)), &PyType_Type); | |||
642 | } | |||
643 | #define PyType_CheckExact(op)_PyType_CheckExact(((PyObject*)(op))) _PyType_CheckExact(_PyObject_CAST(op)((PyObject*)(op))) | |||
644 | ||||
645 | #ifdef __cplusplus | |||
646 | } | |||
647 | #endif | |||
648 | #endif /* !Py_OBJECT_H */ |