utils.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700
  1. /* General utility routines for GDB, the GNU debugger.
  2. Copyright (C) 1986-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 <ctype.h>
  16. #include "gdbsupport/gdb_wait.h"
  17. #include "event-top.h"
  18. #include "gdbthread.h"
  19. #include "fnmatch.h"
  20. #include "gdb_bfd.h"
  21. #ifdef HAVE_SYS_RESOURCE_H
  22. #include <sys/resource.h>
  23. #endif /* HAVE_SYS_RESOURCE_H */
  24. #ifdef TUI
  25. #include "tui/tui.h" /* For tui_get_command_dimension. */
  26. #endif
  27. #ifdef __GO32__
  28. #include <pc.h>
  29. #endif
  30. #include <signal.h>
  31. #include "gdbcmd.h"
  32. #include "serial.h"
  33. #include "bfd.h"
  34. #include "target.h"
  35. #include "gdb-demangle.h"
  36. #include "expression.h"
  37. #include "language.h"
  38. #include "charset.h"
  39. #include "annotate.h"
  40. #include "filenames.h"
  41. #include "symfile.h"
  42. #include "gdbsupport/gdb_obstack.h"
  43. #include "gdbcore.h"
  44. #include "top.h"
  45. #include "main.h"
  46. #include "solist.h"
  47. #include "inferior.h" /* for signed_pointer_to_address */
  48. #include "gdb_curses.h"
  49. #include "readline/readline.h"
  50. #include <chrono>
  51. #include "interps.h"
  52. #include "gdbsupport/gdb_regex.h"
  53. #include "gdbsupport/job-control.h"
  54. #include "gdbsupport/selftest.h"
  55. #include "gdbsupport/gdb_optional.h"
  56. #include "cp-support.h"
  57. #include <algorithm>
  58. #include "gdbsupport/pathstuff.h"
  59. #include "cli/cli-style.h"
  60. #include "gdbsupport/scope-exit.h"
  61. #include "gdbarch.h"
  62. #include "cli-out.h"
  63. #include "gdbsupport/gdb-safe-ctype.h"
  64. #include "bt-utils.h"
  65. #include "gdbsupport/buildargv.h"
  66. #include "pager.h"
  67. #include "run-on-main-thread.h"
  68. void (*deprecated_error_begin_hook) (void);
  69. /* Prototypes for local functions */
  70. static void set_screen_size (void);
  71. static void set_width (void);
  72. /* Time spent in prompt_for_continue in the currently executing command
  73. waiting for user to respond.
  74. Initialized in make_command_stats_cleanup.
  75. Modified in prompt_for_continue and defaulted_query.
  76. Used in report_command_stats. */
  77. static std::chrono::steady_clock::duration prompt_for_continue_wait_time;
  78. /* A flag indicating whether to timestamp debugging messages. */
  79. bool debug_timestamp = false;
  80. /* True means that strings with character values >0x7F should be printed
  81. as octal escapes. False means just print the value (e.g. it's an
  82. international character, and the terminal or window can cope.) */
  83. bool sevenbit_strings = false;
  84. static void
  85. show_sevenbit_strings (struct ui_file *file, int from_tty,
  86. struct cmd_list_element *c, const char *value)
  87. {
  88. gdb_printf (file, _("Printing of 8-bit characters "
  89. "in strings as \\nnn is %s.\n"),
  90. value);
  91. }
  92. /* String to be printed before warning messages, if any. */
  93. const char *warning_pre_print = "\nwarning: ";
  94. bool pagination_enabled = true;
  95. static void
  96. show_pagination_enabled (struct ui_file *file, int from_tty,
  97. struct cmd_list_element *c, const char *value)
  98. {
  99. gdb_printf (file, _("State of pagination is %s.\n"), value);
  100. }
  101. /* Print a warning message. The first argument STRING is the warning
  102. message, used as an fprintf format string, the second is the
  103. va_list of arguments for that string. A warning is unfiltered (not
  104. paginated) so that the user does not need to page through each
  105. screen full of warnings when there are lots of them. */
  106. void
  107. vwarning (const char *string, va_list args)
  108. {
  109. if (deprecated_warning_hook)
  110. (*deprecated_warning_hook) (string, args);
  111. else
  112. {
  113. gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
  114. if (target_supports_terminal_ours ())
  115. {
  116. term_state.emplace ();
  117. target_terminal::ours_for_output ();
  118. }
  119. if (warning_pre_print)
  120. gdb_puts (warning_pre_print, gdb_stderr);
  121. gdb_vprintf (gdb_stderr, string, args);
  122. gdb_printf (gdb_stderr, "\n");
  123. }
  124. }
  125. /* Print an error message and return to command level.
  126. The first argument STRING is the error message, used as a fprintf string,
  127. and the remaining args are passed as arguments to it. */
  128. void
  129. verror (const char *string, va_list args)
  130. {
  131. throw_verror (GENERIC_ERROR, string, args);
  132. }
  133. void
  134. error_stream (const string_file &stream)
  135. {
  136. error (("%s"), stream.c_str ());
  137. }
  138. /* Emit a message and abort. */
  139. static void ATTRIBUTE_NORETURN
  140. abort_with_message (const char *msg)
  141. {
  142. if (current_ui == NULL)
  143. fputs (msg, stderr);
  144. else
  145. gdb_puts (msg, gdb_stderr);
  146. abort (); /* ARI: abort */
  147. }
  148. /* Dump core trying to increase the core soft limit to hard limit first. */
  149. void
  150. dump_core (void)
  151. {
  152. #ifdef HAVE_SETRLIMIT
  153. struct rlimit rlim = { (rlim_t) RLIM_INFINITY, (rlim_t) RLIM_INFINITY };
  154. setrlimit (RLIMIT_CORE, &rlim);
  155. #endif /* HAVE_SETRLIMIT */
  156. /* Ensure that the SIGABRT we're about to raise will immediately cause
  157. GDB to exit and dump core, we don't want to trigger GDB's printing of
  158. a backtrace to the console here. */
  159. signal (SIGABRT, SIG_DFL);
  160. abort (); /* ARI: abort */
  161. }
  162. /* Check whether GDB will be able to dump core using the dump_core
  163. function. Returns zero if GDB cannot or should not dump core.
  164. If LIMIT_KIND is LIMIT_CUR the user's soft limit will be respected.
  165. If LIMIT_KIND is LIMIT_MAX only the hard limit will be respected. */
  166. int
  167. can_dump_core (enum resource_limit_kind limit_kind)
  168. {
  169. #ifdef HAVE_GETRLIMIT
  170. struct rlimit rlim;
  171. /* Be quiet and assume we can dump if an error is returned. */
  172. if (getrlimit (RLIMIT_CORE, &rlim) != 0)
  173. return 1;
  174. switch (limit_kind)
  175. {
  176. case LIMIT_CUR:
  177. if (rlim.rlim_cur == 0)
  178. return 0;
  179. /* Fall through. */
  180. case LIMIT_MAX:
  181. if (rlim.rlim_max == 0)
  182. return 0;
  183. }
  184. #endif /* HAVE_GETRLIMIT */
  185. return 1;
  186. }
  187. /* Print a warning that we cannot dump core. */
  188. void
  189. warn_cant_dump_core (const char *reason)
  190. {
  191. gdb_printf (gdb_stderr,
  192. _("%s\nUnable to dump core, use `ulimit -c"
  193. " unlimited' before executing GDB next time.\n"),
  194. reason);
  195. }
  196. /* Check whether GDB will be able to dump core using the dump_core
  197. function, and print a warning if we cannot. */
  198. static int
  199. can_dump_core_warn (enum resource_limit_kind limit_kind,
  200. const char *reason)
  201. {
  202. int core_dump_allowed = can_dump_core (limit_kind);
  203. if (!core_dump_allowed)
  204. warn_cant_dump_core (reason);
  205. return core_dump_allowed;
  206. }
  207. /* Allow the user to configure the debugger behavior with respect to
  208. what to do when an internal problem is detected. */
  209. const char internal_problem_ask[] = "ask";
  210. const char internal_problem_yes[] = "yes";
  211. const char internal_problem_no[] = "no";
  212. static const char *const internal_problem_modes[] =
  213. {
  214. internal_problem_ask,
  215. internal_problem_yes,
  216. internal_problem_no,
  217. NULL
  218. };
  219. /* Data structure used to control how the internal_vproblem function
  220. should behave. An instance of this structure is created for each
  221. problem type that GDB supports. */
  222. struct internal_problem
  223. {
  224. /* The name of this problem type. This must not contain white space as
  225. this string is used to build command names. */
  226. const char *name;
  227. /* When this is true then a user command is created (based on NAME) that
  228. allows the SHOULD_QUIT field to be modified, otherwise, SHOULD_QUIT
  229. can't be changed from its default value by the user. */
  230. bool user_settable_should_quit;
  231. /* Reference a value from internal_problem_modes to indicate if GDB
  232. should quit when it hits a problem of this type. */
  233. const char *should_quit;
  234. /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_DUMP_CORE. */
  235. bool user_settable_should_dump_core;
  236. /* Like SHOULD_QUIT, but whether GDB should dump core. */
  237. const char *should_dump_core;
  238. /* Like USER_SETTABLE_SHOULD_QUIT but for SHOULD_PRINT_BACKTRACE. */
  239. bool user_settable_should_print_backtrace;
  240. /* When this is true GDB will print a backtrace when a problem of this
  241. type is encountered. */
  242. bool should_print_backtrace;
  243. };
  244. /* Return true if the readline callbacks have been initialized for UI.
  245. This is always true once GDB is fully initialized, but during the early
  246. startup phase this is initially false. */
  247. static bool
  248. readline_initialized (struct ui *ui)
  249. {
  250. return ui->call_readline != nullptr;
  251. }
  252. /* Report a problem, internal to GDB, to the user. Once the problem
  253. has been reported, and assuming GDB didn't quit, the caller can
  254. either allow execution to resume or throw an error. */
  255. static void ATTRIBUTE_PRINTF (4, 0)
  256. internal_vproblem (struct internal_problem *problem,
  257. const char *file, int line, const char *fmt, va_list ap)
  258. {
  259. static int dejavu;
  260. int quit_p;
  261. int dump_core_p;
  262. std::string reason;
  263. /* Don't allow infinite error/warning recursion. */
  264. {
  265. static const char msg[] = "Recursive internal problem.\n";
  266. switch (dejavu)
  267. {
  268. case 0:
  269. dejavu = 1;
  270. break;
  271. case 1:
  272. dejavu = 2;
  273. abort_with_message (msg);
  274. default:
  275. dejavu = 3;
  276. /* Newer GLIBC versions put the warn_unused_result attribute
  277. on write, but this is one of those rare cases where
  278. ignoring the return value is correct. Casting to (void)
  279. does not fix this problem. This is the solution suggested
  280. at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509. */
  281. if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg))
  282. abort (); /* ARI: abort */
  283. exit (1);
  284. }
  285. }
  286. /* Create a string containing the full error/warning message. Need
  287. to call query with this full string, as otherwize the reason
  288. (error/warning) and question become separated. Format using a
  289. style similar to a compiler error message. Include extra detail
  290. so that the user knows that they are living on the edge. */
  291. {
  292. std::string msg = string_vprintf (fmt, ap);
  293. reason = string_printf ("%s:%d: %s: %s\n"
  294. "A problem internal to GDB has been detected,\n"
  295. "further debugging may prove unreliable.",
  296. file, line, problem->name, msg.c_str ());
  297. }
  298. /* Fall back to abort_with_message if gdb_stderr is not set up. */
  299. if (current_ui == NULL)
  300. {
  301. fputs (reason.c_str (), stderr);
  302. abort_with_message ("\n");
  303. }
  304. /* Try to get the message out and at the start of a new line. */
  305. gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
  306. if (target_supports_terminal_ours ())
  307. {
  308. term_state.emplace ();
  309. target_terminal::ours_for_output ();
  310. }
  311. if (filtered_printing_initialized ())
  312. begin_line ();
  313. /* Emit the message unless query will emit it below. */
  314. if (problem->should_quit != internal_problem_ask
  315. || !confirm
  316. || !filtered_printing_initialized ()
  317. || !readline_initialized (current_ui)
  318. || problem->should_print_backtrace)
  319. gdb_printf (gdb_stderr, "%s\n", reason.c_str ());
  320. if (problem->should_print_backtrace)
  321. gdb_internal_backtrace ();
  322. if (problem->should_quit == internal_problem_ask)
  323. {
  324. /* Default (yes/batch case) is to quit GDB. When in batch mode
  325. this lessens the likelihood of GDB going into an infinite
  326. loop. */
  327. if (!confirm || !filtered_printing_initialized ()
  328. || !readline_initialized (current_ui))
  329. quit_p = 1;
  330. else
  331. quit_p = query (_("%s\nQuit this debugging session? "),
  332. reason.c_str ());
  333. }
  334. else if (problem->should_quit == internal_problem_yes)
  335. quit_p = 1;
  336. else if (problem->should_quit == internal_problem_no)
  337. quit_p = 0;
  338. else
  339. internal_error (__FILE__, __LINE__, _("bad switch"));
  340. gdb_puts (_("\nThis is a bug, please report it."), gdb_stderr);
  341. if (REPORT_BUGS_TO[0])
  342. gdb_printf (gdb_stderr, _(" For instructions, see:\n%ps."),
  343. styled_string (file_name_style.style (),
  344. REPORT_BUGS_TO));
  345. gdb_puts ("\n\n", gdb_stderr);
  346. if (problem->should_dump_core == internal_problem_ask)
  347. {
  348. if (!can_dump_core_warn (LIMIT_MAX, reason.c_str ()))
  349. dump_core_p = 0;
  350. else if (!filtered_printing_initialized ()
  351. || !readline_initialized (current_ui))
  352. dump_core_p = 1;
  353. else
  354. {
  355. /* Default (yes/batch case) is to dump core. This leaves a GDB
  356. `dropping' so that it is easier to see that something went
  357. wrong in GDB. */
  358. dump_core_p = query (_("%s\nCreate a core file of GDB? "),
  359. reason.c_str ());
  360. }
  361. }
  362. else if (problem->should_dump_core == internal_problem_yes)
  363. dump_core_p = can_dump_core_warn (LIMIT_MAX, reason.c_str ());
  364. else if (problem->should_dump_core == internal_problem_no)
  365. dump_core_p = 0;
  366. else
  367. internal_error (__FILE__, __LINE__, _("bad switch"));
  368. if (quit_p)
  369. {
  370. if (dump_core_p)
  371. dump_core ();
  372. else
  373. exit (1);
  374. }
  375. else
  376. {
  377. if (dump_core_p)
  378. {
  379. #ifdef HAVE_WORKING_FORK
  380. if (fork () == 0)
  381. dump_core ();
  382. #endif
  383. }
  384. }
  385. dejavu = 0;
  386. }
  387. static struct internal_problem internal_error_problem = {
  388. "internal-error", true, internal_problem_ask, true, internal_problem_ask,
  389. true, GDB_PRINT_INTERNAL_BACKTRACE_INIT_ON
  390. };
  391. void
  392. internal_verror (const char *file, int line, const char *fmt, va_list ap)
  393. {
  394. internal_vproblem (&internal_error_problem, file, line, fmt, ap);
  395. throw_quit (_("Command aborted."));
  396. }
  397. static struct internal_problem internal_warning_problem = {
  398. "internal-warning", true, internal_problem_ask, true, internal_problem_ask,
  399. true, false
  400. };
  401. void
  402. internal_vwarning (const char *file, int line, const char *fmt, va_list ap)
  403. {
  404. internal_vproblem (&internal_warning_problem, file, line, fmt, ap);
  405. }
  406. static struct internal_problem demangler_warning_problem = {
  407. "demangler-warning", true, internal_problem_ask, false, internal_problem_no,
  408. false, false
  409. };
  410. void
  411. demangler_vwarning (const char *file, int line, const char *fmt, va_list ap)
  412. {
  413. internal_vproblem (&demangler_warning_problem, file, line, fmt, ap);
  414. }
  415. void
  416. demangler_warning (const char *file, int line, const char *string, ...)
  417. {
  418. va_list ap;
  419. va_start (ap, string);
  420. demangler_vwarning (file, line, string, ap);
  421. va_end (ap);
  422. }
  423. /* When GDB reports an internal problem (error or warning) it gives
  424. the user the opportunity to quit GDB and/or create a core file of
  425. the current debug session. This function registers a few commands
  426. that make it possible to specify that GDB should always or never
  427. quit or create a core file, without asking. The commands look
  428. like:
  429. maint set PROBLEM-NAME quit ask|yes|no
  430. maint show PROBLEM-NAME quit
  431. maint set PROBLEM-NAME corefile ask|yes|no
  432. maint show PROBLEM-NAME corefile
  433. Where PROBLEM-NAME is currently "internal-error" or
  434. "internal-warning". */
  435. static void
  436. add_internal_problem_command (struct internal_problem *problem)
  437. {
  438. struct cmd_list_element **set_cmd_list;
  439. struct cmd_list_element **show_cmd_list;
  440. set_cmd_list = XNEW (struct cmd_list_element *);
  441. show_cmd_list = XNEW (struct cmd_list_element *);
  442. *set_cmd_list = NULL;
  443. *show_cmd_list = NULL;
  444. /* The add_basic_prefix_cmd and add_show_prefix_cmd functions take
  445. ownership of the string passed in, which is why we don't need to free
  446. set_doc and show_doc in this function. */
  447. const char *set_doc
  448. = xstrprintf (_("Configure what GDB does when %s is detected."),
  449. problem->name).release ();
  450. const char *show_doc
  451. = xstrprintf (_("Show what GDB does when %s is detected."),
  452. problem->name).release ();
  453. add_setshow_prefix_cmd (problem->name, class_maintenance,
  454. set_doc, show_doc, set_cmd_list, show_cmd_list,
  455. &maintenance_set_cmdlist, &maintenance_show_cmdlist);
  456. if (problem->user_settable_should_quit)
  457. {
  458. std::string set_quit_doc
  459. = string_printf (_("Set whether GDB should quit when an %s is "
  460. "detected."), problem->name);
  461. std::string show_quit_doc
  462. = string_printf (_("Show whether GDB will quit when an %s is "
  463. "detected."), problem->name);
  464. add_setshow_enum_cmd ("quit", class_maintenance,
  465. internal_problem_modes,
  466. &problem->should_quit,
  467. set_quit_doc.c_str (),
  468. show_quit_doc.c_str (),
  469. NULL, /* help_doc */
  470. NULL, /* setfunc */
  471. NULL, /* showfunc */
  472. set_cmd_list,
  473. show_cmd_list);
  474. }
  475. if (problem->user_settable_should_dump_core)
  476. {
  477. std::string set_core_doc
  478. = string_printf (_("Set whether GDB should create a core file of "
  479. "GDB when %s is detected."), problem->name);
  480. std::string show_core_doc
  481. = string_printf (_("Show whether GDB will create a core file of "
  482. "GDB when %s is detected."), problem->name);
  483. add_setshow_enum_cmd ("corefile", class_maintenance,
  484. internal_problem_modes,
  485. &problem->should_dump_core,
  486. set_core_doc.c_str (),
  487. show_core_doc.c_str (),
  488. NULL, /* help_doc */
  489. NULL, /* setfunc */
  490. NULL, /* showfunc */
  491. set_cmd_list,
  492. show_cmd_list);
  493. }
  494. if (problem->user_settable_should_print_backtrace)
  495. {
  496. std::string set_bt_doc
  497. = string_printf (_("Set whether GDB should print a backtrace of "
  498. "GDB when %s is detected."), problem->name);
  499. std::string show_bt_doc
  500. = string_printf (_("Show whether GDB will print a backtrace of "
  501. "GDB when %s is detected."), problem->name);
  502. add_setshow_boolean_cmd ("backtrace", class_maintenance,
  503. &problem->should_print_backtrace,
  504. set_bt_doc.c_str (),
  505. show_bt_doc.c_str (),
  506. NULL, /* help_doc */
  507. gdb_internal_backtrace_set_cmd,
  508. NULL, /* showfunc */
  509. set_cmd_list,
  510. show_cmd_list);
  511. }
  512. }
  513. /* Return a newly allocated string, containing the PREFIX followed
  514. by the system error message for errno (separated by a colon). */
  515. static std::string
  516. perror_string (const char *prefix)
  517. {
  518. const char *err = safe_strerror (errno);
  519. return std::string (prefix) + ": " + err;
  520. }
  521. /* Print the system error message for errno, and also mention STRING
  522. as the file name for which the error was encountered. Use ERRCODE
  523. for the thrown exception. Then return to command level. */
  524. void
  525. throw_perror_with_name (enum errors errcode, const char *string)
  526. {
  527. std::string combined = perror_string (string);
  528. /* I understand setting these is a matter of taste. Still, some people
  529. may clear errno but not know about bfd_error. Doing this here is not
  530. unreasonable. */
  531. bfd_set_error (bfd_error_no_error);
  532. errno = 0;
  533. throw_error (errcode, _("%s."), combined.c_str ());
  534. }
  535. /* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR. */
  536. void
  537. perror_with_name (const char *string)
  538. {
  539. throw_perror_with_name (GENERIC_ERROR, string);
  540. }
  541. /* Same as perror_with_name except that it prints a warning instead
  542. of throwing an error. */
  543. void
  544. perror_warning_with_name (const char *string)
  545. {
  546. std::string combined = perror_string (string);
  547. warning (_("%s"), combined.c_str ());
  548. }
  549. /* Print the system error message for ERRCODE, and also mention STRING
  550. as the file name for which the error was encountered. */
  551. void
  552. print_sys_errmsg (const char *string, int errcode)
  553. {
  554. const char *err = safe_strerror (errcode);
  555. gdb_printf (gdb_stderr, "%s: %s.\n", string, err);
  556. }
  557. /* Control C eventually causes this to be called, at a convenient time. */
  558. void
  559. quit (void)
  560. {
  561. if (sync_quit_force_run)
  562. {
  563. sync_quit_force_run = 0;
  564. quit_force (NULL, 0);
  565. }
  566. #ifdef __MSDOS__
  567. /* No steenking SIGINT will ever be coming our way when the
  568. program is resumed. Don't lie. */
  569. throw_quit ("Quit");
  570. #else
  571. if (job_control
  572. /* If there is no terminal switching for this target, then we can't
  573. possibly get screwed by the lack of job control. */
  574. || !target_supports_terminal_ours ())
  575. throw_quit ("Quit");
  576. else
  577. throw_quit ("Quit (expect signal SIGINT when the program is resumed)");
  578. #endif
  579. }
  580. /* See defs.h. */
  581. void
  582. maybe_quit (void)
  583. {
  584. if (!is_main_thread ())
  585. return;
  586. if (sync_quit_force_run)
  587. quit ();
  588. quit_handler ();
  589. }
  590. /* Called when a memory allocation fails, with the number of bytes of
  591. memory requested in SIZE. */
  592. void
  593. malloc_failure (long size)
  594. {
  595. if (size > 0)
  596. {
  597. internal_error (__FILE__, __LINE__,
  598. _("virtual memory exhausted: can't allocate %ld bytes."),
  599. size);
  600. }
  601. else
  602. {
  603. internal_error (__FILE__, __LINE__, _("virtual memory exhausted."));
  604. }
  605. }
  606. /* See common/errors.h. */
  607. void
  608. flush_streams ()
  609. {
  610. gdb_stdout->flush ();
  611. gdb_stderr->flush ();
  612. }
  613. /* My replacement for the read system call.
  614. Used like `read' but keeps going if `read' returns too soon. */
  615. int
  616. myread (int desc, char *addr, int len)
  617. {
  618. int val;
  619. int orglen = len;
  620. while (len > 0)
  621. {
  622. val = read (desc, addr, len);
  623. if (val < 0)
  624. return val;
  625. if (val == 0)
  626. return orglen - len;
  627. len -= val;
  628. addr += val;
  629. }
  630. return orglen;
  631. }
  632. /* See utils.h. */
  633. ULONGEST
  634. uinteger_pow (ULONGEST v1, LONGEST v2)
  635. {
  636. if (v2 < 0)
  637. {
  638. if (v1 == 0)
  639. error (_("Attempt to raise 0 to negative power."));
  640. else
  641. return 0;
  642. }
  643. else
  644. {
  645. /* The Russian Peasant's Algorithm. */
  646. ULONGEST v;
  647. v = 1;
  648. for (;;)
  649. {
  650. if (v2 & 1L)
  651. v *= v1;
  652. v2 >>= 1;
  653. if (v2 == 0)
  654. return v;
  655. v1 *= v1;
  656. }
  657. }
  658. }
  659. /* An RAII class that sets up to handle input and then tears down
  660. during destruction. */
  661. class scoped_input_handler
  662. {
  663. public:
  664. scoped_input_handler ()
  665. : m_quit_handler (&quit_handler, default_quit_handler),
  666. m_ui (NULL)
  667. {
  668. target_terminal::ours ();
  669. ui_register_input_event_handler (current_ui);
  670. if (current_ui->prompt_state == PROMPT_BLOCKED)
  671. m_ui = current_ui;
  672. }
  673. ~scoped_input_handler ()
  674. {
  675. if (m_ui != NULL)
  676. ui_unregister_input_event_handler (m_ui);
  677. }
  678. DISABLE_COPY_AND_ASSIGN (scoped_input_handler);
  679. private:
  680. /* Save and restore the terminal state. */
  681. target_terminal::scoped_restore_terminal_state m_term_state;
  682. /* Save and restore the quit handler. */
  683. scoped_restore_tmpl<quit_handler_ftype *> m_quit_handler;
  684. /* The saved UI, if non-NULL. */
  685. struct ui *m_ui;
  686. };
  687. /* This function supports the query, nquery, and yquery functions.
  688. Ask user a y-or-n question and return 0 if answer is no, 1 if
  689. answer is yes, or default the answer to the specified default
  690. (for yquery or nquery). DEFCHAR may be 'y' or 'n' to provide a
  691. default answer, or '\0' for no default.
  692. CTLSTR is the control string and should end in "? ". It should
  693. not say how to answer, because we do that.
  694. ARGS are the arguments passed along with the CTLSTR argument to
  695. printf. */
  696. static int ATTRIBUTE_PRINTF (1, 0)
  697. defaulted_query (const char *ctlstr, const char defchar, va_list args)
  698. {
  699. int retval;
  700. int def_value;
  701. char def_answer, not_def_answer;
  702. const char *y_string, *n_string;
  703. /* Set up according to which answer is the default. */
  704. if (defchar == '\0')
  705. {
  706. def_value = 1;
  707. def_answer = 'Y';
  708. not_def_answer = 'N';
  709. y_string = "y";
  710. n_string = "n";
  711. }
  712. else if (defchar == 'y')
  713. {
  714. def_value = 1;
  715. def_answer = 'Y';
  716. not_def_answer = 'N';
  717. y_string = "[y]";
  718. n_string = "n";
  719. }
  720. else
  721. {
  722. def_value = 0;
  723. def_answer = 'N';
  724. not_def_answer = 'Y';
  725. y_string = "y";
  726. n_string = "[n]";
  727. }
  728. /* Automatically answer the default value if the user did not want
  729. prompts or the command was issued with the server prefix. */
  730. if (!confirm || server_command)
  731. return def_value;
  732. /* If input isn't coming from the user directly, just say what
  733. question we're asking, and then answer the default automatically. This
  734. way, important error messages don't get lost when talking to GDB
  735. over a pipe. */
  736. if (current_ui->instream != current_ui->stdin_stream
  737. || !input_interactive_p (current_ui)
  738. /* Restrict queries to the main UI. */
  739. || current_ui != main_ui)
  740. {
  741. target_terminal::scoped_restore_terminal_state term_state;
  742. target_terminal::ours_for_output ();
  743. gdb_stdout->wrap_here (0);
  744. gdb_vprintf (gdb_stdout, ctlstr, args);
  745. gdb_printf (_("(%s or %s) [answered %c; "
  746. "input not from terminal]\n"),
  747. y_string, n_string, def_answer);
  748. return def_value;
  749. }
  750. if (deprecated_query_hook)
  751. {
  752. target_terminal::scoped_restore_terminal_state term_state;
  753. return deprecated_query_hook (ctlstr, args);
  754. }
  755. /* Format the question outside of the loop, to avoid reusing args. */
  756. std::string question = string_vprintf (ctlstr, args);
  757. std::string prompt
  758. = string_printf (_("%s%s(%s or %s) %s"),
  759. annotation_level > 1 ? "\n\032\032pre-query\n" : "",
  760. question.c_str (), y_string, n_string,
  761. annotation_level > 1 ? "\n\032\032query\n" : "");
  762. /* Used to add duration we waited for user to respond to
  763. prompt_for_continue_wait_time. */
  764. using namespace std::chrono;
  765. steady_clock::time_point prompt_started = steady_clock::now ();
  766. scoped_input_handler prepare_input;
  767. while (1)
  768. {
  769. char *response, answer;
  770. gdb_flush (gdb_stdout);
  771. response = gdb_readline_wrapper (prompt.c_str ());
  772. if (response == NULL) /* C-d */
  773. {
  774. gdb_printf ("EOF [assumed %c]\n", def_answer);
  775. retval = def_value;
  776. break;
  777. }
  778. answer = response[0];
  779. xfree (response);
  780. if (answer >= 'a')
  781. answer -= 040;
  782. /* Check answer. For the non-default, the user must specify
  783. the non-default explicitly. */
  784. if (answer == not_def_answer)
  785. {
  786. retval = !def_value;
  787. break;
  788. }
  789. /* Otherwise, if a default was specified, the user may either
  790. specify the required input or have it default by entering
  791. nothing. */
  792. if (answer == def_answer
  793. || (defchar != '\0' && answer == '\0'))
  794. {
  795. retval = def_value;
  796. break;
  797. }
  798. /* Invalid entries are not defaulted and require another selection. */
  799. gdb_printf (_("Please answer %s or %s.\n"),
  800. y_string, n_string);
  801. }
  802. /* Add time spend in this routine to prompt_for_continue_wait_time. */
  803. prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
  804. if (annotation_level > 1)
  805. gdb_printf (("\n\032\032post-query\n"));
  806. return retval;
  807. }
  808. /* Ask user a y-or-n question and return 0 if answer is no, 1 if
  809. answer is yes, or 0 if answer is defaulted.
  810. Takes three args which are given to printf to print the question.
  811. The first, a control string, should end in "? ".
  812. It should not say how to answer, because we do that. */
  813. int
  814. nquery (const char *ctlstr, ...)
  815. {
  816. va_list args;
  817. int ret;
  818. va_start (args, ctlstr);
  819. ret = defaulted_query (ctlstr, 'n', args);
  820. va_end (args);
  821. return ret;
  822. }
  823. /* Ask user a y-or-n question and return 0 if answer is no, 1 if
  824. answer is yes, or 1 if answer is defaulted.
  825. Takes three args which are given to printf to print the question.
  826. The first, a control string, should end in "? ".
  827. It should not say how to answer, because we do that. */
  828. int
  829. yquery (const char *ctlstr, ...)
  830. {
  831. va_list args;
  832. int ret;
  833. va_start (args, ctlstr);
  834. ret = defaulted_query (ctlstr, 'y', args);
  835. va_end (args);
  836. return ret;
  837. }
  838. /* Ask user a y-or-n question and return 1 iff answer is yes.
  839. Takes three args which are given to printf to print the question.
  840. The first, a control string, should end in "? ".
  841. It should not say how to answer, because we do that. */
  842. int
  843. query (const char *ctlstr, ...)
  844. {
  845. va_list args;
  846. int ret;
  847. va_start (args, ctlstr);
  848. ret = defaulted_query (ctlstr, '\0', args);
  849. va_end (args);
  850. return ret;
  851. }
  852. /* A helper for parse_escape that converts a host character to a
  853. target character. C is the host character. If conversion is
  854. possible, then the target character is stored in *TARGET_C and the
  855. function returns 1. Otherwise, the function returns 0. */
  856. static int
  857. host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c)
  858. {
  859. char the_char = c;
  860. int result = 0;
  861. auto_obstack host_data;
  862. convert_between_encodings (target_charset (gdbarch), host_charset (),
  863. (gdb_byte *) &the_char, 1, 1,
  864. &host_data, translit_none);
  865. if (obstack_object_size (&host_data) == 1)
  866. {
  867. result = 1;
  868. *target_c = *(char *) obstack_base (&host_data);
  869. }
  870. return result;
  871. }
  872. /* Parse a C escape sequence. STRING_PTR points to a variable
  873. containing a pointer to the string to parse. That pointer
  874. should point to the character after the \. That pointer
  875. is updated past the characters we use. The value of the
  876. escape sequence is returned.
  877. A negative value means the sequence \ newline was seen,
  878. which is supposed to be equivalent to nothing at all.
  879. If \ is followed by a null character, we return a negative
  880. value and leave the string pointer pointing at the null character.
  881. If \ is followed by 000, we return 0 and leave the string pointer
  882. after the zeros. A value of 0 does not mean end of string. */
  883. int
  884. parse_escape (struct gdbarch *gdbarch, const char **string_ptr)
  885. {
  886. int target_char = -2; /* Initialize to avoid GCC warnings. */
  887. int c = *(*string_ptr)++;
  888. switch (c)
  889. {
  890. case '\n':
  891. return -2;
  892. case 0:
  893. (*string_ptr)--;
  894. return 0;
  895. case '0':
  896. case '1':
  897. case '2':
  898. case '3':
  899. case '4':
  900. case '5':
  901. case '6':
  902. case '7':
  903. {
  904. int i = fromhex (c);
  905. int count = 0;
  906. while (++count < 3)
  907. {
  908. c = (**string_ptr);
  909. if (ISDIGIT (c) && c != '8' && c != '9')
  910. {
  911. (*string_ptr)++;
  912. i *= 8;
  913. i += fromhex (c);
  914. }
  915. else
  916. {
  917. break;
  918. }
  919. }
  920. return i;
  921. }
  922. case 'a':
  923. c = '\a';
  924. break;
  925. case 'b':
  926. c = '\b';
  927. break;
  928. case 'f':
  929. c = '\f';
  930. break;
  931. case 'n':
  932. c = '\n';
  933. break;
  934. case 'r':
  935. c = '\r';
  936. break;
  937. case 't':
  938. c = '\t';
  939. break;
  940. case 'v':
  941. c = '\v';
  942. break;
  943. default:
  944. break;
  945. }
  946. if (!host_char_to_target (gdbarch, c, &target_char))
  947. error (_("The escape sequence `\\%c' is equivalent to plain `%c',"
  948. " which has no equivalent\nin the `%s' character set."),
  949. c, c, target_charset (gdbarch));
  950. return target_char;
  951. }
  952. /* Number of lines per page or UINT_MAX if paging is disabled. */
  953. static unsigned int lines_per_page;
  954. static void
  955. show_lines_per_page (struct ui_file *file, int from_tty,
  956. struct cmd_list_element *c, const char *value)
  957. {
  958. gdb_printf (file,
  959. _("Number of lines gdb thinks are in a page is %s.\n"),
  960. value);
  961. }
  962. /* Number of chars per line or UINT_MAX if line folding is disabled. */
  963. static unsigned int chars_per_line;
  964. static void
  965. show_chars_per_line (struct ui_file *file, int from_tty,
  966. struct cmd_list_element *c, const char *value)
  967. {
  968. gdb_printf (file,
  969. _("Number of characters gdb thinks "
  970. "are in a line is %s.\n"),
  971. value);
  972. }
  973. /* Current count of lines printed on this page, chars on this line. */
  974. static unsigned int lines_printed, chars_printed;
  975. /* True if pagination is disabled for just one command. */
  976. static bool pagination_disabled_for_command;
  977. /* Buffer and start column of buffered text, for doing smarter word-
  978. wrapping. When someone calls wrap_here(), we start buffering output
  979. that comes through gdb_puts(). If we see a newline, we just
  980. spit it out and forget about the wrap_here(). If we see another
  981. wrap_here(), we spit it out and remember the newer one. If we see
  982. the end of the line, we spit out a newline, the indent, and then
  983. the buffered output. */
  984. static bool filter_initialized = false;
  985. /* Initialize the number of lines per page and chars per line. */
  986. void
  987. init_page_info (void)
  988. {
  989. if (batch_flag)
  990. {
  991. lines_per_page = UINT_MAX;
  992. chars_per_line = UINT_MAX;
  993. }
  994. else
  995. #if defined(TUI)
  996. if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
  997. #endif
  998. {
  999. int rows, cols;
  1000. #if defined(__GO32__)
  1001. rows = ScreenRows ();
  1002. cols = ScreenCols ();
  1003. lines_per_page = rows;
  1004. chars_per_line = cols;
  1005. #else
  1006. /* Make sure Readline has initialized its terminal settings. */
  1007. rl_reset_terminal (NULL);
  1008. /* Get the screen size from Readline. */
  1009. rl_get_screen_size (&rows, &cols);
  1010. lines_per_page = rows;
  1011. chars_per_line = cols;
  1012. /* Readline should have fetched the termcap entry for us.
  1013. Only try to use tgetnum function if rl_get_screen_size
  1014. did not return a useful value. */
  1015. if (((rows <= 0) && (tgetnum ((char *) "li") < 0))
  1016. /* Also disable paging if inside Emacs. $EMACS was used
  1017. before Emacs v25.1, $INSIDE_EMACS is used since then. */
  1018. || getenv ("EMACS") || getenv ("INSIDE_EMACS"))
  1019. {
  1020. /* The number of lines per page is not mentioned in the terminal
  1021. description or EMACS environment variable is set. This probably
  1022. means that paging is not useful, so disable paging. */
  1023. lines_per_page = UINT_MAX;
  1024. }
  1025. /* If the output is not a terminal, don't paginate it. */
  1026. if (!gdb_stdout->isatty ())
  1027. lines_per_page = UINT_MAX;
  1028. #endif
  1029. }
  1030. /* We handle SIGWINCH ourselves. */
  1031. rl_catch_sigwinch = 0;
  1032. set_screen_size ();
  1033. set_width ();
  1034. }
  1035. /* Return nonzero if filtered printing is initialized. */
  1036. int
  1037. filtered_printing_initialized (void)
  1038. {
  1039. return filter_initialized;
  1040. }
  1041. set_batch_flag_and_restore_page_info::set_batch_flag_and_restore_page_info ()
  1042. : m_save_lines_per_page (lines_per_page),
  1043. m_save_chars_per_line (chars_per_line),
  1044. m_save_batch_flag (batch_flag)
  1045. {
  1046. batch_flag = 1;
  1047. init_page_info ();
  1048. }
  1049. set_batch_flag_and_restore_page_info::~set_batch_flag_and_restore_page_info ()
  1050. {
  1051. batch_flag = m_save_batch_flag;
  1052. chars_per_line = m_save_chars_per_line;
  1053. lines_per_page = m_save_lines_per_page;
  1054. set_screen_size ();
  1055. set_width ();
  1056. }
  1057. /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */
  1058. static void
  1059. set_screen_size (void)
  1060. {
  1061. int rows = lines_per_page;
  1062. int cols = chars_per_line;
  1063. /* If we get 0 or negative ROWS or COLS, treat as "infinite" size.
  1064. A negative number can be seen here with the "set width/height"
  1065. commands and either:
  1066. - the user specified "unlimited", which maps to UINT_MAX, or
  1067. - the user specified some number between INT_MAX and UINT_MAX.
  1068. Cap "infinity" to approximately sqrt(INT_MAX) so that we don't
  1069. overflow in rl_set_screen_size, which multiplies rows and columns
  1070. to compute the number of characters on the screen. */
  1071. const int sqrt_int_max = INT_MAX >> (sizeof (int) * 8 / 2);
  1072. if (rows <= 0 || rows > sqrt_int_max)
  1073. {
  1074. rows = sqrt_int_max;
  1075. lines_per_page = UINT_MAX;
  1076. }
  1077. if (cols <= 0 || cols > sqrt_int_max)
  1078. {
  1079. cols = sqrt_int_max;
  1080. chars_per_line = UINT_MAX;
  1081. }
  1082. /* Update Readline's idea of the terminal size. */
  1083. rl_set_screen_size (rows, cols);
  1084. }
  1085. /* Reinitialize WRAP_BUFFER. */
  1086. static void
  1087. set_width (void)
  1088. {
  1089. if (chars_per_line == 0)
  1090. init_page_info ();
  1091. filter_initialized = true;
  1092. }
  1093. static void
  1094. set_width_command (const char *args, int from_tty, struct cmd_list_element *c)
  1095. {
  1096. set_screen_size ();
  1097. set_width ();
  1098. }
  1099. static void
  1100. set_height_command (const char *args, int from_tty, struct cmd_list_element *c)
  1101. {
  1102. set_screen_size ();
  1103. }
  1104. /* See utils.h. */
  1105. void
  1106. set_screen_width_and_height (int width, int height)
  1107. {
  1108. lines_per_page = height;
  1109. chars_per_line = width;
  1110. set_screen_size ();
  1111. set_width ();
  1112. }
  1113. void
  1114. pager_file::emit_style_escape (const ui_file_style &style)
  1115. {
  1116. if (can_emit_style_escape () && style != m_applied_style)
  1117. {
  1118. m_applied_style = style;
  1119. if (m_paging)
  1120. m_stream->emit_style_escape (style);
  1121. else
  1122. m_wrap_buffer.append (style.to_ansi ());
  1123. }
  1124. }
  1125. /* See pager.h. */
  1126. void
  1127. pager_file::reset_style ()
  1128. {
  1129. if (can_emit_style_escape ())
  1130. {
  1131. m_applied_style = ui_file_style ();
  1132. m_wrap_buffer.append (m_applied_style.to_ansi ());
  1133. }
  1134. }
  1135. /* Wait, so the user can read what's on the screen. Prompt the user
  1136. to continue by pressing RETURN. 'q' is also provided because
  1137. telling users what to do in the prompt is more user-friendly than
  1138. expecting them to think of Ctrl-C/SIGINT. */
  1139. void
  1140. pager_file::prompt_for_continue ()
  1141. {
  1142. char cont_prompt[120];
  1143. /* Used to add duration we waited for user to respond to
  1144. prompt_for_continue_wait_time. */
  1145. using namespace std::chrono;
  1146. steady_clock::time_point prompt_started = steady_clock::now ();
  1147. bool disable_pagination = pagination_disabled_for_command;
  1148. scoped_restore save_paging = make_scoped_restore (&m_paging, true);
  1149. /* Clear the current styling. */
  1150. m_stream->emit_style_escape (ui_file_style ());
  1151. if (annotation_level > 1)
  1152. m_stream->puts (("\n\032\032pre-prompt-for-continue\n"));
  1153. strcpy (cont_prompt,
  1154. "--Type <RET> for more, q to quit, "
  1155. "c to continue without paging--");
  1156. if (annotation_level > 1)
  1157. strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
  1158. /* We must do this *before* we call gdb_readline_wrapper, else it
  1159. will eventually call us -- thinking that we're trying to print
  1160. beyond the end of the screen. */
  1161. reinitialize_more_filter ();
  1162. scoped_input_handler prepare_input;
  1163. /* Call gdb_readline_wrapper, not readline, in order to keep an
  1164. event loop running. */
  1165. gdb::unique_xmalloc_ptr<char> ignore (gdb_readline_wrapper (cont_prompt));
  1166. /* Add time spend in this routine to prompt_for_continue_wait_time. */
  1167. prompt_for_continue_wait_time += steady_clock::now () - prompt_started;
  1168. if (annotation_level > 1)
  1169. m_stream->puts (("\n\032\032post-prompt-for-continue\n"));
  1170. if (ignore != NULL)
  1171. {
  1172. char *p = ignore.get ();
  1173. while (*p == ' ' || *p == '\t')
  1174. ++p;
  1175. if (p[0] == 'q')
  1176. /* Do not call quit here; there is no possibility of SIGINT. */
  1177. throw_quit ("Quit");
  1178. if (p[0] == 'c')
  1179. disable_pagination = true;
  1180. }
  1181. /* Now we have to do this again, so that GDB will know that it doesn't
  1182. need to save the ---Type <return>--- line at the top of the screen. */
  1183. reinitialize_more_filter ();
  1184. pagination_disabled_for_command = disable_pagination;
  1185. dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
  1186. }
  1187. /* Initialize timer to keep track of how long we waited for the user. */
  1188. void
  1189. reset_prompt_for_continue_wait_time (void)
  1190. {
  1191. using namespace std::chrono;
  1192. prompt_for_continue_wait_time = steady_clock::duration::zero ();
  1193. }
  1194. /* Fetch the cumulative time spent in prompt_for_continue. */
  1195. std::chrono::steady_clock::duration
  1196. get_prompt_for_continue_wait_time ()
  1197. {
  1198. return prompt_for_continue_wait_time;
  1199. }
  1200. /* Reinitialize filter; ie. tell it to reset to original values. */
  1201. void
  1202. reinitialize_more_filter (void)
  1203. {
  1204. lines_printed = 0;
  1205. chars_printed = 0;
  1206. pagination_disabled_for_command = false;
  1207. }
  1208. void
  1209. pager_file::flush_wrap_buffer ()
  1210. {
  1211. if (!m_paging && !m_wrap_buffer.empty ())
  1212. {
  1213. m_stream->puts (m_wrap_buffer.c_str ());
  1214. m_wrap_buffer.clear ();
  1215. }
  1216. }
  1217. void
  1218. pager_file::flush ()
  1219. {
  1220. flush_wrap_buffer ();
  1221. m_stream->flush ();
  1222. }
  1223. /* See utils.h. */
  1224. void
  1225. gdb_flush (struct ui_file *stream)
  1226. {
  1227. stream->flush ();
  1228. }
  1229. /* See utils.h. */
  1230. int
  1231. get_chars_per_line ()
  1232. {
  1233. return chars_per_line;
  1234. }
  1235. /* See ui-file.h. */
  1236. void
  1237. pager_file::wrap_here (int indent)
  1238. {
  1239. /* This should have been allocated, but be paranoid anyway. */
  1240. gdb_assert (filter_initialized);
  1241. flush_wrap_buffer ();
  1242. if (chars_per_line == UINT_MAX) /* No line overflow checking. */
  1243. {
  1244. m_wrap_column = 0;
  1245. }
  1246. else if (chars_printed >= chars_per_line)
  1247. {
  1248. this->puts ("\n");
  1249. if (indent != 0)
  1250. this->puts (n_spaces (indent));
  1251. m_wrap_column = 0;
  1252. }
  1253. else
  1254. {
  1255. m_wrap_column = chars_printed;
  1256. m_wrap_indent = indent;
  1257. m_wrap_style = m_applied_style;
  1258. }
  1259. }
  1260. /* Print input string to gdb_stdout arranging strings in columns of n
  1261. chars. String can be right or left justified in the column. Never
  1262. prints trailing spaces. String should never be longer than width.
  1263. FIXME: this could be useful for the EXAMINE command, which
  1264. currently doesn't tabulate very well. */
  1265. void
  1266. puts_tabular (char *string, int width, int right)
  1267. {
  1268. int spaces = 0;
  1269. int stringlen;
  1270. char *spacebuf;
  1271. gdb_assert (chars_per_line > 0);
  1272. if (chars_per_line == UINT_MAX)
  1273. {
  1274. gdb_puts (string);
  1275. gdb_puts ("\n");
  1276. return;
  1277. }
  1278. if (((chars_printed - 1) / width + 2) * width >= chars_per_line)
  1279. gdb_puts ("\n");
  1280. if (width >= chars_per_line)
  1281. width = chars_per_line - 1;
  1282. stringlen = strlen (string);
  1283. if (chars_printed > 0)
  1284. spaces = width - (chars_printed - 1) % width - 1;
  1285. if (right)
  1286. spaces += width - stringlen;
  1287. spacebuf = (char *) alloca (spaces + 1);
  1288. spacebuf[spaces] = '\0';
  1289. while (spaces--)
  1290. spacebuf[spaces] = ' ';
  1291. gdb_puts (spacebuf);
  1292. gdb_puts (string);
  1293. }
  1294. /* Ensure that whatever gets printed next, using the filtered output
  1295. commands, starts at the beginning of the line. I.e. if there is
  1296. any pending output for the current line, flush it and start a new
  1297. line. Otherwise do nothing. */
  1298. void
  1299. begin_line (void)
  1300. {
  1301. if (chars_printed > 0)
  1302. {
  1303. gdb_puts ("\n");
  1304. }
  1305. }
  1306. void
  1307. pager_file::puts (const char *linebuffer)
  1308. {
  1309. const char *lineptr;
  1310. if (linebuffer == 0)
  1311. return;
  1312. /* Don't do any filtering if it is disabled. */
  1313. if (!pagination_enabled
  1314. || pagination_disabled_for_command
  1315. || batch_flag
  1316. || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)
  1317. || top_level_interpreter () == NULL
  1318. || top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
  1319. {
  1320. flush_wrap_buffer ();
  1321. m_stream->puts (linebuffer);
  1322. return;
  1323. }
  1324. auto buffer_clearer
  1325. = make_scope_exit ([&] ()
  1326. {
  1327. m_wrap_buffer.clear ();
  1328. m_wrap_column = 0;
  1329. m_wrap_indent = 0;
  1330. });
  1331. /* Go through and output each character. Show line extension
  1332. when this is necessary; prompt user for new page when this is
  1333. necessary. */
  1334. lineptr = linebuffer;
  1335. while (*lineptr)
  1336. {
  1337. /* Possible new page. Note that PAGINATION_DISABLED_FOR_COMMAND
  1338. might be set during this loop, so we must continue to check
  1339. it here. */
  1340. if ((lines_printed >= lines_per_page - 1)
  1341. && !pagination_disabled_for_command)
  1342. prompt_for_continue ();
  1343. while (*lineptr && *lineptr != '\n')
  1344. {
  1345. int skip_bytes;
  1346. /* Print a single line. */
  1347. if (*lineptr == '\t')
  1348. {
  1349. m_wrap_buffer.push_back ('\t');
  1350. /* Shifting right by 3 produces the number of tab stops
  1351. we have already passed, and then adding one and
  1352. shifting left 3 advances to the next tab stop. */
  1353. chars_printed = ((chars_printed >> 3) + 1) << 3;
  1354. lineptr++;
  1355. }
  1356. else if (*lineptr == '\033'
  1357. && skip_ansi_escape (lineptr, &skip_bytes))
  1358. {
  1359. m_wrap_buffer.append (lineptr, skip_bytes);
  1360. /* Note that we don't consider this a character, so we
  1361. don't increment chars_printed here. */
  1362. lineptr += skip_bytes;
  1363. }
  1364. else if (*lineptr == '\r')
  1365. {
  1366. m_wrap_buffer.push_back (*lineptr);
  1367. chars_printed = 0;
  1368. lineptr++;
  1369. }
  1370. else
  1371. {
  1372. m_wrap_buffer.push_back (*lineptr);
  1373. chars_printed++;
  1374. lineptr++;
  1375. }
  1376. if (chars_printed >= chars_per_line)
  1377. {
  1378. unsigned int save_chars = chars_printed;
  1379. /* If we change the style, below, we'll want to reset it
  1380. before continuing to print. If there is no wrap
  1381. column, then we'll only reset the style if the pager
  1382. prompt is given; and to avoid emitting style
  1383. sequences in the middle of a run of text, we track
  1384. this as well. */
  1385. ui_file_style save_style = m_applied_style;
  1386. bool did_paginate = false;
  1387. chars_printed = 0;
  1388. lines_printed++;
  1389. if (m_wrap_column)
  1390. {
  1391. /* We are about to insert a newline at an historic
  1392. location in the WRAP_BUFFER. Before we do we want to
  1393. restore the default style. To know if we actually
  1394. need to insert an escape sequence we must restore the
  1395. current applied style to how it was at the WRAP_COLUMN
  1396. location. */
  1397. m_applied_style = m_wrap_style;
  1398. m_stream->emit_style_escape (ui_file_style ());
  1399. /* If we aren't actually wrapping, don't output
  1400. newline -- if chars_per_line is right, we
  1401. probably just overflowed anyway; if it's wrong,
  1402. let us keep going. */
  1403. m_stream->puts ("\n");
  1404. }
  1405. else
  1406. this->flush_wrap_buffer ();
  1407. /* Possible new page. Note that
  1408. PAGINATION_DISABLED_FOR_COMMAND might be set during
  1409. this loop, so we must continue to check it here. */
  1410. if (lines_printed >= lines_per_page - 1
  1411. && !pagination_disabled_for_command)
  1412. {
  1413. prompt_for_continue ();
  1414. did_paginate = true;
  1415. }
  1416. /* Now output indentation and wrapped string. */
  1417. if (m_wrap_column)
  1418. {
  1419. m_stream->puts (n_spaces (m_wrap_indent));
  1420. /* Having finished inserting the wrapping we should
  1421. restore the style as it was at the WRAP_COLUMN. */
  1422. m_stream->emit_style_escape (m_wrap_style);
  1423. /* The WRAP_BUFFER will still contain content, and that
  1424. content might set some alternative style. Restore
  1425. APPLIED_STYLE as it was before we started wrapping,
  1426. this reflects the current style for the last character
  1427. in WRAP_BUFFER. */
  1428. m_applied_style = save_style;
  1429. /* Note that this can set chars_printed > chars_per_line
  1430. if we are printing a long string. */
  1431. chars_printed = m_wrap_indent + (save_chars - m_wrap_column);
  1432. m_wrap_column = 0; /* And disable fancy wrap */
  1433. }
  1434. else if (did_paginate)
  1435. m_stream->emit_style_escape (save_style);
  1436. }
  1437. }
  1438. if (*lineptr == '\n')
  1439. {
  1440. chars_printed = 0;
  1441. wrap_here (0); /* Spit out chars, cancel further wraps. */
  1442. lines_printed++;
  1443. m_stream->puts ("\n");
  1444. lineptr++;
  1445. }
  1446. }
  1447. buffer_clearer.release ();
  1448. }
  1449. void
  1450. pager_file::write (const char *buf, long length_buf)
  1451. {
  1452. /* We have to make a string here because the pager uses
  1453. skip_ansi_escape, which requires NUL-termination. */
  1454. std::string str (buf, length_buf);
  1455. this->puts (str.c_str ());
  1456. }
  1457. void
  1458. gdb_puts (const char *linebuffer, struct ui_file *stream)
  1459. {
  1460. stream->puts (linebuffer);
  1461. }
  1462. /* See utils.h. */
  1463. void
  1464. fputs_styled (const char *linebuffer, const ui_file_style &style,
  1465. struct ui_file *stream)
  1466. {
  1467. stream->emit_style_escape (style);
  1468. gdb_puts (linebuffer, stream);
  1469. stream->emit_style_escape (ui_file_style ());
  1470. }
  1471. /* See utils.h. */
  1472. void
  1473. fputs_highlighted (const char *str, const compiled_regex &highlight,
  1474. struct ui_file *stream)
  1475. {
  1476. regmatch_t pmatch;
  1477. while (*str && highlight.exec (str, 1, &pmatch, 0) == 0)
  1478. {
  1479. size_t n_highlight = pmatch.rm_eo - pmatch.rm_so;
  1480. /* Output the part before pmatch with current style. */
  1481. while (pmatch.rm_so > 0)
  1482. {
  1483. gdb_putc (*str, stream);
  1484. pmatch.rm_so--;
  1485. str++;
  1486. }
  1487. /* Output pmatch with the highlight style. */
  1488. stream->emit_style_escape (highlight_style.style ());
  1489. while (n_highlight > 0)
  1490. {
  1491. gdb_putc (*str, stream);
  1492. n_highlight--;
  1493. str++;
  1494. }
  1495. stream->emit_style_escape (ui_file_style ());
  1496. }
  1497. /* Output the trailing part of STR not matching HIGHLIGHT. */
  1498. if (*str)
  1499. gdb_puts (str, stream);
  1500. }
  1501. /* Write character C to gdb_stdout using GDB's paging mechanism and return C.
  1502. May return nonlocally. */
  1503. int
  1504. gdb_putc (int c)
  1505. {
  1506. return gdb_stdout->putc (c);
  1507. }
  1508. int
  1509. gdb_putc (int c, struct ui_file *stream)
  1510. {
  1511. return stream->putc (c);
  1512. }
  1513. void
  1514. gdb_vprintf (struct ui_file *stream, const char *format, va_list args)
  1515. {
  1516. stream->vprintf (format, args);
  1517. }
  1518. void
  1519. gdb_vprintf (const char *format, va_list args)
  1520. {
  1521. gdb_stdout->vprintf (format, args);
  1522. }
  1523. void
  1524. gdb_printf (struct ui_file *stream, const char *format, ...)
  1525. {
  1526. va_list args;
  1527. va_start (args, format);
  1528. gdb_vprintf (stream, format, args);
  1529. va_end (args);
  1530. }
  1531. /* See utils.h. */
  1532. void
  1533. fprintf_styled (struct ui_file *stream, const ui_file_style &style,
  1534. const char *format, ...)
  1535. {
  1536. va_list args;
  1537. stream->emit_style_escape (style);
  1538. va_start (args, format);
  1539. gdb_vprintf (stream, format, args);
  1540. va_end (args);
  1541. stream->emit_style_escape (ui_file_style ());
  1542. }
  1543. void
  1544. gdb_printf (const char *format, ...)
  1545. {
  1546. va_list args;
  1547. va_start (args, format);
  1548. gdb_vprintf (gdb_stdout, format, args);
  1549. va_end (args);
  1550. }
  1551. void
  1552. printf_unfiltered (const char *format, ...)
  1553. {
  1554. va_list args;
  1555. va_start (args, format);
  1556. string_file file (gdb_stdout->can_emit_style_escape ());
  1557. file.vprintf (format, args);
  1558. gdb_stdout->puts_unfiltered (file.string ().c_str ());
  1559. va_end (args);
  1560. }
  1561. /* Easy -- but watch out!
  1562. This routine is *not* a replacement for puts()! puts() appends a newline.
  1563. This one doesn't, and had better not! */
  1564. void
  1565. gdb_puts (const char *string)
  1566. {
  1567. gdb_stdout->puts (string);
  1568. }
  1569. /* Return a pointer to N spaces and a null. The pointer is good
  1570. until the next call to here. */
  1571. const char *
  1572. n_spaces (int n)
  1573. {
  1574. char *t;
  1575. static char *spaces = 0;
  1576. static int max_spaces = -1;
  1577. if (n > max_spaces)
  1578. {
  1579. xfree (spaces);
  1580. spaces = (char *) xmalloc (n + 1);
  1581. for (t = spaces + n; t != spaces;)
  1582. *--t = ' ';
  1583. spaces[n] = '\0';
  1584. max_spaces = n;
  1585. }
  1586. return spaces + max_spaces - n;
  1587. }
  1588. /* Print N spaces. */
  1589. void
  1590. print_spaces (int n, struct ui_file *stream)
  1591. {
  1592. gdb_puts (n_spaces (n), stream);
  1593. }
  1594. /* C++/ObjC demangler stuff. */
  1595. /* fprintf_symbol attempts to demangle NAME, a symbol in language
  1596. LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
  1597. If the name is not mangled, or the language for the name is unknown, or
  1598. demangling is off, the name is printed in its "raw" form. */
  1599. void
  1600. fprintf_symbol (struct ui_file *stream, const char *name,
  1601. enum language lang, int arg_mode)
  1602. {
  1603. if (name != NULL)
  1604. {
  1605. /* If user wants to see raw output, no problem. */
  1606. if (!demangle)
  1607. {
  1608. gdb_puts (name, stream);
  1609. }
  1610. else
  1611. {
  1612. gdb::unique_xmalloc_ptr<char> demangled
  1613. = language_demangle (language_def (lang), name, arg_mode);
  1614. gdb_puts (demangled ? demangled.get () : name, stream);
  1615. }
  1616. }
  1617. }
  1618. /* True if CH is a character that can be part of a symbol name. I.e.,
  1619. either a number, a letter, or a '_'. */
  1620. static bool
  1621. valid_identifier_name_char (int ch)
  1622. {
  1623. return (ISALNUM (ch) || ch == '_');
  1624. }
  1625. /* Skip to end of token, or to END, whatever comes first. Input is
  1626. assumed to be a C++ operator name. */
  1627. static const char *
  1628. cp_skip_operator_token (const char *token, const char *end)
  1629. {
  1630. const char *p = token;
  1631. while (p != end && !ISSPACE (*p) && *p != '(')
  1632. {
  1633. if (valid_identifier_name_char (*p))
  1634. {
  1635. while (p != end && valid_identifier_name_char (*p))
  1636. p++;
  1637. return p;
  1638. }
  1639. else
  1640. {
  1641. /* Note, ordered such that among ops that share a prefix,
  1642. longer comes first. This is so that the loop below can
  1643. bail on first match. */
  1644. static const char *ops[] =
  1645. {
  1646. "[",
  1647. "]",
  1648. "~",
  1649. ",",
  1650. "-=", "--", "->", "-",
  1651. "+=", "++", "+",
  1652. "*=", "*",
  1653. "/=", "/",
  1654. "%=", "%",
  1655. "|=", "||", "|",
  1656. "&=", "&&", "&",
  1657. "^=", "^",
  1658. "!=", "!",
  1659. "<<=", "<=", "<<", "<",
  1660. ">>=", ">=", ">>", ">",
  1661. "==", "=",
  1662. };
  1663. for (const char *op : ops)
  1664. {
  1665. size_t oplen = strlen (op);
  1666. size_t lencmp = std::min<size_t> (oplen, end - p);
  1667. if (strncmp (p, op, lencmp) == 0)
  1668. return p + lencmp;
  1669. }
  1670. /* Some unidentified character. Return it. */
  1671. return p + 1;
  1672. }
  1673. }
  1674. return p;
  1675. }
  1676. /* Advance STRING1/STRING2 past whitespace. */
  1677. static void
  1678. skip_ws (const char *&string1, const char *&string2, const char *end_str2)
  1679. {
  1680. while (ISSPACE (*string1))
  1681. string1++;
  1682. while (string2 < end_str2 && ISSPACE (*string2))
  1683. string2++;
  1684. }
  1685. /* True if STRING points at the start of a C++ operator name. START
  1686. is the start of the string that STRING points to, hence when
  1687. reading backwards, we must not read any character before START. */
  1688. static bool
  1689. cp_is_operator (const char *string, const char *start)
  1690. {
  1691. return ((string == start
  1692. || !valid_identifier_name_char (string[-1]))
  1693. && strncmp (string, CP_OPERATOR_STR, CP_OPERATOR_LEN) == 0
  1694. && !valid_identifier_name_char (string[CP_OPERATOR_LEN]));
  1695. }
  1696. /* If *NAME points at an ABI tag, skip it and return true. Otherwise
  1697. leave *NAME unmodified and return false. (see GCC's abi_tag
  1698. attribute), such names are demangled as e.g.,
  1699. "function[abi:cxx11]()". */
  1700. static bool
  1701. skip_abi_tag (const char **name)
  1702. {
  1703. const char *p = *name;
  1704. if (startswith (p, "[abi:"))
  1705. {
  1706. p += 5;
  1707. while (valid_identifier_name_char (*p))
  1708. p++;
  1709. if (*p == ']')
  1710. {
  1711. p++;
  1712. *name = p;
  1713. return true;
  1714. }
  1715. }
  1716. return false;
  1717. }
  1718. /* If *NAME points at a template parameter list, skip it and return true.
  1719. Otherwise do nothing and return false. */
  1720. static bool
  1721. skip_template_parameter_list (const char **name)
  1722. {
  1723. const char *p = *name;
  1724. if (*p == '<')
  1725. {
  1726. const char *template_param_list_end = find_toplevel_char (p + 1, '>');
  1727. if (template_param_list_end == NULL)
  1728. return false;
  1729. p = template_param_list_end + 1;
  1730. /* Skip any whitespace that might occur after the closing of the
  1731. parameter list, but only if it is the end of parameter list. */
  1732. const char *q = p;
  1733. while (ISSPACE (*q))
  1734. ++q;
  1735. if (*q == '>')
  1736. p = q;
  1737. *name = p;
  1738. return true;
  1739. }
  1740. return false;
  1741. }
  1742. /* See utils.h. */
  1743. int
  1744. strncmp_iw_with_mode (const char *string1, const char *string2,
  1745. size_t string2_len, strncmp_iw_mode mode,
  1746. enum language language,
  1747. completion_match_for_lcd *match_for_lcd,
  1748. bool ignore_template_params)
  1749. {
  1750. const char *string1_start = string1;
  1751. const char *end_str2 = string2 + string2_len;
  1752. bool skip_spaces = true;
  1753. bool have_colon_op = (language == language_cplus
  1754. || language == language_rust
  1755. || language == language_fortran);
  1756. while (1)
  1757. {
  1758. if (skip_spaces
  1759. || ((ISSPACE (*string1) && !valid_identifier_name_char (*string2))
  1760. || (ISSPACE (*string2) && !valid_identifier_name_char (*string1))))
  1761. {
  1762. skip_ws (string1, string2, end_str2);
  1763. skip_spaces = false;
  1764. }
  1765. /* Skip [abi:cxx11] tags in the symbol name if the lookup name
  1766. doesn't include them. E.g.:
  1767. string1: function[abi:cxx1](int)
  1768. string2: function
  1769. string1: function[abi:cxx1](int)
  1770. string2: function(int)
  1771. string1: Struct[abi:cxx1]::function()
  1772. string2: Struct::function()
  1773. string1: function(Struct[abi:cxx1], int)
  1774. string2: function(Struct, int)
  1775. */
  1776. if (string2 == end_str2
  1777. || (*string2 != '[' && !valid_identifier_name_char (*string2)))
  1778. {
  1779. const char *abi_start = string1;
  1780. /* There can be more than one tag. */
  1781. while (*string1 == '[' && skip_abi_tag (&string1))
  1782. ;
  1783. if (match_for_lcd != NULL && abi_start != string1)
  1784. match_for_lcd->mark_ignored_range (abi_start, string1);
  1785. while (ISSPACE (*string1))
  1786. string1++;
  1787. }
  1788. /* Skip template parameters in STRING1 if STRING2 does not contain
  1789. any. E.g.:
  1790. Case 1: User is looking for all functions named "foo".
  1791. string1: foo <...> (...)
  1792. string2: foo
  1793. Case 2: User is looking for all methods named "foo" in all template
  1794. class instantiations.
  1795. string1: Foo<...>::foo <...> (...)
  1796. string2: Foo::foo (...)
  1797. Case 3: User is looking for a specific overload of a template
  1798. function or method.
  1799. string1: foo<...>
  1800. string2: foo(...)
  1801. Case 4: User is looking for a specific overload of a specific
  1802. template instantiation.
  1803. string1: foo<A> (...)
  1804. string2: foo<B> (...)
  1805. Case 5: User is looking wild parameter match.
  1806. string1: foo<A<a<b<...> > > > (...)
  1807. string2: foo<A
  1808. */
  1809. if (language == language_cplus && ignore_template_params
  1810. && *string1 == '<' && *string2 != '<')
  1811. {
  1812. /* Skip any parameter list in STRING1. */
  1813. const char *template_start = string1;
  1814. if (skip_template_parameter_list (&string1))
  1815. {
  1816. /* Don't mark the parameter list ignored if the user didn't
  1817. try to ignore it. [Case #5 above] */
  1818. if (*string2 != '\0'
  1819. && match_for_lcd != NULL && template_start != string1)
  1820. match_for_lcd->mark_ignored_range (template_start, string1);
  1821. }
  1822. }
  1823. if (*string1 == '\0' || string2 == end_str2)
  1824. break;
  1825. /* Handle the :: operator. */
  1826. if (have_colon_op && string1[0] == ':' && string1[1] == ':')
  1827. {
  1828. if (*string2 != ':')
  1829. return 1;
  1830. string1++;
  1831. string2++;
  1832. if (string2 == end_str2)
  1833. break;
  1834. if (*string2 != ':')
  1835. return 1;
  1836. string1++;
  1837. string2++;
  1838. while (ISSPACE (*string1))
  1839. string1++;
  1840. while (string2 < end_str2 && ISSPACE (*string2))
  1841. string2++;
  1842. continue;
  1843. }
  1844. /* Handle C++ user-defined operators. */
  1845. else if (language == language_cplus
  1846. && *string1 == 'o')
  1847. {
  1848. if (cp_is_operator (string1, string1_start))
  1849. {
  1850. /* An operator name in STRING1. Check STRING2. */
  1851. size_t cmplen
  1852. = std::min<size_t> (CP_OPERATOR_LEN, end_str2 - string2);
  1853. if (strncmp (string1, string2, cmplen) != 0)
  1854. return 1;
  1855. string1 += cmplen;
  1856. string2 += cmplen;
  1857. if (string2 != end_str2)
  1858. {
  1859. /* Check for "operatorX" in STRING2. */
  1860. if (valid_identifier_name_char (*string2))
  1861. return 1;
  1862. skip_ws (string1, string2, end_str2);
  1863. }
  1864. /* Handle operator(). */
  1865. if (*string1 == '(')
  1866. {
  1867. if (string2 == end_str2)
  1868. {
  1869. if (mode == strncmp_iw_mode::NORMAL)
  1870. return 0;
  1871. else
  1872. {
  1873. /* Don't break for the regular return at the
  1874. bottom, because "operator" should not
  1875. match "operator()", since this open
  1876. parentheses is not the parameter list
  1877. start. */
  1878. return *string1 != '\0';
  1879. }
  1880. }
  1881. if (*string1 != *string2)
  1882. return 1;
  1883. string1++;
  1884. string2++;
  1885. }
  1886. while (1)
  1887. {
  1888. skip_ws (string1, string2, end_str2);
  1889. /* Skip to end of token, or to END, whatever comes
  1890. first. */
  1891. const char *end_str1 = string1 + strlen (string1);
  1892. const char *p1 = cp_skip_operator_token (string1, end_str1);
  1893. const char *p2 = cp_skip_operator_token (string2, end_str2);
  1894. cmplen = std::min (p1 - string1, p2 - string2);
  1895. if (p2 == end_str2)
  1896. {
  1897. if (strncmp (string1, string2, cmplen) != 0)
  1898. return 1;
  1899. }
  1900. else
  1901. {
  1902. if (p1 - string1 != p2 - string2)
  1903. return 1;
  1904. if (strncmp (string1, string2, cmplen) != 0)
  1905. return 1;
  1906. }
  1907. string1 += cmplen;
  1908. string2 += cmplen;
  1909. if (*string1 == '\0' || string2 == end_str2)
  1910. break;
  1911. if (*string1 == '(' || *string2 == '(')
  1912. break;
  1913. /* If STRING1 or STRING2 starts with a template
  1914. parameter list, break out of operator processing. */
  1915. skip_ws (string1, string2, end_str2);
  1916. if (*string1 == '<' || *string2 == '<')
  1917. break;
  1918. }
  1919. continue;
  1920. }
  1921. }
  1922. if (case_sensitivity == case_sensitive_on && *string1 != *string2)
  1923. break;
  1924. if (case_sensitivity == case_sensitive_off
  1925. && (TOLOWER ((unsigned char) *string1)
  1926. != TOLOWER ((unsigned char) *string2)))
  1927. break;
  1928. /* If we see any non-whitespace, non-identifier-name character
  1929. (any of "()<>*&" etc.), then skip spaces the next time
  1930. around. */
  1931. if (!ISSPACE (*string1) && !valid_identifier_name_char (*string1))
  1932. skip_spaces = true;
  1933. string1++;
  1934. string2++;
  1935. }
  1936. if (string2 == end_str2)
  1937. {
  1938. if (mode == strncmp_iw_mode::NORMAL)
  1939. {
  1940. /* Strip abi tag markers from the matched symbol name.
  1941. Usually the ABI marker will be found on function name
  1942. (automatically added because the function returns an
  1943. object marked with an ABI tag). However, it's also
  1944. possible to see a marker in one of the function
  1945. parameters, for example.
  1946. string2 (lookup name):
  1947. func
  1948. symbol name:
  1949. function(some_struct[abi:cxx11], int)
  1950. and for completion LCD computation we want to say that
  1951. the match was for:
  1952. function(some_struct, int)
  1953. */
  1954. if (match_for_lcd != NULL)
  1955. {
  1956. while ((string1 = strstr (string1, "[abi:")) != NULL)
  1957. {
  1958. const char *abi_start = string1;
  1959. /* There can be more than one tag. */
  1960. while (skip_abi_tag (&string1) && *string1 == '[')
  1961. ;
  1962. if (abi_start != string1)
  1963. match_for_lcd->mark_ignored_range (abi_start, string1);
  1964. }
  1965. }
  1966. return 0;
  1967. }
  1968. else
  1969. return (*string1 != '\0' && *string1 != '(');
  1970. }
  1971. else
  1972. return 1;
  1973. }
  1974. #if GDB_SELF_TEST
  1975. /* Unit tests for strncmp_iw_with_mode. */
  1976. #define CHECK_MATCH_LM(S1, S2, MODE, LANG, LCD) \
  1977. SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)), \
  1978. strncmp_iw_mode::MODE, \
  1979. (LANG), (LCD)) == 0)
  1980. #define CHECK_MATCH_LANG(S1, S2, MODE, LANG) \
  1981. CHECK_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
  1982. #define CHECK_MATCH(S1, S2, MODE) \
  1983. CHECK_MATCH_LANG ((S1), (S2), MODE, language_minimal)
  1984. #define CHECK_NO_MATCH_LM(S1, S2, MODE, LANG, LCD) \
  1985. SELF_CHECK (strncmp_iw_with_mode ((S1), (S2), strlen ((S2)), \
  1986. strncmp_iw_mode::MODE, \
  1987. (LANG)) != 0)
  1988. #define CHECK_NO_MATCH_LANG(S1, S2, MODE, LANG) \
  1989. CHECK_NO_MATCH_LM ((S1), (S2), MODE, (LANG), nullptr)
  1990. #define CHECK_NO_MATCH(S1, S2, MODE) \
  1991. CHECK_NO_MATCH_LANG ((S1), (S2), MODE, language_minimal)
  1992. static void
  1993. check_scope_operator (enum language lang)
  1994. {
  1995. CHECK_MATCH_LANG ("::", "::", NORMAL, lang);
  1996. CHECK_MATCH_LANG ("::foo", "::", NORMAL, lang);
  1997. CHECK_MATCH_LANG ("::foo", "::foo", NORMAL, lang);
  1998. CHECK_MATCH_LANG (" :: foo ", "::foo", NORMAL, lang);
  1999. CHECK_MATCH_LANG ("a::b", "a ::b", NORMAL, lang);
  2000. CHECK_MATCH_LANG ("a::b", "a\t::b", NORMAL, lang);
  2001. CHECK_MATCH_LANG ("a::b", "a \t::b", NORMAL, lang);
  2002. CHECK_MATCH_LANG ("a::b", "a\t ::b", NORMAL, lang);
  2003. CHECK_MATCH_LANG ("a::b", "a:: b", NORMAL, lang);
  2004. CHECK_MATCH_LANG ("a::b", "a::\tb", NORMAL, lang);
  2005. CHECK_MATCH_LANG ("a::b", "a:: \tb", NORMAL, lang);
  2006. CHECK_MATCH_LANG ("a::b", "a::\t b", NORMAL, lang);
  2007. CHECK_MATCH_LANG ("a::b", "a :: b", NORMAL, lang);
  2008. CHECK_MATCH_LANG ("a::b", "a ::\tb", NORMAL, lang);
  2009. CHECK_MATCH_LANG ("a::b", "a\t:: b", NORMAL, lang);
  2010. CHECK_MATCH_LANG ("a::b", "a \t::\t b", NORMAL, lang);
  2011. CHECK_MATCH_LANG ("a ::b", "a::b", NORMAL, lang);
  2012. CHECK_MATCH_LANG ("a\t::b", "a::b", NORMAL, lang);
  2013. CHECK_MATCH_LANG ("a \t::b", "a::b", NORMAL, lang);
  2014. CHECK_MATCH_LANG ("a\t ::b", "a::b", NORMAL, lang);
  2015. CHECK_MATCH_LANG ("a:: b", "a::b", NORMAL, lang);
  2016. CHECK_MATCH_LANG ("a::\tb", "a::b", NORMAL, lang);
  2017. CHECK_MATCH_LANG ("a:: \tb", "a::b", NORMAL, lang);
  2018. CHECK_MATCH_LANG ("a::\t b", "a::b", NORMAL, lang);
  2019. CHECK_MATCH_LANG ("a :: b", "a::b", NORMAL, lang);
  2020. CHECK_MATCH_LANG ("a ::\tb", "a::b", NORMAL, lang);
  2021. CHECK_MATCH_LANG ("a\t:: b", "a::b", NORMAL, lang);
  2022. CHECK_MATCH_LANG ("a \t::\t b", "a::b", NORMAL, lang);
  2023. CHECK_MATCH_LANG ("a::b::c", "a::b::c", NORMAL, lang);
  2024. CHECK_MATCH_LANG (" a:: b:: c", "a::b::c", NORMAL, lang);
  2025. CHECK_MATCH_LANG ("a::b::c", " a:: b:: c", NORMAL, lang);
  2026. CHECK_MATCH_LANG ("a ::b ::c", "a::b::c", NORMAL, lang);
  2027. CHECK_MATCH_LANG ("a::b::c", "a :: b:: c", NORMAL, lang);
  2028. CHECK_MATCH_LANG ("\ta::\tb::\tc", "\ta::\tb::\tc", NORMAL, lang);
  2029. CHECK_MATCH_LANG ("a\t::b\t::c\t", "a\t::b\t::c\t", NORMAL, lang);
  2030. CHECK_MATCH_LANG (" \ta:: \tb:: \tc", " \ta:: \tb:: \tc", NORMAL, lang);
  2031. CHECK_MATCH_LANG ("\t a::\t b::\t c", "\t a::\t b::\t c", NORMAL, lang);
  2032. CHECK_MATCH_LANG ("a::b::c", "\ta::\tb::\tc", NORMAL, lang);
  2033. CHECK_MATCH_LANG ("a::b::c", "a\t::b\t::c\t", NORMAL, lang);
  2034. CHECK_MATCH_LANG ("a::b::c", " \ta:: \tb:: \tc", NORMAL, lang);
  2035. CHECK_MATCH_LANG ("a::b::c", "\t a::\t b::\t c", NORMAL, lang);
  2036. CHECK_MATCH_LANG ("\ta::\tb::\tc", "a::b::c", NORMAL, lang);
  2037. CHECK_MATCH_LANG ("a\t::b\t::c\t", "a::b::c", NORMAL, lang);
  2038. CHECK_MATCH_LANG (" \ta:: \tb:: \tc", "a::b::c", NORMAL, lang);
  2039. CHECK_MATCH_LANG ("\t a::\t b::\t c", "a::b::c", NORMAL, lang);
  2040. CHECK_MATCH_LANG ("a :: b:: c\t", "\ta :: b\t:: c\t\t", NORMAL, lang);
  2041. CHECK_MATCH_LANG (" a::\t \t b:: c\t", "\ta ::b:: c\t\t",
  2042. NORMAL, lang);
  2043. CHECK_MATCH_LANG ("a :: b :: \t\t\tc\t",
  2044. "\t\t\t\ta :: \t\t\t b \t\t::c",
  2045. NORMAL, lang);
  2046. CHECK_MATCH_LANG ("a::b()", "a", NORMAL, lang);
  2047. CHECK_MATCH_LANG ("a::b()", "a::", NORMAL, lang);
  2048. CHECK_MATCH_LANG ("a::b()", "a::b", NORMAL, lang);
  2049. CHECK_MATCH_LANG ("a::b(a)", "a", NORMAL, lang);
  2050. CHECK_MATCH_LANG ("a::b(a)", "a::", NORMAL, lang);
  2051. CHECK_MATCH_LANG ("a::b(a)", "a::b", NORMAL, lang);
  2052. CHECK_MATCH_LANG ("a::b(a,b)", "a", NORMAL, lang);
  2053. CHECK_MATCH_LANG ("a::b(a,b)", "a::", NORMAL, lang);
  2054. CHECK_MATCH_LANG ("a::b(a,b)", "a::b", NORMAL, lang);
  2055. CHECK_MATCH_LANG ("a::b(a,b,c)", "a", NORMAL, lang);
  2056. CHECK_MATCH_LANG ("a::b(a,b,c)", "a::", NORMAL, lang);
  2057. CHECK_MATCH_LANG ("a::b(a,b,c)", "a::b", NORMAL, lang);
  2058. CHECK_NO_MATCH_LANG ("a::", "::a", NORMAL, lang);
  2059. CHECK_NO_MATCH_LANG ("::a", "::a()", NORMAL, lang);
  2060. CHECK_NO_MATCH_LANG ("::", "::a", NORMAL, lang);
  2061. CHECK_NO_MATCH_LANG ("a:::b", "a::b", NORMAL, lang);
  2062. CHECK_NO_MATCH_LANG ("a::b()", "a::b(a)", NORMAL, lang);
  2063. CHECK_NO_MATCH_LANG ("a::b(a)", "a::b()", NORMAL, lang);
  2064. CHECK_NO_MATCH_LANG ("a::b(a,b)", "a::b(a,a)", NORMAL, lang);
  2065. CHECK_NO_MATCH_LANG ("a::b", "a()", NORMAL, lang);
  2066. CHECK_NO_MATCH_LANG ("a::b", "a::()", NORMAL, lang);
  2067. CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
  2068. CHECK_NO_MATCH_LANG ("a::b", "a(a)", NORMAL, lang);
  2069. CHECK_NO_MATCH_LANG ("a::b", "a::(a)", NORMAL, lang);
  2070. CHECK_NO_MATCH_LANG ("a::b", "a::b()", NORMAL, lang);
  2071. CHECK_NO_MATCH_LANG ("a::b", "a(a,b)", NORMAL, lang);
  2072. CHECK_NO_MATCH_LANG ("a::b", "a::(a,b)", NORMAL, lang);
  2073. CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b)", NORMAL, lang);
  2074. CHECK_NO_MATCH_LANG ("a::b", "a(a,b,c)", NORMAL, lang);
  2075. CHECK_NO_MATCH_LANG ("a::b", "a::(a,b,c)", NORMAL, lang);
  2076. CHECK_NO_MATCH_LANG ("a::b", "a::b(a,b,c)", NORMAL, lang);
  2077. }
  2078. /* Callback for strncmp_iw_with_mode unit tests. */
  2079. static void
  2080. strncmp_iw_with_mode_tests ()
  2081. {
  2082. /* Some of the following tests are nonsensical, but could be input by a
  2083. deranged script (or user). */
  2084. /* strncmp_iw_mode::NORMAL: strcmp()-like but ignore any whitespace... */
  2085. CHECK_MATCH ("", "", NORMAL);
  2086. CHECK_MATCH ("foo", "foo", NORMAL);
  2087. CHECK_MATCH (" foo", "foo", NORMAL);
  2088. CHECK_MATCH ("foo ", "foo", NORMAL);
  2089. CHECK_MATCH (" foo ", "foo", NORMAL);
  2090. CHECK_MATCH (" foo", "foo", NORMAL);
  2091. CHECK_MATCH ("foo ", "foo", NORMAL);
  2092. CHECK_MATCH (" foo ", "foo", NORMAL);
  2093. CHECK_MATCH ("\tfoo", "foo", NORMAL);
  2094. CHECK_MATCH ("foo\t", "foo", NORMAL);
  2095. CHECK_MATCH ("\tfoo\t", "foo", NORMAL);
  2096. CHECK_MATCH (" \tfoo \t", "foo", NORMAL);
  2097. CHECK_MATCH ("\t foo\t ", "foo", NORMAL);
  2098. CHECK_MATCH ("\t \t \t\t\t\t foo\t\t\t \t\t \t \t \t \t ",
  2099. "foo", NORMAL);
  2100. CHECK_MATCH ("foo",
  2101. "\t \t \t\t\t\t foo\t\t\t \t\t \t \t \t \t ",
  2102. NORMAL);
  2103. CHECK_MATCH ("foo bar", "foo", NORMAL);
  2104. CHECK_NO_MATCH ("foo", "bar", NORMAL);
  2105. CHECK_NO_MATCH ("foo bar", "foobar", NORMAL);
  2106. CHECK_NO_MATCH (" foo ", "bar", NORMAL);
  2107. CHECK_NO_MATCH ("foo", " bar ", NORMAL);
  2108. CHECK_NO_MATCH (" \t\t foo\t\t ", "\t \t \tbar\t", NORMAL);
  2109. CHECK_NO_MATCH ("@!%&", "@!%&foo", NORMAL);
  2110. /* ... and function parameters in STRING1. */
  2111. CHECK_MATCH ("foo()", "foo()", NORMAL);
  2112. CHECK_MATCH ("foo ()", "foo()", NORMAL);
  2113. CHECK_MATCH ("foo ()", "foo()", NORMAL);
  2114. CHECK_MATCH ("foo\t()", "foo()", NORMAL);
  2115. CHECK_MATCH ("foo\t ()", "foo()", NORMAL);
  2116. CHECK_MATCH ("foo \t()", "foo()", NORMAL);
  2117. CHECK_MATCH ("foo()", "foo ()", NORMAL);
  2118. CHECK_MATCH ("foo()", "foo ()", NORMAL);
  2119. CHECK_MATCH ("foo()", "foo\t()", NORMAL);
  2120. CHECK_MATCH ("foo()", "foo\t ()", NORMAL);
  2121. CHECK_MATCH ("foo()", "foo \t()", NORMAL);
  2122. CHECK_MATCH ("foo()", "foo()", NORMAL);
  2123. CHECK_MATCH ("foo ()", "foo ()", NORMAL);
  2124. CHECK_MATCH ("foo ()", "foo ()", NORMAL);
  2125. CHECK_MATCH ("foo\t()", "foo\t()", NORMAL);
  2126. CHECK_MATCH ("foo\t ()", "foo\t ()", NORMAL);
  2127. CHECK_MATCH ("foo \t()", "foo \t()", NORMAL);
  2128. CHECK_MATCH ("foo(a)", "foo(a)", NORMAL);
  2129. CHECK_MATCH ("foo( a)", "foo(a)", NORMAL);
  2130. CHECK_MATCH ("foo(a )", "foo(a)", NORMAL);
  2131. CHECK_MATCH ("foo(\ta)", "foo(a)", NORMAL);
  2132. CHECK_MATCH ("foo(a\t)", "foo(a)", NORMAL);
  2133. CHECK_MATCH ("foo(\t a)", "foo(a)", NORMAL);
  2134. CHECK_MATCH ("foo( \ta)", "foo(a)", NORMAL);
  2135. CHECK_MATCH ("foo(a\t )", "foo(a)", NORMAL);
  2136. CHECK_MATCH ("foo(a \t)", "foo(a)", NORMAL);
  2137. CHECK_MATCH ("foo( a )", "foo(a)", NORMAL);
  2138. CHECK_MATCH ("foo(\ta\t)", "foo(a)", NORMAL);
  2139. CHECK_MATCH ("foo(\t a\t )", "foo(a)", NORMAL);
  2140. CHECK_MATCH ("foo( \ta \t)", "foo(a)", NORMAL);
  2141. CHECK_MATCH ("foo(a)", "foo( a)", NORMAL);
  2142. CHECK_MATCH ("foo(a)", "foo(a )", NORMAL);
  2143. CHECK_MATCH ("foo(a)", "foo(\ta)", NORMAL);
  2144. CHECK_MATCH ("foo(a)", "foo(a\t)", NORMAL);
  2145. CHECK_MATCH ("foo(a)", "foo(\t a)", NORMAL);
  2146. CHECK_MATCH ("foo(a)", "foo( \ta)", NORMAL);
  2147. CHECK_MATCH ("foo(a)", "foo(a\t )", NORMAL);
  2148. CHECK_MATCH ("foo(a)", "foo(a \t)", NORMAL);
  2149. CHECK_MATCH ("foo(a)", "foo( a )", NORMAL);
  2150. CHECK_MATCH ("foo(a)", "foo(\ta\t)", NORMAL);
  2151. CHECK_MATCH ("foo(a)", "foo(\t a\t )", NORMAL);
  2152. CHECK_MATCH ("foo(a)", "foo( \ta \t)", NORMAL);
  2153. CHECK_MATCH ("foo(a,b)", "foo(a,b)", NORMAL);
  2154. CHECK_MATCH ("foo(a ,b)", "foo(a,b)", NORMAL);
  2155. CHECK_MATCH ("foo(a\t,b)", "foo(a,b)", NORMAL);
  2156. CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
  2157. CHECK_MATCH ("foo(a\t,\tb)", "foo(a,b)", NORMAL);
  2158. CHECK_MATCH ("foo(a \t,b)", "foo(a,b)", NORMAL);
  2159. CHECK_MATCH ("foo(a\t ,b)", "foo(a,b)", NORMAL);
  2160. CHECK_MATCH ("foo(a,\tb)", "foo(a,b)", NORMAL);
  2161. CHECK_MATCH ("foo(a, \tb)", "foo(a,b)", NORMAL);
  2162. CHECK_MATCH ("foo(a,\t b)", "foo(a,b)", NORMAL);
  2163. CHECK_MATCH ("foo(a,b)", "foo(a ,b)", NORMAL);
  2164. CHECK_MATCH ("foo(a,b)", "foo(a\t,b)", NORMAL);
  2165. CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
  2166. CHECK_MATCH ("foo(a,b)", "foo(a\t,\tb)", NORMAL);
  2167. CHECK_MATCH ("foo(a,b)", "foo(a \t,b)", NORMAL);
  2168. CHECK_MATCH ("foo(a,b)", "foo(a\t ,b)", NORMAL);
  2169. CHECK_MATCH ("foo(a,b)", "foo(a,\tb)", NORMAL);
  2170. CHECK_MATCH ("foo(a,b)", "foo(a, \tb)", NORMAL);
  2171. CHECK_MATCH ("foo(a,b)", "foo(a,\t b)", NORMAL);
  2172. CHECK_MATCH ("foo(a,b,c,d)", "foo(a,b,c,d)", NORMAL);
  2173. CHECK_MATCH (" foo ( a , b , c , d ) ", "foo(a,b,c,d)", NORMAL);
  2174. CHECK_MATCH (" foo ( a , b , c , d ) ", "foo( a , b , c , d )", NORMAL);
  2175. CHECK_MATCH ("foo &\t*(\ta b *\t\t&)", "foo", NORMAL);
  2176. CHECK_MATCH ("foo &\t*(\ta b *\t\t&)", "foo&*(a b * &)", NORMAL);
  2177. CHECK_MATCH ("foo(a) b", "foo(a)", NORMAL);
  2178. CHECK_MATCH ("*foo(*a&)", "*foo", NORMAL);
  2179. CHECK_MATCH ("*foo(*a&)", "*foo(*a&)", NORMAL);
  2180. CHECK_MATCH ("*a&b#c/^d$foo(*a&)", "*a&b#c/^d$foo", NORMAL);
  2181. CHECK_MATCH ("* foo", "*foo", NORMAL);
  2182. CHECK_MATCH ("foo&", "foo", NORMAL);
  2183. CHECK_MATCH ("foo*", "foo", NORMAL);
  2184. CHECK_MATCH ("foo.", "foo", NORMAL);
  2185. CHECK_MATCH ("foo->", "foo", NORMAL);
  2186. CHECK_NO_MATCH ("foo", "foo(", NORMAL);
  2187. CHECK_NO_MATCH ("foo", "foo()", NORMAL);
  2188. CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
  2189. CHECK_NO_MATCH ("foo", "foo(a)", NORMAL);
  2190. CHECK_NO_MATCH ("foo", "foo*", NORMAL);
  2191. CHECK_NO_MATCH ("foo", "foo (*", NORMAL);
  2192. CHECK_NO_MATCH ("foo*", "foo (*", NORMAL);
  2193. CHECK_NO_MATCH ("foo *", "foo (*", NORMAL);
  2194. CHECK_NO_MATCH ("foo&", "foo (*", NORMAL);
  2195. CHECK_NO_MATCH ("foo &", "foo (*", NORMAL);
  2196. CHECK_NO_MATCH ("foo &*", "foo (&)", NORMAL);
  2197. CHECK_NO_MATCH ("foo & \t *\t", "foo (*", NORMAL);
  2198. CHECK_NO_MATCH ("foo & \t *\t", "foo (*", NORMAL);
  2199. CHECK_NO_MATCH ("foo(a*) b", "foo(a) b", NORMAL);
  2200. CHECK_NO_MATCH ("foo[aqi:A](a)", "foo(b)", NORMAL);
  2201. CHECK_NO_MATCH ("*foo", "foo", NORMAL);
  2202. CHECK_NO_MATCH ("*foo", "foo*", NORMAL);
  2203. CHECK_NO_MATCH ("*foo*", "*foo&", NORMAL);
  2204. CHECK_NO_MATCH ("*foo*", "foo *", NORMAL);
  2205. CHECK_NO_MATCH ("&foo", "foo", NORMAL);
  2206. CHECK_NO_MATCH ("&foo", "foo&", NORMAL);
  2207. CHECK_NO_MATCH ("foo&", "&foo", NORMAL);
  2208. CHECK_NO_MATCH ("foo", "foo&", NORMAL);
  2209. CHECK_NO_MATCH ("foo", "foo*", NORMAL);
  2210. CHECK_NO_MATCH ("foo", "foo.", NORMAL);
  2211. CHECK_NO_MATCH ("foo", "foo->", NORMAL);
  2212. CHECK_NO_MATCH ("foo bar", "foo()", NORMAL);
  2213. CHECK_NO_MATCH ("foo bar", "foo bar()", NORMAL);
  2214. CHECK_NO_MATCH ("foo()", "foo(a)", NORMAL);
  2215. CHECK_NO_MATCH ("*(*)&", "*(*)*", NORMAL);
  2216. CHECK_NO_MATCH ("foo(a)", "foo()", NORMAL);
  2217. CHECK_NO_MATCH ("foo(a)", "foo(b)", NORMAL);
  2218. CHECK_NO_MATCH ("foo(a,b)", "foo(a,b,c)", NORMAL);
  2219. CHECK_NO_MATCH ("foo(a\\b)", "foo()", NORMAL);
  2220. CHECK_NO_MATCH ("foo bar(a b c d)", "foobar", NORMAL);
  2221. CHECK_NO_MATCH ("foo bar(a b c d)", "foobar ( a b c \td\t)\t", NORMAL);
  2222. /* Test scope operator. */
  2223. check_scope_operator (language_minimal);
  2224. check_scope_operator (language_cplus);
  2225. check_scope_operator (language_fortran);
  2226. check_scope_operator (language_rust);
  2227. /* Test C++ user-defined operators. */
  2228. CHECK_MATCH_LANG ("operator foo(int&)", "operator foo(int &)", NORMAL,
  2229. language_cplus);
  2230. CHECK_MATCH_LANG ("operator foo(int &)", "operator foo(int &)", NORMAL,
  2231. language_cplus);
  2232. CHECK_MATCH_LANG ("operator foo(int\t&)", "operator foo(int\t&)", NORMAL,
  2233. language_cplus);
  2234. CHECK_MATCH_LANG ("operator foo (int)", "operator foo(int)", NORMAL,
  2235. language_cplus);
  2236. CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo(int)", NORMAL,
  2237. language_cplus);
  2238. CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo(int)", NORMAL,
  2239. language_cplus);
  2240. CHECK_MATCH_LANG ("operator foo (int)", "operator foo \t(int)", NORMAL,
  2241. language_cplus);
  2242. CHECK_MATCH_LANG ("operator foo\t(int)", "operator foo \t(int)", NORMAL,
  2243. language_cplus);
  2244. CHECK_MATCH_LANG ("operator foo \t(int)", "operator foo \t(int)", NORMAL,
  2245. language_cplus);
  2246. CHECK_MATCH_LANG ("a::operator foo(int&)", "a::operator foo(int &)", NORMAL,
  2247. language_cplus);
  2248. CHECK_MATCH_LANG ("a :: operator foo(int &)", "a::operator foo(int &)", NORMAL,
  2249. language_cplus);
  2250. CHECK_MATCH_LANG ("a \t:: \toperator foo(int\t&)", "a::operator foo(int\t&)", NORMAL,
  2251. language_cplus);
  2252. CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo(int)", NORMAL,
  2253. language_cplus);
  2254. CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo(int)", NORMAL,
  2255. language_cplus);
  2256. CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo(int)", NORMAL,
  2257. language_cplus);
  2258. CHECK_MATCH_LANG ("a::operator foo (int)", "a::operator foo \t(int)", NORMAL,
  2259. language_cplus);
  2260. CHECK_MATCH_LANG ("a::operator foo\t(int)", "a::operator foo \t(int)", NORMAL,
  2261. language_cplus);
  2262. CHECK_MATCH_LANG ("a::operator foo \t(int)", "a::operator foo \t(int)", NORMAL,
  2263. language_cplus);
  2264. CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(char)", NORMAL,
  2265. language_cplus);
  2266. CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int *)", NORMAL,
  2267. language_cplus);
  2268. CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int &)", NORMAL,
  2269. language_cplus);
  2270. CHECK_NO_MATCH_LANG ("operator foo(int)", "operator foo(int, char *)", NORMAL,
  2271. language_cplus);
  2272. CHECK_NO_MATCH_LANG ("operator foo(int)", "operator bar(int)", NORMAL,
  2273. language_cplus);
  2274. CHECK_NO_MATCH_LANG ("a::operator b::foo(int)", "a::operator a::foo(char)", NORMAL,
  2275. language_cplus);
  2276. CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int *)", NORMAL,
  2277. language_cplus);
  2278. CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int &)", NORMAL,
  2279. language_cplus);
  2280. CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator foo(int, char *)", NORMAL,
  2281. language_cplus);
  2282. CHECK_NO_MATCH_LANG ("a::operator foo(int)", "a::operator bar(int)", NORMAL,
  2283. language_cplus);
  2284. /* Skip "[abi:cxx11]" tags in the symbol name if the lookup name
  2285. doesn't include them. These are not language-specific in
  2286. strncmp_iw_with_mode. */
  2287. CHECK_MATCH ("foo[abi:a]", "foo", NORMAL);
  2288. CHECK_MATCH ("foo[abi:a]()", "foo", NORMAL);
  2289. CHECK_MATCH ("foo[abi:a](a)", "foo", NORMAL);
  2290. CHECK_MATCH ("foo[abi:a](a&,b*)", "foo", NORMAL);
  2291. CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
  2292. CHECK_MATCH ("foo[abi:a](a,b) c", "foo(a,b) c", NORMAL);
  2293. CHECK_MATCH ("foo[abi:a](a)", "foo(a)", NORMAL);
  2294. CHECK_MATCH ("foo[abi:a](a,b)", "foo(a,b)", NORMAL);
  2295. CHECK_MATCH ("foo[abi:a]", "foo[abi:a]", NORMAL);
  2296. CHECK_MATCH ("foo[ abi:a]", "foo[abi:a]", NORMAL);
  2297. CHECK_MATCH ("foo[\tabi:a]", "foo[abi:a]", NORMAL);
  2298. CHECK_MATCH ("foo[ \tabi:a]", "foo[abi:a]", NORMAL);
  2299. CHECK_MATCH ("foo[\t abi:a]", "foo[abi:a]", NORMAL);
  2300. CHECK_MATCH ("foo[abi :a]", "foo[abi:a]", NORMAL);
  2301. CHECK_MATCH ("foo[abi\t:a]", "foo[abi:a]", NORMAL);
  2302. CHECK_MATCH ("foo[abi \t:a]", "foo[abi:a]", NORMAL);
  2303. CHECK_MATCH ("foo[abi\t :a]", "foo[abi:a]", NORMAL);
  2304. CHECK_MATCH ("foo[abi:a]", "foo[ abi:a]", NORMAL);
  2305. CHECK_MATCH ("foo[abi:a]", "foo[\tabi:a]", NORMAL);
  2306. CHECK_MATCH ("foo[abi:a]", "foo[ \tabi:a]", NORMAL);
  2307. CHECK_MATCH ("foo[abi:a]", "foo[\t abi:a]", NORMAL);
  2308. CHECK_MATCH ("foo[abi:a]", "foo[abi :a]", NORMAL);
  2309. CHECK_MATCH ("foo[abi:a]", "foo[abi\t:a]", NORMAL);
  2310. CHECK_MATCH ("foo[abi:a]", "foo[abi \t:a]", NORMAL);
  2311. CHECK_MATCH ("foo[abi:a]", "foo[abi\t :a]", NORMAL);
  2312. CHECK_MATCH ("foo[abi:a]", "foo[abi:a ]", NORMAL);
  2313. CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t]", NORMAL);
  2314. CHECK_MATCH ("foo[abi:a]", "foo[abi:a \t]", NORMAL);
  2315. CHECK_MATCH ("foo[abi:a]", "foo[abi:a\t ]", NORMAL);
  2316. CHECK_MATCH ("foo[abi:a,b]", "foo[abi:a,b]", NORMAL);
  2317. CHECK_MATCH ("foo[abi:::]", "foo[abi:::]", NORMAL);
  2318. CHECK_MATCH ("foo[abi : : : ]", "foo[abi:::]", NORMAL);
  2319. CHECK_MATCH ("foo[abi:::]", "foo[abi : : : ]", NORMAL);
  2320. CHECK_MATCH ("foo[ \t abi \t:\t: : \t]",
  2321. "foo[ abi : \t ::]",
  2322. NORMAL);
  2323. CHECK_MATCH ("foo< bar< baz< quxi > > >(int)", "foo<bar<baz<quxi>>>(int)",
  2324. NORMAL);
  2325. CHECK_MATCH ("\tfoo<\tbar<\tbaz\t<\tquxi\t>\t>\t>(int)",
  2326. "foo<bar<baz<quxi>>>(int)", NORMAL);
  2327. CHECK_MATCH (" \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)",
  2328. "foo<bar<baz<quxi>>>(int)", NORMAL);
  2329. CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
  2330. "foo < bar < baz < quxi > > > (int)", NORMAL);
  2331. CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
  2332. "\tfoo\t<\tbar\t<\tbaz\t<\tquxi\t>\t>\t>\t(int)", NORMAL);
  2333. CHECK_MATCH ("foo<bar<baz<quxi>>>(int)",
  2334. " \tfoo \t< \tbar \t< \tbaz \t< \tquxi \t> \t> \t> \t( \tint \t)", NORMAL);
  2335. CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "fo", NORMAL);
  2336. CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo", NORMAL);
  2337. CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz>>::", NORMAL);
  2338. CHECK_MATCH ("foo<bar<baz>>::foo(quxi &)", "foo<bar<baz> >::foo", NORMAL);
  2339. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
  2340. NORMAL);
  2341. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", NORMAL);
  2342. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar)", NORMAL);
  2343. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar)", NORMAL);
  2344. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo(bar[abi:c])", NORMAL);
  2345. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a](bar[abi:c])", NORMAL);
  2346. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar)", NORMAL);
  2347. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c])",
  2348. NORMAL);
  2349. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo", NORMAL);
  2350. CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo()", NORMAL);
  2351. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>", NORMAL);
  2352. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz)", NORMAL);
  2353. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:b])",
  2354. NORMAL);
  2355. CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar>(char*, baz[abi:A])",
  2356. NORMAL);
  2357. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz)",
  2358. NORMAL);
  2359. CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:A]>(char*, baz)",
  2360. NORMAL);
  2361. CHECK_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])", "foo<bar[abi:a]>(char*, baz[abi:b])",
  2362. NORMAL);
  2363. CHECK_NO_MATCH("foo<bar[abi:a]>(char *, baz[abi:b])",
  2364. "foo<bar[abi:a]>(char*, baz[abi:B])", NORMAL);
  2365. CHECK_NO_MATCH ("foo", "foo[", NORMAL);
  2366. CHECK_NO_MATCH ("foo", "foo[]", NORMAL);
  2367. CHECK_NO_MATCH ("foo", "foo[ a]", NORMAL);
  2368. CHECK_NO_MATCH ("foo", "foo[a ]", NORMAL);
  2369. CHECK_NO_MATCH ("foo", "foo[ a ]", NORMAL);
  2370. CHECK_NO_MATCH ("foo", "foo[\ta]", NORMAL);
  2371. CHECK_NO_MATCH ("foo", "foo[a \t]", NORMAL);
  2372. CHECK_NO_MATCH ("foo", "foo[a\t ]", NORMAL);
  2373. CHECK_NO_MATCH ("foo", "foo[ \ta]", NORMAL);
  2374. CHECK_NO_MATCH ("foo", "foo[\t a]", NORMAL);
  2375. CHECK_NO_MATCH ("foo", "foo[ \ta \t]", NORMAL);
  2376. CHECK_NO_MATCH ("foo", "foo[\t a\t ]", NORMAL);
  2377. CHECK_NO_MATCH ("foo", "foo[abi]", NORMAL);
  2378. CHECK_NO_MATCH ("foo", "foo[ abi]", NORMAL);
  2379. CHECK_NO_MATCH ("foo", "foo[abi ]", NORMAL);
  2380. CHECK_NO_MATCH ("foo", "foo[\tabi]", NORMAL);
  2381. CHECK_NO_MATCH ("foo", "foo[abi\t]", NORMAL);
  2382. CHECK_NO_MATCH ("foo", "foo[ \tabi]", NORMAL);
  2383. CHECK_NO_MATCH ("foo", "foo[\t abi]", NORMAL);
  2384. CHECK_NO_MATCH ("foo", "foo[abi \t]", NORMAL);
  2385. CHECK_NO_MATCH ("foo", "foo[abi\t ]", NORMAL);
  2386. CHECK_NO_MATCH ("foo", "foo[abi :]", NORMAL);
  2387. CHECK_NO_MATCH ("foo", "foo[abi\t:]", NORMAL);
  2388. CHECK_NO_MATCH ("foo", "foo[abi \t:]", NORMAL);
  2389. CHECK_NO_MATCH ("foo", "foo[abi\t :]", NORMAL);
  2390. CHECK_NO_MATCH ("foo", "foo[abi: ]", NORMAL);
  2391. CHECK_NO_MATCH ("foo", "foo[abi:\t]", NORMAL);
  2392. CHECK_NO_MATCH ("foo", "foo[abi: \t]", NORMAL);
  2393. CHECK_NO_MATCH ("foo", "foo[abi:\t ]", NORMAL);
  2394. CHECK_NO_MATCH ("foo", "foo[abi: a]", NORMAL);
  2395. CHECK_NO_MATCH ("foo", "foo[abi:\ta]", NORMAL);
  2396. CHECK_NO_MATCH ("foo", "foo[abi: \ta]", NORMAL);
  2397. CHECK_NO_MATCH ("foo", "foo[abi:\t a]", NORMAL);
  2398. CHECK_NO_MATCH ("foo", "foo[abi:a ]", NORMAL);
  2399. CHECK_NO_MATCH ("foo", "foo[abi:a\t]", NORMAL);
  2400. CHECK_NO_MATCH ("foo", "foo[abi:a \t]", NORMAL);
  2401. CHECK_NO_MATCH ("foo", "foo[abi:a\t ]", NORMAL);
  2402. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
  2403. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
  2404. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
  2405. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a)", NORMAL);
  2406. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) c", NORMAL);
  2407. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) .", NORMAL);
  2408. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) *", NORMAL);
  2409. CHECK_NO_MATCH ("foo[abi:a]()", "foo(a) &", NORMAL);
  2410. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) c", NORMAL);
  2411. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) .", NORMAL);
  2412. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) *", NORMAL);
  2413. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b) &", NORMAL);
  2414. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)c", NORMAL);
  2415. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b).", NORMAL);
  2416. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)*", NORMAL);
  2417. CHECK_NO_MATCH ("foo[abi:a](a,b)", "foo(a,b)&", NORMAL);
  2418. CHECK_NO_MATCH ("foo[abi:a](a,b) d", "foo(a,b) c", NORMAL);
  2419. CHECK_NO_MATCH ("foo[abi:a](a)", "foo()", NORMAL);
  2420. CHECK_NO_MATCH ("foo[abi:a](a)", "foo(b)", NORMAL);
  2421. CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:b](a)", NORMAL);
  2422. CHECK_NO_MATCH ("foo[abi:a](a)", "foo[abi:a](b)", NORMAL);
  2423. CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a]", NORMAL);
  2424. CHECK_NO_MATCH ("foo[abi:", "foo[abi:a]", NORMAL);
  2425. CHECK_NO_MATCH ("foo[abi:]", "foo[abi:a", NORMAL);
  2426. CHECK_NO_MATCH ("foo[abi:,]", "foo[abi:a]", NORMAL);
  2427. CHECK_NO_MATCH ("foo[abi:a,b]", "foo[abi:a]", NORMAL);
  2428. CHECK_NO_MATCH ("foo[abi::a]", "foo[abi:a]", NORMAL);
  2429. CHECK_NO_MATCH ("foo[abi:,([a]", "foo[abi:a]", NORMAL);
  2430. CHECK_MATCH ("foo <a, b [, c (", "foo", NORMAL);
  2431. CHECK_MATCH ("foo >a, b ], c )", "foo", NORMAL);
  2432. CHECK_MATCH ("@!%&\\*", "@!%&\\*", NORMAL);
  2433. CHECK_MATCH ("()", "()", NORMAL);
  2434. CHECK_MATCH ("*(*)*", "*(*)*", NORMAL);
  2435. CHECK_MATCH ("[]", "[]", NORMAL);
  2436. CHECK_MATCH ("<>", "<>", NORMAL);
  2437. /* strncmp_iw_with_mode::MATCH_PARAMS: the "strcmp_iw hack." */
  2438. CHECK_MATCH ("foo2", "foo", NORMAL);
  2439. CHECK_NO_MATCH ("foo2", "foo", MATCH_PARAMS);
  2440. CHECK_NO_MATCH ("foo2", "foo ", MATCH_PARAMS);
  2441. CHECK_NO_MATCH ("foo2", "foo\t", MATCH_PARAMS);
  2442. CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
  2443. CHECK_NO_MATCH ("foo2", "foo\t ", MATCH_PARAMS);
  2444. CHECK_NO_MATCH ("foo2", "foo \t", MATCH_PARAMS);
  2445. CHECK_NO_MATCH ("foo2", " foo", MATCH_PARAMS);
  2446. CHECK_NO_MATCH ("foo2", "\tfoo", MATCH_PARAMS);
  2447. CHECK_NO_MATCH ("foo2", " \tfoo", MATCH_PARAMS);
  2448. CHECK_NO_MATCH ("foo2", "\t foo", MATCH_PARAMS);
  2449. CHECK_NO_MATCH (" foo2", "foo", MATCH_PARAMS);
  2450. CHECK_NO_MATCH ("\tfoo2", "foo", MATCH_PARAMS);
  2451. CHECK_NO_MATCH (" \tfoo2", "foo", MATCH_PARAMS);
  2452. CHECK_NO_MATCH ("\t foo2", "foo", MATCH_PARAMS);
  2453. CHECK_NO_MATCH (" foo2 ", " foo ", MATCH_PARAMS);
  2454. CHECK_NO_MATCH ("\tfoo2\t", "\tfoo\t", MATCH_PARAMS);
  2455. CHECK_NO_MATCH (" \tfoo2 \t", " \tfoo \t", MATCH_PARAMS);
  2456. CHECK_NO_MATCH ("\t foo2\t ", "\t foo\t ", MATCH_PARAMS);
  2457. CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
  2458. CHECK_NO_MATCH ("foo2\t", "foo", MATCH_PARAMS);
  2459. CHECK_NO_MATCH ("foo2 ", "foo", MATCH_PARAMS);
  2460. CHECK_NO_MATCH ("foo2 \t", "foo", MATCH_PARAMS);
  2461. CHECK_NO_MATCH ("foo2\t ", "foo", MATCH_PARAMS);
  2462. CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
  2463. CHECK_NO_MATCH ("foo2 (args)", "foo", MATCH_PARAMS);
  2464. CHECK_NO_MATCH ("foo2\t(args)", "foo", MATCH_PARAMS);
  2465. CHECK_NO_MATCH ("foo2 \t(args)", "foo", MATCH_PARAMS);
  2466. CHECK_NO_MATCH ("foo2\t (args)", "foo", MATCH_PARAMS);
  2467. CHECK_NO_MATCH ("foo2 ( args)", "foo", MATCH_PARAMS);
  2468. CHECK_NO_MATCH ("foo2(args )", "foo", MATCH_PARAMS);
  2469. CHECK_NO_MATCH ("foo2(args\t)", "foo", MATCH_PARAMS);
  2470. CHECK_NO_MATCH ("foo2 (args \t)", "foo", MATCH_PARAMS);
  2471. CHECK_NO_MATCH ("foo2 (args\t )", "foo", MATCH_PARAMS);
  2472. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo[abi:a][abi:b](bar[abi:c][abi:d])",
  2473. MATCH_PARAMS);
  2474. CHECK_MATCH ("foo[abi:a][abi:b](bar[abi:c][abi:d])", "foo", MATCH_PARAMS);
  2475. /* strncmp_iw_with_mode also supports case insensitivity. */
  2476. {
  2477. CHECK_NO_MATCH ("FoO", "foo", NORMAL);
  2478. CHECK_NO_MATCH ("FoO", "foo", MATCH_PARAMS);
  2479. scoped_restore restore_case = make_scoped_restore (&case_sensitivity);
  2480. case_sensitivity = case_sensitive_off;
  2481. CHECK_MATCH ("FoO", "foo", NORMAL);
  2482. CHECK_MATCH ("FoO", "foo", MATCH_PARAMS);
  2483. CHECK_MATCH ("foo", "FoO", NORMAL);
  2484. CHECK_MATCH ("foo", "FoO", MATCH_PARAMS);
  2485. CHECK_MATCH ("FoO[AbI:abC]()", "foo", NORMAL);
  2486. CHECK_NO_MATCH ("FoO[AbI:abC]()", "foo", MATCH_PARAMS);
  2487. CHECK_MATCH ("FoO2[AbI:abC]()", "foo", NORMAL);
  2488. CHECK_NO_MATCH ("FoO2[AbI:abC]()", "foo", MATCH_PARAMS);
  2489. CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:abC]()", NORMAL);
  2490. CHECK_MATCH ("foo[abi:abc]()", "FoO[AbI:AbC]()", MATCH_PARAMS);
  2491. CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
  2492. CHECK_MATCH ("foo[abi:abc](xyz)", "FoO[AbI:abC](XyZ)", MATCH_PARAMS);
  2493. CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)", NORMAL);
  2494. CHECK_MATCH ("foo[abi:abc][abi:def](xyz)", "FoO[AbI:abC](XyZ)",
  2495. MATCH_PARAMS);
  2496. CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
  2497. NORMAL);
  2498. CHECK_MATCH ("foo<bar<baz>>(bar<baz>)", "FoO<bAr<BaZ>>(bAr<BaZ>)",
  2499. MATCH_PARAMS);
  2500. }
  2501. }
  2502. #undef MATCH
  2503. #undef NO_MATCH
  2504. #endif
  2505. /* See utils.h. */
  2506. int
  2507. strncmp_iw (const char *string1, const char *string2, size_t string2_len)
  2508. {
  2509. return strncmp_iw_with_mode (string1, string2, string2_len,
  2510. strncmp_iw_mode::NORMAL, language_minimal);
  2511. }
  2512. /* See utils.h. */
  2513. int
  2514. strcmp_iw (const char *string1, const char *string2)
  2515. {
  2516. return strncmp_iw_with_mode (string1, string2, strlen (string2),
  2517. strncmp_iw_mode::MATCH_PARAMS, language_minimal);
  2518. }
  2519. /* This is like strcmp except that it ignores whitespace and treats
  2520. '(' as the first non-NULL character in terms of ordering. Like
  2521. strcmp (and unlike strcmp_iw), it returns negative if STRING1 <
  2522. STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2
  2523. according to that ordering.
  2524. If a list is sorted according to this function and if you want to
  2525. find names in the list that match some fixed NAME according to
  2526. strcmp_iw(LIST_ELT, NAME), then the place to start looking is right
  2527. where this function would put NAME.
  2528. This function must be neutral to the CASE_SENSITIVITY setting as the user
  2529. may choose it during later lookup. Therefore this function always sorts
  2530. primarily case-insensitively and secondarily case-sensitively.
  2531. Here are some examples of why using strcmp to sort is a bad idea:
  2532. Whitespace example:
  2533. Say your partial symtab contains: "foo<char *>", "goo". Then, if
  2534. we try to do a search for "foo<char*>", strcmp will locate this
  2535. after "foo<char *>" and before "goo". Then lookup_partial_symbol
  2536. will start looking at strings beginning with "goo", and will never
  2537. see the correct match of "foo<char *>".
  2538. Parenthesis example:
  2539. In practice, this is less like to be an issue, but I'll give it a
  2540. shot. Let's assume that '$' is a legitimate character to occur in
  2541. symbols. (Which may well even be the case on some systems.) Then
  2542. say that the partial symbol table contains "foo$" and "foo(int)".
  2543. strcmp will put them in this order, since '$' < '('. Now, if the
  2544. user searches for "foo", then strcmp will sort "foo" before "foo$".
  2545. Then lookup_partial_symbol will notice that strcmp_iw("foo$",
  2546. "foo") is false, so it won't proceed to the actual match of
  2547. "foo(int)" with "foo". */
  2548. int
  2549. strcmp_iw_ordered (const char *string1, const char *string2)
  2550. {
  2551. const char *saved_string1 = string1, *saved_string2 = string2;
  2552. enum case_sensitivity case_pass = case_sensitive_off;
  2553. for (;;)
  2554. {
  2555. /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'.
  2556. Provide stub characters if we are already at the end of one of the
  2557. strings. */
  2558. char c1 = 'X', c2 = 'X';
  2559. while (*string1 != '\0' && *string2 != '\0')
  2560. {
  2561. while (ISSPACE (*string1))
  2562. string1++;
  2563. while (ISSPACE (*string2))
  2564. string2++;
  2565. switch (case_pass)
  2566. {
  2567. case case_sensitive_off:
  2568. c1 = TOLOWER ((unsigned char) *string1);
  2569. c2 = TOLOWER ((unsigned char) *string2);
  2570. break;
  2571. case case_sensitive_on:
  2572. c1 = *string1;
  2573. c2 = *string2;
  2574. break;
  2575. }
  2576. if (c1 != c2)
  2577. break;
  2578. if (*string1 != '\0')
  2579. {
  2580. string1++;
  2581. string2++;
  2582. }
  2583. }
  2584. switch (*string1)
  2585. {
  2586. /* Characters are non-equal unless they're both '\0'; we want to
  2587. make sure we get the comparison right according to our
  2588. comparison in the cases where one of them is '\0' or '('. */
  2589. case '\0':
  2590. if (*string2 == '\0')
  2591. break;
  2592. else
  2593. return -1;
  2594. case '(':
  2595. if (*string2 == '\0')
  2596. return 1;
  2597. else
  2598. return -1;
  2599. default:
  2600. if (*string2 == '\0' || *string2 == '(')
  2601. return 1;
  2602. else if (c1 > c2)
  2603. return 1;
  2604. else if (c1 < c2)
  2605. return -1;
  2606. /* PASSTHRU */
  2607. }
  2608. if (case_pass == case_sensitive_on)
  2609. return 0;
  2610. /* Otherwise the strings were equal in case insensitive way, make
  2611. a more fine grained comparison in a case sensitive way. */
  2612. case_pass = case_sensitive_on;
  2613. string1 = saved_string1;
  2614. string2 = saved_string2;
  2615. }
  2616. }
  2617. /* See utils.h. */
  2618. bool
  2619. streq (const char *lhs, const char *rhs)
  2620. {
  2621. return !strcmp (lhs, rhs);
  2622. }
  2623. /*
  2624. ** subset_compare()
  2625. ** Answer whether string_to_compare is a full or partial match to
  2626. ** template_string. The partial match must be in sequence starting
  2627. ** at index 0.
  2628. */
  2629. int
  2630. subset_compare (const char *string_to_compare, const char *template_string)
  2631. {
  2632. int match;
  2633. if (template_string != NULL && string_to_compare != NULL
  2634. && strlen (string_to_compare) <= strlen (template_string))
  2635. match =
  2636. (startswith (template_string, string_to_compare));
  2637. else
  2638. match = 0;
  2639. return match;
  2640. }
  2641. static void
  2642. show_debug_timestamp (struct ui_file *file, int from_tty,
  2643. struct cmd_list_element *c, const char *value)
  2644. {
  2645. gdb_printf (file, _("Timestamping debugging messages is %s.\n"),
  2646. value);
  2647. }
  2648. /* See utils.h. */
  2649. CORE_ADDR
  2650. address_significant (gdbarch *gdbarch, CORE_ADDR addr)
  2651. {
  2652. /* Clear insignificant bits of a target address and sign extend resulting
  2653. address, avoiding shifts larger or equal than the width of a CORE_ADDR.
  2654. The local variable ADDR_BIT stops the compiler reporting a shift overflow
  2655. when it won't occur. Skip updating of target address if current target
  2656. has not set gdbarch significant_addr_bit. */
  2657. int addr_bit = gdbarch_significant_addr_bit (gdbarch);
  2658. if (addr_bit && (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)))
  2659. {
  2660. CORE_ADDR sign = (CORE_ADDR) 1 << (addr_bit - 1);
  2661. addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
  2662. addr = (addr ^ sign) - sign;
  2663. }
  2664. return addr;
  2665. }
  2666. const char *
  2667. paddress (struct gdbarch *gdbarch, CORE_ADDR addr)
  2668. {
  2669. /* Truncate address to the size of a target address, avoiding shifts
  2670. larger or equal than the width of a CORE_ADDR. The local
  2671. variable ADDR_BIT stops the compiler reporting a shift overflow
  2672. when it won't occur. */
  2673. /* NOTE: This assumes that the significant address information is
  2674. kept in the least significant bits of ADDR - the upper bits were
  2675. either zero or sign extended. Should gdbarch_address_to_pointer or
  2676. some ADDRESS_TO_PRINTABLE() be used to do the conversion? */
  2677. int addr_bit = gdbarch_addr_bit (gdbarch);
  2678. if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  2679. addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
  2680. return hex_string (addr);
  2681. }
  2682. /* This function is described in "defs.h". */
  2683. const char *
  2684. print_core_address (struct gdbarch *gdbarch, CORE_ADDR address)
  2685. {
  2686. int addr_bit = gdbarch_addr_bit (gdbarch);
  2687. if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  2688. address &= ((CORE_ADDR) 1 << addr_bit) - 1;
  2689. /* FIXME: cagney/2002-05-03: Need local_address_string() function
  2690. that returns the language localized string formatted to a width
  2691. based on gdbarch_addr_bit. */
  2692. if (addr_bit <= 32)
  2693. return hex_string_custom (address, 8);
  2694. else
  2695. return hex_string_custom (address, 16);
  2696. }
  2697. /* Convert a string back into a CORE_ADDR. */
  2698. CORE_ADDR
  2699. string_to_core_addr (const char *my_string)
  2700. {
  2701. CORE_ADDR addr = 0;
  2702. if (my_string[0] == '0' && TOLOWER (my_string[1]) == 'x')
  2703. {
  2704. /* Assume that it is in hex. */
  2705. int i;
  2706. for (i = 2; my_string[i] != '\0'; i++)
  2707. {
  2708. if (ISDIGIT (my_string[i]))
  2709. addr = (my_string[i] - '0') + (addr * 16);
  2710. else if (ISXDIGIT (my_string[i]))
  2711. addr = (TOLOWER (my_string[i]) - 'a' + 0xa) + (addr * 16);
  2712. else
  2713. error (_("invalid hex \"%s\""), my_string);
  2714. }
  2715. }
  2716. else
  2717. {
  2718. /* Assume that it is in decimal. */
  2719. int i;
  2720. for (i = 0; my_string[i] != '\0'; i++)
  2721. {
  2722. if (ISDIGIT (my_string[i]))
  2723. addr = (my_string[i] - '0') + (addr * 10);
  2724. else
  2725. error (_("invalid decimal \"%s\""), my_string);
  2726. }
  2727. }
  2728. return addr;
  2729. }
  2730. #if GDB_SELF_TEST
  2731. static void
  2732. gdb_realpath_check_trailer (const char *input, const char *trailer)
  2733. {
  2734. gdb::unique_xmalloc_ptr<char> result = gdb_realpath (input);
  2735. size_t len = strlen (result.get ());
  2736. size_t trail_len = strlen (trailer);
  2737. SELF_CHECK (len >= trail_len
  2738. && strcmp (result.get () + len - trail_len, trailer) == 0);
  2739. }
  2740. static void
  2741. gdb_realpath_tests ()
  2742. {
  2743. /* A file which contains a directory prefix. */
  2744. gdb_realpath_check_trailer ("./xfullpath.exp", "/xfullpath.exp");
  2745. /* A file which contains a directory prefix. */
  2746. gdb_realpath_check_trailer ("../../defs.h", "/defs.h");
  2747. /* A one-character filename. */
  2748. gdb_realpath_check_trailer ("./a", "/a");
  2749. /* A file in the root directory. */
  2750. gdb_realpath_check_trailer ("/root_file_which_should_exist",
  2751. "/root_file_which_should_exist");
  2752. /* A file which does not have a directory prefix. */
  2753. gdb_realpath_check_trailer ("xfullpath.exp", "xfullpath.exp");
  2754. /* A one-char filename without any directory prefix. */
  2755. gdb_realpath_check_trailer ("a", "a");
  2756. /* An empty filename. */
  2757. gdb_realpath_check_trailer ("", "");
  2758. }
  2759. /* Test the gdb_argv::as_array_view method. */
  2760. static void
  2761. gdb_argv_as_array_view_test ()
  2762. {
  2763. {
  2764. gdb_argv argv;
  2765. gdb::array_view<char *> view = argv.as_array_view ();
  2766. SELF_CHECK (view.data () == nullptr);
  2767. SELF_CHECK (view.size () == 0);
  2768. }
  2769. {
  2770. gdb_argv argv ("une bonne 50");
  2771. gdb::array_view<char *> view = argv.as_array_view ();
  2772. SELF_CHECK (view.size () == 3);
  2773. SELF_CHECK (strcmp (view[0], "une") == 0);
  2774. SELF_CHECK (strcmp (view[1], "bonne") == 0);
  2775. SELF_CHECK (strcmp (view[2], "50") == 0);
  2776. }
  2777. }
  2778. #endif /* GDB_SELF_TEST */
  2779. /* Simple, portable version of dirname that does not modify its
  2780. argument. */
  2781. std::string
  2782. ldirname (const char *filename)
  2783. {
  2784. std::string dirname;
  2785. const char *base = lbasename (filename);
  2786. while (base > filename && IS_DIR_SEPARATOR (base[-1]))
  2787. --base;
  2788. if (base == filename)
  2789. return dirname;
  2790. dirname = std::string (filename, base - filename);
  2791. /* On DOS based file systems, convert "d:foo" to "d:.", so that we
  2792. create "d:./bar" later instead of the (different) "d:/bar". */
  2793. if (base - filename == 2 && IS_ABSOLUTE_PATH (base)
  2794. && !IS_DIR_SEPARATOR (filename[0]))
  2795. dirname[base++ - filename] = '.';
  2796. return dirname;
  2797. }
  2798. /* Return ARGS parsed as a valid pid, or throw an error. */
  2799. int
  2800. parse_pid_to_attach (const char *args)
  2801. {
  2802. unsigned long pid;
  2803. char *dummy;
  2804. if (!args)
  2805. error_no_arg (_("process-id to attach"));
  2806. dummy = (char *) args;
  2807. pid = strtoul (args, &dummy, 0);
  2808. /* Some targets don't set errno on errors, grrr! */
  2809. if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)])
  2810. error (_("Illegal process-id: %s."), args);
  2811. return pid;
  2812. }
  2813. /* Substitute all occurrences of string FROM by string TO in *STRINGP. *STRINGP
  2814. must come from xrealloc-compatible allocator and it may be updated. FROM
  2815. needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
  2816. located at the start or end of *STRINGP. */
  2817. void
  2818. substitute_path_component (char **stringp, const char *from, const char *to)
  2819. {
  2820. char *string = *stringp, *s;
  2821. const size_t from_len = strlen (from);
  2822. const size_t to_len = strlen (to);
  2823. for (s = string;;)
  2824. {
  2825. s = strstr (s, from);
  2826. if (s == NULL)
  2827. break;
  2828. if ((s == string || IS_DIR_SEPARATOR (s[-1])
  2829. || s[-1] == DIRNAME_SEPARATOR)
  2830. && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
  2831. || s[from_len] == DIRNAME_SEPARATOR))
  2832. {
  2833. char *string_new;
  2834. string_new
  2835. = (char *) xrealloc (string, (strlen (string) + to_len + 1));
  2836. /* Relocate the current S pointer. */
  2837. s = s - string + string_new;
  2838. string = string_new;
  2839. /* Replace from by to. */
  2840. memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
  2841. memcpy (s, to, to_len);
  2842. s += to_len;
  2843. }
  2844. else
  2845. s++;
  2846. }
  2847. *stringp = string;
  2848. }
  2849. #ifdef HAVE_WAITPID
  2850. #ifdef SIGALRM
  2851. /* SIGALRM handler for waitpid_with_timeout. */
  2852. static void
  2853. sigalrm_handler (int signo)
  2854. {
  2855. /* Nothing to do. */
  2856. }
  2857. #endif
  2858. /* Wrapper to wait for child PID to die with TIMEOUT.
  2859. TIMEOUT is the time to stop waiting in seconds.
  2860. If TIMEOUT is zero, pass WNOHANG to waitpid.
  2861. Returns PID if it was successfully waited for, otherwise -1.
  2862. Timeouts are currently implemented with alarm and SIGALRM.
  2863. If the host does not support them, this waits "forever".
  2864. It would be odd though for a host to have waitpid and not SIGALRM. */
  2865. pid_t
  2866. wait_to_die_with_timeout (pid_t pid, int *status, int timeout)
  2867. {
  2868. pid_t waitpid_result;
  2869. gdb_assert (pid > 0);
  2870. gdb_assert (timeout >= 0);
  2871. if (timeout > 0)
  2872. {
  2873. #ifdef SIGALRM
  2874. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  2875. struct sigaction sa, old_sa;
  2876. sa.sa_handler = sigalrm_handler;
  2877. sigemptyset (&sa.sa_mask);
  2878. sa.sa_flags = 0;
  2879. sigaction (SIGALRM, &sa, &old_sa);
  2880. #else
  2881. sighandler_t ofunc;
  2882. ofunc = signal (SIGALRM, sigalrm_handler);
  2883. #endif
  2884. alarm (timeout);
  2885. #endif
  2886. waitpid_result = waitpid (pid, status, 0);
  2887. #ifdef SIGALRM
  2888. alarm (0);
  2889. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  2890. sigaction (SIGALRM, &old_sa, NULL);
  2891. #else
  2892. signal (SIGALRM, ofunc);
  2893. #endif
  2894. #endif
  2895. }
  2896. else
  2897. waitpid_result = waitpid (pid, status, WNOHANG);
  2898. if (waitpid_result == pid)
  2899. return pid;
  2900. else
  2901. return -1;
  2902. }
  2903. #endif /* HAVE_WAITPID */
  2904. /* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files.
  2905. Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS.
  2906. It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and
  2907. HAVE_CASE_INSENSITIVE_FILE_SYSTEM. */
  2908. int
  2909. gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
  2910. {
  2911. gdb_assert ((flags & FNM_FILE_NAME) != 0);
  2912. /* It is unclear how '\' escaping vs. directory separator should coexist. */
  2913. gdb_assert ((flags & FNM_NOESCAPE) != 0);
  2914. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  2915. {
  2916. char *pattern_slash, *string_slash;
  2917. /* Replace '\' by '/' in both strings. */
  2918. pattern_slash = (char *) alloca (strlen (pattern) + 1);
  2919. strcpy (pattern_slash, pattern);
  2920. pattern = pattern_slash;
  2921. for (; *pattern_slash != 0; pattern_slash++)
  2922. if (IS_DIR_SEPARATOR (*pattern_slash))
  2923. *pattern_slash = '/';
  2924. string_slash = (char *) alloca (strlen (string) + 1);
  2925. strcpy (string_slash, string);
  2926. string = string_slash;
  2927. for (; *string_slash != 0; string_slash++)
  2928. if (IS_DIR_SEPARATOR (*string_slash))
  2929. *string_slash = '/';
  2930. }
  2931. #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
  2932. #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
  2933. flags |= FNM_CASEFOLD;
  2934. #endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */
  2935. return fnmatch (pattern, string, flags);
  2936. }
  2937. /* Return the number of path elements in PATH.
  2938. / = 1
  2939. /foo = 2
  2940. /foo/ = 2
  2941. foo/bar = 2
  2942. foo/ = 1 */
  2943. int
  2944. count_path_elements (const char *path)
  2945. {
  2946. int count = 0;
  2947. const char *p = path;
  2948. if (HAS_DRIVE_SPEC (p))
  2949. {
  2950. p = STRIP_DRIVE_SPEC (p);
  2951. ++count;
  2952. }
  2953. while (*p != '\0')
  2954. {
  2955. if (IS_DIR_SEPARATOR (*p))
  2956. ++count;
  2957. ++p;
  2958. }
  2959. /* Backup one if last character is /, unless it's the only one. */
  2960. if (p > path + 1 && IS_DIR_SEPARATOR (p[-1]))
  2961. --count;
  2962. /* Add one for the file name, if present. */
  2963. if (p > path && !IS_DIR_SEPARATOR (p[-1]))
  2964. ++count;
  2965. return count;
  2966. }
  2967. /* Remove N leading path elements from PATH.
  2968. N must be non-negative.
  2969. If PATH has more than N path elements then return NULL.
  2970. If PATH has exactly N path elements then return "".
  2971. See count_path_elements for a description of how we do the counting. */
  2972. const char *
  2973. strip_leading_path_elements (const char *path, int n)
  2974. {
  2975. int i = 0;
  2976. const char *p = path;
  2977. gdb_assert (n >= 0);
  2978. if (n == 0)
  2979. return p;
  2980. if (HAS_DRIVE_SPEC (p))
  2981. {
  2982. p = STRIP_DRIVE_SPEC (p);
  2983. ++i;
  2984. }
  2985. while (i < n)
  2986. {
  2987. while (*p != '\0' && !IS_DIR_SEPARATOR (*p))
  2988. ++p;
  2989. if (*p == '\0')
  2990. {
  2991. if (i + 1 == n)
  2992. return "";
  2993. return NULL;
  2994. }
  2995. ++p;
  2996. ++i;
  2997. }
  2998. return p;
  2999. }
  3000. /* See utils.h. */
  3001. void
  3002. copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
  3003. const gdb_byte *source, ULONGEST source_offset,
  3004. ULONGEST nbits, int bits_big_endian)
  3005. {
  3006. unsigned int buf, avail;
  3007. if (nbits == 0)
  3008. return;
  3009. if (bits_big_endian)
  3010. {
  3011. /* Start from the end, then work backwards. */
  3012. dest_offset += nbits - 1;
  3013. dest += dest_offset / 8;
  3014. dest_offset = 7 - dest_offset % 8;
  3015. source_offset += nbits - 1;
  3016. source += source_offset / 8;
  3017. source_offset = 7 - source_offset % 8;
  3018. }
  3019. else
  3020. {
  3021. dest += dest_offset / 8;
  3022. dest_offset %= 8;
  3023. source += source_offset / 8;
  3024. source_offset %= 8;
  3025. }
  3026. /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
  3027. SOURCE_OFFSET bits from the source. */
  3028. buf = *(bits_big_endian ? source-- : source++) >> source_offset;
  3029. buf <<= dest_offset;
  3030. buf |= *dest & ((1 << dest_offset) - 1);
  3031. /* NBITS: bits yet to be written; AVAIL: BUF's fill level. */
  3032. nbits += dest_offset;
  3033. avail = dest_offset + 8 - source_offset;
  3034. /* Flush 8 bits from BUF, if appropriate. */
  3035. if (nbits >= 8 && avail >= 8)
  3036. {
  3037. *(bits_big_endian ? dest-- : dest++) = buf;
  3038. buf >>= 8;
  3039. avail -= 8;
  3040. nbits -= 8;
  3041. }
  3042. /* Copy the middle part. */
  3043. if (nbits >= 8)
  3044. {
  3045. size_t len = nbits / 8;
  3046. /* Use a faster method for byte-aligned copies. */
  3047. if (avail == 0)
  3048. {
  3049. if (bits_big_endian)
  3050. {
  3051. dest -= len;
  3052. source -= len;
  3053. memcpy (dest + 1, source + 1, len);
  3054. }
  3055. else
  3056. {
  3057. memcpy (dest, source, len);
  3058. dest += len;
  3059. source += len;
  3060. }
  3061. }
  3062. else
  3063. {
  3064. while (len--)
  3065. {
  3066. buf |= *(bits_big_endian ? source-- : source++) << avail;
  3067. *(bits_big_endian ? dest-- : dest++) = buf;
  3068. buf >>= 8;
  3069. }
  3070. }
  3071. nbits %= 8;
  3072. }
  3073. /* Write the last byte. */
  3074. if (nbits)
  3075. {
  3076. if (avail < nbits)
  3077. buf |= *source << avail;
  3078. buf &= (1 << nbits) - 1;
  3079. *dest = (*dest & (~0U << nbits)) | buf;
  3080. }
  3081. }
  3082. void _initialize_utils ();
  3083. void
  3084. _initialize_utils ()
  3085. {
  3086. add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\
  3087. Set number of characters where GDB should wrap lines of its output."), _("\
  3088. Show number of characters where GDB should wrap lines of its output."), _("\
  3089. This affects where GDB wraps its output to fit the screen width.\n\
  3090. Setting this to \"unlimited\" or zero prevents GDB from wrapping its output."),
  3091. set_width_command,
  3092. show_chars_per_line,
  3093. &setlist, &showlist);
  3094. add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\
  3095. Set number of lines in a page for GDB output pagination."), _("\
  3096. Show number of lines in a page for GDB output pagination."), _("\
  3097. This affects the number of lines after which GDB will pause\n\
  3098. its output and ask you whether to continue.\n\
  3099. Setting this to \"unlimited\" or zero causes GDB never pause during output."),
  3100. set_height_command,
  3101. show_lines_per_page,
  3102. &setlist, &showlist);
  3103. add_setshow_boolean_cmd ("pagination", class_support,
  3104. &pagination_enabled, _("\
  3105. Set state of GDB output pagination."), _("\
  3106. Show state of GDB output pagination."), _("\
  3107. When pagination is ON, GDB pauses at end of each screenful of\n\
  3108. its output and asks you whether to continue.\n\
  3109. Turning pagination off is an alternative to \"set height unlimited\"."),
  3110. NULL,
  3111. show_pagination_enabled,
  3112. &setlist, &showlist);
  3113. add_setshow_boolean_cmd ("sevenbit-strings", class_support,
  3114. &sevenbit_strings, _("\
  3115. Set printing of 8-bit characters in strings as \\nnn."), _("\
  3116. Show printing of 8-bit characters in strings as \\nnn."), NULL,
  3117. NULL,
  3118. show_sevenbit_strings,
  3119. &setprintlist, &showprintlist);
  3120. add_setshow_boolean_cmd ("timestamp", class_maintenance,
  3121. &debug_timestamp, _("\
  3122. Set timestamping of debugging messages."), _("\
  3123. Show timestamping of debugging messages."), _("\
  3124. When set, debugging messages will be marked with seconds and microseconds."),
  3125. NULL,
  3126. show_debug_timestamp,
  3127. &setdebuglist, &showdebuglist);
  3128. add_internal_problem_command (&internal_error_problem);
  3129. add_internal_problem_command (&internal_warning_problem);
  3130. add_internal_problem_command (&demangler_warning_problem);
  3131. #if GDB_SELF_TEST
  3132. selftests::register_test ("gdb_realpath", gdb_realpath_tests);
  3133. selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test);
  3134. selftests::register_test ("strncmp_iw_with_mode",
  3135. strncmp_iw_with_mode_tests);
  3136. #endif
  3137. }