varobj.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. /* Implementation of the GDB variable objects API.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "defs.h"
  14. #include "value.h"
  15. #include "expression.h"
  16. #include "frame.h"
  17. #include "language.h"
  18. #include "gdbcmd.h"
  19. #include "block.h"
  20. #include "valprint.h"
  21. #include "gdbsupport/gdb_regex.h"
  22. #include "varobj.h"
  23. #include "gdbthread.h"
  24. #include "inferior.h"
  25. #include "varobj-iter.h"
  26. #include "parser-defs.h"
  27. #include "gdbarch.h"
  28. #include <algorithm>
  29. #if HAVE_PYTHON
  30. #include "python/python.h"
  31. #include "python/python-internal.h"
  32. #else
  33. typedef int PyObject;
  34. #endif
  35. /* See varobj.h. */
  36. unsigned int varobjdebug = 0;
  37. static void
  38. show_varobjdebug (struct ui_file *file, int from_tty,
  39. struct cmd_list_element *c, const char *value)
  40. {
  41. gdb_printf (file, _("Varobj debugging is %s.\n"), value);
  42. }
  43. /* String representations of gdb's format codes. */
  44. const char *varobj_format_string[] =
  45. { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
  46. /* True if we want to allow Python-based pretty-printing. */
  47. static bool pretty_printing = false;
  48. void
  49. varobj_enable_pretty_printing (void)
  50. {
  51. pretty_printing = true;
  52. }
  53. /* Data structures */
  54. /* Every root variable has one of these structures saved in its
  55. varobj. */
  56. struct varobj_root
  57. {
  58. /* The expression for this parent. */
  59. expression_up exp;
  60. /* Block for which this expression is valid. */
  61. const struct block *valid_block = NULL;
  62. /* The frame for this expression. This field is set iff valid_block is
  63. not NULL. */
  64. struct frame_id frame = null_frame_id;
  65. /* The global thread ID that this varobj_root belongs to. This field
  66. is only valid if valid_block is not NULL.
  67. When not 0, indicates which thread 'frame' belongs to.
  68. When 0, indicates that the thread list was empty when the varobj_root
  69. was created. */
  70. int thread_id = 0;
  71. /* If true, the -var-update always recomputes the value in the
  72. current thread and frame. Otherwise, variable object is
  73. always updated in the specific scope/thread/frame. */
  74. bool floating = false;
  75. /* Flag that indicates validity: set to false when this varobj_root refers
  76. to symbols that do not exist anymore. */
  77. bool is_valid = true;
  78. /* Language-related operations for this variable and its
  79. children. */
  80. const struct lang_varobj_ops *lang_ops = NULL;
  81. /* The varobj for this root node. */
  82. struct varobj *rootvar = NULL;
  83. };
  84. /* Dynamic part of varobj. */
  85. struct varobj_dynamic
  86. {
  87. /* Whether the children of this varobj were requested. This field is
  88. used to decide if dynamic varobj should recompute their children.
  89. In the event that the frontend never asked for the children, we
  90. can avoid that. */
  91. bool children_requested = false;
  92. /* The pretty-printer constructor. If NULL, then the default
  93. pretty-printer will be looked up. If None, then no
  94. pretty-printer will be installed. */
  95. PyObject *constructor = NULL;
  96. /* The pretty-printer that has been constructed. If NULL, then a
  97. new printer object is needed, and one will be constructed. */
  98. PyObject *pretty_printer = NULL;
  99. /* The iterator returned by the printer's 'children' method, or NULL
  100. if not available. */
  101. std::unique_ptr<varobj_iter> child_iter;
  102. /* We request one extra item from the iterator, so that we can
  103. report to the caller whether there are more items than we have
  104. already reported. However, we don't want to install this value
  105. when we read it, because that will mess up future updates. So,
  106. we stash it here instead. */
  107. std::unique_ptr<varobj_item> saved_item;
  108. };
  109. /* Private function prototypes */
  110. /* Helper functions for the above subcommands. */
  111. static int delete_variable (struct varobj *, bool);
  112. static void delete_variable_1 (int *, struct varobj *, bool, bool);
  113. static void install_variable (struct varobj *);
  114. static void uninstall_variable (struct varobj *);
  115. static struct varobj *create_child (struct varobj *, int, std::string &);
  116. static struct varobj *
  117. create_child_with_value (struct varobj *parent, int index,
  118. struct varobj_item *item);
  119. /* Utility routines */
  120. static enum varobj_display_formats variable_default_display (struct varobj *);
  121. static bool update_type_if_necessary (struct varobj *var,
  122. struct value *new_value);
  123. static bool install_new_value (struct varobj *var, struct value *value,
  124. bool initial);
  125. /* Language-specific routines. */
  126. static int number_of_children (const struct varobj *);
  127. static std::string name_of_variable (const struct varobj *);
  128. static std::string name_of_child (struct varobj *, int);
  129. static struct value *value_of_root (struct varobj **var_handle, bool *);
  130. static struct value *value_of_child (const struct varobj *parent, int index);
  131. static std::string my_value_of_variable (struct varobj *var,
  132. enum varobj_display_formats format);
  133. static bool is_root_p (const struct varobj *var);
  134. static struct varobj *varobj_add_child (struct varobj *var,
  135. struct varobj_item *item);
  136. /* Private data */
  137. /* Mappings of varobj_display_formats enums to gdb's format codes. */
  138. static int format_code[] = { 0, 't', 'd', 'x', 'o', 'z' };
  139. /* List of root variable objects. */
  140. static std::list<struct varobj_root *> rootlist;
  141. /* Pointer to the varobj hash table (built at run time). */
  142. static htab_t varobj_table;
  143. /* API Implementation */
  144. static bool
  145. is_root_p (const struct varobj *var)
  146. {
  147. return (var->root->rootvar == var);
  148. }
  149. #ifdef HAVE_PYTHON
  150. /* See python-internal.h. */
  151. gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
  152. : gdbpy_enter (var->root->exp->gdbarch, var->root->exp->language_defn)
  153. {
  154. }
  155. #endif
  156. /* Return the full FRAME which corresponds to the given CORE_ADDR
  157. or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
  158. static struct frame_info *
  159. find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
  160. {
  161. struct frame_info *frame = NULL;
  162. if (frame_addr == (CORE_ADDR) 0)
  163. return NULL;
  164. for (frame = get_current_frame ();
  165. frame != NULL;
  166. frame = get_prev_frame (frame))
  167. {
  168. /* The CORE_ADDR we get as argument was parsed from a string GDB
  169. output as $fp. This output got truncated to gdbarch_addr_bit.
  170. Truncate the frame base address in the same manner before
  171. comparing it against our argument. */
  172. CORE_ADDR frame_base = get_frame_base_address (frame);
  173. int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
  174. if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  175. frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
  176. if (frame_base == frame_addr)
  177. return frame;
  178. }
  179. return NULL;
  180. }
  181. /* Creates a varobj (not its children). */
  182. struct varobj *
  183. varobj_create (const char *objname,
  184. const char *expression, CORE_ADDR frame, enum varobj_type type)
  185. {
  186. /* Fill out a varobj structure for the (root) variable being constructed. */
  187. std::unique_ptr<varobj> var (new varobj (new varobj_root));
  188. if (expression != NULL)
  189. {
  190. struct frame_info *fi;
  191. struct frame_id old_id = null_frame_id;
  192. const struct block *block;
  193. const char *p;
  194. struct value *value = NULL;
  195. CORE_ADDR pc;
  196. /* Parse and evaluate the expression, filling in as much of the
  197. variable's data as possible. */
  198. if (has_stack_frames ())
  199. {
  200. /* Allow creator to specify context of variable. */
  201. if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
  202. fi = get_selected_frame (NULL);
  203. else
  204. /* FIXME: cagney/2002-11-23: This code should be doing a
  205. lookup using the frame ID and not just the frame's
  206. ``address''. This, of course, means an interface
  207. change. However, with out that interface change ISAs,
  208. such as the ia64 with its two stacks, won't work.
  209. Similar goes for the case where there is a frameless
  210. function. */
  211. fi = find_frame_addr_in_frame_chain (frame);
  212. }
  213. else
  214. fi = NULL;
  215. if (type == USE_SELECTED_FRAME)
  216. var->root->floating = true;
  217. pc = 0;
  218. block = NULL;
  219. if (fi != NULL)
  220. {
  221. block = get_frame_block (fi, 0);
  222. pc = get_frame_pc (fi);
  223. }
  224. p = expression;
  225. innermost_block_tracker tracker (INNERMOST_BLOCK_FOR_SYMBOLS
  226. | INNERMOST_BLOCK_FOR_REGISTERS);
  227. /* Wrap the call to parse expression, so we can
  228. return a sensible error. */
  229. try
  230. {
  231. var->root->exp = parse_exp_1 (&p, pc, block, 0, &tracker);
  232. }
  233. catch (const gdb_exception_error &except)
  234. {
  235. return NULL;
  236. }
  237. /* Don't allow variables to be created for types. */
  238. enum exp_opcode opcode = var->root->exp->first_opcode ();
  239. if (opcode == OP_TYPE
  240. || opcode == OP_TYPEOF
  241. || opcode == OP_DECLTYPE)
  242. {
  243. gdb_printf (gdb_stderr, "Attempt to use a type name"
  244. " as an expression.\n");
  245. return NULL;
  246. }
  247. var->format = variable_default_display (var.get ());
  248. var->root->valid_block =
  249. var->root->floating ? NULL : tracker.block ();
  250. var->name = expression;
  251. /* For a root var, the name and the expr are the same. */
  252. var->path_expr = expression;
  253. /* When the frame is different from the current frame,
  254. we must select the appropriate frame before parsing
  255. the expression, otherwise the value will not be current.
  256. Since select_frame is so benign, just call it for all cases. */
  257. if (var->root->valid_block)
  258. {
  259. /* User could specify explicit FRAME-ADDR which was not found but
  260. EXPRESSION is frame specific and we would not be able to evaluate
  261. it correctly next time. With VALID_BLOCK set we must also set
  262. FRAME and THREAD_ID. */
  263. if (fi == NULL)
  264. error (_("Failed to find the specified frame"));
  265. var->root->frame = get_frame_id (fi);
  266. var->root->thread_id = inferior_thread ()->global_num;
  267. old_id = get_frame_id (get_selected_frame (NULL));
  268. select_frame (fi);
  269. }
  270. /* We definitely need to catch errors here.
  271. If evaluate_expression succeeds we got the value we wanted.
  272. But if it fails, we still go on with a call to evaluate_type(). */
  273. try
  274. {
  275. value = evaluate_expression (var->root->exp.get ());
  276. }
  277. catch (const gdb_exception_error &except)
  278. {
  279. /* Error getting the value. Try to at least get the
  280. right type. */
  281. struct value *type_only_value = evaluate_type (var->root->exp.get ());
  282. var->type = value_type (type_only_value);
  283. }
  284. if (value != NULL)
  285. {
  286. int real_type_found = 0;
  287. var->type = value_actual_type (value, 0, &real_type_found);
  288. if (real_type_found)
  289. value = value_cast (var->type, value);
  290. }
  291. /* Set language info */
  292. var->root->lang_ops = var->root->exp->language_defn->varobj_ops ();
  293. install_new_value (var.get (), value, 1 /* Initial assignment */);
  294. /* Set ourselves as our root. */
  295. var->root->rootvar = var.get ();
  296. /* Reset the selected frame. */
  297. if (frame_id_p (old_id))
  298. select_frame (frame_find_by_id (old_id));
  299. }
  300. /* If the variable object name is null, that means this
  301. is a temporary variable, so don't install it. */
  302. if ((var != NULL) && (objname != NULL))
  303. {
  304. var->obj_name = objname;
  305. install_variable (var.get ());
  306. }
  307. return var.release ();
  308. }
  309. /* Generates an unique name that can be used for a varobj. */
  310. std::string
  311. varobj_gen_name (void)
  312. {
  313. static int id = 0;
  314. /* Generate a name for this object. */
  315. id++;
  316. return string_printf ("var%d", id);
  317. }
  318. /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
  319. error if OBJNAME cannot be found. */
  320. struct varobj *
  321. varobj_get_handle (const char *objname)
  322. {
  323. varobj *var = (varobj *) htab_find_with_hash (varobj_table, objname,
  324. htab_hash_string (objname));
  325. if (var == NULL)
  326. error (_("Variable object not found"));
  327. return var;
  328. }
  329. /* Given the handle, return the name of the object. */
  330. const char *
  331. varobj_get_objname (const struct varobj *var)
  332. {
  333. return var->obj_name.c_str ();
  334. }
  335. /* Given the handle, return the expression represented by the
  336. object. */
  337. std::string
  338. varobj_get_expression (const struct varobj *var)
  339. {
  340. return name_of_variable (var);
  341. }
  342. /* See varobj.h. */
  343. int
  344. varobj_delete (struct varobj *var, bool only_children)
  345. {
  346. return delete_variable (var, only_children);
  347. }
  348. #if HAVE_PYTHON
  349. /* Convenience function for varobj_set_visualizer. Instantiate a
  350. pretty-printer for a given value. */
  351. static PyObject *
  352. instantiate_pretty_printer (PyObject *constructor, struct value *value)
  353. {
  354. gdbpy_ref<> val_obj (value_to_value_object (value));
  355. if (val_obj == nullptr)
  356. return NULL;
  357. return PyObject_CallFunctionObjArgs (constructor, val_obj.get (), NULL);
  358. }
  359. #endif
  360. /* Set/Get variable object display format. */
  361. enum varobj_display_formats
  362. varobj_set_display_format (struct varobj *var,
  363. enum varobj_display_formats format)
  364. {
  365. switch (format)
  366. {
  367. case FORMAT_NATURAL:
  368. case FORMAT_BINARY:
  369. case FORMAT_DECIMAL:
  370. case FORMAT_HEXADECIMAL:
  371. case FORMAT_OCTAL:
  372. case FORMAT_ZHEXADECIMAL:
  373. var->format = format;
  374. break;
  375. default:
  376. var->format = variable_default_display (var);
  377. }
  378. if (varobj_value_is_changeable_p (var)
  379. && var->value != nullptr && !value_lazy (var->value.get ()))
  380. {
  381. var->print_value = varobj_value_get_print_value (var->value.get (),
  382. var->format, var);
  383. }
  384. return var->format;
  385. }
  386. enum varobj_display_formats
  387. varobj_get_display_format (const struct varobj *var)
  388. {
  389. return var->format;
  390. }
  391. gdb::unique_xmalloc_ptr<char>
  392. varobj_get_display_hint (const struct varobj *var)
  393. {
  394. gdb::unique_xmalloc_ptr<char> result;
  395. #if HAVE_PYTHON
  396. if (!gdb_python_initialized)
  397. return NULL;
  398. gdbpy_enter_varobj enter_py (var);
  399. if (var->dynamic->pretty_printer != NULL)
  400. result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
  401. #endif
  402. return result;
  403. }
  404. /* Return true if the varobj has items after TO, false otherwise. */
  405. bool
  406. varobj_has_more (const struct varobj *var, int to)
  407. {
  408. if (var->children.size () > to)
  409. return true;
  410. return ((to == -1 || var->children.size () == to)
  411. && (var->dynamic->saved_item != NULL));
  412. }
  413. /* If the variable object is bound to a specific thread, that
  414. is its evaluation can always be done in context of a frame
  415. inside that thread, returns GDB id of the thread -- which
  416. is always positive. Otherwise, returns -1. */
  417. int
  418. varobj_get_thread_id (const struct varobj *var)
  419. {
  420. if (var->root->valid_block && var->root->thread_id > 0)
  421. return var->root->thread_id;
  422. else
  423. return -1;
  424. }
  425. void
  426. varobj_set_frozen (struct varobj *var, bool frozen)
  427. {
  428. /* When a variable is unfrozen, we don't fetch its value.
  429. The 'not_fetched' flag remains set, so next -var-update
  430. won't complain.
  431. We don't fetch the value, because for structures the client
  432. should do -var-update anyway. It would be bad to have different
  433. client-size logic for structure and other types. */
  434. var->frozen = frozen;
  435. }
  436. bool
  437. varobj_get_frozen (const struct varobj *var)
  438. {
  439. return var->frozen;
  440. }
  441. /* A helper function that updates the contents of FROM and TO based on the
  442. size of the vector CHILDREN. If the contents of either FROM or TO are
  443. negative the entire range is used. */
  444. void
  445. varobj_restrict_range (const std::vector<varobj *> &children,
  446. int *from, int *to)
  447. {
  448. int len = children.size ();
  449. if (*from < 0 || *to < 0)
  450. {
  451. *from = 0;
  452. *to = len;
  453. }
  454. else
  455. {
  456. if (*from > len)
  457. *from = len;
  458. if (*to > len)
  459. *to = len;
  460. if (*from > *to)
  461. *from = *to;
  462. }
  463. }
  464. /* A helper for update_dynamic_varobj_children that installs a new
  465. child when needed. */
  466. static void
  467. install_dynamic_child (struct varobj *var,
  468. std::vector<varobj *> *changed,
  469. std::vector<varobj *> *type_changed,
  470. std::vector<varobj *> *newobj,
  471. std::vector<varobj *> *unchanged,
  472. bool *cchanged,
  473. int index,
  474. struct varobj_item *item)
  475. {
  476. if (var->children.size () < index + 1)
  477. {
  478. /* There's no child yet. */
  479. struct varobj *child = varobj_add_child (var, item);
  480. if (newobj != NULL)
  481. {
  482. newobj->push_back (child);
  483. *cchanged = true;
  484. }
  485. }
  486. else
  487. {
  488. varobj *existing = var->children[index];
  489. bool type_updated = update_type_if_necessary (existing,
  490. item->value.get ());
  491. if (type_updated)
  492. {
  493. if (type_changed != NULL)
  494. type_changed->push_back (existing);
  495. }
  496. if (install_new_value (existing, item->value.get (), 0))
  497. {
  498. if (!type_updated && changed != NULL)
  499. changed->push_back (existing);
  500. }
  501. else if (!type_updated && unchanged != NULL)
  502. unchanged->push_back (existing);
  503. }
  504. }
  505. #if HAVE_PYTHON
  506. static bool
  507. dynamic_varobj_has_child_method (const struct varobj *var)
  508. {
  509. PyObject *printer = var->dynamic->pretty_printer;
  510. if (!gdb_python_initialized)
  511. return false;
  512. gdbpy_enter_varobj enter_py (var);
  513. return PyObject_HasAttr (printer, gdbpy_children_cst);
  514. }
  515. #endif
  516. /* A factory for creating dynamic varobj's iterators. Returns an
  517. iterator object suitable for iterating over VAR's children. */
  518. static std::unique_ptr<varobj_iter>
  519. varobj_get_iterator (struct varobj *var)
  520. {
  521. #if HAVE_PYTHON
  522. if (var->dynamic->pretty_printer)
  523. return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
  524. #endif
  525. gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
  526. }
  527. static bool
  528. update_dynamic_varobj_children (struct varobj *var,
  529. std::vector<varobj *> *changed,
  530. std::vector<varobj *> *type_changed,
  531. std::vector<varobj *> *newobj,
  532. std::vector<varobj *> *unchanged,
  533. bool *cchanged,
  534. bool update_children,
  535. int from,
  536. int to)
  537. {
  538. int i;
  539. *cchanged = false;
  540. if (update_children || var->dynamic->child_iter == NULL)
  541. {
  542. var->dynamic->child_iter = varobj_get_iterator (var);
  543. var->dynamic->saved_item.reset (nullptr);
  544. i = 0;
  545. if (var->dynamic->child_iter == NULL)
  546. return false;
  547. }
  548. else
  549. i = var->children.size ();
  550. /* We ask for one extra child, so that MI can report whether there
  551. are more children. */
  552. for (; to < 0 || i < to + 1; ++i)
  553. {
  554. std::unique_ptr<varobj_item> item;
  555. /* See if there was a leftover from last time. */
  556. if (var->dynamic->saved_item != NULL)
  557. item = std::move (var->dynamic->saved_item);
  558. else
  559. item = var->dynamic->child_iter->next ();
  560. if (item == NULL)
  561. {
  562. /* Iteration is done. Remove iterator from VAR. */
  563. var->dynamic->child_iter.reset (nullptr);
  564. break;
  565. }
  566. /* We don't want to push the extra child on any report list. */
  567. if (to < 0 || i < to)
  568. {
  569. bool can_mention = from < 0 || i >= from;
  570. install_dynamic_child (var, can_mention ? changed : NULL,
  571. can_mention ? type_changed : NULL,
  572. can_mention ? newobj : NULL,
  573. can_mention ? unchanged : NULL,
  574. can_mention ? cchanged : NULL, i,
  575. item.get ());
  576. }
  577. else
  578. {
  579. var->dynamic->saved_item = std::move (item);
  580. /* We want to truncate the child list just before this
  581. element. */
  582. break;
  583. }
  584. }
  585. if (i < var->children.size ())
  586. {
  587. *cchanged = true;
  588. for (int j = i; j < var->children.size (); ++j)
  589. varobj_delete (var->children[j], 0);
  590. var->children.resize (i);
  591. }
  592. /* If there are fewer children than requested, note that the list of
  593. children changed. */
  594. if (to >= 0 && var->children.size () < to)
  595. *cchanged = true;
  596. var->num_children = var->children.size ();
  597. return true;
  598. }
  599. int
  600. varobj_get_num_children (struct varobj *var)
  601. {
  602. if (var->num_children == -1)
  603. {
  604. if (varobj_is_dynamic_p (var))
  605. {
  606. bool dummy;
  607. /* If we have a dynamic varobj, don't report -1 children.
  608. So, try to fetch some children first. */
  609. update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
  610. false, 0, 0);
  611. }
  612. else
  613. var->num_children = number_of_children (var);
  614. }
  615. return var->num_children >= 0 ? var->num_children : 0;
  616. }
  617. /* Creates a list of the immediate children of a variable object;
  618. the return code is the number of such children or -1 on error. */
  619. const std::vector<varobj *> &
  620. varobj_list_children (struct varobj *var, int *from, int *to)
  621. {
  622. var->dynamic->children_requested = true;
  623. if (varobj_is_dynamic_p (var))
  624. {
  625. bool children_changed;
  626. /* This, in theory, can result in the number of children changing without
  627. frontend noticing. But well, calling -var-list-children on the same
  628. varobj twice is not something a sane frontend would do. */
  629. update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
  630. &children_changed, false, 0, *to);
  631. varobj_restrict_range (var->children, from, to);
  632. return var->children;
  633. }
  634. if (var->num_children == -1)
  635. var->num_children = number_of_children (var);
  636. /* If that failed, give up. */
  637. if (var->num_children == -1)
  638. return var->children;
  639. /* If we're called when the list of children is not yet initialized,
  640. allocate enough elements in it. */
  641. while (var->children.size () < var->num_children)
  642. var->children.push_back (NULL);
  643. for (int i = 0; i < var->num_children; i++)
  644. {
  645. if (var->children[i] == NULL)
  646. {
  647. /* Either it's the first call to varobj_list_children for
  648. this variable object, and the child was never created,
  649. or it was explicitly deleted by the client. */
  650. std::string name = name_of_child (var, i);
  651. var->children[i] = create_child (var, i, name);
  652. }
  653. }
  654. varobj_restrict_range (var->children, from, to);
  655. return var->children;
  656. }
  657. static struct varobj *
  658. varobj_add_child (struct varobj *var, struct varobj_item *item)
  659. {
  660. varobj *v = create_child_with_value (var, var->children.size (), item);
  661. var->children.push_back (v);
  662. return v;
  663. }
  664. /* Obtain the type of an object Variable as a string similar to the one gdb
  665. prints on the console. The caller is responsible for freeing the string.
  666. */
  667. std::string
  668. varobj_get_type (struct varobj *var)
  669. {
  670. /* For the "fake" variables, do not return a type. (Its type is
  671. NULL, too.)
  672. Do not return a type for invalid variables as well. */
  673. if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
  674. return std::string ();
  675. return type_to_string (var->type);
  676. }
  677. /* Obtain the type of an object variable. */
  678. struct type *
  679. varobj_get_gdb_type (const struct varobj *var)
  680. {
  681. return var->type;
  682. }
  683. /* Is VAR a path expression parent, i.e., can it be used to construct
  684. a valid path expression? */
  685. static bool
  686. is_path_expr_parent (const struct varobj *var)
  687. {
  688. gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
  689. return var->root->lang_ops->is_path_expr_parent (var);
  690. }
  691. /* Is VAR a path expression parent, i.e., can it be used to construct
  692. a valid path expression? By default we assume any VAR can be a path
  693. parent. */
  694. bool
  695. varobj_default_is_path_expr_parent (const struct varobj *var)
  696. {
  697. return true;
  698. }
  699. /* Return the path expression parent for VAR. */
  700. const struct varobj *
  701. varobj_get_path_expr_parent (const struct varobj *var)
  702. {
  703. const struct varobj *parent = var;
  704. while (!is_root_p (parent) && !is_path_expr_parent (parent))
  705. parent = parent->parent;
  706. /* Computation of full rooted expression for children of dynamic
  707. varobjs is not supported. */
  708. if (varobj_is_dynamic_p (parent))
  709. error (_("Invalid variable object (child of a dynamic varobj)"));
  710. return parent;
  711. }
  712. /* Return a pointer to the full rooted expression of varobj VAR.
  713. If it has not been computed yet, compute it. */
  714. const char *
  715. varobj_get_path_expr (const struct varobj *var)
  716. {
  717. if (var->path_expr.empty ())
  718. {
  719. /* For root varobjs, we initialize path_expr
  720. when creating varobj, so here it should be
  721. child varobj. */
  722. struct varobj *mutable_var = (struct varobj *) var;
  723. gdb_assert (!is_root_p (var));
  724. mutable_var->path_expr = (*var->root->lang_ops->path_expr_of_child) (var);
  725. }
  726. return var->path_expr.c_str ();
  727. }
  728. const struct language_defn *
  729. varobj_get_language (const struct varobj *var)
  730. {
  731. return var->root->exp->language_defn;
  732. }
  733. int
  734. varobj_get_attributes (const struct varobj *var)
  735. {
  736. int attributes = 0;
  737. if (varobj_editable_p (var))
  738. /* FIXME: define masks for attributes. */
  739. attributes |= 0x00000001; /* Editable */
  740. return attributes;
  741. }
  742. /* Return true if VAR is a dynamic varobj. */
  743. bool
  744. varobj_is_dynamic_p (const struct varobj *var)
  745. {
  746. return var->dynamic->pretty_printer != NULL;
  747. }
  748. std::string
  749. varobj_get_formatted_value (struct varobj *var,
  750. enum varobj_display_formats format)
  751. {
  752. return my_value_of_variable (var, format);
  753. }
  754. std::string
  755. varobj_get_value (struct varobj *var)
  756. {
  757. return my_value_of_variable (var, var->format);
  758. }
  759. /* Set the value of an object variable (if it is editable) to the
  760. value of the given expression. */
  761. /* Note: Invokes functions that can call error(). */
  762. bool
  763. varobj_set_value (struct varobj *var, const char *expression)
  764. {
  765. struct value *val = NULL; /* Initialize to keep gcc happy. */
  766. /* The argument "expression" contains the variable's new value.
  767. We need to first construct a legal expression for this -- ugh! */
  768. /* Does this cover all the bases? */
  769. struct value *value = NULL; /* Initialize to keep gcc happy. */
  770. int saved_input_radix = input_radix;
  771. const char *s = expression;
  772. gdb_assert (varobj_editable_p (var));
  773. input_radix = 10; /* ALWAYS reset to decimal temporarily. */
  774. expression_up exp = parse_exp_1 (&s, 0, 0, 0);
  775. try
  776. {
  777. value = evaluate_expression (exp.get ());
  778. }
  779. catch (const gdb_exception_error &except)
  780. {
  781. /* We cannot proceed without a valid expression. */
  782. return false;
  783. }
  784. /* All types that are editable must also be changeable. */
  785. gdb_assert (varobj_value_is_changeable_p (var));
  786. /* The value of a changeable variable object must not be lazy. */
  787. gdb_assert (!value_lazy (var->value.get ()));
  788. /* Need to coerce the input. We want to check if the
  789. value of the variable object will be different
  790. after assignment, and the first thing value_assign
  791. does is coerce the input.
  792. For example, if we are assigning an array to a pointer variable we
  793. should compare the pointer with the array's address, not with the
  794. array's content. */
  795. value = coerce_array (value);
  796. /* The new value may be lazy. value_assign, or
  797. rather value_contents, will take care of this. */
  798. try
  799. {
  800. val = value_assign (var->value.get (), value);
  801. }
  802. catch (const gdb_exception_error &except)
  803. {
  804. return false;
  805. }
  806. /* If the value has changed, record it, so that next -var-update can
  807. report this change. If a variable had a value of '1', we've set it
  808. to '333' and then set again to '1', when -var-update will report this
  809. variable as changed -- because the first assignment has set the
  810. 'updated' flag. There's no need to optimize that, because return value
  811. of -var-update should be considered an approximation. */
  812. var->updated = install_new_value (var, val, false /* Compare values. */);
  813. input_radix = saved_input_radix;
  814. return true;
  815. }
  816. #if HAVE_PYTHON
  817. /* A helper function to install a constructor function and visualizer
  818. in a varobj_dynamic. */
  819. static void
  820. install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
  821. PyObject *visualizer)
  822. {
  823. Py_XDECREF (var->constructor);
  824. var->constructor = constructor;
  825. Py_XDECREF (var->pretty_printer);
  826. var->pretty_printer = visualizer;
  827. var->child_iter.reset (nullptr);
  828. }
  829. /* Install the default visualizer for VAR. */
  830. static void
  831. install_default_visualizer (struct varobj *var)
  832. {
  833. /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
  834. if (CPLUS_FAKE_CHILD (var))
  835. return;
  836. if (pretty_printing)
  837. {
  838. gdbpy_ref<> pretty_printer;
  839. if (var->value != nullptr)
  840. {
  841. pretty_printer = gdbpy_get_varobj_pretty_printer (var->value.get ());
  842. if (pretty_printer == nullptr)
  843. {
  844. gdbpy_print_stack ();
  845. error (_("Cannot instantiate printer for default visualizer"));
  846. }
  847. }
  848. if (pretty_printer == Py_None)
  849. pretty_printer.reset (nullptr);
  850. install_visualizer (var->dynamic, NULL, pretty_printer.release ());
  851. }
  852. }
  853. /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
  854. make a new object. */
  855. static void
  856. construct_visualizer (struct varobj *var, PyObject *constructor)
  857. {
  858. PyObject *pretty_printer;
  859. /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
  860. if (CPLUS_FAKE_CHILD (var))
  861. return;
  862. Py_INCREF (constructor);
  863. if (constructor == Py_None)
  864. pretty_printer = NULL;
  865. else
  866. {
  867. pretty_printer = instantiate_pretty_printer (constructor,
  868. var->value.get ());
  869. if (! pretty_printer)
  870. {
  871. gdbpy_print_stack ();
  872. Py_DECREF (constructor);
  873. constructor = Py_None;
  874. Py_INCREF (constructor);
  875. }
  876. if (pretty_printer == Py_None)
  877. {
  878. Py_DECREF (pretty_printer);
  879. pretty_printer = NULL;
  880. }
  881. }
  882. install_visualizer (var->dynamic, constructor, pretty_printer);
  883. }
  884. #endif /* HAVE_PYTHON */
  885. /* A helper function for install_new_value. This creates and installs
  886. a visualizer for VAR, if appropriate. */
  887. static void
  888. install_new_value_visualizer (struct varobj *var)
  889. {
  890. #if HAVE_PYTHON
  891. /* If the constructor is None, then we want the raw value. If VAR
  892. does not have a value, just skip this. */
  893. if (!gdb_python_initialized)
  894. return;
  895. if (var->dynamic->constructor != Py_None && var->value != NULL)
  896. {
  897. gdbpy_enter_varobj enter_py (var);
  898. if (var->dynamic->constructor == NULL)
  899. install_default_visualizer (var);
  900. else
  901. construct_visualizer (var, var->dynamic->constructor);
  902. }
  903. #else
  904. /* Do nothing. */
  905. #endif
  906. }
  907. /* When using RTTI to determine variable type it may be changed in runtime when
  908. the variable value is changed. This function checks whether type of varobj
  909. VAR will change when a new value NEW_VALUE is assigned and if it is so
  910. updates the type of VAR. */
  911. static bool
  912. update_type_if_necessary (struct varobj *var, struct value *new_value)
  913. {
  914. if (new_value)
  915. {
  916. struct value_print_options opts;
  917. get_user_print_options (&opts);
  918. if (opts.objectprint)
  919. {
  920. struct type *new_type = value_actual_type (new_value, 0, 0);
  921. std::string new_type_str = type_to_string (new_type);
  922. std::string curr_type_str = varobj_get_type (var);
  923. /* Did the type name change? */
  924. if (curr_type_str != new_type_str)
  925. {
  926. var->type = new_type;
  927. /* This information may be not valid for a new type. */
  928. varobj_delete (var, 1);
  929. var->children.clear ();
  930. var->num_children = -1;
  931. return true;
  932. }
  933. }
  934. }
  935. return false;
  936. }
  937. /* Assign a new value to a variable object. If INITIAL is true,
  938. this is the first assignment after the variable object was just
  939. created, or changed type. In that case, just assign the value
  940. and return false.
  941. Otherwise, assign the new value, and return true if the value is
  942. different from the current one, false otherwise. The comparison is
  943. done on textual representation of value. Therefore, some types
  944. need not be compared. E.g. for structures the reported value is
  945. always "{...}", so no comparison is necessary here. If the old
  946. value was NULL and new one is not, or vice versa, we always return true.
  947. The VALUE parameter should not be released -- the function will
  948. take care of releasing it when needed. */
  949. static bool
  950. install_new_value (struct varobj *var, struct value *value, bool initial)
  951. {
  952. bool changeable;
  953. bool need_to_fetch;
  954. bool changed = false;
  955. bool intentionally_not_fetched = false;
  956. /* We need to know the varobj's type to decide if the value should
  957. be fetched or not. C++ fake children (public/protected/private)
  958. don't have a type. */
  959. gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
  960. changeable = varobj_value_is_changeable_p (var);
  961. /* If the type has custom visualizer, we consider it to be always
  962. changeable. FIXME: need to make sure this behaviour will not
  963. mess up read-sensitive values. */
  964. if (var->dynamic->pretty_printer != NULL)
  965. changeable = true;
  966. need_to_fetch = changeable;
  967. /* We are not interested in the address of references, and given
  968. that in C++ a reference is not rebindable, it cannot
  969. meaningfully change. So, get hold of the real value. */
  970. if (value)
  971. value = coerce_ref (value);
  972. if (var->type && var->type->code () == TYPE_CODE_UNION)
  973. /* For unions, we need to fetch the value implicitly because
  974. of implementation of union member fetch. When gdb
  975. creates a value for a field and the value of the enclosing
  976. structure is not lazy, it immediately copies the necessary
  977. bytes from the enclosing values. If the enclosing value is
  978. lazy, the call to value_fetch_lazy on the field will read
  979. the data from memory. For unions, that means we'll read the
  980. same memory more than once, which is not desirable. So
  981. fetch now. */
  982. need_to_fetch = true;
  983. /* The new value might be lazy. If the type is changeable,
  984. that is we'll be comparing values of this type, fetch the
  985. value now. Otherwise, on the next update the old value
  986. will be lazy, which means we've lost that old value. */
  987. if (need_to_fetch && value && value_lazy (value))
  988. {
  989. const struct varobj *parent = var->parent;
  990. bool frozen = var->frozen;
  991. for (; !frozen && parent; parent = parent->parent)
  992. frozen |= parent->frozen;
  993. if (frozen && initial)
  994. {
  995. /* For variables that are frozen, or are children of frozen
  996. variables, we don't do fetch on initial assignment.
  997. For non-initial assignment we do the fetch, since it means we're
  998. explicitly asked to compare the new value with the old one. */
  999. intentionally_not_fetched = true;
  1000. }
  1001. else
  1002. {
  1003. try
  1004. {
  1005. value_fetch_lazy (value);
  1006. }
  1007. catch (const gdb_exception_error &except)
  1008. {
  1009. /* Set the value to NULL, so that for the next -var-update,
  1010. we don't try to compare the new value with this value,
  1011. that we couldn't even read. */
  1012. value = NULL;
  1013. }
  1014. }
  1015. }
  1016. /* Get a reference now, before possibly passing it to any Python
  1017. code that might release it. */
  1018. value_ref_ptr value_holder;
  1019. if (value != NULL)
  1020. value_holder = value_ref_ptr::new_reference (value);
  1021. /* Below, we'll be comparing string rendering of old and new
  1022. values. Don't get string rendering if the value is
  1023. lazy -- if it is, the code above has decided that the value
  1024. should not be fetched. */
  1025. std::string print_value;
  1026. if (value != NULL && !value_lazy (value)
  1027. && var->dynamic->pretty_printer == NULL)
  1028. print_value = varobj_value_get_print_value (value, var->format, var);
  1029. /* If the type is changeable, compare the old and the new values.
  1030. If this is the initial assignment, we don't have any old value
  1031. to compare with. */
  1032. if (!initial && changeable)
  1033. {
  1034. /* If the value of the varobj was changed by -var-set-value,
  1035. then the value in the varobj and in the target is the same.
  1036. However, that value is different from the value that the
  1037. varobj had after the previous -var-update. So need to the
  1038. varobj as changed. */
  1039. if (var->updated)
  1040. changed = true;
  1041. else if (var->dynamic->pretty_printer == NULL)
  1042. {
  1043. /* Try to compare the values. That requires that both
  1044. values are non-lazy. */
  1045. if (var->not_fetched && value_lazy (var->value.get ()))
  1046. {
  1047. /* This is a frozen varobj and the value was never read.
  1048. Presumably, UI shows some "never read" indicator.
  1049. Now that we've fetched the real value, we need to report
  1050. this varobj as changed so that UI can show the real
  1051. value. */
  1052. changed = true;
  1053. }
  1054. else if (var->value == NULL && value == NULL)
  1055. /* Equal. */
  1056. ;
  1057. else if (var->value == NULL || value == NULL)
  1058. {
  1059. changed = true;
  1060. }
  1061. else
  1062. {
  1063. gdb_assert (!value_lazy (var->value.get ()));
  1064. gdb_assert (!value_lazy (value));
  1065. gdb_assert (!var->print_value.empty () && !print_value.empty ());
  1066. if (var->print_value != print_value)
  1067. changed = true;
  1068. }
  1069. }
  1070. }
  1071. if (!initial && !changeable)
  1072. {
  1073. /* For values that are not changeable, we don't compare the values.
  1074. However, we want to notice if a value was not NULL and now is NULL,
  1075. or vise versa, so that we report when top-level varobjs come in scope
  1076. and leave the scope. */
  1077. changed = (var->value != NULL) != (value != NULL);
  1078. }
  1079. /* We must always keep the new value, since children depend on it. */
  1080. var->value = value_holder;
  1081. if (value && value_lazy (value) && intentionally_not_fetched)
  1082. var->not_fetched = true;
  1083. else
  1084. var->not_fetched = false;
  1085. var->updated = false;
  1086. install_new_value_visualizer (var);
  1087. /* If we installed a pretty-printer, re-compare the printed version
  1088. to see if the variable changed. */
  1089. if (var->dynamic->pretty_printer != NULL)
  1090. {
  1091. print_value = varobj_value_get_print_value (var->value.get (),
  1092. var->format, var);
  1093. if ((var->print_value.empty () && !print_value.empty ())
  1094. || (!var->print_value.empty () && print_value.empty ())
  1095. || (!var->print_value.empty () && !print_value.empty ()
  1096. && var->print_value != print_value))
  1097. changed = true;
  1098. }
  1099. var->print_value = print_value;
  1100. gdb_assert (var->value == nullptr || value_type (var->value.get ()));
  1101. return changed;
  1102. }
  1103. /* Return the requested range for a varobj. VAR is the varobj. FROM
  1104. and TO are out parameters; *FROM and *TO will be set to the
  1105. selected sub-range of VAR. If no range was selected using
  1106. -var-set-update-range, then both will be -1. */
  1107. void
  1108. varobj_get_child_range (const struct varobj *var, int *from, int *to)
  1109. {
  1110. *from = var->from;
  1111. *to = var->to;
  1112. }
  1113. /* Set the selected sub-range of children of VAR to start at index
  1114. FROM and end at index TO. If either FROM or TO is less than zero,
  1115. this is interpreted as a request for all children. */
  1116. void
  1117. varobj_set_child_range (struct varobj *var, int from, int to)
  1118. {
  1119. var->from = from;
  1120. var->to = to;
  1121. }
  1122. void
  1123. varobj_set_visualizer (struct varobj *var, const char *visualizer)
  1124. {
  1125. #if HAVE_PYTHON
  1126. PyObject *mainmod;
  1127. if (!gdb_python_initialized)
  1128. return;
  1129. gdbpy_enter_varobj enter_py (var);
  1130. mainmod = PyImport_AddModule ("__main__");
  1131. gdbpy_ref<> globals
  1132. = gdbpy_ref<>::new_reference (PyModule_GetDict (mainmod));
  1133. gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
  1134. globals.get (), globals.get ()));
  1135. if (constructor == NULL)
  1136. {
  1137. gdbpy_print_stack ();
  1138. error (_("Could not evaluate visualizer expression: %s"), visualizer);
  1139. }
  1140. construct_visualizer (var, constructor.get ());
  1141. /* If there are any children now, wipe them. */
  1142. varobj_delete (var, 1 /* children only */);
  1143. var->num_children = -1;
  1144. #else
  1145. error (_("Python support required"));
  1146. #endif
  1147. }
  1148. /* If NEW_VALUE is the new value of the given varobj (var), return
  1149. true if var has mutated. In other words, if the type of
  1150. the new value is different from the type of the varobj's old
  1151. value.
  1152. NEW_VALUE may be NULL, if the varobj is now out of scope. */
  1153. static bool
  1154. varobj_value_has_mutated (const struct varobj *var, struct value *new_value,
  1155. struct type *new_type)
  1156. {
  1157. /* If we haven't previously computed the number of children in var,
  1158. it does not matter from the front-end's perspective whether
  1159. the type has mutated or not. For all intents and purposes,
  1160. it has not mutated. */
  1161. if (var->num_children < 0)
  1162. return false;
  1163. if (var->root->lang_ops->value_has_mutated != NULL)
  1164. {
  1165. /* The varobj module, when installing new values, explicitly strips
  1166. references, saying that we're not interested in those addresses.
  1167. But detection of mutation happens before installing the new
  1168. value, so our value may be a reference that we need to strip
  1169. in order to remain consistent. */
  1170. if (new_value != NULL)
  1171. new_value = coerce_ref (new_value);
  1172. return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
  1173. }
  1174. else
  1175. return false;
  1176. }
  1177. /* Update the values for a variable and its children. This is a
  1178. two-pronged attack. First, re-parse the value for the root's
  1179. expression to see if it's changed. Then go all the way
  1180. through its children, reconstructing them and noting if they've
  1181. changed.
  1182. The IS_EXPLICIT parameter specifies if this call is result
  1183. of MI request to update this specific variable, or
  1184. result of implicit -var-update *. For implicit request, we don't
  1185. update frozen variables.
  1186. NOTE: This function may delete the caller's varobj. If it
  1187. returns TYPE_CHANGED, then it has done this and VARP will be modified
  1188. to point to the new varobj. */
  1189. std::vector<varobj_update_result>
  1190. varobj_update (struct varobj **varp, bool is_explicit)
  1191. {
  1192. bool type_changed = false;
  1193. struct value *newobj;
  1194. std::vector<varobj_update_result> stack;
  1195. std::vector<varobj_update_result> result;
  1196. /* Frozen means frozen -- we don't check for any change in
  1197. this varobj, including its going out of scope, or
  1198. changing type. One use case for frozen varobjs is
  1199. retaining previously evaluated expressions, and we don't
  1200. want them to be reevaluated at all. */
  1201. if (!is_explicit && (*varp)->frozen)
  1202. return result;
  1203. if (!(*varp)->root->is_valid)
  1204. {
  1205. result.emplace_back (*varp, VAROBJ_INVALID);
  1206. return result;
  1207. }
  1208. if ((*varp)->root->rootvar == *varp)
  1209. {
  1210. varobj_update_result r (*varp);
  1211. /* Update the root variable. value_of_root can return NULL
  1212. if the variable is no longer around, i.e. we stepped out of
  1213. the frame in which a local existed. We are letting the
  1214. value_of_root variable dispose of the varobj if the type
  1215. has changed. */
  1216. newobj = value_of_root (varp, &type_changed);
  1217. if (update_type_if_necessary (*varp, newobj))
  1218. type_changed = true;
  1219. r.varobj = *varp;
  1220. r.type_changed = type_changed;
  1221. if (install_new_value ((*varp), newobj, type_changed))
  1222. r.changed = true;
  1223. if (newobj == NULL)
  1224. r.status = VAROBJ_NOT_IN_SCOPE;
  1225. r.value_installed = true;
  1226. if (r.status == VAROBJ_NOT_IN_SCOPE)
  1227. {
  1228. if (r.type_changed || r.changed)
  1229. result.push_back (std::move (r));
  1230. return result;
  1231. }
  1232. stack.push_back (std::move (r));
  1233. }
  1234. else
  1235. stack.emplace_back (*varp);
  1236. /* Walk through the children, reconstructing them all. */
  1237. while (!stack.empty ())
  1238. {
  1239. varobj_update_result r = std::move (stack.back ());
  1240. stack.pop_back ();
  1241. struct varobj *v = r.varobj;
  1242. /* Update this variable, unless it's a root, which is already
  1243. updated. */
  1244. if (!r.value_installed)
  1245. {
  1246. struct type *new_type;
  1247. newobj = value_of_child (v->parent, v->index);
  1248. if (update_type_if_necessary (v, newobj))
  1249. r.type_changed = true;
  1250. if (newobj)
  1251. new_type = value_type (newobj);
  1252. else
  1253. new_type = v->root->lang_ops->type_of_child (v->parent, v->index);
  1254. if (varobj_value_has_mutated (v, newobj, new_type))
  1255. {
  1256. /* The children are no longer valid; delete them now.
  1257. Report the fact that its type changed as well. */
  1258. varobj_delete (v, 1 /* only_children */);
  1259. v->num_children = -1;
  1260. v->to = -1;
  1261. v->from = -1;
  1262. v->type = new_type;
  1263. r.type_changed = true;
  1264. }
  1265. if (install_new_value (v, newobj, r.type_changed))
  1266. {
  1267. r.changed = true;
  1268. v->updated = false;
  1269. }
  1270. }
  1271. /* We probably should not get children of a dynamic varobj, but
  1272. for which -var-list-children was never invoked. */
  1273. if (varobj_is_dynamic_p (v))
  1274. {
  1275. std::vector<varobj *> changed, type_changed_vec, unchanged, newobj_vec;
  1276. bool children_changed = false;
  1277. if (v->frozen)
  1278. continue;
  1279. if (!v->dynamic->children_requested)
  1280. {
  1281. bool dummy;
  1282. /* If we initially did not have potential children, but
  1283. now we do, consider the varobj as changed.
  1284. Otherwise, if children were never requested, consider
  1285. it as unchanged -- presumably, such varobj is not yet
  1286. expanded in the UI, so we need not bother getting
  1287. it. */
  1288. if (!varobj_has_more (v, 0))
  1289. {
  1290. update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
  1291. &dummy, false, 0, 0);
  1292. if (varobj_has_more (v, 0))
  1293. r.changed = true;
  1294. }
  1295. if (r.changed)
  1296. result.push_back (std::move (r));
  1297. continue;
  1298. }
  1299. /* If update_dynamic_varobj_children returns false, then we have
  1300. a non-conforming pretty-printer, so we skip it. */
  1301. if (update_dynamic_varobj_children (v, &changed, &type_changed_vec,
  1302. &newobj_vec,
  1303. &unchanged, &children_changed,
  1304. true, v->from, v->to))
  1305. {
  1306. if (children_changed || !newobj_vec.empty ())
  1307. {
  1308. r.children_changed = true;
  1309. r.newobj = std::move (newobj_vec);
  1310. }
  1311. /* Push in reverse order so that the first child is
  1312. popped from the work stack first, and so will be
  1313. added to result first. This does not affect
  1314. correctness, just "nicer". */
  1315. for (int i = type_changed_vec.size () - 1; i >= 0; --i)
  1316. {
  1317. varobj_update_result item (type_changed_vec[i]);
  1318. /* Type may change only if value was changed. */
  1319. item.changed = true;
  1320. item.type_changed = true;
  1321. item.value_installed = true;
  1322. stack.push_back (std::move (item));
  1323. }
  1324. for (int i = changed.size () - 1; i >= 0; --i)
  1325. {
  1326. varobj_update_result item (changed[i]);
  1327. item.changed = true;
  1328. item.value_installed = true;
  1329. stack.push_back (std::move (item));
  1330. }
  1331. for (int i = unchanged.size () - 1; i >= 0; --i)
  1332. {
  1333. if (!unchanged[i]->frozen)
  1334. {
  1335. varobj_update_result item (unchanged[i]);
  1336. item.value_installed = true;
  1337. stack.push_back (std::move (item));
  1338. }
  1339. }
  1340. if (r.changed || r.children_changed)
  1341. result.push_back (std::move (r));
  1342. continue;
  1343. }
  1344. }
  1345. /* Push any children. Use reverse order so that the first
  1346. child is popped from the work stack first, and so
  1347. will be added to result first. This does not
  1348. affect correctness, just "nicer". */
  1349. for (int i = v->children.size () - 1; i >= 0; --i)
  1350. {
  1351. varobj *c = v->children[i];
  1352. /* Child may be NULL if explicitly deleted by -var-delete. */
  1353. if (c != NULL && !c->frozen)
  1354. stack.emplace_back (c);
  1355. }
  1356. if (r.changed || r.type_changed)
  1357. result.push_back (std::move (r));
  1358. }
  1359. return result;
  1360. }
  1361. /* Helper functions */
  1362. /*
  1363. * Variable object construction/destruction
  1364. */
  1365. static int
  1366. delete_variable (struct varobj *var, bool only_children_p)
  1367. {
  1368. int delcount = 0;
  1369. delete_variable_1 (&delcount, var, only_children_p,
  1370. true /* remove_from_parent_p */ );
  1371. return delcount;
  1372. }
  1373. /* Delete the variable object VAR and its children. */
  1374. /* IMPORTANT NOTE: If we delete a variable which is a child
  1375. and the parent is not removed we dump core. It must be always
  1376. initially called with remove_from_parent_p set. */
  1377. static void
  1378. delete_variable_1 (int *delcountp, struct varobj *var, bool only_children_p,
  1379. bool remove_from_parent_p)
  1380. {
  1381. /* Delete any children of this variable, too. */
  1382. for (varobj *child : var->children)
  1383. {
  1384. if (!child)
  1385. continue;
  1386. if (!remove_from_parent_p)
  1387. child->parent = NULL;
  1388. delete_variable_1 (delcountp, child, false, only_children_p);
  1389. }
  1390. var->children.clear ();
  1391. /* if we were called to delete only the children we are done here. */
  1392. if (only_children_p)
  1393. return;
  1394. /* Otherwise, add it to the list of deleted ones and proceed to do so. */
  1395. /* If the name is empty, this is a temporary variable, that has not
  1396. yet been installed, don't report it, it belongs to the caller... */
  1397. if (!var->obj_name.empty ())
  1398. {
  1399. *delcountp = *delcountp + 1;
  1400. }
  1401. /* If this variable has a parent, remove it from its parent's list. */
  1402. /* OPTIMIZATION: if the parent of this variable is also being deleted,
  1403. (as indicated by remove_from_parent_p) we don't bother doing an
  1404. expensive list search to find the element to remove when we are
  1405. discarding the list afterwards. */
  1406. if ((remove_from_parent_p) && (var->parent != NULL))
  1407. var->parent->children[var->index] = NULL;
  1408. if (!var->obj_name.empty ())
  1409. uninstall_variable (var);
  1410. /* Free memory associated with this variable. */
  1411. delete var;
  1412. }
  1413. /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
  1414. static void
  1415. install_variable (struct varobj *var)
  1416. {
  1417. hashval_t hash = htab_hash_string (var->obj_name.c_str ());
  1418. void **slot = htab_find_slot_with_hash (varobj_table,
  1419. var->obj_name.c_str (),
  1420. hash, INSERT);
  1421. if (*slot != nullptr)
  1422. error (_("Duplicate variable object name"));
  1423. /* Add varobj to hash table. */
  1424. *slot = var;
  1425. /* If root, add varobj to root list. */
  1426. if (is_root_p (var))
  1427. rootlist.push_front (var->root);
  1428. }
  1429. /* Uninstall the object VAR. */
  1430. static void
  1431. uninstall_variable (struct varobj *var)
  1432. {
  1433. hashval_t hash = htab_hash_string (var->obj_name.c_str ());
  1434. htab_remove_elt_with_hash (varobj_table, var->obj_name.c_str (), hash);
  1435. if (varobjdebug)
  1436. gdb_printf (gdb_stdlog, "Deleting %s\n", var->obj_name.c_str ());
  1437. /* If root, remove varobj from root list. */
  1438. if (is_root_p (var))
  1439. {
  1440. auto iter = std::find (rootlist.begin (), rootlist.end (), var->root);
  1441. rootlist.erase (iter);
  1442. }
  1443. }
  1444. /* Create and install a child of the parent of the given name.
  1445. The created VAROBJ takes ownership of the allocated NAME. */
  1446. static struct varobj *
  1447. create_child (struct varobj *parent, int index, std::string &name)
  1448. {
  1449. struct varobj_item item;
  1450. std::swap (item.name, name);
  1451. item.value = release_value (value_of_child (parent, index));
  1452. return create_child_with_value (parent, index, &item);
  1453. }
  1454. static struct varobj *
  1455. create_child_with_value (struct varobj *parent, int index,
  1456. struct varobj_item *item)
  1457. {
  1458. varobj *child = new varobj (parent->root);
  1459. /* NAME is allocated by caller. */
  1460. std::swap (child->name, item->name);
  1461. child->index = index;
  1462. child->parent = parent;
  1463. if (varobj_is_anonymous_child (child))
  1464. child->obj_name = string_printf ("%s.%d_anonymous",
  1465. parent->obj_name.c_str (), index);
  1466. else
  1467. child->obj_name = string_printf ("%s.%s",
  1468. parent->obj_name.c_str (),
  1469. child->name.c_str ());
  1470. install_variable (child);
  1471. /* Compute the type of the child. Must do this before
  1472. calling install_new_value. */
  1473. if (item->value != NULL)
  1474. /* If the child had no evaluation errors, var->value
  1475. will be non-NULL and contain a valid type. */
  1476. child->type = value_actual_type (item->value.get (), 0, NULL);
  1477. else
  1478. /* Otherwise, we must compute the type. */
  1479. child->type = (*child->root->lang_ops->type_of_child) (child->parent,
  1480. child->index);
  1481. install_new_value (child, item->value.get (), 1);
  1482. return child;
  1483. }
  1484. /*
  1485. * Miscellaneous utility functions.
  1486. */
  1487. /* Allocate memory and initialize a new variable. */
  1488. varobj::varobj (varobj_root *root_)
  1489. : root (root_), dynamic (new varobj_dynamic)
  1490. {
  1491. }
  1492. /* Free any allocated memory associated with VAR. */
  1493. varobj::~varobj ()
  1494. {
  1495. varobj *var = this;
  1496. #if HAVE_PYTHON
  1497. if (var->dynamic->pretty_printer != NULL)
  1498. {
  1499. gdbpy_enter_varobj enter_py (var);
  1500. Py_XDECREF (var->dynamic->constructor);
  1501. Py_XDECREF (var->dynamic->pretty_printer);
  1502. }
  1503. #endif
  1504. /* This must be deleted before the root object, because Python-based
  1505. destructors need access to some components. */
  1506. delete var->dynamic;
  1507. if (is_root_p (var))
  1508. delete var->root;
  1509. }
  1510. /* Return the type of the value that's stored in VAR,
  1511. or that would have being stored there if the
  1512. value were accessible.
  1513. This differs from VAR->type in that VAR->type is always
  1514. the true type of the expression in the source language.
  1515. The return value of this function is the type we're
  1516. actually storing in varobj, and using for displaying
  1517. the values and for comparing previous and new values.
  1518. For example, top-level references are always stripped. */
  1519. struct type *
  1520. varobj_get_value_type (const struct varobj *var)
  1521. {
  1522. struct type *type;
  1523. if (var->value != nullptr)
  1524. type = value_type (var->value.get ());
  1525. else
  1526. type = var->type;
  1527. type = check_typedef (type);
  1528. if (TYPE_IS_REFERENCE (type))
  1529. type = get_target_type (type);
  1530. type = check_typedef (type);
  1531. return type;
  1532. }
  1533. /* What is the default display for this variable? We assume that
  1534. everything is "natural". Any exceptions? */
  1535. static enum varobj_display_formats
  1536. variable_default_display (struct varobj *var)
  1537. {
  1538. return FORMAT_NATURAL;
  1539. }
  1540. /*
  1541. * Language-dependencies
  1542. */
  1543. /* Common entry points */
  1544. /* Return the number of children for a given variable.
  1545. The result of this function is defined by the language
  1546. implementation. The number of children returned by this function
  1547. is the number of children that the user will see in the variable
  1548. display. */
  1549. static int
  1550. number_of_children (const struct varobj *var)
  1551. {
  1552. return (*var->root->lang_ops->number_of_children) (var);
  1553. }
  1554. /* What is the expression for the root varobj VAR? */
  1555. static std::string
  1556. name_of_variable (const struct varobj *var)
  1557. {
  1558. return (*var->root->lang_ops->name_of_variable) (var);
  1559. }
  1560. /* What is the name of the INDEX'th child of VAR? */
  1561. static std::string
  1562. name_of_child (struct varobj *var, int index)
  1563. {
  1564. return (*var->root->lang_ops->name_of_child) (var, index);
  1565. }
  1566. /* If frame associated with VAR can be found, switch
  1567. to it and return true. Otherwise, return false. */
  1568. static bool
  1569. check_scope (const struct varobj *var)
  1570. {
  1571. struct frame_info *fi;
  1572. bool scope;
  1573. fi = frame_find_by_id (var->root->frame);
  1574. scope = fi != NULL;
  1575. if (fi)
  1576. {
  1577. CORE_ADDR pc = get_frame_pc (fi);
  1578. if (pc < BLOCK_START (var->root->valid_block) ||
  1579. pc >= BLOCK_END (var->root->valid_block))
  1580. scope = false;
  1581. else
  1582. select_frame (fi);
  1583. }
  1584. return scope;
  1585. }
  1586. /* Helper function to value_of_root. */
  1587. static struct value *
  1588. value_of_root_1 (struct varobj **var_handle)
  1589. {
  1590. struct value *new_val = NULL;
  1591. struct varobj *var = *var_handle;
  1592. bool within_scope = false;
  1593. /* Only root variables can be updated... */
  1594. if (!is_root_p (var))
  1595. /* Not a root var. */
  1596. return NULL;
  1597. scoped_restore_current_thread restore_thread;
  1598. /* Determine whether the variable is still around. */
  1599. if (var->root->valid_block == NULL || var->root->floating)
  1600. within_scope = true;
  1601. else if (var->root->thread_id == 0)
  1602. {
  1603. /* The program was single-threaded when the variable object was
  1604. created. Technically, it's possible that the program became
  1605. multi-threaded since then, but we don't support such
  1606. scenario yet. */
  1607. within_scope = check_scope (var);
  1608. }
  1609. else
  1610. {
  1611. thread_info *thread = find_thread_global_id (var->root->thread_id);
  1612. if (thread != NULL)
  1613. {
  1614. switch_to_thread (thread);
  1615. within_scope = check_scope (var);
  1616. }
  1617. }
  1618. if (within_scope)
  1619. {
  1620. /* We need to catch errors here, because if evaluate
  1621. expression fails we want to just return NULL. */
  1622. try
  1623. {
  1624. new_val = evaluate_expression (var->root->exp.get ());
  1625. }
  1626. catch (const gdb_exception_error &except)
  1627. {
  1628. }
  1629. }
  1630. return new_val;
  1631. }
  1632. /* What is the ``struct value *'' of the root variable VAR?
  1633. For floating variable object, evaluation can get us a value
  1634. of different type from what is stored in varobj already. In
  1635. that case:
  1636. - *type_changed will be set to 1
  1637. - old varobj will be freed, and new one will be
  1638. created, with the same name.
  1639. - *var_handle will be set to the new varobj
  1640. Otherwise, *type_changed will be set to 0. */
  1641. static struct value *
  1642. value_of_root (struct varobj **var_handle, bool *type_changed)
  1643. {
  1644. struct varobj *var;
  1645. if (var_handle == NULL)
  1646. return NULL;
  1647. var = *var_handle;
  1648. /* This should really be an exception, since this should
  1649. only get called with a root variable. */
  1650. if (!is_root_p (var))
  1651. return NULL;
  1652. if (var->root->floating)
  1653. {
  1654. struct varobj *tmp_var;
  1655. tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
  1656. USE_SELECTED_FRAME);
  1657. if (tmp_var == NULL)
  1658. {
  1659. return NULL;
  1660. }
  1661. std::string old_type = varobj_get_type (var);
  1662. std::string new_type = varobj_get_type (tmp_var);
  1663. if (old_type == new_type)
  1664. {
  1665. /* The expression presently stored inside var->root->exp
  1666. remembers the locations of local variables relatively to
  1667. the frame where the expression was created (in DWARF location
  1668. button, for example). Naturally, those locations are not
  1669. correct in other frames, so update the expression. */
  1670. std::swap (var->root->exp, tmp_var->root->exp);
  1671. varobj_delete (tmp_var, 0);
  1672. *type_changed = 0;
  1673. }
  1674. else
  1675. {
  1676. tmp_var->obj_name = var->obj_name;
  1677. tmp_var->from = var->from;
  1678. tmp_var->to = var->to;
  1679. varobj_delete (var, 0);
  1680. install_variable (tmp_var);
  1681. *var_handle = tmp_var;
  1682. var = *var_handle;
  1683. *type_changed = true;
  1684. }
  1685. }
  1686. else
  1687. {
  1688. *type_changed = 0;
  1689. }
  1690. {
  1691. struct value *value;
  1692. value = value_of_root_1 (var_handle);
  1693. if (var->value == NULL || value == NULL)
  1694. {
  1695. /* For root varobj-s, a NULL value indicates a scoping issue.
  1696. So, nothing to do in terms of checking for mutations. */
  1697. }
  1698. else if (varobj_value_has_mutated (var, value, value_type (value)))
  1699. {
  1700. /* The type has mutated, so the children are no longer valid.
  1701. Just delete them, and tell our caller that the type has
  1702. changed. */
  1703. varobj_delete (var, 1 /* only_children */);
  1704. var->num_children = -1;
  1705. var->to = -1;
  1706. var->from = -1;
  1707. *type_changed = true;
  1708. }
  1709. return value;
  1710. }
  1711. }
  1712. /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
  1713. static struct value *
  1714. value_of_child (const struct varobj *parent, int index)
  1715. {
  1716. struct value *value;
  1717. value = (*parent->root->lang_ops->value_of_child) (parent, index);
  1718. return value;
  1719. }
  1720. /* GDB already has a command called "value_of_variable". Sigh. */
  1721. static std::string
  1722. my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
  1723. {
  1724. if (var->root->is_valid)
  1725. {
  1726. if (var->dynamic->pretty_printer != NULL)
  1727. return varobj_value_get_print_value (var->value.get (), var->format,
  1728. var);
  1729. return (*var->root->lang_ops->value_of_variable) (var, format);
  1730. }
  1731. else
  1732. return std::string ();
  1733. }
  1734. void
  1735. varobj_formatted_print_options (struct value_print_options *opts,
  1736. enum varobj_display_formats format)
  1737. {
  1738. get_formatted_print_options (opts, format_code[(int) format]);
  1739. opts->deref_ref = 0;
  1740. opts->raw = !pretty_printing;
  1741. }
  1742. std::string
  1743. varobj_value_get_print_value (struct value *value,
  1744. enum varobj_display_formats format,
  1745. const struct varobj *var)
  1746. {
  1747. struct value_print_options opts;
  1748. struct type *type = NULL;
  1749. long len = 0;
  1750. gdb::unique_xmalloc_ptr<char> encoding;
  1751. /* Initialize it just to avoid a GCC false warning. */
  1752. CORE_ADDR str_addr = 0;
  1753. bool string_print = false;
  1754. if (value == NULL)
  1755. return std::string ();
  1756. string_file stb;
  1757. std::string thevalue;
  1758. #if HAVE_PYTHON
  1759. if (gdb_python_initialized)
  1760. {
  1761. PyObject *value_formatter = var->dynamic->pretty_printer;
  1762. gdbpy_enter_varobj enter_py (var);
  1763. if (value_formatter)
  1764. {
  1765. /* First check to see if we have any children at all. If so,
  1766. we simply return {...}. */
  1767. if (dynamic_varobj_has_child_method (var))
  1768. return "{...}";
  1769. if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
  1770. {
  1771. struct value *replacement;
  1772. gdbpy_ref<> output = apply_varobj_pretty_printer (value_formatter,
  1773. &replacement,
  1774. &stb);
  1775. /* If we have string like output ... */
  1776. if (output != NULL)
  1777. {
  1778. /* If this is a lazy string, extract it. For lazy
  1779. strings we always print as a string, so set
  1780. string_print. */
  1781. if (gdbpy_is_lazy_string (output.get ()))
  1782. {
  1783. gdbpy_extract_lazy_string (output.get (), &str_addr,
  1784. &type, &len, &encoding);
  1785. string_print = true;
  1786. }
  1787. else
  1788. {
  1789. /* If it is a regular (non-lazy) string, extract
  1790. it and copy the contents into THEVALUE. If the
  1791. hint says to print it as a string, set
  1792. string_print. Otherwise just return the extracted
  1793. string as a value. */
  1794. gdb::unique_xmalloc_ptr<char> s
  1795. = python_string_to_target_string (output.get ());
  1796. if (s)
  1797. {
  1798. struct gdbarch *gdbarch;
  1799. gdb::unique_xmalloc_ptr<char> hint
  1800. = gdbpy_get_display_hint (value_formatter);
  1801. if (hint)
  1802. {
  1803. if (!strcmp (hint.get (), "string"))
  1804. string_print = true;
  1805. }
  1806. thevalue = std::string (s.get ());
  1807. len = thevalue.size ();
  1808. gdbarch = value_type (value)->arch ();
  1809. type = builtin_type (gdbarch)->builtin_char;
  1810. if (!string_print)
  1811. return thevalue;
  1812. }
  1813. else
  1814. gdbpy_print_stack ();
  1815. }
  1816. }
  1817. /* If the printer returned a replacement value, set VALUE
  1818. to REPLACEMENT. If there is not a replacement value,
  1819. just use the value passed to this function. */
  1820. if (replacement)
  1821. value = replacement;
  1822. }
  1823. }
  1824. }
  1825. #endif
  1826. varobj_formatted_print_options (&opts, format);
  1827. /* If the THEVALUE has contents, it is a regular string. */
  1828. if (!thevalue.empty ())
  1829. current_language->printstr (&stb, type, (gdb_byte *) thevalue.c_str (),
  1830. len, encoding.get (), 0, &opts);
  1831. else if (string_print)
  1832. /* Otherwise, if string_print is set, and it is not a regular
  1833. string, it is a lazy string. */
  1834. val_print_string (type, encoding.get (), str_addr, len, &stb, &opts);
  1835. else
  1836. /* All other cases. */
  1837. common_val_print (value, &stb, 0, &opts, current_language);
  1838. return stb.release ();
  1839. }
  1840. bool
  1841. varobj_editable_p (const struct varobj *var)
  1842. {
  1843. struct type *type;
  1844. if (!(var->root->is_valid && var->value != nullptr
  1845. && VALUE_LVAL (var->value.get ())))
  1846. return false;
  1847. type = varobj_get_value_type (var);
  1848. switch (type->code ())
  1849. {
  1850. case TYPE_CODE_STRUCT:
  1851. case TYPE_CODE_UNION:
  1852. case TYPE_CODE_ARRAY:
  1853. case TYPE_CODE_FUNC:
  1854. case TYPE_CODE_METHOD:
  1855. return false;
  1856. break;
  1857. default:
  1858. return true;
  1859. break;
  1860. }
  1861. }
  1862. /* Call VAR's value_is_changeable_p language-specific callback. */
  1863. bool
  1864. varobj_value_is_changeable_p (const struct varobj *var)
  1865. {
  1866. return var->root->lang_ops->value_is_changeable_p (var);
  1867. }
  1868. /* Return true if that varobj is floating, that is is always evaluated in the
  1869. selected frame, and not bound to thread/frame. Such variable objects
  1870. are created using '@' as frame specifier to -var-create. */
  1871. bool
  1872. varobj_floating_p (const struct varobj *var)
  1873. {
  1874. return var->root->floating;
  1875. }
  1876. /* Implement the "value_is_changeable_p" varobj callback for most
  1877. languages. */
  1878. bool
  1879. varobj_default_value_is_changeable_p (const struct varobj *var)
  1880. {
  1881. bool r;
  1882. struct type *type;
  1883. if (CPLUS_FAKE_CHILD (var))
  1884. return false;
  1885. type = varobj_get_value_type (var);
  1886. switch (type->code ())
  1887. {
  1888. case TYPE_CODE_STRUCT:
  1889. case TYPE_CODE_UNION:
  1890. case TYPE_CODE_ARRAY:
  1891. r = false;
  1892. break;
  1893. default:
  1894. r = true;
  1895. }
  1896. return r;
  1897. }
  1898. /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
  1899. for each one. */
  1900. void
  1901. all_root_varobjs (gdb::function_view<void (struct varobj *var)> func)
  1902. {
  1903. /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
  1904. auto iter = rootlist.begin ();
  1905. auto end = rootlist.end ();
  1906. while (iter != end)
  1907. {
  1908. auto self = iter++;
  1909. func ((*self)->rootvar);
  1910. }
  1911. }
  1912. /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
  1913. defined on globals. It is a helper for varobj_invalidate.
  1914. This function is called after changing the symbol file, in this case the
  1915. pointers to "struct type" stored by the varobj are no longer valid. All
  1916. varobj must be either re-evaluated, or marked as invalid here. */
  1917. static void
  1918. varobj_invalidate_iter (struct varobj *var)
  1919. {
  1920. /* global and floating var must be re-evaluated. */
  1921. if (var->root->floating || var->root->valid_block == NULL)
  1922. {
  1923. struct varobj *tmp_var;
  1924. /* Try to create a varobj with same expression. If we succeed
  1925. replace the old varobj, otherwise invalidate it. */
  1926. tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
  1927. USE_CURRENT_FRAME);
  1928. if (tmp_var != NULL)
  1929. {
  1930. tmp_var->obj_name = var->obj_name;
  1931. varobj_delete (var, 0);
  1932. install_variable (tmp_var);
  1933. }
  1934. else
  1935. var->root->is_valid = false;
  1936. }
  1937. else /* locals must be invalidated. */
  1938. var->root->is_valid = false;
  1939. }
  1940. /* Invalidate the varobjs that are tied to locals and re-create the ones that
  1941. are defined on globals.
  1942. Invalidated varobjs will be always printed in_scope="invalid". */
  1943. void
  1944. varobj_invalidate (void)
  1945. {
  1946. all_root_varobjs (varobj_invalidate_iter);
  1947. }
  1948. /* A hash function for a varobj. */
  1949. static hashval_t
  1950. hash_varobj (const void *a)
  1951. {
  1952. const varobj *obj = (const varobj *) a;
  1953. return htab_hash_string (obj->obj_name.c_str ());
  1954. }
  1955. /* A hash table equality function for varobjs. */
  1956. static int
  1957. eq_varobj_and_string (const void *a, const void *b)
  1958. {
  1959. const varobj *obj = (const varobj *) a;
  1960. const char *name = (const char *) b;
  1961. return obj->obj_name == name;
  1962. }
  1963. void _initialize_varobj ();
  1964. void
  1965. _initialize_varobj ()
  1966. {
  1967. varobj_table = htab_create_alloc (5, hash_varobj, eq_varobj_and_string,
  1968. nullptr, xcalloc, xfree);
  1969. add_setshow_zuinteger_cmd ("varobj", class_maintenance,
  1970. &varobjdebug,
  1971. _("Set varobj debugging."),
  1972. _("Show varobj debugging."),
  1973. _("When non-zero, varobj debugging is enabled."),
  1974. NULL, show_varobjdebug,
  1975. &setdebuglist, &showdebuglist);
  1976. }