location.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* Data structures and API for event locations in GDB.
  2. Copyright (C) 2013-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 "gdbsupport/gdb_assert.h"
  16. #include "location.h"
  17. #include "symtab.h"
  18. #include "language.h"
  19. #include "linespec.h"
  20. #include "cli/cli-utils.h"
  21. #include "probe.h"
  22. #include "cp-support.h"
  23. #include <ctype.h>
  24. #include <string.h>
  25. static std::string explicit_location_to_string
  26. (const struct explicit_location *explicit_loc);
  27. /* The base class for all an event locations used to set a stop event
  28. in the inferior. */
  29. struct event_location
  30. {
  31. virtual ~event_location () = default;
  32. /* Clone this object. */
  33. virtual event_location_up clone () const = 0;
  34. /* Return true if this location is empty, false otherwise. */
  35. virtual bool empty_p () const = 0;
  36. /* Return a string representation of this location. */
  37. const char *to_string () const
  38. {
  39. if (as_string.empty ())
  40. as_string = compute_string ();
  41. if (as_string.empty ())
  42. return nullptr;
  43. return as_string.c_str ();
  44. }
  45. DISABLE_COPY_AND_ASSIGN (event_location);
  46. /* The type of this breakpoint specification. */
  47. enum event_location_type type;
  48. /* Cached string representation of this location. This is used,
  49. e.g., to save stop event locations to file. */
  50. mutable std::string as_string;
  51. protected:
  52. explicit event_location (enum event_location_type t)
  53. : type (t)
  54. {
  55. }
  56. event_location (enum event_location_type t, std::string &&str)
  57. : type (t),
  58. as_string (std::move (str))
  59. {
  60. }
  61. explicit event_location (const event_location *to_clone)
  62. : type (to_clone->type),
  63. as_string (to_clone->as_string)
  64. {
  65. }
  66. /* Compute the string representation of this object. This is called
  67. by to_string when needed. */
  68. virtual std::string compute_string () const = 0;
  69. };
  70. /* A probe. */
  71. struct event_location_probe : public event_location
  72. {
  73. explicit event_location_probe (std::string &&probe)
  74. : event_location (PROBE_LOCATION, std::move (probe))
  75. {
  76. }
  77. event_location_up clone () const override
  78. {
  79. return event_location_up (new event_location_probe (this));
  80. }
  81. bool empty_p () const override
  82. {
  83. return false;
  84. }
  85. protected:
  86. explicit event_location_probe (const event_location_probe *to_clone)
  87. : event_location (to_clone)
  88. {
  89. }
  90. std::string compute_string () const override
  91. {
  92. return std::move (as_string);
  93. }
  94. };
  95. /* A "normal" linespec. */
  96. struct event_location_linespec : public event_location
  97. {
  98. event_location_linespec (const char **linespec,
  99. symbol_name_match_type match_type)
  100. : event_location (LINESPEC_LOCATION)
  101. {
  102. linespec_location.match_type = match_type;
  103. if (*linespec != NULL)
  104. {
  105. const char *p;
  106. const char *orig = *linespec;
  107. linespec_lex_to_end (linespec);
  108. p = remove_trailing_whitespace (orig, *linespec);
  109. /* If there is no valid linespec then this will leave the
  110. spec_string as nullptr. This behaviour is relied on in the
  111. breakpoint setting code, where spec_string being nullptr means
  112. to use the default breakpoint location. */
  113. if ((p - orig) > 0)
  114. linespec_location.spec_string = savestring (orig, p - orig);
  115. }
  116. }
  117. ~event_location_linespec ()
  118. {
  119. xfree (linespec_location.spec_string);
  120. }
  121. event_location_up clone () const override
  122. {
  123. return event_location_up (new event_location_linespec (this));
  124. }
  125. bool empty_p () const override
  126. {
  127. return false;
  128. }
  129. struct linespec_location linespec_location {};
  130. protected:
  131. explicit event_location_linespec (const event_location_linespec *to_clone)
  132. : event_location (to_clone),
  133. linespec_location (to_clone->linespec_location)
  134. {
  135. if (linespec_location.spec_string != nullptr)
  136. linespec_location.spec_string = xstrdup (linespec_location.spec_string);
  137. }
  138. std::string compute_string () const override
  139. {
  140. if (linespec_location.spec_string != nullptr)
  141. {
  142. const struct linespec_location *ls = &linespec_location;
  143. if (ls->match_type == symbol_name_match_type::FULL)
  144. return std::string ("-qualified ") + ls->spec_string;
  145. else
  146. return ls->spec_string;
  147. }
  148. return {};
  149. }
  150. };
  151. /* An address in the inferior. */
  152. struct event_location_address : public event_location
  153. {
  154. event_location_address (CORE_ADDR addr, const char *addr_string,
  155. int addr_string_len)
  156. : event_location (ADDRESS_LOCATION),
  157. address (addr)
  158. {
  159. if (addr_string != nullptr)
  160. as_string = std::string (addr_string, addr_string_len);
  161. }
  162. event_location_up clone () const override
  163. {
  164. return event_location_up (new event_location_address (this));
  165. }
  166. bool empty_p () const override
  167. {
  168. return false;
  169. }
  170. CORE_ADDR address;
  171. protected:
  172. event_location_address (const event_location_address *to_clone)
  173. : event_location (to_clone),
  174. address (to_clone->address)
  175. {
  176. }
  177. std::string compute_string () const override
  178. {
  179. const char *addr_string = core_addr_to_string (address);
  180. return std::string ("*") + addr_string;
  181. }
  182. };
  183. /* An explicit location. */
  184. struct event_location_explicit : public event_location
  185. {
  186. explicit event_location_explicit (const struct explicit_location *loc)
  187. : event_location (EXPLICIT_LOCATION)
  188. {
  189. copy_loc (loc);
  190. }
  191. ~event_location_explicit ()
  192. {
  193. xfree (explicit_loc.source_filename);
  194. xfree (explicit_loc.function_name);
  195. xfree (explicit_loc.label_name);
  196. }
  197. event_location_up clone () const override
  198. {
  199. return event_location_up (new event_location_explicit (this));
  200. }
  201. bool empty_p () const override
  202. {
  203. return (explicit_loc.source_filename == nullptr
  204. && explicit_loc.function_name == nullptr
  205. && explicit_loc.label_name == nullptr
  206. && explicit_loc.line_offset.sign == LINE_OFFSET_UNKNOWN);
  207. }
  208. struct explicit_location explicit_loc;
  209. protected:
  210. explicit event_location_explicit (const event_location_explicit *to_clone)
  211. : event_location (to_clone)
  212. {
  213. copy_loc (&to_clone->explicit_loc);
  214. }
  215. std::string compute_string () const override
  216. {
  217. return explicit_location_to_string (&explicit_loc);
  218. }
  219. private:
  220. void copy_loc (const struct explicit_location *loc)
  221. {
  222. initialize_explicit_location (&explicit_loc);
  223. if (loc != nullptr)
  224. {
  225. explicit_loc.func_name_match_type = loc->func_name_match_type;
  226. if (loc->source_filename != nullptr)
  227. explicit_loc.source_filename = xstrdup (loc->source_filename);
  228. if (loc->function_name != nullptr)
  229. explicit_loc.function_name = xstrdup (loc->function_name);
  230. if (loc->label_name != nullptr)
  231. explicit_loc.label_name = xstrdup (loc->label_name);
  232. explicit_loc.line_offset = loc->line_offset;
  233. }
  234. }
  235. };
  236. /* See description in location.h. */
  237. enum event_location_type
  238. event_location_type (const struct event_location *location)
  239. {
  240. return location->type;
  241. }
  242. /* See description in location.h. */
  243. void
  244. initialize_explicit_location (struct explicit_location *explicit_loc)
  245. {
  246. memset (explicit_loc, 0, sizeof (struct explicit_location));
  247. explicit_loc->line_offset.sign = LINE_OFFSET_UNKNOWN;
  248. explicit_loc->func_name_match_type = symbol_name_match_type::WILD;
  249. }
  250. /* See description in location.h. */
  251. event_location_up
  252. new_linespec_location (const char **linespec,
  253. symbol_name_match_type match_type)
  254. {
  255. return event_location_up (new event_location_linespec (linespec,
  256. match_type));
  257. }
  258. /* See description in location.h. */
  259. const linespec_location *
  260. get_linespec_location (const struct event_location *location)
  261. {
  262. gdb_assert (location->type == LINESPEC_LOCATION);
  263. return &((event_location_linespec *) location)->linespec_location;
  264. }
  265. /* See description in location.h. */
  266. event_location_up
  267. new_address_location (CORE_ADDR addr, const char *addr_string,
  268. int addr_string_len)
  269. {
  270. return event_location_up (new event_location_address (addr, addr_string,
  271. addr_string_len));
  272. }
  273. /* See description in location.h. */
  274. CORE_ADDR
  275. get_address_location (const struct event_location *location)
  276. {
  277. gdb_assert (location->type == ADDRESS_LOCATION);
  278. return ((event_location_address *) location)->address;
  279. }
  280. /* See description in location.h. */
  281. const char *
  282. get_address_string_location (const struct event_location *location)
  283. {
  284. gdb_assert (location->type == ADDRESS_LOCATION);
  285. return location->to_string ();
  286. }
  287. /* See description in location.h. */
  288. event_location_up
  289. new_probe_location (std::string &&probe)
  290. {
  291. return event_location_up (new event_location_probe (std::move (probe)));
  292. }
  293. /* See description in location.h. */
  294. const char *
  295. get_probe_location (const struct event_location *location)
  296. {
  297. gdb_assert (location->type == PROBE_LOCATION);
  298. return location->to_string ();
  299. }
  300. /* See description in location.h. */
  301. event_location_up
  302. new_explicit_location (const struct explicit_location *explicit_loc)
  303. {
  304. return event_location_up (new event_location_explicit (explicit_loc));
  305. }
  306. /* See description in location.h. */
  307. struct explicit_location *
  308. get_explicit_location (struct event_location *location)
  309. {
  310. gdb_assert (location->type == EXPLICIT_LOCATION);
  311. return &((event_location_explicit *) location)->explicit_loc;
  312. }
  313. /* See description in location.h. */
  314. const struct explicit_location *
  315. get_explicit_location_const (const struct event_location *location)
  316. {
  317. gdb_assert (location->type == EXPLICIT_LOCATION);
  318. return &((event_location_explicit *) location)->explicit_loc;
  319. }
  320. /* This convenience function returns a malloc'd string which
  321. represents the location in EXPLICIT_LOC.
  322. AS_LINESPEC is true if this string should be a linespec.
  323. Otherwise it will be output in explicit form. */
  324. static std::string
  325. explicit_to_string_internal (bool as_linespec,
  326. const struct explicit_location *explicit_loc)
  327. {
  328. bool need_space = false;
  329. char space = as_linespec ? ':' : ' ';
  330. string_file buf;
  331. if (explicit_loc->source_filename != NULL)
  332. {
  333. if (!as_linespec)
  334. buf.puts ("-source ");
  335. buf.puts (explicit_loc->source_filename);
  336. need_space = true;
  337. }
  338. if (explicit_loc->function_name != NULL)
  339. {
  340. if (need_space)
  341. buf.putc (space);
  342. if (explicit_loc->func_name_match_type == symbol_name_match_type::FULL)
  343. buf.puts ("-qualified ");
  344. if (!as_linespec)
  345. buf.puts ("-function ");
  346. buf.puts (explicit_loc->function_name);
  347. need_space = true;
  348. }
  349. if (explicit_loc->label_name != NULL)
  350. {
  351. if (need_space)
  352. buf.putc (space);
  353. if (!as_linespec)
  354. buf.puts ("-label ");
  355. buf.puts (explicit_loc->label_name);
  356. need_space = true;
  357. }
  358. if (explicit_loc->line_offset.sign != LINE_OFFSET_UNKNOWN)
  359. {
  360. if (need_space)
  361. buf.putc (space);
  362. if (!as_linespec)
  363. buf.puts ("-line ");
  364. buf.printf ("%s%d",
  365. (explicit_loc->line_offset.sign == LINE_OFFSET_NONE ? ""
  366. : (explicit_loc->line_offset.sign
  367. == LINE_OFFSET_PLUS ? "+" : "-")),
  368. explicit_loc->line_offset.offset);
  369. }
  370. return buf.release ();
  371. }
  372. /* See description in location.h. */
  373. static std::string
  374. explicit_location_to_string (const struct explicit_location *explicit_loc)
  375. {
  376. return explicit_to_string_internal (false, explicit_loc);
  377. }
  378. /* See description in location.h. */
  379. std::string
  380. explicit_location_to_linespec (const struct explicit_location *explicit_loc)
  381. {
  382. return explicit_to_string_internal (true, explicit_loc);
  383. }
  384. /* See description in location.h. */
  385. event_location_up
  386. copy_event_location (const struct event_location *src)
  387. {
  388. return src->clone ();
  389. }
  390. void
  391. event_location_deleter::operator() (event_location *location) const
  392. {
  393. delete location;
  394. }
  395. /* See description in location.h. */
  396. const char *
  397. event_location_to_string (struct event_location *location)
  398. {
  399. return location->to_string ();
  400. }
  401. /* Find an instance of the quote character C in the string S that is
  402. outside of all single- and double-quoted strings (i.e., any quoting
  403. other than C). */
  404. static const char *
  405. find_end_quote (const char *s, char end_quote_char)
  406. {
  407. /* zero if we're not in quotes;
  408. '"' if we're in a double-quoted string;
  409. '\'' if we're in a single-quoted string. */
  410. char nested_quote_char = '\0';
  411. for (const char *scan = s; *scan != '\0'; scan++)
  412. {
  413. if (nested_quote_char != '\0')
  414. {
  415. if (*scan == nested_quote_char)
  416. nested_quote_char = '\0';
  417. else if (scan[0] == '\\' && *(scan + 1) != '\0')
  418. scan++;
  419. }
  420. else if (*scan == end_quote_char && nested_quote_char == '\0')
  421. return scan;
  422. else if (*scan == '"' || *scan == '\'')
  423. nested_quote_char = *scan;
  424. }
  425. return 0;
  426. }
  427. /* A lexer for explicit locations. This function will advance INP
  428. past any strings that it lexes. Returns a malloc'd copy of the
  429. lexed string or NULL if no lexing was done. */
  430. static gdb::unique_xmalloc_ptr<char>
  431. explicit_location_lex_one (const char **inp,
  432. const struct language_defn *language,
  433. explicit_completion_info *completion_info)
  434. {
  435. const char *start = *inp;
  436. if (*start == '\0')
  437. return NULL;
  438. /* If quoted, skip to the ending quote. */
  439. if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
  440. {
  441. if (completion_info != NULL)
  442. completion_info->quoted_arg_start = start;
  443. const char *end = find_end_quote (start + 1, *start);
  444. if (end == NULL)
  445. {
  446. if (completion_info == NULL)
  447. error (_("Unmatched quote, %s."), start);
  448. end = start + strlen (start);
  449. *inp = end;
  450. return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
  451. *inp - start - 1));
  452. }
  453. if (completion_info != NULL)
  454. completion_info->quoted_arg_end = end;
  455. *inp = end + 1;
  456. return gdb::unique_xmalloc_ptr<char> (savestring (start + 1,
  457. *inp - start - 2));
  458. }
  459. /* If the input starts with '-' or '+', the string ends with the next
  460. whitespace or comma. */
  461. if (*start == '-' || *start == '+')
  462. {
  463. while (*inp[0] != '\0' && *inp[0] != ',' && !isspace (*inp[0]))
  464. ++(*inp);
  465. }
  466. else
  467. {
  468. /* Handle numbers first, stopping at the next whitespace or ','. */
  469. while (isdigit (*inp[0]))
  470. ++(*inp);
  471. if (*inp[0] == '\0' || isspace (*inp[0]) || *inp[0] == ',')
  472. return gdb::unique_xmalloc_ptr<char> (savestring (start,
  473. *inp - start));
  474. /* Otherwise stop at the next occurrence of whitespace, '\0',
  475. keyword, or ','. */
  476. *inp = start;
  477. while ((*inp)[0]
  478. && (*inp)[0] != ','
  479. && !(isspace ((*inp)[0])
  480. || linespec_lexer_lex_keyword (&(*inp)[1])))
  481. {
  482. /* Special case: C++ operator,. */
  483. if (language->la_language == language_cplus
  484. && startswith (*inp, CP_OPERATOR_STR))
  485. (*inp) += CP_OPERATOR_LEN;
  486. ++(*inp);
  487. }
  488. }
  489. if (*inp - start > 0)
  490. return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
  491. return NULL;
  492. }
  493. /* Return true if COMMA points past "operator". START is the start of
  494. the line that COMMAND points to, hence when reading backwards, we
  495. must not read any character before START. */
  496. static bool
  497. is_cp_operator (const char *start, const char *comma)
  498. {
  499. if (comma != NULL
  500. && (comma - start) >= CP_OPERATOR_LEN)
  501. {
  502. const char *p = comma;
  503. while (p > start && isspace (p[-1]))
  504. p--;
  505. if (p - start >= CP_OPERATOR_LEN)
  506. {
  507. p -= CP_OPERATOR_LEN;
  508. if (strncmp (p, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
  509. && (p == start
  510. || !(isalnum (p[-1]) || p[-1] == '_')))
  511. {
  512. return true;
  513. }
  514. }
  515. }
  516. return false;
  517. }
  518. /* When scanning the input string looking for the next explicit
  519. location option/delimiter, we jump to the next option by looking
  520. for ",", and "-". Such a character can also appear in C++ symbols
  521. like "operator," and "operator-". So when we find such a
  522. character, we call this function to check if we found such a
  523. symbol, meaning we had a false positive for an option string. In
  524. that case, we keep looking for the next delimiter, until we find
  525. one that is not a false positive, or we reach end of string. FOUND
  526. is the character that scanning found (either '-' or ','), and START
  527. is the start of the line that FOUND points to, hence when reading
  528. backwards, we must not read any character before START. Returns a
  529. pointer to the next non-false-positive delimiter character, or NULL
  530. if none was found. */
  531. static const char *
  532. skip_op_false_positives (const char *start, const char *found)
  533. {
  534. while (found != NULL && is_cp_operator (start, found))
  535. {
  536. if (found[0] == '-' && found[1] == '-')
  537. start = found + 2;
  538. else
  539. start = found + 1;
  540. found = find_toplevel_char (start, *found);
  541. }
  542. return found;
  543. }
  544. /* Assuming both FIRST and NEW_TOK point into the same string, return
  545. the pointer that is closer to the start of the string. If FIRST is
  546. NULL, returns NEW_TOK. If NEW_TOK is NULL, returns FIRST. */
  547. static const char *
  548. first_of (const char *first, const char *new_tok)
  549. {
  550. if (first == NULL)
  551. return new_tok;
  552. else if (new_tok != NULL && new_tok < first)
  553. return new_tok;
  554. else
  555. return first;
  556. }
  557. /* A lexer for functions in explicit locations. This function will
  558. advance INP past a function until the next option, or until end of
  559. string. Returns a malloc'd copy of the lexed string or NULL if no
  560. lexing was done. */
  561. static gdb::unique_xmalloc_ptr<char>
  562. explicit_location_lex_one_function (const char **inp,
  563. const struct language_defn *language,
  564. explicit_completion_info *completion_info)
  565. {
  566. const char *start = *inp;
  567. if (*start == '\0')
  568. return NULL;
  569. /* If quoted, skip to the ending quote. */
  570. if (strchr (get_gdb_linespec_parser_quote_characters (), *start))
  571. {
  572. char quote_char = *start;
  573. /* If the input is not an Ada operator, skip to the matching
  574. closing quote and return the string. */
  575. if (!(language->la_language == language_ada
  576. && quote_char == '\"' && is_ada_operator (start)))
  577. {
  578. if (completion_info != NULL)
  579. completion_info->quoted_arg_start = start;
  580. const char *end = find_toplevel_char (start + 1, quote_char);
  581. if (end == NULL)
  582. {
  583. if (completion_info == NULL)
  584. error (_("Unmatched quote, %s."), start);
  585. end = start + strlen (start);
  586. *inp = end;
  587. char *saved = savestring (start + 1, *inp - start - 1);
  588. return gdb::unique_xmalloc_ptr<char> (saved);
  589. }
  590. if (completion_info != NULL)
  591. completion_info->quoted_arg_end = end;
  592. *inp = end + 1;
  593. char *saved = savestring (start + 1, *inp - start - 2);
  594. return gdb::unique_xmalloc_ptr<char> (saved);
  595. }
  596. }
  597. const char *comma = find_toplevel_char (start, ',');
  598. /* If we have "-function -myfunction", or perhaps better example,
  599. "-function -[BasicClass doIt]" (objc selector), treat
  600. "-myfunction" as the function name. I.e., skip the first char if
  601. it is an hyphen. Don't skip the first char always, because we
  602. may have C++ "operator<", and find_toplevel_char needs to see the
  603. 'o' in that case. */
  604. const char *hyphen
  605. = (*start == '-'
  606. ? find_toplevel_char (start + 1, '-')
  607. : find_toplevel_char (start, '-'));
  608. /* Check for C++ "operator," and "operator-". */
  609. comma = skip_op_false_positives (start, comma);
  610. hyphen = skip_op_false_positives (start, hyphen);
  611. /* Pick the one that appears first. */
  612. const char *end = first_of (hyphen, comma);
  613. /* See if a linespec keyword appears first. */
  614. const char *s = start;
  615. const char *ws = find_toplevel_char (start, ' ');
  616. while (ws != NULL && linespec_lexer_lex_keyword (ws + 1) == NULL)
  617. {
  618. s = ws + 1;
  619. ws = find_toplevel_char (s, ' ');
  620. }
  621. if (ws != NULL)
  622. end = first_of (end, ws + 1);
  623. /* If we don't have any terminator, then take the whole string. */
  624. if (end == NULL)
  625. end = start + strlen (start);
  626. /* Trim whitespace at the end. */
  627. while (end > start && end[-1] == ' ')
  628. end--;
  629. *inp = end;
  630. if (*inp - start > 0)
  631. return gdb::unique_xmalloc_ptr<char> (savestring (start, *inp - start));
  632. return NULL;
  633. }
  634. /* See description in location.h. */
  635. event_location_up
  636. string_to_explicit_location (const char **argp,
  637. const struct language_defn *language,
  638. explicit_completion_info *completion_info)
  639. {
  640. /* It is assumed that input beginning with '-' and a non-digit
  641. character is an explicit location. "-p" is reserved, though,
  642. for probe locations. */
  643. if (argp == NULL
  644. || *argp == NULL
  645. || *argp[0] != '-'
  646. || !isalpha ((*argp)[1])
  647. || ((*argp)[0] == '-' && (*argp)[1] == 'p'))
  648. return NULL;
  649. std::unique_ptr<event_location_explicit> location
  650. (new event_location_explicit ((const explicit_location *) nullptr));
  651. /* Process option/argument pairs. dprintf_command
  652. requires that processing stop on ','. */
  653. while ((*argp)[0] != '\0' && (*argp)[0] != ',')
  654. {
  655. int len;
  656. const char *start;
  657. /* Clear these on each iteration, since they should be filled
  658. with info about the last option. */
  659. if (completion_info != NULL)
  660. {
  661. completion_info->quoted_arg_start = NULL;
  662. completion_info->quoted_arg_end = NULL;
  663. }
  664. /* If *ARGP starts with a keyword, stop processing
  665. options. */
  666. if (linespec_lexer_lex_keyword (*argp) != NULL)
  667. break;
  668. /* Mark the start of the string in case we need to rewind. */
  669. start = *argp;
  670. if (completion_info != NULL)
  671. completion_info->last_option = start;
  672. /* Get the option string. */
  673. gdb::unique_xmalloc_ptr<char> opt
  674. = explicit_location_lex_one (argp, language, NULL);
  675. /* Use the length of the option to allow abbreviations. */
  676. len = strlen (opt.get ());
  677. /* Get the argument string. */
  678. *argp = skip_spaces (*argp);
  679. /* All options have a required argument. Checking for this
  680. required argument is deferred until later. */
  681. gdb::unique_xmalloc_ptr<char> oarg;
  682. /* True if we have an argument. This is required because we'll
  683. move from OARG before checking whether we have an
  684. argument. */
  685. bool have_oarg = false;
  686. /* True if the option needs an argument. */
  687. bool need_oarg = false;
  688. /* Convenience to consistently set both OARG/HAVE_OARG from
  689. ARG. */
  690. auto set_oarg = [&] (gdb::unique_xmalloc_ptr<char> arg)
  691. {
  692. if (completion_info != NULL)
  693. {
  694. /* We do this here because the set of options that take
  695. arguments matches the set of explicit location
  696. options. */
  697. completion_info->saw_explicit_location_option = true;
  698. }
  699. oarg = std::move (arg);
  700. have_oarg = oarg != NULL;
  701. need_oarg = true;
  702. };
  703. if (strncmp (opt.get (), "-source", len) == 0)
  704. {
  705. set_oarg (explicit_location_lex_one (argp, language,
  706. completion_info));
  707. location->explicit_loc.source_filename = oarg.release ();
  708. }
  709. else if (strncmp (opt.get (), "-function", len) == 0)
  710. {
  711. set_oarg (explicit_location_lex_one_function (argp, language,
  712. completion_info));
  713. location->explicit_loc.function_name = oarg.release ();
  714. }
  715. else if (strncmp (opt.get (), "-qualified", len) == 0)
  716. {
  717. location->explicit_loc.func_name_match_type
  718. = symbol_name_match_type::FULL;
  719. }
  720. else if (strncmp (opt.get (), "-line", len) == 0)
  721. {
  722. set_oarg (explicit_location_lex_one (argp, language, NULL));
  723. *argp = skip_spaces (*argp);
  724. if (have_oarg)
  725. {
  726. location->explicit_loc.line_offset
  727. = linespec_parse_line_offset (oarg.get ());
  728. continue;
  729. }
  730. }
  731. else if (strncmp (opt.get (), "-label", len) == 0)
  732. {
  733. set_oarg (explicit_location_lex_one (argp, language, completion_info));
  734. location->explicit_loc.label_name = oarg.release ();
  735. }
  736. /* Only emit an "invalid argument" error for options
  737. that look like option strings. */
  738. else if (opt.get ()[0] == '-' && !isdigit (opt.get ()[1]))
  739. {
  740. if (completion_info == NULL)
  741. error (_("invalid explicit location argument, \"%s\""), opt.get ());
  742. }
  743. else
  744. {
  745. /* End of the explicit location specification.
  746. Stop parsing and return whatever explicit location was
  747. parsed. */
  748. *argp = start;
  749. break;
  750. }
  751. *argp = skip_spaces (*argp);
  752. /* It's a little lame to error after the fact, but in this
  753. case, it provides a much better user experience to issue
  754. the "invalid argument" error before any missing
  755. argument error. */
  756. if (need_oarg && !have_oarg && completion_info == NULL)
  757. error (_("missing argument for \"%s\""), opt.get ());
  758. }
  759. /* One special error check: If a source filename was given
  760. without offset, function, or label, issue an error. */
  761. if (location->explicit_loc.source_filename != NULL
  762. && location->explicit_loc.function_name == NULL
  763. && location->explicit_loc.label_name == NULL
  764. && (location->explicit_loc.line_offset.sign == LINE_OFFSET_UNKNOWN)
  765. && completion_info == NULL)
  766. {
  767. error (_("Source filename requires function, label, or "
  768. "line offset."));
  769. }
  770. return event_location_up (location.release ());
  771. }
  772. /* See description in location.h. */
  773. event_location_up
  774. string_to_event_location_basic (const char **stringp,
  775. const struct language_defn *language,
  776. symbol_name_match_type match_type)
  777. {
  778. event_location_up location;
  779. const char *cs;
  780. /* Try the input as a probe spec. */
  781. cs = *stringp;
  782. if (cs != NULL && probe_linespec_to_static_ops (&cs) != NULL)
  783. {
  784. location = new_probe_location (*stringp);
  785. *stringp += strlen (*stringp);
  786. }
  787. else
  788. {
  789. /* Try an address location. */
  790. if (*stringp != NULL && **stringp == '*')
  791. {
  792. const char *arg, *orig;
  793. CORE_ADDR addr;
  794. orig = arg = *stringp;
  795. addr = linespec_expression_to_pc (&arg);
  796. location = new_address_location (addr, orig, arg - orig);
  797. *stringp += arg - orig;
  798. }
  799. else
  800. {
  801. /* Everything else is a linespec. */
  802. location = new_linespec_location (stringp, match_type);
  803. }
  804. }
  805. return location;
  806. }
  807. /* See description in location.h. */
  808. event_location_up
  809. string_to_event_location (const char **stringp,
  810. const struct language_defn *language,
  811. symbol_name_match_type match_type)
  812. {
  813. const char *arg, *orig;
  814. /* Try an explicit location. */
  815. orig = arg = *stringp;
  816. event_location_up location = string_to_explicit_location (&arg, language, NULL);
  817. if (location != NULL)
  818. {
  819. /* It was a valid explicit location. Advance STRINGP to
  820. the end of input. */
  821. *stringp += arg - orig;
  822. /* If the user really specified a location, then we're done. */
  823. if (!event_location_empty_p (location.get ()))
  824. return location;
  825. /* Otherwise, the user _only_ specified optional flags like
  826. "-qualified", otherwise string_to_explicit_location would
  827. have thrown an error. Save the flags for "basic" linespec
  828. parsing below and discard the explicit location. */
  829. event_location_explicit *xloc
  830. = dynamic_cast<event_location_explicit *> (location.get ());
  831. gdb_assert (xloc != nullptr);
  832. match_type = xloc->explicit_loc.func_name_match_type;
  833. }
  834. /* Everything else is a "basic" linespec, address, or probe
  835. location. */
  836. return string_to_event_location_basic (stringp, language, match_type);
  837. }
  838. /* See description in location.h. */
  839. int
  840. event_location_empty_p (const struct event_location *location)
  841. {
  842. return location->empty_p ();
  843. }
  844. /* See description in location.h. */
  845. void
  846. set_event_location_string (struct event_location *location,
  847. std::string &&string)
  848. {
  849. location->as_string = std::move (string);
  850. }