首次提交
This commit is contained in:
+317
@@ -0,0 +1,317 @@
|
||||
理想的方法契约:
|
||||
方法功能描述
|
||||
参数定义及合法性要求
|
||||
调用线程环境要求
|
||||
返回值定义
|
||||
当参数不合法时,方法的行为
|
||||
当出现其他错误时,方法的行为
|
||||
当方法正常返回时,相关对象(系统)的状态
|
||||
当方法抛出异常时,相关对象(系统)的状态
|
||||
|
||||
|
||||
其中最后一条即所谓的“异常安全”
|
||||
|
||||
|
||||
异常或错误,追究其原因,有三种:
|
||||
|
||||
程序逻辑错误
|
||||
非法的用户输入
|
||||
硬件错误
|
||||
|
||||
其中非法的用户输入其实是可以(或者应该)不用异常或错误进行处理的,属于程序正常的流程
|
||||
因为Java或者任何支持try catch的异常系统,建立异常对象通常会收集调用栈信息,是比较昂贵的路径,
|
||||
除非出错否则你是不想创建异常的。但是在实际实践中,为了系统设计上的“干净”和开发的方便,
|
||||
对非法用户输入的处理常常也用异常系统进行了统一处理。这算是开发维护成本和运行效率的取舍吧
|
||||
|
||||
程序逻辑错误,在运维系统越来越先进的今天,很多团队选择比较激进的let it fail哲学,
|
||||
辅以充分的单元和集成测试,在大部分商业应用场景都是可行的。表现在代码里,就是任其崩溃,
|
||||
语言运行环境或应用框架内建的最后一层捕获机制会去处理异常。这个过程虽然不需要程序员做什么,
|
||||
但是有经验的老司机还是会去考虑一些敏感场景的异常安全问题,
|
||||
比如影响到持久化数据正确性或者与其他系统交互正确性的场景。
|
||||
|
||||
硬件错误通常是不做考虑的,那完全是架构需要操心的事。
|
||||
|
||||
|
||||
|
||||
Design by Contract (契约式设计)
|
||||
|
||||
契约式设计强调三个概念:前置条件,后置条件和不变式
|
||||
|
||||
|
||||
|
||||
|
||||
错误处理
|
||||
|
||||
程序本身的组成形式都能展开成一条条最基本的语句,这个东西会读取输入,中间各种运算,产生输出。
|
||||
我们使用输入输出的时候已经天然带类型了,自带一层约束
|
||||
我们整个流程是有预期的,从输入到输出,中间的各种阶段都会有预期。
|
||||
天然类型的约束可能不符合我们对输入输出的预期,所以更健壮的代码可以对输入输出进行检查。加入更多的约束
|
||||
程序有依赖硬件,如果硬件出错,那么不符合预期是无法从程序层面解决的。这是无需处理
|
||||
如果输入符合预期,输出不符合预期,那可能就是程序逻辑错误
|
||||
如果输入不符合预期,那么可能就是
|
||||
当如输入输出可以广泛一点的定义,也包括线程环境,数据状态等各种信息
|
||||
问题在于如何处理,我理解的处理是分环境的,开发环境下的处理方式,和生产时的处理方式不一定一样,开发要直接崩溃
|
||||
打印调用栈,生产时要看是什么程序,如果时系统级程序也要直接崩溃,或者时数据库要弄事务,如果是后端这种,可能就打日志
|
||||
然后处理掉
|
||||
|
||||
那么基于此,在c++如何处理
|
||||
|
||||
那一定是基于宏判断是Debug还是release 然后下面细分类型,也可以像spdlog一样有日志级别
|
||||
|
||||
|
||||
|
||||
那么问题来了 如果是不重要的情景下如何不让他崩溃,对我的程序来说解码不符合预期应当忽略整个报文的结果
|
||||
或者激进一点 只忽略这一个报文的一个字段的结果
|
||||
|
||||
那么返回值应该如何处理加std::optional?
|
||||
|
||||
我认为std::optional 其实很局限 他就表达了空对象概念,当然我可以全局弄个错误码,但是那不就是c的方式了吗
|
||||
他本质就是c的方式的语法糖
|
||||
错误处理就c的方式错误码方式,异常,然后就是let it fail 直接崩, 然后各种事物,乱七八糟根据具体情况来的
|
||||
|
||||
|
||||
总体来看像是后端那种程序我直接打日志的错误不敏感的,异常要好的多
|
||||
|
||||
|
||||
那我现在开始定义了
|
||||
|
||||
|
||||
(lldb) settings list
|
||||
Top level variables:
|
||||
|
||||
auto-confirm -- If true all confirmation prompts will receive their default reply.
|
||||
disassembly-format -- The default disassembly format string to use when disassembling instruction sequences.
|
||||
frame-format -- The default frame format string to use when displaying stack frame information for
|
||||
threads.
|
||||
notify-void -- Notify the user explicitly if an expression returns void (default: false).
|
||||
prompt -- The debugger command line prompt displayed for the user.
|
||||
script-lang -- The script language to be used for evaluating user-written scripts.
|
||||
stop-disassembly-count -- The number of disassembly lines to show when displaying a stopped context.
|
||||
stop-disassembly-display -- Control when to display disassembly when displaying a stopped context.
|
||||
stop-line-count-after -- The number of sources lines to display that come after the current source line when
|
||||
displaying a stopped context.
|
||||
stop-line-count-before -- The number of sources lines to display that come before the current source line when
|
||||
displaying a stopped context.
|
||||
highlight-source -- If true, LLDB will highlight the displayed source code.
|
||||
stop-show-column -- If true, LLDB will use the column information from the debug info to mark the current
|
||||
position when displaying a stopped context.
|
||||
stop-show-column-ansi-prefix -- When displaying the column marker in a color-enabled (i.e. ANSI) terminal, use the
|
||||
ANSI terminal code specified in this format at the immediately before the column to
|
||||
be marked.
|
||||
stop-show-column-ansi-suffix -- When displaying the column marker in a color-enabled (i.e. ANSI) terminal, use the
|
||||
ANSI terminal code specified in this format immediately after the column to be marked.
|
||||
term-width -- The maximum number of columns to use for displaying text.
|
||||
thread-format -- The default thread format string to use when displaying thread information.
|
||||
thread-stop-format -- The default thread format string to use when displaying thread information as part
|
||||
of the stop display.
|
||||
use-external-editor -- Whether to use an external editor or not.
|
||||
use-color -- Whether to use Ansi color codes or not.
|
||||
auto-one-line-summaries -- If true, LLDB will automatically display small structs in one-liner format (default:
|
||||
true).
|
||||
auto-indent -- If true, LLDB will auto indent/outdent code. Currently only supported in the REPL
|
||||
(default: true).
|
||||
print-decls -- If true, LLDB will print the values of variables declared in an expression. Currently
|
||||
only supported in the REPL (default: true).
|
||||
tab-size -- The tab size to use when indenting code in multi-line input mode (default: 4).
|
||||
escape-non-printables -- If true, LLDB will automatically escape non-printable and escape characters when
|
||||
formatting strings.
|
||||
frame-format-unique -- The default frame format string to use when displaying stack frameinformation for
|
||||
threads from thread backtrace unique.
|
||||
|
||||
'target' variables:
|
||||
|
||||
default-arch -- Default architecture to choose, when there's a choice.
|
||||
move-to-nearest-code -- Move breakpoints to nearest code.
|
||||
language -- The language to use when interpreting expressions entered in commands.
|
||||
expr-prefix -- Path to a file containing expressions to be prepended to all expressions.
|
||||
prefer-dynamic-value -- Should printed values be shown as their dynamic value.
|
||||
enable-synthetic-value -- Should synthetic values be used by default whenever available.
|
||||
skip-prologue -- Skip function prologues when setting breakpoints by name.
|
||||
breakpoint-smart-move -- Check function boundaries when setting breakpoints by file and line.
|
||||
source-map -- Source path remappings are used to track the change of location between a
|
||||
source file when built, and where it exists on the current system. It
|
||||
consists of an array of duples, the first element of each duple is some part
|
||||
(starting at the root) of the path to the file when it was built, and the
|
||||
second is where the remainder of the original build hierarchy is rooted on the
|
||||
local system. Each element of the array is checked in order and the first one
|
||||
that results in a match wins.
|
||||
exec-search-paths -- Executable search paths to use when locating executable files whose paths
|
||||
don't match the local file system.
|
||||
debug-file-search-paths -- List of directories to be searched when locating debug symbol files. See also
|
||||
symbols.enable-external-lookup.
|
||||
clang-module-search-paths -- List of directories to be searched when locating modules for Clang.
|
||||
auto-import-clang-modules -- Automatically load Clang modules referred to by the program.
|
||||
import-std-module -- Import the C++ std module to improve debugging STL containers.
|
||||
auto-apply-fixits -- Automatically apply fix-it hints to expressions.
|
||||
notify-about-fixits -- Print the fixed expression text.
|
||||
save-jit-objects -- Save intermediate object files generated by the LLVM JIT
|
||||
max-children-count -- Maximum number of children to expand in any level of depth.
|
||||
max-string-summary-length -- Maximum number of characters to show when using %s in summary strings.
|
||||
max-memory-read-size -- Maximum number of bytes that 'memory read' will fetch before --force must be
|
||||
specified.
|
||||
breakpoints-use-platform-avoid-list -- Consult the platform module avoid list when setting non-module specific
|
||||
breakpoints.
|
||||
arg0 -- The first argument passed to the program in the argument array which can be
|
||||
different from the executable itself.
|
||||
run-args -- A list containing all the arguments to be passed to the executable when it is
|
||||
run. Note that this does NOT include the argv[0] which is in target.arg0.
|
||||
env-vars -- A list of all the environment variables to be passed to the executable's
|
||||
environment, and their values.
|
||||
inherit-env -- Inherit the environment from the process that is running LLDB.
|
||||
input-path -- The file/path to be used by the executable program for reading its standard
|
||||
input.
|
||||
output-path -- The file/path to be used by the executable program for writing its standard
|
||||
output.
|
||||
error-path -- The file/path to be used by the executable program for writing its standard
|
||||
error.
|
||||
detach-on-error -- debugserver will detach (rather than killing) a process if it loses connection
|
||||
with lldb.
|
||||
preload-symbols -- Enable loading of symbol tables before they are needed.
|
||||
disable-aslr -- Disable Address Space Layout Randomization (ASLR)
|
||||
disable-stdio -- Disable stdin/stdout for process (e.g. for a GUI application)
|
||||
inline-breakpoint-strategy -- The strategy to use when settings breakpoints by file and line. Breakpoint
|
||||
locations can end up being inlined by the compiler, so that a compile unit
|
||||
'a.c' might contain an inlined function from another source file. Usually this
|
||||
is limited to breakpoint locations from inlined functions from header or other
|
||||
include files, or more accurately non-implementation source files. Sometimes
|
||||
code might #include implementation files and cause inlined breakpoint
|
||||
locations in inlined implementation files. Always checking for inlined
|
||||
breakpoint locations can be expensive (memory and time), so if you have a
|
||||
project with many headers and find that setting breakpoints is slow, then you
|
||||
can change this setting to headers. This setting allows you to control exactly
|
||||
which strategy is used when setting file and line breakpoints.
|
||||
x86-disassembly-flavor -- The default disassembly flavor to use for x86 or x86-64 targets.
|
||||
use-hex-immediates -- Show immediates in disassembly as hexadecimal.
|
||||
hex-immediate-style -- Which style to use for printing hexadecimal disassembly values.
|
||||
use-fast-stepping -- Use a fast stepping algorithm based on running from branch to branch rather
|
||||
than instruction single-stepping.
|
||||
load-script-from-symbol-file -- Allow LLDB to load scripting resources embedded in symbol files when available.
|
||||
load-cwd-lldbinit -- Allow LLDB to .lldbinit files from the current directory automatically.
|
||||
memory-module-load-level -- Loading modules from memory can be slow as reading the symbol tables and other
|
||||
data can take a long time depending on your connection to the debug target.
|
||||
This setting helps users control how much information gets loaded when loading
|
||||
modules from memory.'complete' is the default value for this setting which
|
||||
will load all sections and symbols by reading them from memory (slowest, most
|
||||
accurate). 'partial' will load sections and attempt to find function bounds
|
||||
without downloading the symbol table (faster, still accurate, missing symbol
|
||||
names). 'minimal' is the fastest setting and will load section data with no
|
||||
symbols, but should rarely be used as stack frames in these memory regions
|
||||
will be inaccurate and not provide any context (fastest).
|
||||
display-expression-in-crashlogs -- Expressions that crash will show up in crash logs if the host system supports
|
||||
executable specific crash log strings and this setting is set to true.
|
||||
trap-handler-names -- A list of trap handler function names, e.g. a common Unix user process one is
|
||||
_sigtramp.
|
||||
display-runtime-support-values -- If true, LLDB will show variables that are meant to support the operation of a
|
||||
language's runtime support.
|
||||
display-recognized-arguments -- Show recognized arguments in variable listings by default.
|
||||
non-stop-mode -- Disable lock-step debugging, instead control threads independently.
|
||||
require-hardware-breakpoint -- Require all breakpoints to be hardware breakpoints.
|
||||
|
||||
'target.experimental' variables:
|
||||
|
||||
inject-local-vars -- If true, inject local variables explicitly into the expression text. This will
|
||||
fix symbol resolution when there are name collisions between ivars and local
|
||||
variables. But it can make expressions run much more slowly.
|
||||
use-modern-type-lookup -- If true, use Clang's modern type lookup infrastructure.
|
||||
cache-evaluating-expressions -- If true, evaluated expressions ast is cached for further use.
|
||||
cache-evaluating-expressions-limit -- Expression ast cache limit.
|
||||
|
||||
'target.process' variables:
|
||||
|
||||
disable-memory-cache -- Disable reading and caching of memory in fixed-size units.
|
||||
extra-startup-command -- A list containing extra commands understood by the particular process plugin
|
||||
used. For instance, to turn on debugserver logging set this to
|
||||
"QSetLogging:bitmask=LOG_DEFAULT;"
|
||||
ignore-breakpoints-in-expressions -- If true, breakpoints will be ignored during expression evaluation.
|
||||
unwind-on-error-in-expressions -- If true, errors in expression evaluation will unwind the stack back to the state
|
||||
before the call.
|
||||
python-os-plugin-path -- A path to a python OS plug-in module file that contains a OperatingSystemPlugIn
|
||||
class.
|
||||
stop-on-sharedlibrary-events -- If true, stop when a shared library is loaded or unloaded.
|
||||
detach-keeps-stopped -- If true, detach will attempt to keep the process stopped.
|
||||
memory-cache-line-size -- The memory cache line size
|
||||
optimization-warnings -- If true, warn when stopped in code that is optimized where stepping and variable
|
||||
availability may not behave as expected.
|
||||
stop-on-exec -- If true, stop when a shared library is loaded or unloaded.
|
||||
utility-expression-timeout -- The time in seconds to wait for LLDB-internal utility expressions.
|
||||
|
||||
'target.process.thread' variables:
|
||||
|
||||
step-in-avoid-nodebug -- If true, step-in will not stop in functions with no debug information.
|
||||
step-out-avoid-nodebug -- If true, when step-in/step-out/step-over leave the current frame, they will continue to
|
||||
step out till they come to a function with debug information. Passing a frame argument to
|
||||
step-out will override this option.
|
||||
step-avoid-regexp -- A regular expression defining functions step-in won't stop in.
|
||||
step-avoid-libraries -- A list of libraries that source stepping won't stop in.
|
||||
trace-thread -- If true, this thread will single-step and log execution.
|
||||
max-backtrace-depth -- Maximum number of frames to backtrace.
|
||||
|
||||
'platform' variables:
|
||||
|
||||
use-module-cache -- Use module cache.
|
||||
module-cache-directory -- Root directory for cached modules.
|
||||
|
||||
'symbols' variables:
|
||||
|
||||
enable-external-lookup -- Control the use of external tools and repositories to locate symbol files. Directories
|
||||
listed in target.debug-file-search-paths and directory of the executable are always
|
||||
checked first for separate debug info files. Then depending on this setting: On macOS,
|
||||
Spotlight would be also used to locate a matching .dSYM bundle based on the UUID of the
|
||||
executable. On NetBSD, directory /usr/libdata/debug would be also searched. On platforms
|
||||
other than NetBSD directory /usr/lib/debug would be also searched.
|
||||
clang-modules-cache-path -- The path to the clang modules cache directory (-fmodules-cache-path).
|
||||
|
||||
'interpreter' variables:
|
||||
|
||||
expand-regex-aliases -- If true, regular expression alias commands will show the expanded command that will
|
||||
be executed. This can be used to debug new regular expression alias commands.
|
||||
prompt-on-quit -- If true, LLDB will prompt you before quitting if there are any live processes being
|
||||
debugged. If false, LLDB will quit without asking in any case.
|
||||
stop-command-source-on-error -- If true, LLDB will stop running a 'command source' script upon encountering an error.
|
||||
space-repl-prompts -- If true, blank lines will be printed between between REPL submissions.
|
||||
echo-commands -- If true, commands will be echoed before they are evaluated.
|
||||
echo-comment-commands -- If true, commands will be echoed even if they are pure comment lines.
|
||||
|
||||
'plugin' variables:
|
||||
|
||||
|
||||
'plugin.jit-loader' variables:
|
||||
|
||||
|
||||
'plugin.jit-loader.gdb' variables:
|
||||
|
||||
enable -- Enable GDB's JIT compilation interface (default: enabled on all platforms except macOS)
|
||||
|
||||
'plugin.process' variables:
|
||||
|
||||
|
||||
'plugin.process.windows' variables:
|
||||
|
||||
suppress-debug-output -- If true, debug output string events will be suppressed
|
||||
|
||||
'plugin.process.gdb-remote' variables:
|
||||
|
||||
packet-timeout -- Specify the default packet timeout in seconds.
|
||||
target-definition-file -- The file that provides the description for remote target registers.
|
||||
use-libraries-svr4 -- If true, the libraries-svr4 feature will be used to get a hold of the process's loaded
|
||||
modules.
|
||||
|
||||
'plugin.symbol-file' variables:
|
||||
|
||||
|
||||
'plugin.symbol-file.dwarf' variables:
|
||||
|
||||
comp-dir-symlink-paths -- If the DW_AT_comp_dir matches any of these paths the symbolic links will be resolved at
|
||||
DWARF parse time.
|
||||
ignore-file-indexes -- Ignore indexes present in the object files and always index DWARF manually.
|
||||
|
||||
'plugin.structured-data' variables:
|
||||
|
||||
|
||||
'plugin.structured-data.darwin-log' variables:
|
||||
|
||||
enable-on-startup -- Enable Darwin os_log collection when debugged process is launched or attached.
|
||||
auto-enable-options -- Specify the options to 'plugin structured-data darwin-log enable' that should be applied when
|
||||
automatically enabling logging on startup/attach.
|
||||
(lldb)
|
||||
|
||||
Reference in New Issue
Block a user