py-param.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /* GDB parameters implemented in Python
  2. Copyright (C) 2008-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 "value.h"
  16. #include "python-internal.h"
  17. #include "charset.h"
  18. #include "gdbcmd.h"
  19. #include "cli/cli-decode.h"
  20. #include "completer.h"
  21. #include "language.h"
  22. #include "arch-utils.h"
  23. /* Parameter constants and their values. */
  24. static struct {
  25. const char *name;
  26. int value;
  27. } parm_constants[] =
  28. {
  29. { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
  30. { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
  31. { "PARAM_UINTEGER", var_uinteger },
  32. { "PARAM_INTEGER", var_integer },
  33. { "PARAM_STRING", var_string },
  34. { "PARAM_STRING_NOESCAPE", var_string_noescape },
  35. { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
  36. { "PARAM_FILENAME", var_filename },
  37. { "PARAM_ZINTEGER", var_zinteger },
  38. { "PARAM_ZUINTEGER", var_zuinteger },
  39. { "PARAM_ZUINTEGER_UNLIMITED", var_zuinteger_unlimited },
  40. { "PARAM_ENUM", var_enum },
  41. { NULL, 0 }
  42. };
  43. /* A union that can hold anything described by enum var_types. */
  44. union parmpy_variable
  45. {
  46. /* Hold a boolean value. */
  47. bool boolval;
  48. /* Hold an integer value. */
  49. int intval;
  50. /* Hold an auto_boolean. */
  51. enum auto_boolean autoboolval;
  52. /* Hold an unsigned integer value, for uinteger. */
  53. unsigned int uintval;
  54. /* Hold a string, for the various string types. The std::string is
  55. new-ed. */
  56. std::string *stringval;
  57. /* Hold a string, for enums. */
  58. const char *cstringval;
  59. };
  60. /* A GDB parameter. */
  61. struct parmpy_object
  62. {
  63. PyObject_HEAD
  64. /* The type of the parameter. */
  65. enum var_types type;
  66. /* The value of the parameter. */
  67. union parmpy_variable value;
  68. /* For an enum command, the possible values. The vector is
  69. allocated with xmalloc, as is each element. It is
  70. NULL-terminated. */
  71. const char **enumeration;
  72. };
  73. /* Wraps a setting around an existing parmpy_object. This abstraction
  74. is used to manipulate the value in S->VALUE in a type safe manner using
  75. the setting interface. */
  76. static setting
  77. make_setting (parmpy_object *s)
  78. {
  79. if (var_type_uses<bool> (s->type))
  80. return setting (s->type, &s->value.boolval);
  81. else if (var_type_uses<int> (s->type))
  82. return setting (s->type, &s->value.intval);
  83. else if (var_type_uses<auto_boolean> (s->type))
  84. return setting (s->type, &s->value.autoboolval);
  85. else if (var_type_uses<unsigned int> (s->type))
  86. return setting (s->type, &s->value.uintval);
  87. else if (var_type_uses<std::string> (s->type))
  88. return setting (s->type, s->value.stringval);
  89. else if (var_type_uses<const char *> (s->type))
  90. return setting (s->type, &s->value.cstringval);
  91. else
  92. gdb_assert_not_reached ("unhandled var type");
  93. }
  94. extern PyTypeObject parmpy_object_type
  95. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
  96. /* Some handy string constants. */
  97. static PyObject *set_doc_cst;
  98. static PyObject *show_doc_cst;
  99. /* Get an attribute. */
  100. static PyObject *
  101. get_attr (PyObject *obj, PyObject *attr_name)
  102. {
  103. if (PyUnicode_Check (attr_name)
  104. && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
  105. {
  106. parmpy_object *self = (parmpy_object *) obj;
  107. return gdbpy_parameter_value (make_setting (self));
  108. }
  109. return PyObject_GenericGetAttr (obj, attr_name);
  110. }
  111. /* Set a parameter value from a Python value. Return 0 on success. Returns
  112. -1 on error, with a python exception set. */
  113. static int
  114. set_parameter_value (parmpy_object *self, PyObject *value)
  115. {
  116. int cmp;
  117. switch (self->type)
  118. {
  119. case var_string:
  120. case var_string_noescape:
  121. case var_optional_filename:
  122. case var_filename:
  123. if (! gdbpy_is_string (value)
  124. && (self->type == var_filename
  125. || value != Py_None))
  126. {
  127. PyErr_SetString (PyExc_RuntimeError,
  128. _("String required for filename."));
  129. return -1;
  130. }
  131. if (value == Py_None)
  132. self->value.stringval->clear ();
  133. else
  134. {
  135. gdb::unique_xmalloc_ptr<char>
  136. string (python_string_to_host_string (value));
  137. if (string == NULL)
  138. return -1;
  139. *self->value.stringval = string.get ();
  140. }
  141. break;
  142. case var_enum:
  143. {
  144. int i;
  145. if (! gdbpy_is_string (value))
  146. {
  147. PyErr_SetString (PyExc_RuntimeError,
  148. _("ENUM arguments must be a string."));
  149. return -1;
  150. }
  151. gdb::unique_xmalloc_ptr<char>
  152. str (python_string_to_host_string (value));
  153. if (str == NULL)
  154. return -1;
  155. for (i = 0; self->enumeration[i]; ++i)
  156. if (! strcmp (self->enumeration[i], str.get ()))
  157. break;
  158. if (! self->enumeration[i])
  159. {
  160. PyErr_SetString (PyExc_RuntimeError,
  161. _("The value must be member of an enumeration."));
  162. return -1;
  163. }
  164. self->value.cstringval = self->enumeration[i];
  165. break;
  166. }
  167. case var_boolean:
  168. if (! PyBool_Check (value))
  169. {
  170. PyErr_SetString (PyExc_RuntimeError,
  171. _("A boolean argument is required."));
  172. return -1;
  173. }
  174. cmp = PyObject_IsTrue (value);
  175. if (cmp < 0)
  176. return -1;
  177. self->value.boolval = cmp;
  178. break;
  179. case var_auto_boolean:
  180. if (! PyBool_Check (value) && value != Py_None)
  181. {
  182. PyErr_SetString (PyExc_RuntimeError,
  183. _("A boolean or None is required"));
  184. return -1;
  185. }
  186. if (value == Py_None)
  187. self->value.autoboolval = AUTO_BOOLEAN_AUTO;
  188. else
  189. {
  190. cmp = PyObject_IsTrue (value);
  191. if (cmp < 0 )
  192. return -1;
  193. if (cmp == 1)
  194. self->value.autoboolval = AUTO_BOOLEAN_TRUE;
  195. else
  196. self->value.autoboolval = AUTO_BOOLEAN_FALSE;
  197. }
  198. break;
  199. case var_integer:
  200. case var_zinteger:
  201. case var_uinteger:
  202. case var_zuinteger:
  203. case var_zuinteger_unlimited:
  204. {
  205. long l;
  206. int ok;
  207. if (!PyLong_Check (value))
  208. {
  209. PyErr_SetString (PyExc_RuntimeError,
  210. _("The value must be integer."));
  211. return -1;
  212. }
  213. if (! gdb_py_int_as_long (value, &l))
  214. return -1;
  215. switch (self->type)
  216. {
  217. case var_uinteger:
  218. if (l == 0)
  219. l = UINT_MAX;
  220. /* Fall through. */
  221. case var_zuinteger:
  222. ok = (l >= 0 && l <= UINT_MAX);
  223. break;
  224. case var_zuinteger_unlimited:
  225. ok = (l >= -1 && l <= INT_MAX);
  226. break;
  227. case var_integer:
  228. ok = (l >= INT_MIN && l <= INT_MAX);
  229. if (l == 0)
  230. l = INT_MAX;
  231. break;
  232. case var_zinteger:
  233. ok = (l >= INT_MIN && l <= INT_MAX);
  234. break;
  235. default:
  236. gdb_assert_not_reached ("unknown var_ constant");
  237. }
  238. if (! ok)
  239. {
  240. PyErr_SetString (PyExc_RuntimeError,
  241. _("Range exceeded."));
  242. return -1;
  243. }
  244. if (self->type == var_uinteger || self->type == var_zuinteger)
  245. self->value.uintval = (unsigned) l;
  246. else
  247. self->value.intval = (int) l;
  248. break;
  249. }
  250. default:
  251. PyErr_SetString (PyExc_RuntimeError,
  252. _("Unhandled type in parameter value."));
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. /* Set an attribute. Returns -1 on error, with a python exception set. */
  258. static int
  259. set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
  260. {
  261. if (PyUnicode_Check (attr_name)
  262. && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
  263. {
  264. if (!val)
  265. {
  266. PyErr_SetString (PyExc_RuntimeError,
  267. _("Cannot delete a parameter's value."));
  268. return -1;
  269. }
  270. return set_parameter_value ((parmpy_object *) obj, val);
  271. }
  272. return PyObject_GenericSetAttr (obj, attr_name, val);
  273. }
  274. /* Build up the path to command C, but drop the first component of the
  275. command prefix. This is only intended for use with the set/show
  276. parameters this file deals with, the first prefix should always be
  277. either 'set' or 'show'.
  278. As an example, if this full command is 'set prefix_a prefix_b command'
  279. this function will return the string 'prefix_a prefix_b command'. */
  280. static std::string
  281. full_cmd_name_without_first_prefix (struct cmd_list_element *c)
  282. {
  283. std::vector<std::string> components
  284. = c->command_components ();
  285. gdb_assert (components.size () > 1);
  286. std::string result = components[1];
  287. for (int i = 2; i < components.size (); ++i)
  288. result += " " + components[i];
  289. return result;
  290. }
  291. /* The different types of documentation string. */
  292. enum doc_string_type
  293. {
  294. doc_string_set,
  295. doc_string_show,
  296. doc_string_description
  297. };
  298. /* A helper function which returns a documentation string for an
  299. object. */
  300. static gdb::unique_xmalloc_ptr<char>
  301. get_doc_string (PyObject *object, enum doc_string_type doc_type,
  302. const char *cmd_name)
  303. {
  304. gdb::unique_xmalloc_ptr<char> result;
  305. PyObject *attr = nullptr;
  306. switch (doc_type)
  307. {
  308. case doc_string_set:
  309. attr = set_doc_cst;
  310. break;
  311. case doc_string_show:
  312. attr = show_doc_cst;
  313. break;
  314. case doc_string_description:
  315. attr = gdbpy_doc_cst;
  316. break;
  317. }
  318. gdb_assert (attr != nullptr);
  319. if (PyObject_HasAttr (object, attr))
  320. {
  321. gdbpy_ref<> ds_obj (PyObject_GetAttr (object, attr));
  322. if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
  323. {
  324. result = python_string_to_host_string (ds_obj.get ());
  325. if (result == NULL)
  326. gdbpy_print_stack ();
  327. }
  328. }
  329. if (result == nullptr)
  330. {
  331. if (doc_type == doc_string_description)
  332. result.reset (xstrdup (_("This command is not documented.")));
  333. else
  334. {
  335. if (doc_type == doc_string_show)
  336. result = xstrprintf (_("Show the current value of '%s'."),
  337. cmd_name);
  338. else
  339. result = xstrprintf (_("Set the current value of '%s'."),
  340. cmd_name);
  341. }
  342. }
  343. return result;
  344. }
  345. /* Helper function which will execute a METHOD in OBJ passing the
  346. argument ARG. ARG can be NULL. METHOD should return a Python
  347. string. If this function returns NULL, there has been an error and
  348. the appropriate exception set. */
  349. static gdb::unique_xmalloc_ptr<char>
  350. call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
  351. {
  352. gdb::unique_xmalloc_ptr<char> data;
  353. gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, method, arg, NULL));
  354. if (result == NULL)
  355. return NULL;
  356. if (gdbpy_is_string (result.get ()))
  357. {
  358. data = python_string_to_host_string (result.get ());
  359. if (! data)
  360. return NULL;
  361. }
  362. else
  363. {
  364. PyErr_SetString (PyExc_RuntimeError,
  365. _("Parameter must return a string value."));
  366. return NULL;
  367. }
  368. return data;
  369. }
  370. /* A callback function that is registered against the respective
  371. add_setshow_* set_doc prototype. This function calls the Python function
  372. "get_set_string" if it exists, which will return a string. That string
  373. is then printed. If "get_set_string" does not exist, or returns an
  374. empty string, then nothing is printed. */
  375. static void
  376. get_set_value (const char *args, int from_tty,
  377. struct cmd_list_element *c)
  378. {
  379. PyObject *obj = (PyObject *) c->context ();
  380. gdb::unique_xmalloc_ptr<char> set_doc_string;
  381. gdbpy_enter enter_py;
  382. gdbpy_ref<> set_doc_func (PyUnicode_FromString ("get_set_string"));
  383. if (set_doc_func == NULL)
  384. {
  385. gdbpy_print_stack ();
  386. return;
  387. }
  388. if (PyObject_HasAttr (obj, set_doc_func.get ()))
  389. {
  390. set_doc_string = call_doc_function (obj, set_doc_func.get (), NULL);
  391. if (! set_doc_string)
  392. gdbpy_handle_exception ();
  393. }
  394. const char *str = set_doc_string.get ();
  395. if (str != nullptr && str[0] != '\0')
  396. gdb_printf ("%s\n", str);
  397. }
  398. /* A callback function that is registered against the respective
  399. add_setshow_* show_doc prototype. This function will either call
  400. the Python function "get_show_string" or extract the Python
  401. attribute "show_doc" and return the contents as a string. If
  402. neither exist, insert a string indicating the Parameter is not
  403. documented. */
  404. static void
  405. get_show_value (struct ui_file *file, int from_tty,
  406. struct cmd_list_element *c,
  407. const char *value)
  408. {
  409. PyObject *obj = (PyObject *) c->context ();
  410. gdb::unique_xmalloc_ptr<char> show_doc_string;
  411. gdbpy_enter enter_py;
  412. gdbpy_ref<> show_doc_func (PyUnicode_FromString ("get_show_string"));
  413. if (show_doc_func == NULL)
  414. {
  415. gdbpy_print_stack ();
  416. return;
  417. }
  418. if (PyObject_HasAttr (obj, show_doc_func.get ()))
  419. {
  420. gdbpy_ref<> val_obj (PyUnicode_FromString (value));
  421. if (val_obj == NULL)
  422. {
  423. gdbpy_print_stack ();
  424. return;
  425. }
  426. show_doc_string = call_doc_function (obj, show_doc_func.get (),
  427. val_obj.get ());
  428. if (! show_doc_string)
  429. {
  430. gdbpy_print_stack ();
  431. return;
  432. }
  433. gdb_printf (file, "%s\n", show_doc_string.get ());
  434. }
  435. else
  436. {
  437. /* If there is no 'get_show_string' callback then we want to show
  438. something sensible here. In older versions of GDB (< 7.3) we
  439. didn't support 'get_show_string', and instead we just made use of
  440. GDB's builtin use of the show_doc. However, GDB's builtin
  441. show_doc adjustment is not i18n friendly, so, instead, we just
  442. print this generic string. */
  443. std::string cmd_path = full_cmd_name_without_first_prefix (c);
  444. gdb_printf (file, _("The current value of '%s' is \"%s\".\n"),
  445. cmd_path.c_str (), value);
  446. }
  447. }
  448. /* A helper function that dispatches to the appropriate add_setshow
  449. function. */
  450. static void
  451. add_setshow_generic (int parmclass, enum command_class cmdclass,
  452. gdb::unique_xmalloc_ptr<char> cmd_name,
  453. parmpy_object *self,
  454. const char *set_doc, const char *show_doc,
  455. const char *help_doc,
  456. struct cmd_list_element **set_list,
  457. struct cmd_list_element **show_list)
  458. {
  459. set_show_commands commands;
  460. switch (parmclass)
  461. {
  462. case var_boolean:
  463. commands = add_setshow_boolean_cmd (cmd_name.get (), cmdclass,
  464. &self->value.boolval, set_doc,
  465. show_doc, help_doc, get_set_value,
  466. get_show_value, set_list, show_list);
  467. break;
  468. case var_auto_boolean:
  469. commands = add_setshow_auto_boolean_cmd (cmd_name.get (), cmdclass,
  470. &self->value.autoboolval,
  471. set_doc, show_doc, help_doc,
  472. get_set_value, get_show_value,
  473. set_list, show_list);
  474. break;
  475. case var_uinteger:
  476. commands = add_setshow_uinteger_cmd (cmd_name.get (), cmdclass,
  477. &self->value.uintval, set_doc,
  478. show_doc, help_doc, get_set_value,
  479. get_show_value, set_list, show_list);
  480. break;
  481. case var_integer:
  482. commands = add_setshow_integer_cmd (cmd_name.get (), cmdclass,
  483. &self->value.intval, set_doc,
  484. show_doc, help_doc, get_set_value,
  485. get_show_value, set_list, show_list);
  486. break;
  487. case var_string:
  488. commands = add_setshow_string_cmd (cmd_name.get (), cmdclass,
  489. self->value.stringval, set_doc,
  490. show_doc, help_doc, get_set_value,
  491. get_show_value, set_list, show_list);
  492. break;
  493. case var_string_noescape:
  494. commands = add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
  495. self->value.stringval,
  496. set_doc, show_doc, help_doc,
  497. get_set_value, get_show_value,
  498. set_list, show_list);
  499. break;
  500. case var_optional_filename:
  501. commands = add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
  502. self->value.stringval,
  503. set_doc, show_doc, help_doc,
  504. get_set_value,
  505. get_show_value, set_list,
  506. show_list);
  507. break;
  508. case var_filename:
  509. commands = add_setshow_filename_cmd (cmd_name.get (), cmdclass,
  510. self->value.stringval, set_doc,
  511. show_doc, help_doc, get_set_value,
  512. get_show_value, set_list, show_list);
  513. break;
  514. case var_zinteger:
  515. commands = add_setshow_zinteger_cmd (cmd_name.get (), cmdclass,
  516. &self->value.intval, set_doc,
  517. show_doc, help_doc, get_set_value,
  518. get_show_value, set_list, show_list);
  519. break;
  520. case var_zuinteger:
  521. commands = add_setshow_zuinteger_cmd (cmd_name.get (), cmdclass,
  522. &self->value.uintval, set_doc,
  523. show_doc, help_doc, get_set_value,
  524. get_show_value, set_list,
  525. show_list);
  526. break;
  527. case var_zuinteger_unlimited:
  528. commands = add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
  529. &self->value.intval,
  530. set_doc, show_doc,
  531. help_doc, get_set_value,
  532. get_show_value, set_list,
  533. show_list);
  534. break;
  535. case var_enum:
  536. /* Initialize the value, just in case. */
  537. self->value.cstringval = self->enumeration[0];
  538. commands = add_setshow_enum_cmd (cmd_name.get (), cmdclass,
  539. self->enumeration,
  540. &self->value.cstringval, set_doc,
  541. show_doc, help_doc, get_set_value,
  542. get_show_value, set_list, show_list);
  543. break;
  544. default:
  545. gdb_assert_not_reached ("Unhandled parameter class.");
  546. }
  547. /* Register Python objects in both commands' context. */
  548. commands.set->set_context (self);
  549. commands.show->set_context (self);
  550. /* We (unfortunately) currently leak the command name. */
  551. cmd_name.release ();
  552. }
  553. /* A helper which computes enum values. Returns 1 on success. Returns 0 on
  554. error, with a python exception set. */
  555. static int
  556. compute_enum_values (parmpy_object *self, PyObject *enum_values)
  557. {
  558. Py_ssize_t size, i;
  559. if (! enum_values)
  560. {
  561. PyErr_SetString (PyExc_RuntimeError,
  562. _("An enumeration is required for PARAM_ENUM."));
  563. return 0;
  564. }
  565. if (! PySequence_Check (enum_values))
  566. {
  567. PyErr_SetString (PyExc_RuntimeError,
  568. _("The enumeration is not a sequence."));
  569. return 0;
  570. }
  571. size = PySequence_Size (enum_values);
  572. if (size < 0)
  573. return 0;
  574. if (size == 0)
  575. {
  576. PyErr_SetString (PyExc_RuntimeError,
  577. _("The enumeration is empty."));
  578. return 0;
  579. }
  580. gdb_argv holder (XCNEWVEC (char *, size + 1));
  581. char **enumeration = holder.get ();
  582. for (i = 0; i < size; ++i)
  583. {
  584. gdbpy_ref<> item (PySequence_GetItem (enum_values, i));
  585. if (item == NULL)
  586. return 0;
  587. if (! gdbpy_is_string (item.get ()))
  588. {
  589. PyErr_SetString (PyExc_RuntimeError,
  590. _("The enumeration item not a string."));
  591. return 0;
  592. }
  593. enumeration[i] = python_string_to_host_string (item.get ()).release ();
  594. if (enumeration[i] == NULL)
  595. return 0;
  596. }
  597. self->enumeration = const_cast<const char**> (holder.release ());
  598. return 1;
  599. }
  600. /* Object initializer; sets up gdb-side structures for command.
  601. Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
  602. NAME is the name of the parameter. It may consist of multiple
  603. words, in which case the final word is the name of the new command,
  604. and earlier words must be prefix commands.
  605. CMDCLASS is the kind of command. It should be one of the COMMAND_*
  606. constants defined in the gdb module.
  607. PARMCLASS is the type of the parameter. It should be one of the
  608. PARAM_* constants defined in the gdb module.
  609. If PARMCLASS is PARAM_ENUM, then the final argument should be a
  610. collection of strings. These strings are the valid values for this
  611. parameter.
  612. The documentation for the parameter is taken from the doc string
  613. for the python class.
  614. Returns -1 on error, with a python exception set. */
  615. static int
  616. parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
  617. {
  618. parmpy_object *obj = (parmpy_object *) self;
  619. const char *name;
  620. gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
  621. int parmclass, cmdtype;
  622. PyObject *enum_values = NULL;
  623. struct cmd_list_element **set_list, **show_list;
  624. if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
  625. &enum_values))
  626. return -1;
  627. if (cmdtype != no_class && cmdtype != class_run
  628. && cmdtype != class_vars && cmdtype != class_stack
  629. && cmdtype != class_files && cmdtype != class_support
  630. && cmdtype != class_info && cmdtype != class_breakpoint
  631. && cmdtype != class_trace && cmdtype != class_obscure
  632. && cmdtype != class_maintenance)
  633. {
  634. PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
  635. return -1;
  636. }
  637. if (parmclass != var_boolean /* ARI: var_boolean */
  638. && parmclass != var_auto_boolean
  639. && parmclass != var_uinteger && parmclass != var_integer
  640. && parmclass != var_string && parmclass != var_string_noescape
  641. && parmclass != var_optional_filename && parmclass != var_filename
  642. && parmclass != var_zinteger && parmclass != var_zuinteger
  643. && parmclass != var_zuinteger_unlimited && parmclass != var_enum)
  644. {
  645. PyErr_SetString (PyExc_RuntimeError,
  646. _("Invalid parameter class argument."));
  647. return -1;
  648. }
  649. if (enum_values && parmclass != var_enum)
  650. {
  651. PyErr_SetString (PyExc_RuntimeError,
  652. _("Only PARAM_ENUM accepts a fourth argument."));
  653. return -1;
  654. }
  655. if (parmclass == var_enum)
  656. {
  657. if (! compute_enum_values (obj, enum_values))
  658. return -1;
  659. }
  660. else
  661. obj->enumeration = NULL;
  662. obj->type = (enum var_types) parmclass;
  663. memset (&obj->value, 0, sizeof (obj->value));
  664. if (var_type_uses<std::string> (obj->type))
  665. obj->value.stringval = new std::string;
  666. gdb::unique_xmalloc_ptr<char> cmd_name
  667. = gdbpy_parse_command_name (name, &set_list, &setlist);
  668. if (cmd_name == nullptr)
  669. return -1;
  670. cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
  671. if (cmd_name == nullptr)
  672. return -1;
  673. set_doc = get_doc_string (self, doc_string_set, name);
  674. show_doc = get_doc_string (self, doc_string_show, name);
  675. doc = get_doc_string (self, doc_string_description, cmd_name.get ());
  676. Py_INCREF (self);
  677. try
  678. {
  679. add_setshow_generic (parmclass, (enum command_class) cmdtype,
  680. std::move (cmd_name), obj,
  681. set_doc.get (), show_doc.get (),
  682. doc.get (), set_list, show_list);
  683. }
  684. catch (const gdb_exception &except)
  685. {
  686. Py_DECREF (self);
  687. gdbpy_convert_exception (except);
  688. return -1;
  689. }
  690. return 0;
  691. }
  692. /* Deallocate function for a gdb.Parameter. */
  693. static void
  694. parmpy_dealloc (PyObject *obj)
  695. {
  696. parmpy_object *parm_obj = (parmpy_object *) obj;
  697. if (var_type_uses<std::string> (parm_obj->type))
  698. delete parm_obj->value.stringval;
  699. }
  700. /* Initialize the 'parameters' module. */
  701. int
  702. gdbpy_initialize_parameters (void)
  703. {
  704. int i;
  705. parmpy_object_type.tp_new = PyType_GenericNew;
  706. if (PyType_Ready (&parmpy_object_type) < 0)
  707. return -1;
  708. set_doc_cst = PyUnicode_FromString ("set_doc");
  709. if (! set_doc_cst)
  710. return -1;
  711. show_doc_cst = PyUnicode_FromString ("show_doc");
  712. if (! show_doc_cst)
  713. return -1;
  714. for (i = 0; parm_constants[i].name; ++i)
  715. {
  716. if (PyModule_AddIntConstant (gdb_module,
  717. parm_constants[i].name,
  718. parm_constants[i].value) < 0)
  719. return -1;
  720. }
  721. return gdb_pymodule_addobject (gdb_module, "Parameter",
  722. (PyObject *) &parmpy_object_type);
  723. }
  724. PyTypeObject parmpy_object_type =
  725. {
  726. PyVarObject_HEAD_INIT (NULL, 0)
  727. "gdb.Parameter", /*tp_name*/
  728. sizeof (parmpy_object), /*tp_basicsize*/
  729. 0, /*tp_itemsize*/
  730. parmpy_dealloc, /*tp_dealloc*/
  731. 0, /*tp_print*/
  732. 0, /*tp_getattr*/
  733. 0, /*tp_setattr*/
  734. 0, /*tp_compare*/
  735. 0, /*tp_repr*/
  736. 0, /*tp_as_number*/
  737. 0, /*tp_as_sequence*/
  738. 0, /*tp_as_mapping*/
  739. 0, /*tp_hash */
  740. 0, /*tp_call*/
  741. 0, /*tp_str*/
  742. get_attr, /*tp_getattro*/
  743. set_attr, /*tp_setattro*/
  744. 0, /*tp_as_buffer*/
  745. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  746. "GDB parameter object", /* tp_doc */
  747. 0, /* tp_traverse */
  748. 0, /* tp_clear */
  749. 0, /* tp_richcompare */
  750. 0, /* tp_weaklistoffset */
  751. 0, /* tp_iter */
  752. 0, /* tp_iternext */
  753. 0, /* tp_methods */
  754. 0, /* tp_members */
  755. 0, /* tp_getset */
  756. 0, /* tp_base */
  757. 0, /* tp_dict */
  758. 0, /* tp_descr_get */
  759. 0, /* tp_descr_set */
  760. 0, /* tp_dictoffset */
  761. parmpy_init, /* tp_init */
  762. 0, /* tp_alloc */
  763. };