kdb_main.c 70 KB

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