kdb_main.c 69 KB

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