py-infthread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Python interface to inferior threads.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbthread.h"
  16. #include "inferior.h"
  17. #include "python-internal.h"
  18. extern PyTypeObject thread_object_type
  19. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
  20. /* Require that INFERIOR be a valid inferior ID. */
  21. #define THPY_REQUIRE_VALID(Thread) \
  22. do { \
  23. if (!Thread->thread) \
  24. { \
  25. PyErr_SetString (PyExc_RuntimeError, \
  26. _("Thread no longer exists.")); \
  27. return NULL; \
  28. } \
  29. } while (0)
  30. gdbpy_ref<thread_object>
  31. create_thread_object (struct thread_info *tp)
  32. {
  33. gdbpy_ref<thread_object> thread_obj;
  34. gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
  35. if (inf_obj == NULL)
  36. return NULL;
  37. thread_obj.reset (PyObject_New (thread_object, &thread_object_type));
  38. if (thread_obj == NULL)
  39. return NULL;
  40. thread_obj->thread = tp;
  41. thread_obj->inf_obj = (PyObject *) inf_obj.release ();
  42. return thread_obj;
  43. }
  44. static void
  45. thpy_dealloc (PyObject *self)
  46. {
  47. Py_DECREF (((thread_object *) self)->inf_obj);
  48. Py_TYPE (self)->tp_free (self);
  49. }
  50. static PyObject *
  51. thpy_get_name (PyObject *self, void *ignore)
  52. {
  53. thread_object *thread_obj = (thread_object *) self;
  54. THPY_REQUIRE_VALID (thread_obj);
  55. const char *name = thread_name (thread_obj->thread);
  56. if (name == NULL)
  57. Py_RETURN_NONE;
  58. return PyUnicode_FromString (name);
  59. }
  60. /* Return a string containing target specific additional information about
  61. the state of the thread, or None, if there is no such additional
  62. information. */
  63. static PyObject *
  64. thpy_get_details (PyObject *self, void *ignore)
  65. {
  66. thread_object *thread_obj = (thread_object *) self;
  67. THPY_REQUIRE_VALID (thread_obj);
  68. /* GCC can't tell that extra_info will always be assigned after the
  69. 'catch', so initialize it. */
  70. const char *extra_info = nullptr;
  71. try
  72. {
  73. extra_info = target_extra_thread_info (thread_obj->thread);
  74. }
  75. catch (const gdb_exception &except)
  76. {
  77. GDB_PY_HANDLE_EXCEPTION (except);
  78. }
  79. if (extra_info == nullptr)
  80. Py_RETURN_NONE;
  81. return PyUnicode_FromString (extra_info);
  82. }
  83. static int
  84. thpy_set_name (PyObject *self, PyObject *newvalue, void *ignore)
  85. {
  86. thread_object *thread_obj = (thread_object *) self;
  87. gdb::unique_xmalloc_ptr<char> name;
  88. if (! thread_obj->thread)
  89. {
  90. PyErr_SetString (PyExc_RuntimeError, _("Thread no longer exists."));
  91. return -1;
  92. }
  93. if (newvalue == NULL)
  94. {
  95. PyErr_SetString (PyExc_TypeError,
  96. _("Cannot delete `name' attribute."));
  97. return -1;
  98. }
  99. else if (newvalue == Py_None)
  100. {
  101. /* Nothing. */
  102. }
  103. else if (! gdbpy_is_string (newvalue))
  104. {
  105. PyErr_SetString (PyExc_TypeError,
  106. _("The value of `name' must be a string."));
  107. return -1;
  108. }
  109. else
  110. {
  111. name = python_string_to_host_string (newvalue);
  112. if (! name)
  113. return -1;
  114. }
  115. thread_obj->thread->set_name (std::move (name));
  116. return 0;
  117. }
  118. /* Getter for InferiorThread.num. */
  119. static PyObject *
  120. thpy_get_num (PyObject *self, void *closure)
  121. {
  122. thread_object *thread_obj = (thread_object *) self;
  123. THPY_REQUIRE_VALID (thread_obj);
  124. gdbpy_ref<> result
  125. = gdb_py_object_from_longest (thread_obj->thread->per_inf_num);
  126. return result.release ();
  127. }
  128. /* Getter for InferiorThread.global_num. */
  129. static PyObject *
  130. thpy_get_global_num (PyObject *self, void *closure)
  131. {
  132. thread_object *thread_obj = (thread_object *) self;
  133. THPY_REQUIRE_VALID (thread_obj);
  134. gdbpy_ref<> result
  135. = gdb_py_object_from_longest (thread_obj->thread->global_num);
  136. return result.release ();
  137. }
  138. /* Getter for InferiorThread.ptid -> (pid, lwp, tid).
  139. Returns a tuple with the thread's ptid components. */
  140. static PyObject *
  141. thpy_get_ptid (PyObject *self, void *closure)
  142. {
  143. thread_object *thread_obj = (thread_object *) self;
  144. THPY_REQUIRE_VALID (thread_obj);
  145. return gdbpy_create_ptid_object (thread_obj->thread->ptid);
  146. }
  147. /* Getter for InferiorThread.inferior -> Inferior. */
  148. static PyObject *
  149. thpy_get_inferior (PyObject *self, void *ignore)
  150. {
  151. thread_object *thread_obj = (thread_object *) self;
  152. THPY_REQUIRE_VALID (thread_obj);
  153. Py_INCREF (thread_obj->inf_obj);
  154. return thread_obj->inf_obj;
  155. }
  156. /* Implementation of InferiorThread.switch ().
  157. Makes this the GDB selected thread. */
  158. static PyObject *
  159. thpy_switch (PyObject *self, PyObject *args)
  160. {
  161. thread_object *thread_obj = (thread_object *) self;
  162. THPY_REQUIRE_VALID (thread_obj);
  163. try
  164. {
  165. switch_to_thread (thread_obj->thread);
  166. }
  167. catch (const gdb_exception &except)
  168. {
  169. GDB_PY_HANDLE_EXCEPTION (except);
  170. }
  171. Py_RETURN_NONE;
  172. }
  173. /* Implementation of InferiorThread.is_stopped () -> Boolean.
  174. Return whether the thread is stopped. */
  175. static PyObject *
  176. thpy_is_stopped (PyObject *self, PyObject *args)
  177. {
  178. thread_object *thread_obj = (thread_object *) self;
  179. THPY_REQUIRE_VALID (thread_obj);
  180. if (thread_obj->thread->state == THREAD_STOPPED)
  181. Py_RETURN_TRUE;
  182. Py_RETURN_FALSE;
  183. }
  184. /* Implementation of InferiorThread.is_running () -> Boolean.
  185. Return whether the thread is running. */
  186. static PyObject *
  187. thpy_is_running (PyObject *self, PyObject *args)
  188. {
  189. thread_object *thread_obj = (thread_object *) self;
  190. THPY_REQUIRE_VALID (thread_obj);
  191. if (thread_obj->thread->state == THREAD_RUNNING)
  192. Py_RETURN_TRUE;
  193. Py_RETURN_FALSE;
  194. }
  195. /* Implementation of InferiorThread.is_exited () -> Boolean.
  196. Return whether the thread is exited. */
  197. static PyObject *
  198. thpy_is_exited (PyObject *self, PyObject *args)
  199. {
  200. thread_object *thread_obj = (thread_object *) self;
  201. THPY_REQUIRE_VALID (thread_obj);
  202. if (thread_obj->thread->state == THREAD_EXITED)
  203. Py_RETURN_TRUE;
  204. Py_RETURN_FALSE;
  205. }
  206. /* Implementation of gdb.InfThread.is_valid (self) -> Boolean.
  207. Returns True if this inferior Thread object still exists
  208. in GDB. */
  209. static PyObject *
  210. thpy_is_valid (PyObject *self, PyObject *args)
  211. {
  212. thread_object *thread_obj = (thread_object *) self;
  213. if (! thread_obj->thread)
  214. Py_RETURN_FALSE;
  215. Py_RETURN_TRUE;
  216. }
  217. /* Implementation of gdb.InferiorThread.handle (self) -> handle. */
  218. static PyObject *
  219. thpy_thread_handle (PyObject *self, PyObject *args)
  220. {
  221. thread_object *thread_obj = (thread_object *) self;
  222. THPY_REQUIRE_VALID (thread_obj);
  223. gdb::byte_vector hv;
  224. try
  225. {
  226. hv = target_thread_info_to_thread_handle (thread_obj->thread);
  227. }
  228. catch (const gdb_exception &except)
  229. {
  230. GDB_PY_HANDLE_EXCEPTION (except);
  231. }
  232. if (hv.size () == 0)
  233. {
  234. PyErr_SetString (PyExc_RuntimeError, _("Thread handle not found."));
  235. return NULL;
  236. }
  237. PyObject *object = PyBytes_FromStringAndSize ((const char *) hv.data (),
  238. hv.size());
  239. return object;
  240. }
  241. /* Return a reference to a new Python object representing a ptid_t.
  242. The object is a tuple containing (pid, lwp, tid). */
  243. PyObject *
  244. gdbpy_create_ptid_object (ptid_t ptid)
  245. {
  246. int pid;
  247. long lwp;
  248. ULONGEST tid;
  249. PyObject *ret;
  250. ret = PyTuple_New (3);
  251. if (!ret)
  252. return NULL;
  253. pid = ptid.pid ();
  254. lwp = ptid.lwp ();
  255. tid = ptid.tid ();
  256. gdbpy_ref<> pid_obj = gdb_py_object_from_longest (pid);
  257. if (pid_obj == nullptr)
  258. return nullptr;
  259. gdbpy_ref<> lwp_obj = gdb_py_object_from_longest (lwp);
  260. if (lwp_obj == nullptr)
  261. return nullptr;
  262. gdbpy_ref<> tid_obj = gdb_py_object_from_ulongest (tid);
  263. if (tid_obj == nullptr)
  264. return nullptr;
  265. /* Note that these steal references, hence the use of 'release'. */
  266. PyTuple_SET_ITEM (ret, 0, pid_obj.release ());
  267. PyTuple_SET_ITEM (ret, 1, lwp_obj.release ());
  268. PyTuple_SET_ITEM (ret, 2, tid_obj.release ());
  269. return ret;
  270. }
  271. /* Implementation of gdb.selected_thread () -> gdb.InferiorThread.
  272. Returns the selected thread object. */
  273. PyObject *
  274. gdbpy_selected_thread (PyObject *self, PyObject *args)
  275. {
  276. if (inferior_ptid != null_ptid)
  277. return thread_to_thread_object (inferior_thread ()).release ();
  278. Py_RETURN_NONE;
  279. }
  280. int
  281. gdbpy_initialize_thread (void)
  282. {
  283. if (PyType_Ready (&thread_object_type) < 0)
  284. return -1;
  285. return gdb_pymodule_addobject (gdb_module, "InferiorThread",
  286. (PyObject *) &thread_object_type);
  287. }
  288. static gdb_PyGetSetDef thread_object_getset[] =
  289. {
  290. { "name", thpy_get_name, thpy_set_name,
  291. "The name of the thread, as set by the user or the OS.", NULL },
  292. { "details", thpy_get_details, NULL,
  293. "A target specific string containing extra thread state details.",
  294. NULL },
  295. { "num", thpy_get_num, NULL,
  296. "Per-inferior number of the thread, as assigned by GDB.", NULL },
  297. { "global_num", thpy_get_global_num, NULL,
  298. "Global number of the thread, as assigned by GDB.", NULL },
  299. { "ptid", thpy_get_ptid, NULL, "ID of the thread, as assigned by the OS.",
  300. NULL },
  301. { "inferior", thpy_get_inferior, NULL,
  302. "The Inferior object this thread belongs to.", NULL },
  303. { NULL }
  304. };
  305. static PyMethodDef thread_object_methods[] =
  306. {
  307. { "is_valid", thpy_is_valid, METH_NOARGS,
  308. "is_valid () -> Boolean.\n\
  309. Return true if this inferior thread is valid, false if not." },
  310. { "switch", thpy_switch, METH_NOARGS,
  311. "switch ()\n\
  312. Makes this the GDB selected thread." },
  313. { "is_stopped", thpy_is_stopped, METH_NOARGS,
  314. "is_stopped () -> Boolean\n\
  315. Return whether the thread is stopped." },
  316. { "is_running", thpy_is_running, METH_NOARGS,
  317. "is_running () -> Boolean\n\
  318. Return whether the thread is running." },
  319. { "is_exited", thpy_is_exited, METH_NOARGS,
  320. "is_exited () -> Boolean\n\
  321. Return whether the thread is exited." },
  322. { "handle", thpy_thread_handle, METH_NOARGS,
  323. "handle () -> handle\n\
  324. Return thread library specific handle for thread." },
  325. { NULL }
  326. };
  327. PyTypeObject thread_object_type =
  328. {
  329. PyVarObject_HEAD_INIT (NULL, 0)
  330. "gdb.InferiorThread", /*tp_name*/
  331. sizeof (thread_object), /*tp_basicsize*/
  332. 0, /*tp_itemsize*/
  333. thpy_dealloc, /*tp_dealloc*/
  334. 0, /*tp_print*/
  335. 0, /*tp_getattr*/
  336. 0, /*tp_setattr*/
  337. 0, /*tp_compare*/
  338. 0, /*tp_repr*/
  339. 0, /*tp_as_number*/
  340. 0, /*tp_as_sequence*/
  341. 0, /*tp_as_mapping*/
  342. 0, /*tp_hash */
  343. 0, /*tp_call*/
  344. 0, /*tp_str*/
  345. 0, /*tp_getattro*/
  346. 0, /*tp_setattro*/
  347. 0, /*tp_as_buffer*/
  348. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  349. "GDB thread object", /* tp_doc */
  350. 0, /* tp_traverse */
  351. 0, /* tp_clear */
  352. 0, /* tp_richcompare */
  353. 0, /* tp_weaklistoffset */
  354. 0, /* tp_iter */
  355. 0, /* tp_iternext */
  356. thread_object_methods, /* tp_methods */
  357. 0, /* tp_members */
  358. thread_object_getset, /* tp_getset */
  359. 0, /* tp_base */
  360. 0, /* tp_dict */
  361. 0, /* tp_descr_get */
  362. 0, /* tp_descr_set */
  363. 0, /* tp_dictoffset */
  364. 0, /* tp_init */
  365. 0 /* tp_alloc */
  366. };