kdb_main.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970
  1. /*
  2. * Kernel Debugger Architecture Independent Main Code
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
  9. * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
  10. * Xscale (R) modifications copyright (C) 2003 Intel Corporation.
  11. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  12. */
  13. #include <linux/ctype.h>
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kmsg_dump.h>
  18. #include <linux/reboot.h>
  19. #include <linux/sched.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/smp.h>
  22. #include <linux/utsname.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/atomic.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/mm.h>
  28. #include <linux/init.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/kgdb.h>
  31. #include <linux/kdb.h>
  32. #include <linux/notifier.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/nmi.h>
  36. #include <linux/time.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/sysctl.h>
  39. #include <linux/cpu.h>
  40. #include <linux/kdebug.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/slab.h>
  44. #include "kdb_private.h"
  45. #undef MODULE_PARAM_PREFIX
  46. #define MODULE_PARAM_PREFIX "kdb."
  47. static int kdb_cmd_enabled = CONFIG_KDB_DEFAULT_ENABLE;
  48. module_param_named(cmd_enable, kdb_cmd_enabled, int, 0600);
  49. char kdb_grep_string[KDB_GREP_STRLEN];
  50. int kdb_grepping_flag;
  51. EXPORT_SYMBOL(kdb_grepping_flag);
  52. int kdb_grep_leading;
  53. int kdb_grep_trailing;
  54. /*
  55. * Kernel debugger state flags
  56. */
  57. int kdb_flags;
  58. atomic_t kdb_event;
  59. /*
  60. * kdb_lock protects updates to kdb_initial_cpu. Used to
  61. * single thread processors through the kernel debugger.
  62. */
  63. int kdb_initial_cpu = -1; /* cpu number that owns kdb */
  64. int kdb_nextline = 1;
  65. int kdb_state; /* General KDB state */
  66. struct task_struct *kdb_current_task;
  67. EXPORT_SYMBOL(kdb_current_task);
  68. struct pt_regs *kdb_current_regs;
  69. const char *kdb_diemsg;
  70. static int kdb_go_count;
  71. #ifdef CONFIG_KDB_CONTINUE_CATASTROPHIC
  72. static unsigned int kdb_continue_catastrophic =
  73. CONFIG_KDB_CONTINUE_CATASTROPHIC;
  74. #else
  75. static unsigned int kdb_continue_catastrophic;
  76. #endif
  77. /* kdb_commands describes the available commands. */
  78. static kdbtab_t *kdb_commands;
  79. #define KDB_BASE_CMD_MAX 50
  80. static int kdb_max_commands = KDB_BASE_CMD_MAX;
  81. static kdbtab_t kdb_base_commands[KDB_BASE_CMD_MAX];
  82. #define for_each_kdbcmd(cmd, num) \
  83. for ((cmd) = kdb_base_commands, (num) = 0; \
  84. num < kdb_max_commands; \
  85. num++, num == KDB_BASE_CMD_MAX ? cmd = kdb_commands : cmd++)
  86. typedef struct _kdbmsg {
  87. int km_diag; /* kdb diagnostic */
  88. char *km_msg; /* Corresponding message text */
  89. } kdbmsg_t;
  90. #define KDBMSG(msgnum, text) \
  91. { KDB_##msgnum, text }
  92. static kdbmsg_t kdbmsgs[] = {
  93. KDBMSG(NOTFOUND, "Command Not Found"),
  94. KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
  95. KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, "
  96. "8 is only allowed on 64 bit systems"),
  97. KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
  98. KDBMSG(NOTENV, "Cannot find environment variable"),
  99. KDBMSG(NOENVVALUE, "Environment variable should have value"),
  100. KDBMSG(NOTIMP, "Command not implemented"),
  101. KDBMSG(ENVFULL, "Environment full"),
  102. KDBMSG(ENVBUFFULL, "Environment buffer full"),
  103. KDBMSG(TOOMANYBPT, "Too many breakpoints defined"),
  104. #ifdef CONFIG_CPU_XSCALE
  105. KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"),
  106. #else
  107. KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"),
  108. #endif
  109. KDBMSG(DUPBPT, "Duplicate breakpoint address"),
  110. KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
  111. KDBMSG(BADMODE, "Invalid IDMODE"),
  112. KDBMSG(BADINT, "Illegal numeric value"),
  113. KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
  114. KDBMSG(BADREG, "Invalid register name"),
  115. KDBMSG(BADCPUNUM, "Invalid cpu number"),
  116. KDBMSG(BADLENGTH, "Invalid length field"),
  117. KDBMSG(NOBP, "No Breakpoint exists"),
  118. KDBMSG(BADADDR, "Invalid address"),
  119. KDBMSG(NOPERM, "Permission denied"),
  120. };
  121. #undef KDBMSG
  122. static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
  123. /*
  124. * Initial environment. This is all kept static and local to
  125. * this file. We don't want to rely on the memory allocation
  126. * mechanisms in the kernel, so we use a very limited allocate-only
  127. * heap for new and altered environment variables. The entire
  128. * environment is limited to a fixed number of entries (add more
  129. * to __env[] if required) and a fixed amount of heap (add more to
  130. * KDB_ENVBUFSIZE if required).
  131. */
  132. static char *__env[] = {
  133. #if defined(CONFIG_SMP)
  134. "PROMPT=[%d]kdb> ",
  135. #else
  136. "PROMPT=kdb> ",
  137. #endif
  138. "MOREPROMPT=more> ",
  139. "RADIX=16",
  140. "MDCOUNT=8", /* lines of md output */
  141. KDB_PLATFORM_ENV,
  142. "DTABCOUNT=30",
  143. "NOSECT=1",
  144. (char *)0,
  145. (char *)0,
  146. (char *)0,
  147. (char *)0,
  148. (char *)0,
  149. (char *)0,
  150. (char *)0,
  151. (char *)0,
  152. (char *)0,
  153. (char *)0,
  154. (char *)0,
  155. (char *)0,
  156. (char *)0,
  157. (char *)0,
  158. (char *)0,
  159. (char *)0,
  160. (char *)0,
  161. (char *)0,
  162. (char *)0,
  163. (char *)0,
  164. (char *)0,
  165. (char *)0,
  166. (char *)0,
  167. (char *)0,
  168. };
  169. static const int __nenv = ARRAY_SIZE(__env);
  170. struct task_struct *kdb_curr_task(int cpu)
  171. {
  172. struct task_struct *p = curr_task(cpu);
  173. #ifdef _TIF_MCA_INIT
  174. if ((task_thread_info(p)->flags & _TIF_MCA_INIT) && KDB_TSK(cpu))
  175. p = krp->p;
  176. #endif
  177. return p;
  178. }
  179. /*
  180. * Check whether the flags of the current command and the permissions
  181. * of the kdb console has allow a command to be run.
  182. */
  183. static inline bool kdb_check_flags(kdb_cmdflags_t flags, int permissions,
  184. bool no_args)
  185. {
  186. /* permissions comes from userspace so needs massaging slightly */
  187. permissions &= KDB_ENABLE_MASK;
  188. permissions |= KDB_ENABLE_ALWAYS_SAFE;
  189. /* some commands change group when launched with no arguments */
  190. if (no_args)
  191. permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT;
  192. flags |= KDB_ENABLE_ALL;
  193. return permissions & flags;
  194. }
  195. /*
  196. * kdbgetenv - This function will return the character string value of
  197. * an environment variable.
  198. * Parameters:
  199. * match A character string representing an environment variable.
  200. * Returns:
  201. * NULL No environment variable matches 'match'
  202. * char* Pointer to string value of environment variable.
  203. */
  204. char *kdbgetenv(const char *match)
  205. {
  206. char **ep = __env;
  207. int matchlen = strlen(match);
  208. int i;
  209. for (i = 0; i < __nenv; i++) {
  210. char *e = *ep++;
  211. if (!e)
  212. continue;
  213. if ((strncmp(match, e, matchlen) == 0)
  214. && ((e[matchlen] == '\0')
  215. || (e[matchlen] == '='))) {
  216. char *cp = strchr(e, '=');
  217. return cp ? ++cp : "";
  218. }
  219. }
  220. return NULL;
  221. }
  222. /*
  223. * kdballocenv - This function is used to allocate bytes for
  224. * environment entries.
  225. * Parameters:
  226. * match A character string representing a numeric value
  227. * Outputs:
  228. * *value the unsigned long representation of the env variable 'match'
  229. * Returns:
  230. * Zero on success, a kdb diagnostic on failure.
  231. * Remarks:
  232. * We use a static environment buffer (envbuffer) to hold the values
  233. * of dynamically generated environment variables (see kdb_set). Buffer
  234. * space once allocated is never free'd, so over time, the amount of space
  235. * (currently 512 bytes) will be exhausted if env variables are changed
  236. * frequently.
  237. */
  238. static char *kdballocenv(size_t bytes)
  239. {
  240. #define KDB_ENVBUFSIZE 512
  241. static char envbuffer[KDB_ENVBUFSIZE];
  242. static int envbufsize;
  243. char *ep = NULL;
  244. if ((KDB_ENVBUFSIZE - envbufsize) >= bytes) {
  245. ep = &envbuffer[envbufsize];
  246. envbufsize += bytes;
  247. }
  248. return ep;
  249. }
  250. /*
  251. * kdbgetulenv - This function will return the value of an unsigned
  252. * long-valued environment variable.
  253. * Parameters:
  254. * match A character string representing a numeric value
  255. * Outputs:
  256. * *value the unsigned long represntation of the env variable 'match'
  257. * Returns:
  258. * Zero on success, a kdb diagnostic on failure.
  259. */
  260. static int kdbgetulenv(const char *match, unsigned long *value)
  261. {
  262. char *ep;
  263. ep = kdbgetenv(match);
  264. if (!ep)
  265. return KDB_NOTENV;
  266. if (strlen(ep) == 0)
  267. return KDB_NOENVVALUE;
  268. *value = simple_strtoul(ep, NULL, 0);
  269. return 0;
  270. }
  271. /*
  272. * kdbgetintenv - This function will return the value of an
  273. * integer-valued environment variable.
  274. * Parameters:
  275. * match A character string representing an integer-valued env variable
  276. * Outputs:
  277. * *value the integer representation of the environment variable 'match'
  278. * Returns:
  279. * Zero on success, a kdb diagnostic on failure.
  280. */
  281. int kdbgetintenv(const char *match, int *value)
  282. {
  283. unsigned long val;
  284. int diag;
  285. diag = kdbgetulenv(match, &val);
  286. if (!diag)
  287. *value = (int) val;
  288. return diag;
  289. }
  290. /*
  291. * kdbgetularg - This function will convert a numeric string into an
  292. * unsigned long value.
  293. * Parameters:
  294. * arg A character string representing a numeric value
  295. * Outputs:
  296. * *value the unsigned long represntation of arg.
  297. * Returns:
  298. * Zero on success, a kdb diagnostic on failure.
  299. */
  300. int kdbgetularg(const char *arg, unsigned long *value)
  301. {
  302. char *endp;
  303. unsigned long val;
  304. val = simple_strtoul(arg, &endp, 0);
  305. if (endp == arg) {
  306. /*
  307. * Also try base 16, for us folks too lazy to type the
  308. * leading 0x...
  309. */
  310. val = simple_strtoul(arg, &endp, 16);
  311. if (endp == arg)
  312. return KDB_BADINT;
  313. }
  314. *value = val;
  315. return 0;
  316. }
  317. int kdbgetu64arg(const char *arg, u64 *value)
  318. {
  319. char *endp;
  320. u64 val;
  321. val = simple_strtoull(arg, &endp, 0);
  322. if (endp == arg) {
  323. val = simple_strtoull(arg, &endp, 16);
  324. if (endp == arg)
  325. return KDB_BADINT;
  326. }
  327. *value = val;
  328. return 0;
  329. }
  330. /*
  331. * kdb_set - This function implements the 'set' command. Alter an
  332. * existing environment variable or create a new one.
  333. */
  334. int kdb_set(int argc, const char **argv)
  335. {
  336. int i;
  337. char *ep;
  338. size_t varlen, vallen;
  339. /*
  340. * we can be invoked two ways:
  341. * set var=value argv[1]="var", argv[2]="value"
  342. * set var = value argv[1]="var", argv[2]="=", argv[3]="value"
  343. * - if the latter, shift 'em down.
  344. */
  345. if (argc == 3) {
  346. argv[2] = argv[3];
  347. argc--;
  348. }
  349. if (argc != 2)
  350. return KDB_ARGCOUNT;
  351. /*
  352. * Check for internal variables
  353. */
  354. if (strcmp(argv[1], "KDBDEBUG") == 0) {
  355. unsigned int debugflags;
  356. char *cp;
  357. debugflags = simple_strtoul(argv[2], &cp, 0);
  358. if (cp == argv[2] || debugflags & ~KDB_DEBUG_FLAG_MASK) {
  359. kdb_printf("kdb: illegal debug flags '%s'\n",
  360. argv[2]);
  361. return 0;
  362. }
  363. kdb_flags = (kdb_flags &
  364. ~(KDB_DEBUG_FLAG_MASK << KDB_DEBUG_FLAG_SHIFT))
  365. | (debugflags << KDB_DEBUG_FLAG_SHIFT);
  366. return 0;
  367. }
  368. /*
  369. * Tokenizer squashed the '=' sign. argv[1] is variable
  370. * name, argv[2] = value.
  371. */
  372. varlen = strlen(argv[1]);
  373. vallen = strlen(argv[2]);
  374. ep = kdballocenv(varlen + vallen + 2);
  375. if (ep == (char *)0)
  376. return KDB_ENVBUFFULL;
  377. sprintf(ep, "%s=%s", argv[1], argv[2]);
  378. ep[varlen+vallen+1] = '\0';
  379. for (i = 0; i < __nenv; i++) {
  380. if (__env[i]
  381. && ((strncmp(__env[i], argv[1], varlen) == 0)
  382. && ((__env[i][varlen] == '\0')
  383. || (__env[i][varlen] == '=')))) {
  384. __env[i] = ep;
  385. return 0;
  386. }
  387. }
  388. /*
  389. * Wasn't existing variable. Fit into slot.
  390. */
  391. for (i = 0; i < __nenv-1; i++) {
  392. if (__env[i] == (char *)0) {
  393. __env[i] = ep;
  394. return 0;
  395. }
  396. }
  397. return KDB_ENVFULL;
  398. }
  399. static int kdb_check_regs(void)
  400. {
  401. if (!kdb_current_regs) {
  402. kdb_printf("No current kdb registers."
  403. " You may need to select another task\n");
  404. return KDB_BADREG;
  405. }
  406. return 0;
  407. }
  408. /*
  409. * kdbgetaddrarg - This function is responsible for parsing an
  410. * address-expression and returning the value of the expression,
  411. * symbol name, and offset to the caller.
  412. *
  413. * The argument may consist of a numeric value (decimal or
  414. * hexidecimal), a symbol name, a register name (preceded by the
  415. * percent sign), an environment variable with a numeric value
  416. * (preceded by a dollar sign) or a simple arithmetic expression
  417. * consisting of a symbol name, +/-, and a numeric constant value
  418. * (offset).
  419. * Parameters:
  420. * argc - count of arguments in argv
  421. * argv - argument vector
  422. * *nextarg - index to next unparsed argument in argv[]
  423. * regs - Register state at time of KDB entry
  424. * Outputs:
  425. * *value - receives the value of the address-expression
  426. * *offset - receives the offset specified, if any
  427. * *name - receives the symbol name, if any
  428. * *nextarg - index to next unparsed argument in argv[]
  429. * Returns:
  430. * zero is returned on success, a kdb diagnostic code is
  431. * returned on error.
  432. */
  433. int kdbgetaddrarg(int argc, const char **argv, int *nextarg,
  434. unsigned long *value, long *offset,
  435. char **name)
  436. {
  437. unsigned long addr;
  438. unsigned long off = 0;
  439. int positive;
  440. int diag;
  441. int found = 0;
  442. char *symname;
  443. char symbol = '\0';
  444. char *cp;
  445. kdb_symtab_t symtab;
  446. /*
  447. * If the enable flags prohibit both arbitrary memory access
  448. * and flow control then there are no reasonable grounds to
  449. * provide symbol lookup.
  450. */
  451. if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL,
  452. kdb_cmd_enabled, false))
  453. return KDB_NOPERM;
  454. /*
  455. * Process arguments which follow the following syntax:
  456. *
  457. * symbol | numeric-address [+/- numeric-offset]
  458. * %register
  459. * $environment-variable
  460. */
  461. if (*nextarg > argc)
  462. return KDB_ARGCOUNT;
  463. symname = (char *)argv[*nextarg];
  464. /*
  465. * If there is no whitespace between the symbol
  466. * or address and the '+' or '-' symbols, we
  467. * remember the character and replace it with a
  468. * null so the symbol/value can be properly parsed
  469. */
  470. cp = strpbrk(symname, "+-");
  471. if (cp != NULL) {
  472. symbol = *cp;
  473. *cp++ = '\0';
  474. }
  475. if (symname[0] == '$') {
  476. diag = kdbgetulenv(&symname[1], &addr);
  477. if (diag)
  478. return diag;
  479. } else if (symname[0] == '%') {
  480. diag = kdb_check_regs();
  481. if (diag)
  482. return diag;
  483. /* Implement register values with % at a later time as it is
  484. * arch optional.
  485. */
  486. return KDB_NOTIMP;
  487. } else {
  488. found = kdbgetsymval(symname, &symtab);
  489. if (found) {
  490. addr = symtab.sym_start;
  491. } else {
  492. diag = kdbgetularg(argv[*nextarg], &addr);
  493. if (diag)
  494. return diag;
  495. }
  496. }
  497. if (!found)
  498. found = kdbnearsym(addr, &symtab);
  499. (*nextarg)++;
  500. if (name)
  501. *name = symname;
  502. if (value)
  503. *value = addr;
  504. if (offset && name && *name)
  505. *offset = addr - symtab.sym_start;
  506. if ((*nextarg > argc)
  507. && (symbol == '\0'))
  508. return 0;
  509. /*
  510. * check for +/- and offset
  511. */
  512. if (symbol == '\0') {
  513. if ((argv[*nextarg][0] != '+')
  514. && (argv[*nextarg][0] != '-')) {
  515. /*
  516. * Not our argument. Return.
  517. */
  518. return 0;
  519. } else {
  520. positive = (argv[*nextarg][0] == '+');
  521. (*nextarg)++;
  522. }
  523. } else
  524. positive = (symbol == '+');
  525. /*
  526. * Now there must be an offset!
  527. */
  528. if ((*nextarg > argc)
  529. && (symbol == '\0')) {
  530. return KDB_INVADDRFMT;
  531. }
  532. if (!symbol) {
  533. cp = (char *)argv[*nextarg];
  534. (*nextarg)++;
  535. }
  536. diag = kdbgetularg(cp, &off);
  537. if (diag)
  538. return diag;
  539. if (!positive)
  540. off = -off;
  541. if (offset)
  542. *offset += off;
  543. if (value)
  544. *value += off;
  545. return 0;
  546. }
  547. static void kdb_cmderror(int diag)
  548. {
  549. int i;
  550. if (diag >= 0) {
  551. kdb_printf("no error detected (diagnostic is %d)\n", diag);
  552. return;
  553. }
  554. for (i = 0; i < __nkdb_err; i++) {
  555. if (kdbmsgs[i].km_diag == diag) {
  556. kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg);
  557. return;
  558. }
  559. }
  560. kdb_printf("Unknown diag %d\n", -diag);
  561. }
  562. /*
  563. * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd'
  564. * command which defines one command as a set of other commands,
  565. * terminated by endefcmd. kdb_defcmd processes the initial
  566. * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for
  567. * the following commands until 'endefcmd'.
  568. * Inputs:
  569. * argc argument count
  570. * argv argument vector
  571. * Returns:
  572. * zero for success, a kdb diagnostic if error
  573. */
  574. struct defcmd_set {
  575. int count;
  576. int usable;
  577. char *name;
  578. char *usage;
  579. char *help;
  580. char **command;
  581. };
  582. static struct defcmd_set *defcmd_set;
  583. static int defcmd_set_count;
  584. static int defcmd_in_progress;
  585. /* Forward references */
  586. static int kdb_exec_defcmd(int argc, const char **argv);
  587. static int kdb_defcmd2(const char *cmdstr, const char *argv0)
  588. {
  589. struct defcmd_set *s = defcmd_set + defcmd_set_count - 1;
  590. char **save_command = s->command;
  591. if (strcmp(argv0, "endefcmd") == 0) {
  592. defcmd_in_progress = 0;
  593. if (!s->count)
  594. s->usable = 0;
  595. if (s->usable)
  596. /* macros are always safe because when executed each
  597. * internal command re-enters kdb_parse() and is
  598. * safety checked individually.
  599. */
  600. kdb_register_flags(s->name, kdb_exec_defcmd, s->usage,
  601. s->help, 0,
  602. KDB_ENABLE_ALWAYS_SAFE);
  603. return 0;
  604. }
  605. if (!s->usable)
  606. return KDB_NOTIMP;
  607. s->command = kzalloc((s->count + 1) * sizeof(*(s->command)), GFP_KDB);
  608. if (!s->command) {
  609. kdb_printf("Could not allocate new kdb_defcmd table for %s\n",
  610. cmdstr);
  611. s->usable = 0;
  612. return KDB_NOTIMP;
  613. }
  614. memcpy(s->command, save_command, s->count * sizeof(*(s->command)));
  615. s->command[s->count++] = kdb_strdup(cmdstr, GFP_KDB);
  616. kfree(save_command);
  617. return 0;
  618. }
  619. static int kdb_defcmd(int argc, const char **argv)
  620. {
  621. struct defcmd_set *save_defcmd_set = defcmd_set, *s;
  622. if (defcmd_in_progress) {
  623. kdb_printf("kdb: nested defcmd detected, assuming missing "
  624. "endefcmd\n");
  625. kdb_defcmd2("endefcmd", "endefcmd");
  626. }
  627. if (argc == 0) {
  628. int i;
  629. for (s = defcmd_set; s < defcmd_set + defcmd_set_count; ++s) {
  630. kdb_printf("defcmd %s \"%s\" \"%s\"\n", s->name,
  631. s->usage, s->help);
  632. for (i = 0; i < s->count; ++i)
  633. kdb_printf("%s", s->command[i]);
  634. kdb_printf("endefcmd\n");
  635. }
  636. return 0;
  637. }
  638. if (argc != 3)
  639. return KDB_ARGCOUNT;
  640. if (in_dbg_master()) {
  641. kdb_printf("Command only available during kdb_init()\n");
  642. return KDB_NOTIMP;
  643. }
  644. defcmd_set = kmalloc((defcmd_set_count + 1) * sizeof(*defcmd_set),
  645. GFP_KDB);
  646. if (!defcmd_set)
  647. goto fail_defcmd;
  648. memcpy(defcmd_set, save_defcmd_set,
  649. defcmd_set_count * sizeof(*defcmd_set));
  650. s = defcmd_set + defcmd_set_count;
  651. memset(s, 0, sizeof(*s));
  652. s->usable = 1;
  653. s->name = kdb_strdup(argv[1], GFP_KDB);
  654. if (!s->name)
  655. goto fail_name;
  656. s->usage = kdb_strdup(argv[2], GFP_KDB);
  657. if (!s->usage)
  658. goto fail_usage;
  659. s->help = kdb_strdup(argv[3], GFP_KDB);
  660. if (!s->help)
  661. goto fail_help;
  662. if (s->usage[0] == '"') {
  663. strcpy(s->usage, argv[2]+1);
  664. s->usage[strlen(s->usage)-1] = '\0';
  665. }
  666. if (s->help[0] == '"') {
  667. strcpy(s->help, argv[3]+1);
  668. s->help[strlen(s->help)-1] = '\0';
  669. }
  670. ++defcmd_set_count;
  671. defcmd_in_progress = 1;
  672. kfree(save_defcmd_set);
  673. return 0;
  674. fail_help:
  675. kfree(s->usage);
  676. fail_usage:
  677. kfree(s->name);
  678. fail_name:
  679. kfree(defcmd_set);
  680. fail_defcmd:
  681. kdb_printf("Could not allocate new defcmd_set entry for %s\n", argv[1]);
  682. defcmd_set = save_defcmd_set;
  683. return KDB_NOTIMP;
  684. }
  685. /*
  686. * kdb_exec_defcmd - Execute the set of commands associated with this
  687. * defcmd name.
  688. * Inputs:
  689. * argc argument count
  690. * argv argument vector
  691. * Returns:
  692. * zero for success, a kdb diagnostic if error
  693. */
  694. static int kdb_exec_defcmd(int argc, const char **argv)
  695. {
  696. int i, ret;
  697. struct defcmd_set *s;
  698. if (argc != 0)
  699. return KDB_ARGCOUNT;
  700. for (s = defcmd_set, i = 0; i < defcmd_set_count; ++i, ++s) {
  701. if (strcmp(s->name, argv[0]) == 0)
  702. break;
  703. }
  704. if (i == defcmd_set_count) {
  705. kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
  706. argv[0]);
  707. return KDB_NOTIMP;
  708. }
  709. for (i = 0; i < s->count; ++i) {
  710. /* Recursive use of kdb_parse, do not use argv after
  711. * this point */
  712. argv = NULL;
  713. kdb_printf("[%s]kdb> %s\n", s->name, s->command[i]);
  714. ret = kdb_parse(s->command[i]);
  715. if (ret)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. /* Command history */
  721. #define KDB_CMD_HISTORY_COUNT 32
  722. #define CMD_BUFLEN 200 /* kdb_printf: max printline
  723. * size == 256 */
  724. static unsigned int cmd_head, cmd_tail;
  725. static unsigned int cmdptr;
  726. static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
  727. static char cmd_cur[CMD_BUFLEN];
  728. /*
  729. * The "str" argument may point to something like | grep xyz
  730. */
  731. static void parse_grep(const char *str)
  732. {
  733. int len;
  734. char *cp = (char *)str, *cp2;
  735. /* sanity check: we should have been called with the \ first */
  736. if (*cp != '|')
  737. return;
  738. cp++;
  739. while (isspace(*cp))
  740. cp++;
  741. if (strncmp(cp, "grep ", 5)) {
  742. kdb_printf("invalid 'pipe', see grephelp\n");
  743. return;
  744. }
  745. cp += 5;
  746. while (isspace(*cp))
  747. cp++;
  748. cp2 = strchr(cp, '\n');
  749. if (cp2)
  750. *cp2 = '\0'; /* remove the trailing newline */
  751. len = strlen(cp);
  752. if (len == 0) {
  753. kdb_printf("invalid 'pipe', see grephelp\n");
  754. return;
  755. }
  756. /* now cp points to a nonzero length search string */
  757. if (*cp == '"') {
  758. /* allow it be "x y z" by removing the "'s - there must
  759. be two of them */
  760. cp++;
  761. cp2 = strchr(cp, '"');
  762. if (!cp2) {
  763. kdb_printf("invalid quoted string, see grephelp\n");
  764. return;
  765. }
  766. *cp2 = '\0'; /* end the string where the 2nd " was */
  767. }
  768. kdb_grep_leading = 0;
  769. if (*cp == '^') {
  770. kdb_grep_leading = 1;
  771. cp++;
  772. }
  773. len = strlen(cp);
  774. kdb_grep_trailing = 0;
  775. if (*(cp+len-1) == '$') {
  776. kdb_grep_trailing = 1;
  777. *(cp+len-1) = '\0';
  778. }
  779. len = strlen(cp);
  780. if (!len)
  781. return;
  782. if (len >= KDB_GREP_STRLEN) {
  783. kdb_printf("search string too long\n");
  784. return;
  785. }
  786. strcpy(kdb_grep_string, cp);
  787. kdb_grepping_flag++;
  788. return;
  789. }
  790. /*
  791. * kdb_parse - Parse the command line, search the command table for a
  792. * matching command and invoke the command function. This
  793. * function may be called recursively, if it is, the second call
  794. * will overwrite argv and cbuf. It is the caller's
  795. * responsibility to save their argv if they recursively call
  796. * kdb_parse().
  797. * Parameters:
  798. * cmdstr The input command line to be parsed.
  799. * regs The registers at the time kdb was entered.
  800. * Returns:
  801. * Zero for success, a kdb diagnostic if failure.
  802. * Remarks:
  803. * Limited to 20 tokens.
  804. *
  805. * Real rudimentary tokenization. Basically only whitespace
  806. * is considered a token delimeter (but special consideration
  807. * is taken of the '=' sign as used by the 'set' command).
  808. *
  809. * The algorithm used to tokenize the input string relies on
  810. * there being at least one whitespace (or otherwise useless)
  811. * character between tokens as the character immediately following
  812. * the token is altered in-place to a null-byte to terminate the
  813. * token string.
  814. */
  815. #define MAXARGC 20
  816. int kdb_parse(const char *cmdstr)
  817. {
  818. static char *argv[MAXARGC];
  819. static int argc;
  820. static char cbuf[CMD_BUFLEN+2];
  821. char *cp;
  822. char *cpp, quoted;
  823. kdbtab_t *tp;
  824. int i, escaped, ignore_errors = 0, check_grep = 0;
  825. /*
  826. * First tokenize the command string.
  827. */
  828. cp = (char *)cmdstr;
  829. if (KDB_FLAG(CMD_INTERRUPT)) {
  830. /* Previous command was interrupted, newline must not
  831. * repeat the command */
  832. KDB_FLAG_CLEAR(CMD_INTERRUPT);
  833. KDB_STATE_SET(PAGER);
  834. argc = 0; /* no repeat */
  835. }
  836. if (*cp != '\n' && *cp != '\0') {
  837. argc = 0;
  838. cpp = cbuf;
  839. while (*cp) {
  840. /* skip whitespace */
  841. while (isspace(*cp))
  842. cp++;
  843. if ((*cp == '\0') || (*cp == '\n') ||
  844. (*cp == '#' && !defcmd_in_progress))
  845. break;
  846. /* special case: check for | grep pattern */
  847. if (*cp == '|') {
  848. check_grep++;
  849. break;
  850. }
  851. if (cpp >= cbuf + CMD_BUFLEN) {
  852. kdb_printf("kdb_parse: command buffer "
  853. "overflow, command ignored\n%s\n",
  854. cmdstr);
  855. return KDB_NOTFOUND;
  856. }
  857. if (argc >= MAXARGC - 1) {
  858. kdb_printf("kdb_parse: too many arguments, "
  859. "command ignored\n%s\n", cmdstr);
  860. return KDB_NOTFOUND;
  861. }
  862. argv[argc++] = cpp;
  863. escaped = 0;
  864. quoted = '\0';
  865. /* Copy to next unquoted and unescaped
  866. * whitespace or '=' */
  867. while (*cp && *cp != '\n' &&
  868. (escaped || quoted || !isspace(*cp))) {
  869. if (cpp >= cbuf + CMD_BUFLEN)
  870. break;
  871. if (escaped) {
  872. escaped = 0;
  873. *cpp++ = *cp++;
  874. continue;
  875. }
  876. if (*cp == '\\') {
  877. escaped = 1;
  878. ++cp;
  879. continue;
  880. }
  881. if (*cp == quoted)
  882. quoted = '\0';
  883. else if (*cp == '\'' || *cp == '"')
  884. quoted = *cp;
  885. *cpp = *cp++;
  886. if (*cpp == '=' && !quoted)
  887. break;
  888. ++cpp;
  889. }
  890. *cpp++ = '\0'; /* Squash a ws or '=' character */
  891. }
  892. }
  893. if (!argc)
  894. return 0;
  895. if (check_grep)
  896. parse_grep(cp);
  897. if (defcmd_in_progress) {
  898. int result = kdb_defcmd2(cmdstr, argv[0]);
  899. if (!defcmd_in_progress) {
  900. argc = 0; /* avoid repeat on endefcmd */
  901. *(argv[0]) = '\0';
  902. }
  903. return result;
  904. }
  905. if (argv[0][0] == '-' && argv[0][1] &&
  906. (argv[0][1] < '0' || argv[0][1] > '9')) {
  907. ignore_errors = 1;
  908. ++argv[0];
  909. }
  910. for_each_kdbcmd(tp, i) {
  911. if (tp->cmd_name) {
  912. /*
  913. * If this command is allowed to be abbreviated,
  914. * check to see if this is it.
  915. */
  916. if (tp->cmd_minlen
  917. && (strlen(argv[0]) <= tp->cmd_minlen)) {
  918. if (strncmp(argv[0],
  919. tp->cmd_name,
  920. tp->cmd_minlen) == 0) {
  921. break;
  922. }
  923. }
  924. if (strcmp(argv[0], tp->cmd_name) == 0)
  925. break;
  926. }
  927. }
  928. /*
  929. * If we don't find a command by this name, see if the first
  930. * few characters of this match any of the known commands.
  931. * e.g., md1c20 should match md.
  932. */
  933. if (i == kdb_max_commands) {
  934. for_each_kdbcmd(tp, i) {
  935. if (tp->cmd_name) {
  936. if (strncmp(argv[0],
  937. tp->cmd_name,
  938. strlen(tp->cmd_name)) == 0) {
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. if (i < kdb_max_commands) {
  945. int result;
  946. if (!kdb_check_flags(tp->cmd_flags, kdb_cmd_enabled, argc <= 1))
  947. return KDB_NOPERM;
  948. KDB_STATE_SET(CMD);
  949. result = (*tp->cmd_func)(argc-1, (const char **)argv);
  950. if (result && ignore_errors && result > KDB_CMD_GO)
  951. result = 0;
  952. KDB_STATE_CLEAR(CMD);
  953. if (tp->cmd_flags & KDB_REPEAT_WITH_ARGS)
  954. return result;
  955. argc = tp->cmd_flags & KDB_REPEAT_NO_ARGS ? 1 : 0;
  956. if (argv[argc])
  957. *(argv[argc]) = '\0';
  958. return result;
  959. }
  960. /*
  961. * If the input with which we were presented does not
  962. * map to an existing command, attempt to parse it as an
  963. * address argument and display the result. Useful for
  964. * obtaining the address of a variable, or the nearest symbol
  965. * to an address contained in a register.
  966. */
  967. {
  968. unsigned long value;
  969. char *name = NULL;
  970. long offset;
  971. int nextarg = 0;
  972. if (kdbgetaddrarg(0, (const char **)argv, &nextarg,
  973. &value, &offset, &name)) {
  974. return KDB_NOTFOUND;
  975. }
  976. kdb_printf("%s = ", argv[0]);
  977. kdb_symbol_print(value, NULL, KDB_SP_DEFAULT);
  978. kdb_printf("\n");
  979. return 0;
  980. }
  981. }
  982. static int handle_ctrl_cmd(char *cmd)
  983. {
  984. #define CTRL_P 16
  985. #define CTRL_N 14
  986. /* initial situation */
  987. if (cmd_head == cmd_tail)
  988. return 0;
  989. switch (*cmd) {
  990. case CTRL_P:
  991. if (cmdptr != cmd_tail)
  992. cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
  993. strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
  994. return 1;
  995. case CTRL_N:
  996. if (cmdptr != cmd_head)
  997. cmdptr = (cmdptr+1) % KDB_CMD_HISTORY_COUNT;
  998. strncpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
  999. return 1;
  1000. }
  1001. return 0;
  1002. }
  1003. /*
  1004. * kdb_reboot - This function implements the 'reboot' command. Reboot
  1005. * the system immediately, or loop for ever on failure.
  1006. */
  1007. static int kdb_reboot(int argc, const char **argv)
  1008. {
  1009. emergency_restart();
  1010. kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n");
  1011. while (1)
  1012. cpu_relax();
  1013. /* NOTREACHED */
  1014. return 0;
  1015. }
  1016. static void kdb_dumpregs(struct pt_regs *regs)
  1017. {
  1018. int old_lvl = console_loglevel;
  1019. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  1020. kdb_trap_printk++;
  1021. show_regs(regs);
  1022. kdb_trap_printk--;
  1023. kdb_printf("\n");
  1024. console_loglevel = old_lvl;
  1025. }
  1026. void kdb_set_current_task(struct task_struct *p)
  1027. {
  1028. kdb_current_task = p;
  1029. if (kdb_task_has_cpu(p)) {
  1030. kdb_current_regs = KDB_TSKREGS(kdb_process_cpu(p));
  1031. return;
  1032. }
  1033. kdb_current_regs = NULL;
  1034. }
  1035. /*
  1036. * kdb_local - The main code for kdb. This routine is invoked on a
  1037. * specific processor, it is not global. The main kdb() routine
  1038. * ensures that only one processor at a time is in this routine.
  1039. * This code is called with the real reason code on the first
  1040. * entry to a kdb session, thereafter it is called with reason
  1041. * SWITCH, even if the user goes back to the original cpu.
  1042. * Inputs:
  1043. * reason The reason KDB was invoked
  1044. * error The hardware-defined error code
  1045. * regs The exception frame at time of fault/breakpoint.
  1046. * db_result Result code from the break or debug point.
  1047. * Returns:
  1048. * 0 KDB was invoked for an event which it wasn't responsible
  1049. * 1 KDB handled the event for which it was invoked.
  1050. * KDB_CMD_GO User typed 'go'.
  1051. * KDB_CMD_CPU User switched to another cpu.
  1052. * KDB_CMD_SS Single step.
  1053. */
  1054. static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
  1055. kdb_dbtrap_t db_result)
  1056. {
  1057. char *cmdbuf;
  1058. int diag;
  1059. struct task_struct *kdb_current =
  1060. kdb_curr_task(raw_smp_processor_id());
  1061. KDB_DEBUG_STATE("kdb_local 1", reason);
  1062. kdb_go_count = 0;
  1063. if (reason == KDB_REASON_DEBUG) {
  1064. /* special case below */
  1065. } else {
  1066. kdb_printf("\nEntering kdb (current=0x%p, pid %d) ",
  1067. kdb_current, kdb_current ? kdb_current->pid : 0);
  1068. #if defined(CONFIG_SMP)
  1069. kdb_printf("on processor %d ", raw_smp_processor_id());
  1070. #endif
  1071. }
  1072. switch (reason) {
  1073. case KDB_REASON_DEBUG:
  1074. {
  1075. /*
  1076. * If re-entering kdb after a single step
  1077. * command, don't print the message.
  1078. */
  1079. switch (db_result) {
  1080. case KDB_DB_BPT:
  1081. kdb_printf("\nEntering kdb (0x%p, pid %d) ",
  1082. kdb_current, kdb_current->pid);
  1083. #if defined(CONFIG_SMP)
  1084. kdb_printf("on processor %d ", raw_smp_processor_id());
  1085. #endif
  1086. kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
  1087. instruction_pointer(regs));
  1088. break;
  1089. case KDB_DB_SS:
  1090. break;
  1091. case KDB_DB_SSBPT:
  1092. KDB_DEBUG_STATE("kdb_local 4", reason);
  1093. return 1; /* kdba_db_trap did the work */
  1094. default:
  1095. kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
  1096. db_result);
  1097. break;
  1098. }
  1099. }
  1100. break;
  1101. case KDB_REASON_ENTER:
  1102. if (KDB_STATE(KEYBOARD))
  1103. kdb_printf("due to Keyboard Entry\n");
  1104. else
  1105. kdb_printf("due to KDB_ENTER()\n");
  1106. break;
  1107. case KDB_REASON_KEYBOARD:
  1108. KDB_STATE_SET(KEYBOARD);
  1109. kdb_printf("due to Keyboard Entry\n");
  1110. break;
  1111. case KDB_REASON_ENTER_SLAVE:
  1112. /* drop through, slaves only get released via cpu switch */
  1113. case KDB_REASON_SWITCH:
  1114. kdb_printf("due to cpu switch\n");
  1115. break;
  1116. case KDB_REASON_OOPS:
  1117. kdb_printf("Oops: %s\n", kdb_diemsg);
  1118. kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
  1119. instruction_pointer(regs));
  1120. kdb_dumpregs(regs);
  1121. break;
  1122. case KDB_REASON_SYSTEM_NMI:
  1123. kdb_printf("due to System NonMaskable Interrupt\n");
  1124. break;
  1125. case KDB_REASON_NMI:
  1126. kdb_printf("due to NonMaskable Interrupt @ "
  1127. kdb_machreg_fmt "\n",
  1128. instruction_pointer(regs));
  1129. break;
  1130. case KDB_REASON_SSTEP:
  1131. case KDB_REASON_BREAK:
  1132. kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
  1133. reason == KDB_REASON_BREAK ?
  1134. "Breakpoint" : "SS trap", instruction_pointer(regs));
  1135. /*
  1136. * Determine if this breakpoint is one that we
  1137. * are interested in.
  1138. */
  1139. if (db_result != KDB_DB_BPT) {
  1140. kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
  1141. db_result);
  1142. KDB_DEBUG_STATE("kdb_local 6", reason);
  1143. return 0; /* Not for us, dismiss it */
  1144. }
  1145. break;
  1146. case KDB_REASON_RECURSE:
  1147. kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
  1148. instruction_pointer(regs));
  1149. break;
  1150. default:
  1151. kdb_printf("kdb: unexpected reason code: %d\n", reason);
  1152. KDB_DEBUG_STATE("kdb_local 8", reason);
  1153. return 0; /* Not for us, dismiss it */
  1154. }
  1155. while (1) {
  1156. /*
  1157. * Initialize pager context.
  1158. */
  1159. kdb_nextline = 1;
  1160. KDB_STATE_CLEAR(SUPPRESS);
  1161. kdb_grepping_flag = 0;
  1162. /* ensure the old search does not leak into '/' commands */
  1163. kdb_grep_string[0] = '\0';
  1164. cmdbuf = cmd_cur;
  1165. *cmdbuf = '\0';
  1166. *(cmd_hist[cmd_head]) = '\0';
  1167. do_full_getstr:
  1168. #if defined(CONFIG_SMP)
  1169. snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
  1170. raw_smp_processor_id());
  1171. #else
  1172. snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"));
  1173. #endif
  1174. if (defcmd_in_progress)
  1175. strncat(kdb_prompt_str, "[defcmd]", CMD_BUFLEN);
  1176. /*
  1177. * Fetch command from keyboard
  1178. */
  1179. cmdbuf = kdb_getstr(cmdbuf, CMD_BUFLEN, kdb_prompt_str);
  1180. if (*cmdbuf != '\n') {
  1181. if (*cmdbuf < 32) {
  1182. if (cmdptr == cmd_head) {
  1183. strncpy(cmd_hist[cmd_head], cmd_cur,
  1184. CMD_BUFLEN);
  1185. *(cmd_hist[cmd_head] +
  1186. strlen(cmd_hist[cmd_head])-1) = '\0';
  1187. }
  1188. if (!handle_ctrl_cmd(cmdbuf))
  1189. *(cmd_cur+strlen(cmd_cur)-1) = '\0';
  1190. cmdbuf = cmd_cur;
  1191. goto do_full_getstr;
  1192. } else {
  1193. strncpy(cmd_hist[cmd_head], cmd_cur,
  1194. CMD_BUFLEN);
  1195. }
  1196. cmd_head = (cmd_head+1) % KDB_CMD_HISTORY_COUNT;
  1197. if (cmd_head == cmd_tail)
  1198. cmd_tail = (cmd_tail+1) % KDB_CMD_HISTORY_COUNT;
  1199. }
  1200. cmdptr = cmd_head;
  1201. diag = kdb_parse(cmdbuf);
  1202. if (diag == KDB_NOTFOUND) {
  1203. kdb_printf("Unknown kdb command: '%s'\n", cmdbuf);
  1204. diag = 0;
  1205. }
  1206. if (diag == KDB_CMD_GO
  1207. || diag == KDB_CMD_CPU
  1208. || diag == KDB_CMD_SS
  1209. || diag == KDB_CMD_KGDB)
  1210. break;
  1211. if (diag)
  1212. kdb_cmderror(diag);
  1213. }
  1214. KDB_DEBUG_STATE("kdb_local 9", diag);
  1215. return diag;
  1216. }
  1217. /*
  1218. * kdb_print_state - Print the state data for the current processor
  1219. * for debugging.
  1220. * Inputs:
  1221. * text Identifies the debug point
  1222. * value Any integer value to be printed, e.g. reason code.
  1223. */
  1224. void kdb_print_state(const char *text, int value)
  1225. {
  1226. kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
  1227. text, raw_smp_processor_id(), value, kdb_initial_cpu,
  1228. kdb_state);
  1229. }
  1230. /*
  1231. * kdb_main_loop - After initial setup and assignment of the
  1232. * controlling cpu, all cpus are in this loop. One cpu is in
  1233. * control and will issue the kdb prompt, the others will spin
  1234. * until 'go' or cpu switch.
  1235. *
  1236. * To get a consistent view of the kernel stacks for all
  1237. * processes, this routine is invoked from the main kdb code via
  1238. * an architecture specific routine. kdba_main_loop is
  1239. * responsible for making the kernel stacks consistent for all
  1240. * processes, there should be no difference between a blocked
  1241. * process and a running process as far as kdb is concerned.
  1242. * Inputs:
  1243. * reason The reason KDB was invoked
  1244. * error The hardware-defined error code
  1245. * reason2 kdb's current reason code.
  1246. * Initially error but can change
  1247. * according to kdb state.
  1248. * db_result Result code from break or debug point.
  1249. * regs The exception frame at time of fault/breakpoint.
  1250. * should always be valid.
  1251. * Returns:
  1252. * 0 KDB was invoked for an event which it wasn't responsible
  1253. * 1 KDB handled the event for which it was invoked.
  1254. */
  1255. int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
  1256. kdb_dbtrap_t db_result, struct pt_regs *regs)
  1257. {
  1258. int result = 1;
  1259. /* Stay in kdb() until 'go', 'ss[b]' or an error */
  1260. while (1) {
  1261. /*
  1262. * All processors except the one that is in control
  1263. * will spin here.
  1264. */
  1265. KDB_DEBUG_STATE("kdb_main_loop 1", reason);
  1266. while (KDB_STATE(HOLD_CPU)) {
  1267. /* state KDB is turned off by kdb_cpu to see if the
  1268. * other cpus are still live, each cpu in this loop
  1269. * turns it back on.
  1270. */
  1271. if (!KDB_STATE(KDB))
  1272. KDB_STATE_SET(KDB);
  1273. }
  1274. KDB_STATE_CLEAR(SUPPRESS);
  1275. KDB_DEBUG_STATE("kdb_main_loop 2", reason);
  1276. if (KDB_STATE(LEAVING))
  1277. break; /* Another cpu said 'go' */
  1278. /* Still using kdb, this processor is in control */
  1279. result = kdb_local(reason2, error, regs, db_result);
  1280. KDB_DEBUG_STATE("kdb_main_loop 3", result);
  1281. if (result == KDB_CMD_CPU)
  1282. break;
  1283. if (result == KDB_CMD_SS) {
  1284. KDB_STATE_SET(DOING_SS);
  1285. break;
  1286. }
  1287. if (result == KDB_CMD_KGDB) {
  1288. if (!KDB_STATE(DOING_KGDB))
  1289. kdb_printf("Entering please attach debugger "
  1290. "or use $D#44+ or $3#33\n");
  1291. break;
  1292. }
  1293. if (result && result != 1 && result != KDB_CMD_GO)
  1294. kdb_printf("\nUnexpected kdb_local return code %d\n",
  1295. result);
  1296. KDB_DEBUG_STATE("kdb_main_loop 4", reason);
  1297. break;
  1298. }
  1299. if (KDB_STATE(DOING_SS))
  1300. KDB_STATE_CLEAR(SSBPT);
  1301. /* Clean up any keyboard devices before leaving */
  1302. kdb_kbd_cleanup_state();
  1303. return result;
  1304. }
  1305. /*
  1306. * kdb_mdr - This function implements the guts of the 'mdr', memory
  1307. * read command.
  1308. * mdr <addr arg>,<byte count>
  1309. * Inputs:
  1310. * addr Start address
  1311. * count Number of bytes
  1312. * Returns:
  1313. * Always 0. Any errors are detected and printed by kdb_getarea.
  1314. */
  1315. static int kdb_mdr(unsigned long addr, unsigned int count)
  1316. {
  1317. unsigned char c;
  1318. while (count--) {
  1319. if (kdb_getarea(c, addr))
  1320. return 0;
  1321. kdb_printf("%02x", c);
  1322. addr++;
  1323. }
  1324. kdb_printf("\n");
  1325. return 0;
  1326. }
  1327. /*
  1328. * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4',
  1329. * 'md8' 'mdr' and 'mds' commands.
  1330. *
  1331. * md|mds [<addr arg> [<line count> [<radix>]]]
  1332. * mdWcN [<addr arg> [<line count> [<radix>]]]
  1333. * where W = is the width (1, 2, 4 or 8) and N is the count.
  1334. * for eg., md1c20 reads 20 bytes, 1 at a time.
  1335. * mdr <addr arg>,<byte count>
  1336. */
  1337. static void kdb_md_line(const char *fmtstr, unsigned long addr,
  1338. int symbolic, int nosect, int bytesperword,
  1339. int num, int repeat, int phys)
  1340. {
  1341. /* print just one line of data */
  1342. kdb_symtab_t symtab;
  1343. char cbuf[32];
  1344. char *c = cbuf;
  1345. int i;
  1346. unsigned long word;
  1347. memset(cbuf, '\0', sizeof(cbuf));
  1348. if (phys)
  1349. kdb_printf("phys " kdb_machreg_fmt0 " ", addr);
  1350. else
  1351. kdb_printf(kdb_machreg_fmt0 " ", addr);
  1352. for (i = 0; i < num && repeat--; i++) {
  1353. if (phys) {
  1354. if (kdb_getphysword(&word, addr, bytesperword))
  1355. break;
  1356. } else if (kdb_getword(&word, addr, bytesperword))
  1357. break;
  1358. kdb_printf(fmtstr, word);
  1359. if (symbolic)
  1360. kdbnearsym(word, &symtab);
  1361. else
  1362. memset(&symtab, 0, sizeof(symtab));
  1363. if (symtab.sym_name) {
  1364. kdb_symbol_print(word, &symtab, 0);
  1365. if (!nosect) {
  1366. kdb_printf("\n");
  1367. kdb_printf(" %s %s "
  1368. kdb_machreg_fmt " "
  1369. kdb_machreg_fmt " "
  1370. kdb_machreg_fmt, symtab.mod_name,
  1371. symtab.sec_name, symtab.sec_start,
  1372. symtab.sym_start, symtab.sym_end);
  1373. }
  1374. addr += bytesperword;
  1375. } else {
  1376. union {
  1377. u64 word;
  1378. unsigned char c[8];
  1379. } wc;
  1380. unsigned char *cp;
  1381. #ifdef __BIG_ENDIAN
  1382. cp = wc.c + 8 - bytesperword;
  1383. #else
  1384. cp = wc.c;
  1385. #endif
  1386. wc.word = word;
  1387. #define printable_char(c) \
  1388. ({unsigned char __c = c; isascii(__c) && isprint(__c) ? __c : '.'; })
  1389. switch (bytesperword) {
  1390. case 8:
  1391. *c++ = printable_char(*cp++);
  1392. *c++ = printable_char(*cp++);
  1393. *c++ = printable_char(*cp++);
  1394. *c++ = printable_char(*cp++);
  1395. addr += 4;
  1396. case 4:
  1397. *c++ = printable_char(*cp++);
  1398. *c++ = printable_char(*cp++);
  1399. addr += 2;
  1400. case 2:
  1401. *c++ = printable_char(*cp++);
  1402. addr++;
  1403. case 1:
  1404. *c++ = printable_char(*cp++);
  1405. addr++;
  1406. break;
  1407. }
  1408. #undef printable_char
  1409. }
  1410. }
  1411. kdb_printf("%*s %s\n", (int)((num-i)*(2*bytesperword + 1)+1),
  1412. " ", cbuf);
  1413. }
  1414. static int kdb_md(int argc, const char **argv)
  1415. {
  1416. static unsigned long last_addr;
  1417. static int last_radix, last_bytesperword, last_repeat;
  1418. int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat;
  1419. int nosect = 0;
  1420. char fmtchar, fmtstr[64];
  1421. unsigned long addr;
  1422. unsigned long word;
  1423. long offset = 0;
  1424. int symbolic = 0;
  1425. int valid = 0;
  1426. int phys = 0;
  1427. int raw = 0;
  1428. kdbgetintenv("MDCOUNT", &mdcount);
  1429. kdbgetintenv("RADIX", &radix);
  1430. kdbgetintenv("BYTESPERWORD", &bytesperword);
  1431. /* Assume 'md <addr>' and start with environment values */
  1432. repeat = mdcount * 16 / bytesperword;
  1433. if (strcmp(argv[0], "mdr") == 0) {
  1434. if (argc == 2 || (argc == 0 && last_addr != 0))
  1435. valid = raw = 1;
  1436. else
  1437. return KDB_ARGCOUNT;
  1438. } else if (isdigit(argv[0][2])) {
  1439. bytesperword = (int)(argv[0][2] - '0');
  1440. if (bytesperword == 0) {
  1441. bytesperword = last_bytesperword;
  1442. if (bytesperword == 0)
  1443. bytesperword = 4;
  1444. }
  1445. last_bytesperword = bytesperword;
  1446. repeat = mdcount * 16 / bytesperword;
  1447. if (!argv[0][3])
  1448. valid = 1;
  1449. else if (argv[0][3] == 'c' && argv[0][4]) {
  1450. char *p;
  1451. repeat = simple_strtoul(argv[0] + 4, &p, 10);
  1452. mdcount = ((repeat * bytesperword) + 15) / 16;
  1453. valid = !*p;
  1454. }
  1455. last_repeat = repeat;
  1456. } else if (strcmp(argv[0], "md") == 0)
  1457. valid = 1;
  1458. else if (strcmp(argv[0], "mds") == 0)
  1459. valid = 1;
  1460. else if (strcmp(argv[0], "mdp") == 0) {
  1461. phys = valid = 1;
  1462. }
  1463. if (!valid)
  1464. return KDB_NOTFOUND;
  1465. if (argc == 0) {
  1466. if (last_addr == 0)
  1467. return KDB_ARGCOUNT;
  1468. addr = last_addr;
  1469. radix = last_radix;
  1470. bytesperword = last_bytesperword;
  1471. repeat = last_repeat;
  1472. if (raw)
  1473. mdcount = repeat;
  1474. else
  1475. mdcount = ((repeat * bytesperword) + 15) / 16;
  1476. }
  1477. if (argc) {
  1478. unsigned long val;
  1479. int diag, nextarg = 1;
  1480. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
  1481. &offset, NULL);
  1482. if (diag)
  1483. return diag;
  1484. if (argc > nextarg+2)
  1485. return KDB_ARGCOUNT;
  1486. if (argc >= nextarg) {
  1487. diag = kdbgetularg(argv[nextarg], &val);
  1488. if (!diag) {
  1489. mdcount = (int) val;
  1490. if (raw)
  1491. repeat = mdcount;
  1492. else
  1493. repeat = mdcount * 16 / bytesperword;
  1494. }
  1495. }
  1496. if (argc >= nextarg+1) {
  1497. diag = kdbgetularg(argv[nextarg+1], &val);
  1498. if (!diag)
  1499. radix = (int) val;
  1500. }
  1501. }
  1502. if (strcmp(argv[0], "mdr") == 0) {
  1503. int ret;
  1504. last_addr = addr;
  1505. ret = kdb_mdr(addr, mdcount);
  1506. last_addr += mdcount;
  1507. last_repeat = mdcount;
  1508. last_bytesperword = bytesperword; // to make REPEAT happy
  1509. return ret;
  1510. }
  1511. switch (radix) {
  1512. case 10:
  1513. fmtchar = 'd';
  1514. break;
  1515. case 16:
  1516. fmtchar = 'x';
  1517. break;
  1518. case 8:
  1519. fmtchar = 'o';
  1520. break;
  1521. default:
  1522. return KDB_BADRADIX;
  1523. }
  1524. last_radix = radix;
  1525. if (bytesperword > KDB_WORD_SIZE)
  1526. return KDB_BADWIDTH;
  1527. switch (bytesperword) {
  1528. case 8:
  1529. sprintf(fmtstr, "%%16.16l%c ", fmtchar);
  1530. break;
  1531. case 4:
  1532. sprintf(fmtstr, "%%8.8l%c ", fmtchar);
  1533. break;
  1534. case 2:
  1535. sprintf(fmtstr, "%%4.4l%c ", fmtchar);
  1536. break;
  1537. case 1:
  1538. sprintf(fmtstr, "%%2.2l%c ", fmtchar);
  1539. break;
  1540. default:
  1541. return KDB_BADWIDTH;
  1542. }
  1543. last_repeat = repeat;
  1544. last_bytesperword = bytesperword;
  1545. if (strcmp(argv[0], "mds") == 0) {
  1546. symbolic = 1;
  1547. /* Do not save these changes as last_*, they are temporary mds
  1548. * overrides.
  1549. */
  1550. bytesperword = KDB_WORD_SIZE;
  1551. repeat = mdcount;
  1552. kdbgetintenv("NOSECT", &nosect);
  1553. }
  1554. /* Round address down modulo BYTESPERWORD */
  1555. addr &= ~(bytesperword-1);
  1556. while (repeat > 0) {
  1557. unsigned long a;
  1558. int n, z, num = (symbolic ? 1 : (16 / bytesperword));
  1559. if (KDB_FLAG(CMD_INTERRUPT))
  1560. return 0;
  1561. for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) {
  1562. if (phys) {
  1563. if (kdb_getphysword(&word, a, bytesperword)
  1564. || word)
  1565. break;
  1566. } else if (kdb_getword(&word, a, bytesperword) || word)
  1567. break;
  1568. }
  1569. n = min(num, repeat);
  1570. kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword,
  1571. num, repeat, phys);
  1572. addr += bytesperword * n;
  1573. repeat -= n;
  1574. z = (z + num - 1) / num;
  1575. if (z > 2) {
  1576. int s = num * (z-2);
  1577. kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0
  1578. " zero suppressed\n",
  1579. addr, addr + bytesperword * s - 1);
  1580. addr += bytesperword * s;
  1581. repeat -= s;
  1582. }
  1583. }
  1584. last_addr = addr;
  1585. return 0;
  1586. }
  1587. /*
  1588. * kdb_mm - This function implements the 'mm' command.
  1589. * mm address-expression new-value
  1590. * Remarks:
  1591. * mm works on machine words, mmW works on bytes.
  1592. */
  1593. static int kdb_mm(int argc, const char **argv)
  1594. {
  1595. int diag;
  1596. unsigned long addr;
  1597. long offset = 0;
  1598. unsigned long contents;
  1599. int nextarg;
  1600. int width;
  1601. if (argv[0][2] && !isdigit(argv[0][2]))
  1602. return KDB_NOTFOUND;
  1603. if (argc < 2)
  1604. return KDB_ARGCOUNT;
  1605. nextarg = 1;
  1606. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
  1607. if (diag)
  1608. return diag;
  1609. if (nextarg > argc)
  1610. return KDB_ARGCOUNT;
  1611. diag = kdbgetaddrarg(argc, argv, &nextarg, &contents, NULL, NULL);
  1612. if (diag)
  1613. return diag;
  1614. if (nextarg != argc + 1)
  1615. return KDB_ARGCOUNT;
  1616. width = argv[0][2] ? (argv[0][2] - '0') : (KDB_WORD_SIZE);
  1617. diag = kdb_putword(addr, contents, width);
  1618. if (diag)
  1619. return diag;
  1620. kdb_printf(kdb_machreg_fmt " = " kdb_machreg_fmt "\n", addr, contents);
  1621. return 0;
  1622. }
  1623. /*
  1624. * kdb_go - This function implements the 'go' command.
  1625. * go [address-expression]
  1626. */
  1627. static int kdb_go(int argc, const char **argv)
  1628. {
  1629. unsigned long addr;
  1630. int diag;
  1631. int nextarg;
  1632. long offset;
  1633. if (raw_smp_processor_id() != kdb_initial_cpu) {
  1634. kdb_printf("go must execute on the entry cpu, "
  1635. "please use \"cpu %d\" and then execute go\n",
  1636. kdb_initial_cpu);
  1637. return KDB_BADCPUNUM;
  1638. }
  1639. if (argc == 1) {
  1640. nextarg = 1;
  1641. diag = kdbgetaddrarg(argc, argv, &nextarg,
  1642. &addr, &offset, NULL);
  1643. if (diag)
  1644. return diag;
  1645. } else if (argc) {
  1646. return KDB_ARGCOUNT;
  1647. }
  1648. diag = KDB_CMD_GO;
  1649. if (KDB_FLAG(CATASTROPHIC)) {
  1650. kdb_printf("Catastrophic error detected\n");
  1651. kdb_printf("kdb_continue_catastrophic=%d, ",
  1652. kdb_continue_catastrophic);
  1653. if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
  1654. kdb_printf("type go a second time if you really want "
  1655. "to continue\n");
  1656. return 0;
  1657. }
  1658. if (kdb_continue_catastrophic == 2) {
  1659. kdb_printf("forcing reboot\n");
  1660. kdb_reboot(0, NULL);
  1661. }
  1662. kdb_printf("attempting to continue\n");
  1663. }
  1664. return diag;
  1665. }
  1666. /*
  1667. * kdb_rd - This function implements the 'rd' command.
  1668. */
  1669. static int kdb_rd(int argc, const char **argv)
  1670. {
  1671. int len = kdb_check_regs();
  1672. #if DBG_MAX_REG_NUM > 0
  1673. int i;
  1674. char *rname;
  1675. int rsize;
  1676. u64 reg64;
  1677. u32 reg32;
  1678. u16 reg16;
  1679. u8 reg8;
  1680. if (len)
  1681. return len;
  1682. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  1683. rsize = dbg_reg_def[i].size * 2;
  1684. if (rsize > 16)
  1685. rsize = 2;
  1686. if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) {
  1687. len = 0;
  1688. kdb_printf("\n");
  1689. }
  1690. if (len)
  1691. len += kdb_printf(" ");
  1692. switch(dbg_reg_def[i].size * 8) {
  1693. case 8:
  1694. rname = dbg_get_reg(i, &reg8, kdb_current_regs);
  1695. if (!rname)
  1696. break;
  1697. len += kdb_printf("%s: %02x", rname, reg8);
  1698. break;
  1699. case 16:
  1700. rname = dbg_get_reg(i, &reg16, kdb_current_regs);
  1701. if (!rname)
  1702. break;
  1703. len += kdb_printf("%s: %04x", rname, reg16);
  1704. break;
  1705. case 32:
  1706. rname = dbg_get_reg(i, &reg32, kdb_current_regs);
  1707. if (!rname)
  1708. break;
  1709. len += kdb_printf("%s: %08x", rname, reg32);
  1710. break;
  1711. case 64:
  1712. rname = dbg_get_reg(i, &reg64, kdb_current_regs);
  1713. if (!rname)
  1714. break;
  1715. len += kdb_printf("%s: %016llx", rname, reg64);
  1716. break;
  1717. default:
  1718. len += kdb_printf("%s: ??", dbg_reg_def[i].name);
  1719. }
  1720. }
  1721. kdb_printf("\n");
  1722. #else
  1723. if (len)
  1724. return len;
  1725. kdb_dumpregs(kdb_current_regs);
  1726. #endif
  1727. return 0;
  1728. }
  1729. /*
  1730. * kdb_rm - This function implements the 'rm' (register modify) command.
  1731. * rm register-name new-contents
  1732. * Remarks:
  1733. * Allows register modification with the same restrictions as gdb
  1734. */
  1735. static int kdb_rm(int argc, const char **argv)
  1736. {
  1737. #if DBG_MAX_REG_NUM > 0
  1738. int diag;
  1739. const char *rname;
  1740. int i;
  1741. u64 reg64;
  1742. u32 reg32;
  1743. u16 reg16;
  1744. u8 reg8;
  1745. if (argc != 2)
  1746. return KDB_ARGCOUNT;
  1747. /*
  1748. * Allow presence or absence of leading '%' symbol.
  1749. */
  1750. rname = argv[1];
  1751. if (*rname == '%')
  1752. rname++;
  1753. diag = kdbgetu64arg(argv[2], &reg64);
  1754. if (diag)
  1755. return diag;
  1756. diag = kdb_check_regs();
  1757. if (diag)
  1758. return diag;
  1759. diag = KDB_BADREG;
  1760. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  1761. if (strcmp(rname, dbg_reg_def[i].name) == 0) {
  1762. diag = 0;
  1763. break;
  1764. }
  1765. }
  1766. if (!diag) {
  1767. switch(dbg_reg_def[i].size * 8) {
  1768. case 8:
  1769. reg8 = reg64;
  1770. dbg_set_reg(i, &reg8, kdb_current_regs);
  1771. break;
  1772. case 16:
  1773. reg16 = reg64;
  1774. dbg_set_reg(i, &reg16, kdb_current_regs);
  1775. break;
  1776. case 32:
  1777. reg32 = reg64;
  1778. dbg_set_reg(i, &reg32, kdb_current_regs);
  1779. break;
  1780. case 64:
  1781. dbg_set_reg(i, &reg64, kdb_current_regs);
  1782. break;
  1783. }
  1784. }
  1785. return diag;
  1786. #else
  1787. kdb_printf("ERROR: Register set currently not implemented\n");
  1788. return 0;
  1789. #endif
  1790. }
  1791. #if defined(CONFIG_MAGIC_SYSRQ)
  1792. /*
  1793. * kdb_sr - This function implements the 'sr' (SYSRQ key) command
  1794. * which interfaces to the soi-disant MAGIC SYSRQ functionality.
  1795. * sr <magic-sysrq-code>
  1796. */
  1797. static int kdb_sr(int argc, const char **argv)
  1798. {
  1799. bool check_mask =
  1800. !kdb_check_flags(KDB_ENABLE_ALL, kdb_cmd_enabled, false);
  1801. if (argc != 1)
  1802. return KDB_ARGCOUNT;
  1803. kdb_trap_printk++;
  1804. __handle_sysrq(*argv[1], check_mask);
  1805. kdb_trap_printk--;
  1806. return 0;
  1807. }
  1808. #endif /* CONFIG_MAGIC_SYSRQ */
  1809. /*
  1810. * kdb_ef - This function implements the 'regs' (display exception
  1811. * frame) command. This command takes an address and expects to
  1812. * find an exception frame at that address, formats and prints
  1813. * it.
  1814. * regs address-expression
  1815. * Remarks:
  1816. * Not done yet.
  1817. */
  1818. static int kdb_ef(int argc, const char **argv)
  1819. {
  1820. int diag;
  1821. unsigned long addr;
  1822. long offset;
  1823. int nextarg;
  1824. if (argc != 1)
  1825. return KDB_ARGCOUNT;
  1826. nextarg = 1;
  1827. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, NULL);
  1828. if (diag)
  1829. return diag;
  1830. show_regs((struct pt_regs *)addr);
  1831. return 0;
  1832. }
  1833. #if defined(CONFIG_MODULES)
  1834. /*
  1835. * kdb_lsmod - This function implements the 'lsmod' command. Lists
  1836. * currently loaded kernel modules.
  1837. * Mostly taken from userland lsmod.
  1838. */
  1839. static int kdb_lsmod(int argc, const char **argv)
  1840. {
  1841. struct module *mod;
  1842. if (argc != 0)
  1843. return KDB_ARGCOUNT;
  1844. kdb_printf("Module Size modstruct Used by\n");
  1845. list_for_each_entry(mod, kdb_modules, list) {
  1846. if (mod->state == MODULE_STATE_UNFORMED)
  1847. continue;
  1848. kdb_printf("%-20s%8u 0x%p ", mod->name,
  1849. mod->core_layout.size, (void *)mod);
  1850. #ifdef CONFIG_MODULE_UNLOAD
  1851. kdb_printf("%4d ", module_refcount(mod));
  1852. #endif
  1853. if (mod->state == MODULE_STATE_GOING)
  1854. kdb_printf(" (Unloading)");
  1855. else if (mod->state == MODULE_STATE_COMING)
  1856. kdb_printf(" (Loading)");
  1857. else
  1858. kdb_printf(" (Live)");
  1859. kdb_printf(" 0x%p", mod->core_layout.base);
  1860. #ifdef CONFIG_MODULE_UNLOAD
  1861. {
  1862. struct module_use *use;
  1863. kdb_printf(" [ ");
  1864. list_for_each_entry(use, &mod->source_list,
  1865. source_list)
  1866. kdb_printf("%s ", use->target->name);
  1867. kdb_printf("]\n");
  1868. }
  1869. #endif
  1870. }
  1871. return 0;
  1872. }
  1873. #endif /* CONFIG_MODULES */
  1874. /*
  1875. * kdb_env - This function implements the 'env' command. Display the
  1876. * current environment variables.
  1877. */
  1878. static int kdb_env(int argc, const char **argv)
  1879. {
  1880. int i;
  1881. for (i = 0; i < __nenv; i++) {
  1882. if (__env[i])
  1883. kdb_printf("%s\n", __env[i]);
  1884. }
  1885. if (KDB_DEBUG(MASK))
  1886. kdb_printf("KDBFLAGS=0x%x\n", kdb_flags);
  1887. return 0;
  1888. }
  1889. #ifdef CONFIG_PRINTK
  1890. /*
  1891. * kdb_dmesg - This function implements the 'dmesg' command to display
  1892. * the contents of the syslog buffer.
  1893. * dmesg [lines] [adjust]
  1894. */
  1895. static int kdb_dmesg(int argc, const char **argv)
  1896. {
  1897. int diag;
  1898. int logging;
  1899. int lines = 0;
  1900. int adjust = 0;
  1901. int n = 0;
  1902. int skip = 0;
  1903. struct kmsg_dumper dumper = { .active = 1 };
  1904. size_t len;
  1905. char buf[201];
  1906. if (argc > 2)
  1907. return KDB_ARGCOUNT;
  1908. if (argc) {
  1909. char *cp;
  1910. lines = simple_strtol(argv[1], &cp, 0);
  1911. if (*cp)
  1912. lines = 0;
  1913. if (argc > 1) {
  1914. adjust = simple_strtoul(argv[2], &cp, 0);
  1915. if (*cp || adjust < 0)
  1916. adjust = 0;
  1917. }
  1918. }
  1919. /* disable LOGGING if set */
  1920. diag = kdbgetintenv("LOGGING", &logging);
  1921. if (!diag && logging) {
  1922. const char *setargs[] = { "set", "LOGGING", "0" };
  1923. kdb_set(2, setargs);
  1924. }
  1925. kmsg_dump_rewind_nolock(&dumper);
  1926. while (kmsg_dump_get_line_nolock(&dumper, 1, NULL, 0, NULL))
  1927. n++;
  1928. if (lines < 0) {
  1929. if (adjust >= n)
  1930. kdb_printf("buffer only contains %d lines, nothing "
  1931. "printed\n", n);
  1932. else if (adjust - lines >= n)
  1933. kdb_printf("buffer only contains %d lines, last %d "
  1934. "lines printed\n", n, n - adjust);
  1935. skip = adjust;
  1936. lines = abs(lines);
  1937. } else if (lines > 0) {
  1938. skip = n - lines - adjust;
  1939. lines = abs(lines);
  1940. if (adjust >= n) {
  1941. kdb_printf("buffer only contains %d lines, "
  1942. "nothing printed\n", n);
  1943. skip = n;
  1944. } else if (skip < 0) {
  1945. lines += skip;
  1946. skip = 0;
  1947. kdb_printf("buffer only contains %d lines, first "
  1948. "%d lines printed\n", n, lines);
  1949. }
  1950. } else {
  1951. lines = n;
  1952. }
  1953. if (skip >= n || skip < 0)
  1954. return 0;
  1955. kmsg_dump_rewind_nolock(&dumper);
  1956. while (kmsg_dump_get_line_nolock(&dumper, 1, buf, sizeof(buf), &len)) {
  1957. if (skip) {
  1958. skip--;
  1959. continue;
  1960. }
  1961. if (!lines--)
  1962. break;
  1963. if (KDB_FLAG(CMD_INTERRUPT))
  1964. return 0;
  1965. kdb_printf("%.*s\n", (int)len - 1, buf);
  1966. }
  1967. return 0;
  1968. }
  1969. #endif /* CONFIG_PRINTK */
  1970. /* Make sure we balance enable/disable calls, must disable first. */
  1971. static atomic_t kdb_nmi_disabled;
  1972. static int kdb_disable_nmi(int argc, const char *argv[])
  1973. {
  1974. if (atomic_read(&kdb_nmi_disabled))
  1975. return 0;
  1976. atomic_set(&kdb_nmi_disabled, 1);
  1977. arch_kgdb_ops.enable_nmi(0);
  1978. return 0;
  1979. }
  1980. static int kdb_param_enable_nmi(const char *val, const struct kernel_param *kp)
  1981. {
  1982. if (!atomic_add_unless(&kdb_nmi_disabled, -1, 0))
  1983. return -EINVAL;
  1984. arch_kgdb_ops.enable_nmi(1);
  1985. return 0;
  1986. }
  1987. static const struct kernel_param_ops kdb_param_ops_enable_nmi = {
  1988. .set = kdb_param_enable_nmi,
  1989. };
  1990. module_param_cb(enable_nmi, &kdb_param_ops_enable_nmi, NULL, 0600);
  1991. /*
  1992. * kdb_cpu - This function implements the 'cpu' command.
  1993. * cpu [<cpunum>]
  1994. * Returns:
  1995. * KDB_CMD_CPU for success, a kdb diagnostic if error
  1996. */
  1997. static void kdb_cpu_status(void)
  1998. {
  1999. int i, start_cpu, first_print = 1;
  2000. char state, prev_state = '?';
  2001. kdb_printf("Currently on cpu %d\n", raw_smp_processor_id());
  2002. kdb_printf("Available cpus: ");
  2003. for (start_cpu = -1, i = 0; i < NR_CPUS; i++) {
  2004. if (!cpu_online(i)) {
  2005. state = 'F'; /* cpu is offline */
  2006. } else if (!kgdb_info[i].enter_kgdb) {
  2007. state = 'D'; /* cpu is online but unresponsive */
  2008. } else {
  2009. state = ' '; /* cpu is responding to kdb */
  2010. if (kdb_task_state_char(KDB_TSK(i)) == 'I')
  2011. state = 'I'; /* idle task */
  2012. }
  2013. if (state != prev_state) {
  2014. if (prev_state != '?') {
  2015. if (!first_print)
  2016. kdb_printf(", ");
  2017. first_print = 0;
  2018. kdb_printf("%d", start_cpu);
  2019. if (start_cpu < i-1)
  2020. kdb_printf("-%d", i-1);
  2021. if (prev_state != ' ')
  2022. kdb_printf("(%c)", prev_state);
  2023. }
  2024. prev_state = state;
  2025. start_cpu = i;
  2026. }
  2027. }
  2028. /* print the trailing cpus, ignoring them if they are all offline */
  2029. if (prev_state != 'F') {
  2030. if (!first_print)
  2031. kdb_printf(", ");
  2032. kdb_printf("%d", start_cpu);
  2033. if (start_cpu < i-1)
  2034. kdb_printf("-%d", i-1);
  2035. if (prev_state != ' ')
  2036. kdb_printf("(%c)", prev_state);
  2037. }
  2038. kdb_printf("\n");
  2039. }
  2040. static int kdb_cpu(int argc, const char **argv)
  2041. {
  2042. unsigned long cpunum;
  2043. int diag;
  2044. if (argc == 0) {
  2045. kdb_cpu_status();
  2046. return 0;
  2047. }
  2048. if (argc != 1)
  2049. return KDB_ARGCOUNT;
  2050. diag = kdbgetularg(argv[1], &cpunum);
  2051. if (diag)
  2052. return diag;
  2053. /*
  2054. * Validate cpunum
  2055. */
  2056. if ((cpunum >= CONFIG_NR_CPUS) || !kgdb_info[cpunum].enter_kgdb)
  2057. return KDB_BADCPUNUM;
  2058. dbg_switch_cpu = cpunum;
  2059. /*
  2060. * Switch to other cpu
  2061. */
  2062. return KDB_CMD_CPU;
  2063. }
  2064. /* The user may not realize that ps/bta with no parameters does not print idle
  2065. * or sleeping system daemon processes, so tell them how many were suppressed.
  2066. */
  2067. void kdb_ps_suppressed(void)
  2068. {
  2069. int idle = 0, daemon = 0;
  2070. unsigned long mask_I = kdb_task_state_string("I"),
  2071. mask_M = kdb_task_state_string("M");
  2072. unsigned long cpu;
  2073. const struct task_struct *p, *g;
  2074. for_each_online_cpu(cpu) {
  2075. p = kdb_curr_task(cpu);
  2076. if (kdb_task_state(p, mask_I))
  2077. ++idle;
  2078. }
  2079. kdb_do_each_thread(g, p) {
  2080. if (kdb_task_state(p, mask_M))
  2081. ++daemon;
  2082. } kdb_while_each_thread(g, p);
  2083. if (idle || daemon) {
  2084. if (idle)
  2085. kdb_printf("%d idle process%s (state I)%s\n",
  2086. idle, idle == 1 ? "" : "es",
  2087. daemon ? " and " : "");
  2088. if (daemon)
  2089. kdb_printf("%d sleeping system daemon (state M) "
  2090. "process%s", daemon,
  2091. daemon == 1 ? "" : "es");
  2092. kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
  2093. }
  2094. }
  2095. /*
  2096. * kdb_ps - This function implements the 'ps' command which shows a
  2097. * list of the active processes.
  2098. * ps [DRSTCZEUIMA] All processes, optionally filtered by state
  2099. */
  2100. void kdb_ps1(const struct task_struct *p)
  2101. {
  2102. int cpu;
  2103. unsigned long tmp;
  2104. if (!p || probe_kernel_read(&tmp, (char *)p, sizeof(unsigned long)))
  2105. return;
  2106. cpu = kdb_process_cpu(p);
  2107. kdb_printf("0x%p %8d %8d %d %4d %c 0x%p %c%s\n",
  2108. (void *)p, p->pid, p->parent->pid,
  2109. kdb_task_has_cpu(p), kdb_process_cpu(p),
  2110. kdb_task_state_char(p),
  2111. (void *)(&p->thread),
  2112. p == kdb_curr_task(raw_smp_processor_id()) ? '*' : ' ',
  2113. p->comm);
  2114. if (kdb_task_has_cpu(p)) {
  2115. if (!KDB_TSK(cpu)) {
  2116. kdb_printf(" Error: no saved data for this cpu\n");
  2117. } else {
  2118. if (KDB_TSK(cpu) != p)
  2119. kdb_printf(" Error: does not match running "
  2120. "process table (0x%p)\n", KDB_TSK(cpu));
  2121. }
  2122. }
  2123. }
  2124. static int kdb_ps(int argc, const char **argv)
  2125. {
  2126. struct task_struct *g, *p;
  2127. unsigned long mask, cpu;
  2128. if (argc == 0)
  2129. kdb_ps_suppressed();
  2130. kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
  2131. (int)(2*sizeof(void *))+2, "Task Addr",
  2132. (int)(2*sizeof(void *))+2, "Thread");
  2133. mask = kdb_task_state_string(argc ? argv[1] : NULL);
  2134. /* Run the active tasks first */
  2135. for_each_online_cpu(cpu) {
  2136. if (KDB_FLAG(CMD_INTERRUPT))
  2137. return 0;
  2138. p = kdb_curr_task(cpu);
  2139. if (kdb_task_state(p, mask))
  2140. kdb_ps1(p);
  2141. }
  2142. kdb_printf("\n");
  2143. /* Now the real tasks */
  2144. kdb_do_each_thread(g, p) {
  2145. if (KDB_FLAG(CMD_INTERRUPT))
  2146. return 0;
  2147. if (kdb_task_state(p, mask))
  2148. kdb_ps1(p);
  2149. } kdb_while_each_thread(g, p);
  2150. return 0;
  2151. }
  2152. /*
  2153. * kdb_pid - This function implements the 'pid' command which switches
  2154. * the currently active process.
  2155. * pid [<pid> | R]
  2156. */
  2157. static int kdb_pid(int argc, const char **argv)
  2158. {
  2159. struct task_struct *p;
  2160. unsigned long val;
  2161. int diag;
  2162. if (argc > 1)
  2163. return KDB_ARGCOUNT;
  2164. if (argc) {
  2165. if (strcmp(argv[1], "R") == 0) {
  2166. p = KDB_TSK(kdb_initial_cpu);
  2167. } else {
  2168. diag = kdbgetularg(argv[1], &val);
  2169. if (diag)
  2170. return KDB_BADINT;
  2171. p = find_task_by_pid_ns((pid_t)val, &init_pid_ns);
  2172. if (!p) {
  2173. kdb_printf("No task with pid=%d\n", (pid_t)val);
  2174. return 0;
  2175. }
  2176. }
  2177. kdb_set_current_task(p);
  2178. }
  2179. kdb_printf("KDB current process is %s(pid=%d)\n",
  2180. kdb_current_task->comm,
  2181. kdb_current_task->pid);
  2182. return 0;
  2183. }
  2184. static int kdb_kgdb(int argc, const char **argv)
  2185. {
  2186. return KDB_CMD_KGDB;
  2187. }
  2188. /*
  2189. * kdb_help - This function implements the 'help' and '?' commands.
  2190. */
  2191. static int kdb_help(int argc, const char **argv)
  2192. {
  2193. kdbtab_t *kt;
  2194. int i;
  2195. kdb_printf("%-15.15s %-20.20s %s\n", "Command", "Usage", "Description");
  2196. kdb_printf("-----------------------------"
  2197. "-----------------------------\n");
  2198. for_each_kdbcmd(kt, i) {
  2199. char *space = "";
  2200. if (KDB_FLAG(CMD_INTERRUPT))
  2201. return 0;
  2202. if (!kt->cmd_name)
  2203. continue;
  2204. if (!kdb_check_flags(kt->cmd_flags, kdb_cmd_enabled, true))
  2205. continue;
  2206. if (strlen(kt->cmd_usage) > 20)
  2207. space = "\n ";
  2208. kdb_printf("%-15.15s %-20s%s%s\n", kt->cmd_name,
  2209. kt->cmd_usage, space, kt->cmd_help);
  2210. }
  2211. return 0;
  2212. }
  2213. /*
  2214. * kdb_kill - This function implements the 'kill' commands.
  2215. */
  2216. static int kdb_kill(int argc, const char **argv)
  2217. {
  2218. long sig, pid;
  2219. char *endp;
  2220. struct task_struct *p;
  2221. struct siginfo info;
  2222. if (argc != 2)
  2223. return KDB_ARGCOUNT;
  2224. sig = simple_strtol(argv[1], &endp, 0);
  2225. if (*endp)
  2226. return KDB_BADINT;
  2227. if (sig >= 0) {
  2228. kdb_printf("Invalid signal parameter.<-signal>\n");
  2229. return 0;
  2230. }
  2231. sig = -sig;
  2232. pid = simple_strtol(argv[2], &endp, 0);
  2233. if (*endp)
  2234. return KDB_BADINT;
  2235. if (pid <= 0) {
  2236. kdb_printf("Process ID must be large than 0.\n");
  2237. return 0;
  2238. }
  2239. /* Find the process. */
  2240. p = find_task_by_pid_ns(pid, &init_pid_ns);
  2241. if (!p) {
  2242. kdb_printf("The specified process isn't found.\n");
  2243. return 0;
  2244. }
  2245. p = p->group_leader;
  2246. info.si_signo = sig;
  2247. info.si_errno = 0;
  2248. info.si_code = SI_USER;
  2249. info.si_pid = pid; /* same capabilities as process being signalled */
  2250. info.si_uid = 0; /* kdb has root authority */
  2251. kdb_send_sig_info(p, &info);
  2252. return 0;
  2253. }
  2254. struct kdb_tm {
  2255. int tm_sec; /* seconds */
  2256. int tm_min; /* minutes */
  2257. int tm_hour; /* hours */
  2258. int tm_mday; /* day of the month */
  2259. int tm_mon; /* month */
  2260. int tm_year; /* year */
  2261. };
  2262. static void kdb_gmtime(struct timespec *tv, struct kdb_tm *tm)
  2263. {
  2264. /* This will work from 1970-2099, 2100 is not a leap year */
  2265. static int mon_day[] = { 31, 29, 31, 30, 31, 30, 31,
  2266. 31, 30, 31, 30, 31 };
  2267. memset(tm, 0, sizeof(*tm));
  2268. tm->tm_sec = tv->tv_sec % (24 * 60 * 60);
  2269. tm->tm_mday = tv->tv_sec / (24 * 60 * 60) +
  2270. (2 * 365 + 1); /* shift base from 1970 to 1968 */
  2271. tm->tm_min = tm->tm_sec / 60 % 60;
  2272. tm->tm_hour = tm->tm_sec / 60 / 60;
  2273. tm->tm_sec = tm->tm_sec % 60;
  2274. tm->tm_year = 68 + 4*(tm->tm_mday / (4*365+1));
  2275. tm->tm_mday %= (4*365+1);
  2276. mon_day[1] = 29;
  2277. while (tm->tm_mday >= mon_day[tm->tm_mon]) {
  2278. tm->tm_mday -= mon_day[tm->tm_mon];
  2279. if (++tm->tm_mon == 12) {
  2280. tm->tm_mon = 0;
  2281. ++tm->tm_year;
  2282. mon_day[1] = 28;
  2283. }
  2284. }
  2285. ++tm->tm_mday;
  2286. }
  2287. /*
  2288. * Most of this code has been lifted from kernel/timer.c::sys_sysinfo().
  2289. * I cannot call that code directly from kdb, it has an unconditional
  2290. * cli()/sti() and calls routines that take locks which can stop the debugger.
  2291. */
  2292. static void kdb_sysinfo(struct sysinfo *val)
  2293. {
  2294. struct timespec uptime;
  2295. ktime_get_ts(&uptime);
  2296. memset(val, 0, sizeof(*val));
  2297. val->uptime = uptime.tv_sec;
  2298. val->loads[0] = avenrun[0];
  2299. val->loads[1] = avenrun[1];
  2300. val->loads[2] = avenrun[2];
  2301. val->procs = nr_threads-1;
  2302. si_meminfo(val);
  2303. return;
  2304. }
  2305. /*
  2306. * kdb_summary - This function implements the 'summary' command.
  2307. */
  2308. static int kdb_summary(int argc, const char **argv)
  2309. {
  2310. struct timespec now;
  2311. struct kdb_tm tm;
  2312. struct sysinfo val;
  2313. if (argc)
  2314. return KDB_ARGCOUNT;
  2315. kdb_printf("sysname %s\n", init_uts_ns.name.sysname);
  2316. kdb_printf("release %s\n", init_uts_ns.name.release);
  2317. kdb_printf("version %s\n", init_uts_ns.name.version);
  2318. kdb_printf("machine %s\n", init_uts_ns.name.machine);
  2319. kdb_printf("nodename %s\n", init_uts_ns.name.nodename);
  2320. kdb_printf("domainname %s\n", init_uts_ns.name.domainname);
  2321. kdb_printf("ccversion %s\n", __stringify(CCVERSION));
  2322. now = __current_kernel_time();
  2323. kdb_gmtime(&now, &tm);
  2324. kdb_printf("date %04d-%02d-%02d %02d:%02d:%02d "
  2325. "tz_minuteswest %d\n",
  2326. 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday,
  2327. tm.tm_hour, tm.tm_min, tm.tm_sec,
  2328. sys_tz.tz_minuteswest);
  2329. kdb_sysinfo(&val);
  2330. kdb_printf("uptime ");
  2331. if (val.uptime > (24*60*60)) {
  2332. int days = val.uptime / (24*60*60);
  2333. val.uptime %= (24*60*60);
  2334. kdb_printf("%d day%s ", days, days == 1 ? "" : "s");
  2335. }
  2336. kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
  2337. /* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */
  2338. #define LOAD_INT(x) ((x) >> FSHIFT)
  2339. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  2340. kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
  2341. LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
  2342. LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
  2343. LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
  2344. #undef LOAD_INT
  2345. #undef LOAD_FRAC
  2346. /* Display in kilobytes */
  2347. #define K(x) ((x) << (PAGE_SHIFT - 10))
  2348. kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"
  2349. "Buffers: %8lu kB\n",
  2350. K(val.totalram), K(val.freeram), K(val.bufferram));
  2351. return 0;
  2352. }
  2353. /*
  2354. * kdb_per_cpu - This function implements the 'per_cpu' command.
  2355. */
  2356. static int kdb_per_cpu(int argc, const char **argv)
  2357. {
  2358. char fmtstr[64];
  2359. int cpu, diag, nextarg = 1;
  2360. unsigned long addr, symaddr, val, bytesperword = 0, whichcpu = ~0UL;
  2361. if (argc < 1 || argc > 3)
  2362. return KDB_ARGCOUNT;
  2363. diag = kdbgetaddrarg(argc, argv, &nextarg, &symaddr, NULL, NULL);
  2364. if (diag)
  2365. return diag;
  2366. if (argc >= 2) {
  2367. diag = kdbgetularg(argv[2], &bytesperword);
  2368. if (diag)
  2369. return diag;
  2370. }
  2371. if (!bytesperword)
  2372. bytesperword = KDB_WORD_SIZE;
  2373. else if (bytesperword > KDB_WORD_SIZE)
  2374. return KDB_BADWIDTH;
  2375. sprintf(fmtstr, "%%0%dlx ", (int)(2*bytesperword));
  2376. if (argc >= 3) {
  2377. diag = kdbgetularg(argv[3], &whichcpu);
  2378. if (diag)
  2379. return diag;
  2380. if (!cpu_online(whichcpu)) {
  2381. kdb_printf("cpu %ld is not online\n", whichcpu);
  2382. return KDB_BADCPUNUM;
  2383. }
  2384. }
  2385. /* Most architectures use __per_cpu_offset[cpu], some use
  2386. * __per_cpu_offset(cpu), smp has no __per_cpu_offset.
  2387. */
  2388. #ifdef __per_cpu_offset
  2389. #define KDB_PCU(cpu) __per_cpu_offset(cpu)
  2390. #else
  2391. #ifdef CONFIG_SMP
  2392. #define KDB_PCU(cpu) __per_cpu_offset[cpu]
  2393. #else
  2394. #define KDB_PCU(cpu) 0
  2395. #endif
  2396. #endif
  2397. for_each_online_cpu(cpu) {
  2398. if (KDB_FLAG(CMD_INTERRUPT))
  2399. return 0;
  2400. if (whichcpu != ~0UL && whichcpu != cpu)
  2401. continue;
  2402. addr = symaddr + KDB_PCU(cpu);
  2403. diag = kdb_getword(&val, addr, bytesperword);
  2404. if (diag) {
  2405. kdb_printf("%5d " kdb_bfd_vma_fmt0 " - unable to "
  2406. "read, diag=%d\n", cpu, addr, diag);
  2407. continue;
  2408. }
  2409. kdb_printf("%5d ", cpu);
  2410. kdb_md_line(fmtstr, addr,
  2411. bytesperword == KDB_WORD_SIZE,
  2412. 1, bytesperword, 1, 1, 0);
  2413. }
  2414. #undef KDB_PCU
  2415. return 0;
  2416. }
  2417. /*
  2418. * display help for the use of cmd | grep pattern
  2419. */
  2420. static int kdb_grep_help(int argc, const char **argv)
  2421. {
  2422. kdb_printf("Usage of cmd args | grep pattern:\n");
  2423. kdb_printf(" Any command's output may be filtered through an ");
  2424. kdb_printf("emulated 'pipe'.\n");
  2425. kdb_printf(" 'grep' is just a key word.\n");
  2426. kdb_printf(" The pattern may include a very limited set of "
  2427. "metacharacters:\n");
  2428. kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n");
  2429. kdb_printf(" And if there are spaces in the pattern, you may "
  2430. "quote it:\n");
  2431. kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\""
  2432. " or \"^pat tern$\"\n");
  2433. return 0;
  2434. }
  2435. /*
  2436. * kdb_register_flags - This function is used to register a kernel
  2437. * debugger command.
  2438. * Inputs:
  2439. * cmd Command name
  2440. * func Function to execute the command
  2441. * usage A simple usage string showing arguments
  2442. * help A simple help string describing command
  2443. * repeat Does the command auto repeat on enter?
  2444. * Returns:
  2445. * zero for success, one if a duplicate command.
  2446. */
  2447. #define kdb_command_extend 50 /* arbitrary */
  2448. int kdb_register_flags(char *cmd,
  2449. kdb_func_t func,
  2450. char *usage,
  2451. char *help,
  2452. short minlen,
  2453. kdb_cmdflags_t flags)
  2454. {
  2455. int i;
  2456. kdbtab_t *kp;
  2457. /*
  2458. * Brute force method to determine duplicates
  2459. */
  2460. for_each_kdbcmd(kp, i) {
  2461. if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
  2462. kdb_printf("Duplicate kdb command registered: "
  2463. "%s, func %p help %s\n", cmd, func, help);
  2464. return 1;
  2465. }
  2466. }
  2467. /*
  2468. * Insert command into first available location in table
  2469. */
  2470. for_each_kdbcmd(kp, i) {
  2471. if (kp->cmd_name == NULL)
  2472. break;
  2473. }
  2474. if (i >= kdb_max_commands) {
  2475. kdbtab_t *new = kmalloc((kdb_max_commands - KDB_BASE_CMD_MAX +
  2476. kdb_command_extend) * sizeof(*new), GFP_KDB);
  2477. if (!new) {
  2478. kdb_printf("Could not allocate new kdb_command "
  2479. "table\n");
  2480. return 1;
  2481. }
  2482. if (kdb_commands) {
  2483. memcpy(new, kdb_commands,
  2484. (kdb_max_commands - KDB_BASE_CMD_MAX) * sizeof(*new));
  2485. kfree(kdb_commands);
  2486. }
  2487. memset(new + kdb_max_commands - KDB_BASE_CMD_MAX, 0,
  2488. kdb_command_extend * sizeof(*new));
  2489. kdb_commands = new;
  2490. kp = kdb_commands + kdb_max_commands - KDB_BASE_CMD_MAX;
  2491. kdb_max_commands += kdb_command_extend;
  2492. }
  2493. kp->cmd_name = cmd;
  2494. kp->cmd_func = func;
  2495. kp->cmd_usage = usage;
  2496. kp->cmd_help = help;
  2497. kp->cmd_minlen = minlen;
  2498. kp->cmd_flags = flags;
  2499. return 0;
  2500. }
  2501. EXPORT_SYMBOL_GPL(kdb_register_flags);
  2502. /*
  2503. * kdb_register - Compatibility register function for commands that do
  2504. * not need to specify a repeat state. Equivalent to
  2505. * kdb_register_flags with flags set to 0.
  2506. * Inputs:
  2507. * cmd Command name
  2508. * func Function to execute the command
  2509. * usage A simple usage string showing arguments
  2510. * help A simple help string describing command
  2511. * Returns:
  2512. * zero for success, one if a duplicate command.
  2513. */
  2514. int kdb_register(char *cmd,
  2515. kdb_func_t func,
  2516. char *usage,
  2517. char *help,
  2518. short minlen)
  2519. {
  2520. return kdb_register_flags(cmd, func, usage, help, minlen, 0);
  2521. }
  2522. EXPORT_SYMBOL_GPL(kdb_register);
  2523. /*
  2524. * kdb_unregister - This function is used to unregister a kernel
  2525. * debugger command. It is generally called when a module which
  2526. * implements kdb commands is unloaded.
  2527. * Inputs:
  2528. * cmd Command name
  2529. * Returns:
  2530. * zero for success, one command not registered.
  2531. */
  2532. int kdb_unregister(char *cmd)
  2533. {
  2534. int i;
  2535. kdbtab_t *kp;
  2536. /*
  2537. * find the command.
  2538. */
  2539. for_each_kdbcmd(kp, i) {
  2540. if (kp->cmd_name && (strcmp(kp->cmd_name, cmd) == 0)) {
  2541. kp->cmd_name = NULL;
  2542. return 0;
  2543. }
  2544. }
  2545. /* Couldn't find it. */
  2546. return 1;
  2547. }
  2548. EXPORT_SYMBOL_GPL(kdb_unregister);
  2549. /* Initialize the kdb command table. */
  2550. static void __init kdb_inittab(void)
  2551. {
  2552. int i;
  2553. kdbtab_t *kp;
  2554. for_each_kdbcmd(kp, i)
  2555. kp->cmd_name = NULL;
  2556. kdb_register_flags("md", kdb_md, "<vaddr>",
  2557. "Display Memory Contents, also mdWcN, e.g. md8c1", 1,
  2558. KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
  2559. kdb_register_flags("mdr", kdb_md, "<vaddr> <bytes>",
  2560. "Display Raw Memory", 0,
  2561. KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
  2562. kdb_register_flags("mdp", kdb_md, "<paddr> <bytes>",
  2563. "Display Physical Memory", 0,
  2564. KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
  2565. kdb_register_flags("mds", kdb_md, "<vaddr>",
  2566. "Display Memory Symbolically", 0,
  2567. KDB_ENABLE_MEM_READ | KDB_REPEAT_NO_ARGS);
  2568. kdb_register_flags("mm", kdb_mm, "<vaddr> <contents>",
  2569. "Modify Memory Contents", 0,
  2570. KDB_ENABLE_MEM_WRITE | KDB_REPEAT_NO_ARGS);
  2571. kdb_register_flags("go", kdb_go, "[<vaddr>]",
  2572. "Continue Execution", 1,
  2573. KDB_ENABLE_REG_WRITE | KDB_ENABLE_ALWAYS_SAFE_NO_ARGS);
  2574. kdb_register_flags("rd", kdb_rd, "",
  2575. "Display Registers", 0,
  2576. KDB_ENABLE_REG_READ);
  2577. kdb_register_flags("rm", kdb_rm, "<reg> <contents>",
  2578. "Modify Registers", 0,
  2579. KDB_ENABLE_REG_WRITE);
  2580. kdb_register_flags("ef", kdb_ef, "<vaddr>",
  2581. "Display exception frame", 0,
  2582. KDB_ENABLE_MEM_READ);
  2583. kdb_register_flags("bt", kdb_bt, "[<vaddr>]",
  2584. "Stack traceback", 1,
  2585. KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS);
  2586. kdb_register_flags("btp", kdb_bt, "<pid>",
  2587. "Display stack for process <pid>", 0,
  2588. KDB_ENABLE_INSPECT);
  2589. kdb_register_flags("bta", kdb_bt, "[D|R|S|T|C|Z|E|U|I|M|A]",
  2590. "Backtrace all processes matching state flag", 0,
  2591. KDB_ENABLE_INSPECT);
  2592. kdb_register_flags("btc", kdb_bt, "",
  2593. "Backtrace current process on each cpu", 0,
  2594. KDB_ENABLE_INSPECT);
  2595. kdb_register_flags("btt", kdb_bt, "<vaddr>",
  2596. "Backtrace process given its struct task address", 0,
  2597. KDB_ENABLE_MEM_READ | KDB_ENABLE_INSPECT_NO_ARGS);
  2598. kdb_register_flags("env", kdb_env, "",
  2599. "Show environment variables", 0,
  2600. KDB_ENABLE_ALWAYS_SAFE);
  2601. kdb_register_flags("set", kdb_set, "",
  2602. "Set environment variables", 0,
  2603. KDB_ENABLE_ALWAYS_SAFE);
  2604. kdb_register_flags("help", kdb_help, "",
  2605. "Display Help Message", 1,
  2606. KDB_ENABLE_ALWAYS_SAFE);
  2607. kdb_register_flags("?", kdb_help, "",
  2608. "Display Help Message", 0,
  2609. KDB_ENABLE_ALWAYS_SAFE);
  2610. kdb_register_flags("cpu", kdb_cpu, "<cpunum>",
  2611. "Switch to new cpu", 0,
  2612. KDB_ENABLE_ALWAYS_SAFE_NO_ARGS);
  2613. kdb_register_flags("kgdb", kdb_kgdb, "",
  2614. "Enter kgdb mode", 0, 0);
  2615. kdb_register_flags("ps", kdb_ps, "[<flags>|A]",
  2616. "Display active task list", 0,
  2617. KDB_ENABLE_INSPECT);
  2618. kdb_register_flags("pid", kdb_pid, "<pidnum>",
  2619. "Switch to another task", 0,
  2620. KDB_ENABLE_INSPECT);
  2621. kdb_register_flags("reboot", kdb_reboot, "",
  2622. "Reboot the machine immediately", 0,
  2623. KDB_ENABLE_REBOOT);
  2624. #if defined(CONFIG_MODULES)
  2625. kdb_register_flags("lsmod", kdb_lsmod, "",
  2626. "List loaded kernel modules", 0,
  2627. KDB_ENABLE_INSPECT);
  2628. #endif
  2629. #if defined(CONFIG_MAGIC_SYSRQ)
  2630. kdb_register_flags("sr", kdb_sr, "<key>",
  2631. "Magic SysRq key", 0,
  2632. KDB_ENABLE_ALWAYS_SAFE);
  2633. #endif
  2634. #if defined(CONFIG_PRINTK)
  2635. kdb_register_flags("dmesg", kdb_dmesg, "[lines]",
  2636. "Display syslog buffer", 0,
  2637. KDB_ENABLE_ALWAYS_SAFE);
  2638. #endif
  2639. if (arch_kgdb_ops.enable_nmi) {
  2640. kdb_register_flags("disable_nmi", kdb_disable_nmi, "",
  2641. "Disable NMI entry to KDB", 0,
  2642. KDB_ENABLE_ALWAYS_SAFE);
  2643. }
  2644. kdb_register_flags("defcmd", kdb_defcmd, "name \"usage\" \"help\"",
  2645. "Define a set of commands, down to endefcmd", 0,
  2646. KDB_ENABLE_ALWAYS_SAFE);
  2647. kdb_register_flags("kill", kdb_kill, "<-signal> <pid>",
  2648. "Send a signal to a process", 0,
  2649. KDB_ENABLE_SIGNAL);
  2650. kdb_register_flags("summary", kdb_summary, "",
  2651. "Summarize the system", 4,
  2652. KDB_ENABLE_ALWAYS_SAFE);
  2653. kdb_register_flags("per_cpu", kdb_per_cpu, "<sym> [<bytes>] [<cpu>]",
  2654. "Display per_cpu variables", 3,
  2655. KDB_ENABLE_MEM_READ);
  2656. kdb_register_flags("grephelp", kdb_grep_help, "",
  2657. "Display help on | grep", 0,
  2658. KDB_ENABLE_ALWAYS_SAFE);
  2659. }
  2660. /* Execute any commands defined in kdb_cmds. */
  2661. static void __init kdb_cmd_init(void)
  2662. {
  2663. int i, diag;
  2664. for (i = 0; kdb_cmds[i]; ++i) {
  2665. diag = kdb_parse(kdb_cmds[i]);
  2666. if (diag)
  2667. kdb_printf("kdb command %s failed, kdb diag %d\n",
  2668. kdb_cmds[i], diag);
  2669. }
  2670. if (defcmd_in_progress) {
  2671. kdb_printf("Incomplete 'defcmd' set, forcing endefcmd\n");
  2672. kdb_parse("endefcmd");
  2673. }
  2674. }
  2675. /* Initialize kdb_printf, breakpoint tables and kdb state */
  2676. void __init kdb_init(int lvl)
  2677. {
  2678. static int kdb_init_lvl = KDB_NOT_INITIALIZED;
  2679. int i;
  2680. if (kdb_init_lvl == KDB_INIT_FULL || lvl <= kdb_init_lvl)
  2681. return;
  2682. for (i = kdb_init_lvl; i < lvl; i++) {
  2683. switch (i) {
  2684. case KDB_NOT_INITIALIZED:
  2685. kdb_inittab(); /* Initialize Command Table */
  2686. kdb_initbptab(); /* Initialize Breakpoints */
  2687. break;
  2688. case KDB_INIT_EARLY:
  2689. kdb_cmd_init(); /* Build kdb_cmds tables */
  2690. break;
  2691. }
  2692. }
  2693. kdb_init_lvl = lvl;
  2694. }