kdb_main.c 70 KB

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