kdb_main.c 70 KB

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